home *** CD-ROM | disk | FTP | other *** search
/ ftp.alaska-software.com / 2014.06.ftp.alaska-software.com.tar / ftp.alaska-software.com / acsn / BDZIP.ZIP / BDZIP.PRG < prev    next >
Text File  |  2004-08-23  |  13KB  |  350 lines

  1. ///////////////////////////////////////////////////////////////////////////
  2. //      BDZip functions   2000 - Brent Dubs (bdubs@vanityshops.com)
  3. //        Xbase++ functions for using the addZIP compression library.
  4. //
  5. //      If you do not have the XbToolsIII library, you need to replace
  6. //        the RAND function calls, they are only used to create an
  7. //        unique file name.
  8. //
  9. //      To use these functions you will need the addZIP compression library
  10. //        available at http://www.littlebigware.com and you need the
  11. //        BAP library availible from the Alaska Software website in the
  12. //        Download/ASCN section.
  13. //
  14. //      These functions will work with both the demo and registered
  15. //        versions of addZIP.  You just need to uncomment the addZIP_Register
  16. //        calls and add your registration string you receive from addZIP.
  17. //
  18. //      Version History:
  19. //        08|23|2004    Minor corrections in calls to "addUNZIP_Test()",
  20. //                      "addUNZIP_View()" (parameter was missing)  
  21. //        10|06|2004    Original Version by Brent Dubs
  22. //
  23. ///////////////////////////////////////////////////////////////////////////
  24.  
  25.  
  26. #include "common.ch"
  27. #include "FileIO.ch"
  28. #include "bap.ch"
  29. #include "dll.ch"
  30. #include "addzip.ch"
  31. #pragma Library( "BAP.LIB" )
  32. #define READFLATRECORD_BUFFERSIZE 1000
  33.  
  34. //-----------------------------------------------------------------------------
  35. // The ZipFile function compresses a file or files into an archive (.ZIP) file.
  36. //
  37. //   Parameters:
  38. //            xFile: File or files to compress. Can be a string (single file)
  39. //                   or an array of strings (multiple files).
  40. //         cArcFile: Name of the archive file to create.
  41. //    nWindowHandle: Where to display the progress output (optional). You may
  42. //                   pass either the window handle, or the object.
  43. //
  44. //   Returns: True (.T.) if it successfully created the archive file.
  45. //            or False (.F.) if an error occured.
  46. //
  47. FUNCTION ZipFile(xFile,cArcFile,nWindowHandle)
  48.   LOCAL lValid := .F., nDllZip := 0, aFileList := {}, lFileMissing := .F.
  49.   LOCAL cFiles := ""
  50.   IF ValType(xFile) == "C"
  51.     aFileList := {xFile}
  52.     IF cArcFile == NIL
  53.       cArcFile := SUBSTR(xFile,1,AT(".",xFile)) + "ZIP"
  54.     ENDIF
  55.   ELSE
  56.     aFileList := xFile
  57.   ENDIF
  58.   AEVAL(aFileList,{|x|IF(!File(x),lFileMissing := .T.,NIL)})
  59.   IF ! lFileMissing
  60.     FOR I := 1 TO LEN(aFileList)
  61.       cFiles += ALLTRIM(aFileList[I]) + "|"
  62.     NEXT
  63.     nDllZip := DllLoad("azip32.dll")
  64.     IF nDllZip == 0
  65.       MSGBOX("Could not find the addZip azip32.dll file!","DLL missing")
  66.     ENDIF
  67.     DllCall(nDllZip,DLL_STDCALL,"addZIP_Initialise")
  68.  
  69. // Uncomment the next line and replace with your registration info. (If you want it registered)
  70. //    DllCall(nDllZip,DLL_STDCALL,"addZIP_Register","Your Name",0x????????)
  71.  
  72.     IF nWindowHandle != NIL
  73.       IF VALTYPE(nWindowHandle) == "N"
  74.         DllCall(nDllZip,DLL_STDCALL,"addZIP_SetWindowHandle",nWindowHandle)
  75.       ELSE
  76.         DllCall(nDllZip,DLL_STDCALL,"addZIP_SetWindowHandle",nWindowHandle:getHwnd())
  77.       ENDIF
  78.     ENDIF
  79.     DllCall(nDllZip,DLL_STDCALL,"addZIP_Recurse",0)
  80.     DllCall(nDllZip,DLL_STDCALL,"addZIP_SaveStructure",0)
  81.     DllCall(nDllZip,DLL_STDCALL,"addZIP_ArchiveName",cArcFile)
  82.     DllCall(nDllZip,DLL_STDCALL,"addZIP_Include",cFiles)
  83.     DllCall(nDllZip,DLL_STDCALL,"addZIP")
  84.     DllUnload(nDllZip)
  85.     IF File(cArcFile)
  86.       lValid := .T.
  87.     ENDIF
  88.   ENDIF
  89. RETURN lValid
  90.  
  91. //-----------------------------------------------------------------------------
  92. // The UnZipFile function extracts files from an archive (.ZIP) file
  93. //
  94. //   Parameters:
  95. //         cArcFile: Name of the archive file (.ZIP) to extract from.
  96. //            cPath: Directory where to extract the files to (optional).
  97. //                   If not passed, then the files will be extracted into
  98. //                   the current directory.
  99. //        lFullPath: Include the path in the returned results (optional)?
  100. //
  101. //   Returns: An array of file names that were extracted from the archive
  102. //            file. If True (.T.) was passed to the lFullPath parameter,
  103. //            then the file names will include the path.
  104. //
  105. FUNCTION UnZipFile(cArcFile,cPath,lFullPath)
  106.   LOCAL nDllZip := 0, aOFN, cOFN, cRecord := "", aFileList := {}
  107.   LOCAL nHook := 0, cCall := "", nError := 0
  108.   DEFAULT lFullPath TO .F.
  109.   DEFAULT cPath TO ".\"
  110.  
  111.   cFileName := "Z" + ALLTRIM(STR(INT(100000 * RAND(-1)) + 1)) + ".TMP"
  112.   UnZipCallBack(,,,cFileName)
  113.  
  114.   nHook = BaCallBack("UnZipCallBack",BA_CB_ADDZIP)
  115.  
  116.   aOFN = BaInit(1) // BAP.DLL from Gernot Trautmann
  117.   BaStruct(aOFN,nHook)
  118.   cOFN = BaAccess(aOFN)
  119.  
  120.   nDllZip := DllLoad("aunzip32.dll")
  121.   IF nDllZip == 0
  122.     MSGBOX("Could not find the addZip aunzip32.dll file!","DLL missing")
  123.   ENDIF
  124.   DllCall(nDllZip,DLL_STDCALL,"addUNZIP_Initialise")
  125.  
  126. // Uncomment the next line and replace with your registration info. (If you want it registered)
  127. //    DllCall(nDllZip,DLL_STDCALL,"addUNZIP_Register","Your Name",0x????????)
  128.  
  129.   // This is where the callback is being used
  130.   cCall := DllPrepareCall(nDllZip,DLL_STDCALL,"addUNZIP_InstallCallback")
  131.   nError := DLLExecuteCall(cCall,nHook)
  132.  
  133.   DllCall(nDllZip,DLL_STDCALL,"addUNZIP_ArchiveName",cArcFile)
  134.   DllCall(nDllZip,DLL_STDCALL,"addUNZIP_Include","*.*")
  135.   DllCall(nDllZip,DLL_STDCALL,"addUNZIP_RestoreStructure",0)
  136.   DllCall(nDllZip,DLL_STDCALL,"addUNZIP_ExtractTo",cPath)
  137.   DllCall(nDllZip,DLL_STDCALL,"addUNZIP")
  138.   DllUnload(nDllZip)
  139.   UnZipCallBack(,,,,.T.)
  140.  
  141.   nHandle := FOPEN(cFileName)
  142.   cRecord := ReadFlatRecord(nHandle)
  143.   DO WHILE !(cRecord == "")
  144.     IF lFullPath
  145.       AADD(aFileList,cRecord)
  146.     ELSE
  147.       AADD(aFileList,SUBSTR(cRecord,RAT("\",cRecord)+1))
  148.     ENDIF
  149.     cRecord := ReadFlatRecord(nHandle)
  150.   ENDDO
  151.   FCLOSE(nHandle)
  152.   FERASE(cFileName)
  153. RETURN aFileList
  154.  
  155. //----------------------------------------------------------------------------
  156. // The ViewZip function returns the name of the files contained in an
  157. //     archive file. It DOES NOT extract an archive file.
  158. //
  159. //   Parameters:
  160. //         cArcFile: Name of the archive file (.ZIP) to view.
  161. //        lFullPath: Include the path in the returned results (optional)?
  162. //
  163. //   Returns: An array of file names that are contained in the archive
  164. //            file. If True (.T.) was passed to the lFullPath parameter,
  165. //            then the file names will include the path.
  166. //
  167. FUNCTION ViewZip(cArcFile,lFullPath)
  168.   LOCAL nDllZip := 0, aOFN, cOFN, cRecord := "", aFileList := {}
  169.   LOCAL nHook := 0, cCall := "", nError := 0, lStatus := .T.
  170.   DEFAULT lFullPath TO .F.
  171.  
  172.   cFileName := "Z" + ALLTRIM(STR(INT(100000 * RAND(-1)) + 1)) + ".TMP"
  173.   UnZipCallBack(,,,cFileName)
  174.  
  175.   nHook = BaCallBack("UnZipCallBack",BA_CB_ADDZIP)
  176.  
  177.   aOFN = BaInit(1) // BAP.DLL from Gernot Trautmann
  178.   BaStruct(aOFN,nHook)
  179.   cOFN = BaAccess(aOFN)
  180.  
  181.   nDllZip := DllLoad("aunzip32.dll")
  182.   IF nDllZip == 0
  183.     MSGBOX("Could not find the addZip aunzip32.dll file!","DLL missing")
  184.   ENDIF
  185.   DllCall(nDllZip,DLL_STDCALL,"addUNZIP_Initialise")
  186.  
  187. // Uncomment the next line and replace with your registration info. (If you want it registered)
  188. //    DllCall(nDllZip,DLL_STDCALL,"addUNZIP_Register","Your Name",0x????????)
  189.  
  190.   // This is where the callback is being used
  191.   cCall := DllPrepareCall(nDllZip,DLL_STDCALL,"addUNZIP_InstallCallback")
  192.   nError := DLLExecuteCall(cCall,nHook)
  193.  
  194.   DllCall(nDllZip,DLL_STDCALL,"addUNZIP_ArchiveName",cArcFile)
  195.   DllCall(nDllZip,DLL_STDCALL,"addUNZIP_View", 1)
  196.   DllCall(nDllZip,DLL_STDCALL,"addUNZIP")
  197.   DllUnload(nDllZip)
  198.   UnZipCallBack(,,,,.T.)
  199.  
  200.   nHandle := FOPEN(cFileName)
  201.   cRecord := ReadFlatRecord(nHandle)
  202.   DO WHILE !(cRecord == "")
  203.     IF lFullPath
  204.       AADD(aFileList,SplitZipMessage(cRecord))
  205.     ELSE
  206.       AADD(aFileList,SUBSTR(cRecord,RAT("\",cRecord)+1))
  207.     ENDIF
  208.     cRecord := ReadFlatRecord(nHandle)
  209.   ENDDO
  210.   FCLOSE(nHandle)
  211.   FERASE(cFileName)
  212. RETURN aFileList
  213.  
  214. //-------------------------------------------------------------------------
  215. // The TestUnZip function determines if a file is a valid archive (.ZIP) file.
  216. //   It DOES NOT extract the archive file.
  217. //
  218. //   Parameters:
  219. //         cArcFile: Name of the archive file (.ZIP) to test.
  220. //
  221. //   Returns: True (.T.) if the file is a valid archive file or False (.F.)
  222. //              if the archive file is corrupted.
  223. //
  224. FUNCTION TestUnZip(cArcFile)
  225.   LOCAL nDllZip := 0, aOFN, cOFN, cRecord := "", aFileList := {}
  226.   LOCAL nHook := 0, cCall := "", nError := 0, lStatus := .T.
  227.  
  228.   cFileName := "Z" + ALLTRIM(STR(INT(100000 * RAND(-1)) + 1)) + ".TMP"
  229.   UnZipCallBack(,,,cFileName)
  230.  
  231.   nHook = BaCallBack("UnZipCallBack",BA_CB_ADDZIP)
  232.  
  233.   aOFN = BaInit(1) // BAP.DLL from Gernot Trautmann
  234.   BaStruct(aOFN,nHook)
  235.   cOFN = BaAccess(aOFN)
  236.  
  237.   nDllZip := DllLoad("aunzip32.dll")
  238.   IF nDllZip == 0
  239.     MSGBOX("Could not find the addZip aunzip32.dll file!","DLL missing")
  240.   ENDIF
  241.   DllCall(nDllZip,DLL_STDCALL,"addUNZIP_Initialise")
  242.  
  243. // Uncomment the next line and replace with your registration info. (If you want it registered)
  244. //    DllCall(nDllZip,DLL_STDCALL,"addUNZIP_Register","Your Name",0x????????)
  245.  
  246.   // This is where the callback is being used
  247.   cCall := DllPrepareCall(nDllZip,DLL_STDCALL,"addUNZIP_InstallCallback")
  248.   nError := DLLExecuteCall(cCall,nHook)
  249.  
  250.   DllCall(nDllZip,DLL_STDCALL,"addUNZIP_ArchiveName",cArcFile)
  251.   DllCall(nDllZip,DLL_STDCALL,"addUNZIP_Test", 1)
  252.   DllCall(nDllZip,DLL_STDCALL,"addUNZIP")
  253.   DllUnload(nDllZip)
  254.   UnZipCallBack(,,,,.T.)
  255.  
  256.   nHandle := FOPEN(cFileName)
  257.   cRecord := ReadFlatRecord(nHandle)
  258.   DO WHILE !(cRecord == "")
  259.     AADD(aFileList,cRecord)
  260.     cRecord := ReadFlatRecord(nHandle)
  261.   ENDDO
  262.   FCLOSE(nHandle)
  263.   IF !EMPTY(aFileList)
  264.     lStatus := .F.
  265.   ENDIF
  266.   FERASE(cFileName)
  267. RETURN lStatus
  268.  
  269. //-----------------------------------------------------------------------------
  270. // The UnZipCallBack function is used internally by the UnZipFile, ViewZip
  271. //   and TestUnZip functions.  You do not need to call it in your application.
  272. //
  273. FUNCTION UnZipCallBack(hwnd, nMsgNum, cMsg, cFileName, lClose)
  274.   STATIC nHandle := 0
  275.   DEFAULT lClose TO .F.
  276.   IF !(cFileName == NIL)
  277.     nHandle := FCREATE(cFileName)
  278.   ELSE
  279.     IF lClose
  280.       FCLOSE(nHandle)
  281.     ELSE
  282.       DO CASE
  283.       CASE nMsgNum == ADDZIP_ERROR
  284.         FWRITE(nHandle,cMsg+CHR(13)+CHR(10))
  285.       CASE nMsgNum == ADDZIP_VIEW
  286.         FWRITE(nHandle,cMsg+CHR(13)+CHR(10))
  287.       CASE nMsgNum == ADDZIP_UNZIPPED
  288.         FWRITE(nHandle,SplitZipMessage(cMsg)[2]+CHR(13)+CHR(10))
  289.       ENDCASE
  290.     ENDIF
  291.   ENDIF
  292. RETURN NIL
  293.  
  294. //----------------------------------------------------------
  295. // The SplitZipMessage function is used internally by the ViewZip and
  296. //   UnZipCallBack functions.  You do not need to call it in your application.
  297. //
  298. FUNCTION SplitZipMessage(cMsg)
  299.   LOCAL aZipInfo := {}, nPos := AT("|",cMsg)
  300.   WHILE !(nPos == 0)
  301.     AADD(aZipInfo,SUBSTR(cMsg,1,nPos-1))
  302.     cMsg := SUBSTR(cMsg,nPos+1)
  303.     nPos := AT("|",cMsg)
  304.   ENDDO
  305. RETURN aZipInfo
  306.  
  307. //-----------------------------------------------------------------------------
  308. //  The ReadFlatRecord function is used internally by the UnZipFile, ViewZip,
  309. //    and TestUnZip functions.  You do not need to call it in your application.
  310. //    It does not use any addZIP functions, it is just used to read a record
  311. //    from a text file.
  312. //
  313. FUNCTION ReadFlatRecord(nFileHandle,nRecNum,cErrorRecord)
  314.   LOCAL nPos := 0, cRecord := "", I := 0, lError := .F., nRecLen := 0
  315.   IF cErrorRecord == NIL
  316.     cRecord := ""
  317.   ENDIF
  318.   IF nRecNum != NIL
  319.     IF nRecNum > 1
  320.       I := 1
  321.       FSEEK(nFileHandle,0,FS_SET)
  322.       WHILE !lError .AND. (I < nRecNum)
  323.         cRecord := FREADSTR(nFileHandle,READFLATRECORD_BUFFERSIZE)
  324.         nRecLen := LEN(cRecord)
  325.         nPos := AT(CHR(13),cRecord)
  326.         IF (LEN(cRecord) == READFLATRECORD_BUFFERSIZE) .OR. (nPos != 0)
  327.           FSEEK(nFileHandle,((nPos +1)-nRecLen),FS_RELATIVE)
  328.           ++I
  329.         ELSE
  330.           lError := .T.
  331.           cRecord := ""
  332.         ENDIF
  333.       ENDDO
  334.     ELSE
  335.       FSEEK(nFileHandle,0,FS_SET)
  336.     ENDIF
  337.   ENDIF
  338.   cRecord := FREADSTR(nFileHandle,READFLATRECORD_BUFFERSIZE)
  339.   nRecLen := LEN(cRecord)
  340.   nPos := AT(CHR(13),cRecord)
  341.   IF (LEN(cRecord) == READFLATRECORD_BUFFERSIZE) .OR. (nPos != 0)
  342.     cRecord := SUBSTR(cRecord,1,nPos-1)
  343.     FSEEK(nFileHandle,((nPos +1)-nRecLen),FS_RELATIVE)
  344.   ELSE
  345.     cErrorRecord := cRecord
  346.     cRecord := ""
  347.   ENDIF
  348. RETURN cRecord
  349.  
  350.