Updated version of ZLIB.DLL headerfile.
Added AddToZip and ExtractFromZip functions.
------------------
Peter.
mailto
[email protected][email protected]</A>
[This message has been edited by Peter Lameijn (edited September 08, 2004).]
Added AddToZip and ExtractFromZip functions.
Code:
'================================================================================================== ' ZLIB is a compression library compatible with the zip format. ' It is written by Jean-Loup Gailly and Mark Adler, and is freely available. ' Added AddToZip and ExtractFromZip functions. Peter Lameijn. 5/2002 ' Added UnzClose function (was missing) ' PBZLIB.INC PB/Dll 6.x / PB/WIN 7.0 Zlib.dll interface include ' Updated Jan 24, 2003 : Function AddToZipEx added. ' Updated Sep 08, 2004 : Added relative root zipping to AddToZipEx function '================================================================================================== #Include "Win32api.inc" %Z_NO_COMPRESSION =0 %Z_BEST_SPEED =1 %Z_BEST_COMPRESSION =9 %Z_DEFAULT_COMPRESSION =(-1) %Z_DEFLATED =8 %ZIP_PATH = &h0001 %ZIP_CLOSE = &h0002 %ZIP_REL_PATH = &h0004 '-------------------------------------------------------------------------------------------------- Type tm_unz_s tm_sec As Dword tm_min As Dword tm_hour As Dword tm_mday As Dword tm_mon As Dword tm_year As Dword End Type Type unz_global_info_s number_entry As Dword size_comment As Dword End Type Type unz_file_info_s version As Dword version_needed As Dword flag As Dword compression_method As Dword dosDate As Dword crc As Dword compressed_size As Dword uncompressed_size As Dword size_filename As Dword size_file_extra As Dword size_file_comment As Dword disk_num_start As Dword internal_fa As Dword external_fa As Dword tmu_date As tm_unz_s End Type Type zip_fileinfo_s tm_zip As tm_unz_s dosDate As Dword internal_fa As Dword external_fa As Dword End Type '================================================================================================== Declare Function compress Lib "zlib.dll" Alias "compress" (ByRef dest As Any, _ ByRef destLen As Any, ByRef src As Any, ByVal srcLen As Long) As Long '================================================================================================== Declare Function uncompress Lib "zlib.dll" Alias "uncompress" (ByRef dest As Any, _ ByRef destLen As Any, ByRef src As Any, ByVal srcLen As Long) As Long '================================================================================================== Declare Function unzOpen Lib "zlib.dll" Alias "unzOpen" (ByRef zpath As Asciiz) As Dword '================================================================================================== Declare Function unzGetGlobalInfo Lib "zlib.dll" Alias "unzGetGlobalInfo" (ByVal fh As Dword, _ ByRef pGlobalInfo As unz_global_info_s) As Long '================================================================================================== Declare Function unzGetGlobalComment Lib "zlib.dll" Alias "unzGetGlobalComment" _ (ByVal fh As Dword, ByRef szComment As Asciiz, ByVal BufSize As Dword) As Long '================================================================================================== Declare Function unzGoToFirstFile Lib "zlib.dll" Alias "unzGoToFirstFile"(ByVal fh As Dword) As Long '================================================================================================== Declare Function unzGoToNextFile Lib "zlib.dll" Alias "unzGoToNextFile" (ByVal fh As Dword) As Long '================================================================================================== Declare Function unzLocateFile Lib "zlib.dll" Alias "unzLocateFile" (ByVal fh As Dword, _ ByRef szFileName As Asciiz, ByVal CaseSensitivity As Long) As Long '================================================================================================== Declare Function unzGetCurrentFileInfo Lib "zlib.dll" Alias "unzGetCurrentFileInfo" ( _ ByVal fh As Dword, ByRef pFile_Info As unz_file_info_s, ByRef szFileName As Asciiz, _ ByVal FileNameBufLen As Dword, ByRef pExtra As Any, ByVal pExtraLen As Dword, _ ByRef szComment As Asciiz, ByVal szCommentLen As Dword) As Long '================================================================================================== Declare Function unzOpenCurrentFile Lib "zlib.dll" Alias "unzOpenCurrentFile" ( _ ByVal fh As Dword) As Long '================================================================================================== Declare Function unzCloseCurrentFile Lib "zlib.dll" Alias "unzCloseCurrentFile" ( _ ByVal fh As Dword) As Long '================================================================================================== Declare Function unzReadCurrentFile Lib "zlib.dll" Alias "unzReadCurrentFile" ( _ ByVal fh As Long, ByRef buf As Any, ByVal bufLen As Dword) As Long '================================================================================================== Declare Function UnzClose Lib "zlib.dll" Alias "unzClose" ( _ ByVal fh As Dword) As Long '================================================================================================== Declare Function zipOpen Lib "zlib.dll" Alias "zipOpen" (ByRef fPath As Asciiz, _ ByVal DoAppend As Long) As Dword '================================================================================================== Declare Function zipOpenNewFileInZip Lib "zlib.dll" Alias "zipOpenNewFileInZip" ( _ ByVal fh As Dword, ByRef szFileName As Asciiz, ByRef zipfi As zip_fileinfo_s, _ ByRef Extra_local As Any, ByVal Extra_local_size As Dword, ByRef Extra_global As Any, _ ByVal Extra_global_size As Dword, ByRef Comment As Asciiz, ByVal Method As Long, _ ByVal level As Long) As Long '================================================================================================== Declare Function zipWriteInFileInZip Lib "zlib.dll" Alias "zipWriteInFileInZip" ( _ ByVal fh As Dword, ByRef pBuf As Any, ByVal BufLen As Dword) As Long '================================================================================================== Declare Function zipCloseFileInZip Lib "zlib.dll" Alias "zipCloseFileInZip" ( _ ByVal fh As Dword) As Long '================================================================================================== Declare Function zipClose Lib "zlib.dll" Alias "zipClose" ( _ ByVal fh As Dword, ByRef global_comment As Asciiz) As Long '================================================================================================== Function CompressString (ByRef TheString As String) As Long Dim CmpSize As Long, TBuff As String, OrgSize As Long, Result As Long OrgSize = Len(TheString) 'Allocate stringspace buffers TBuff = String$(OrgSize + (OrgSize * 0.01) + 12, 0) CmpSize = Len(TBuff) 'Compress string (temporary string buffer) data Result = compress(ByVal StrPtr(TBuff), CmpSize, ByVal StrPtr(TheString), OrgSize) 'Crop the string and set it to the actual string. TheString = Left$(TBuff, CmpSize) 'Return error code (if any) Function = Result End Function '================================================================================================== Function DecompressString (ByRef TheString As String, ByRef OriginalSize As Long) As Long Dim CmpSize As Long, TBuff As String, Result As Long 'Allocate string space TBuff = String$(OriginalSize + (OriginalSize * 0.01) + 12, 0) CmpSize = Len(TBuff) 'Decompress result = uncompress(ByVal StrPtr(TBuff), CmpSize, ByVal StrPtr(TheString), Len(TheString)) 'Make string the size of the uncompressed string TheString = Left$(TBuff, CmpSize) 'Return error code (if any) DecompressString = Result End Function '================================================================================================== ' Extracts all files from a zip archive. If present, subdir paths are extracted and recreated. ' Returns: ' 0 : Error ' 1 and up: Number of files extracted ' ' On start, zipname must hold full zipfile path/name '================================================================================================== Function ExtractFromzip(ZipName As Asciiz) As Long Local hZip As Dword, szFilePath As Asciiz * %MAX_PATH, dwRet As Dword, fCnt As Dword Local szStr As Asciiz * %MAX_PATH, Extra As Asciiz * %MAX_PATH, FIS As unz_file_info_s Local Path As String, CreatePath As String, hFile As Long, RxBuf As String, ST As SYSTEMTIME Local FT As FILETIME, hWinFile As Dword, fLen As Dword fCnt = 0 hZip = UnzOpen (ZipName) 'Open zipfile If hZip Then 'If success, go to 1st file dwRet = UnzGotoFirstFile(hZip) 'in zip archive While IsFalse(dwRet) ' UnzGetCurrentFileInfo hZip, _ 'Get info of file FIS, _ ' szFilePath, _ ' SizeOf(szFilePath), _ ' Extra, _ ' SizeOf(Extra), _ ' szStr, _ ' SizeOf(szStr) ' Path = szFilePath ' '-------------------------------------------------------------------------------------------------- If Right$(Path,1) <> "/" Then ' While InStr (Path, Any "/") 'Change "/" into "\" and CreatePath = Extract$(Path, Any "/") 'create Dirs if nonexistant dwRet = InStr(Path, Any "/") ' Mid$(Path, dwRet, 1) = "\" ' If Dir$(CreatePath, %SUBDIR) = "" Then MkDir CreatePath ' Wend ' '-------------------------------------------------------------------------------------------------- dwRet = UnzOpenCurrentFile (hZip) 'Open the file If IsFalse (dwRet) Then ' RxBuf = Space$(FIS.Uncompressed_Size) 'Allocate buffer space fLen = Len(RxBuf) + 10 ' dwRet = UnzReadCurrentFile (hZip, ByVal StrPtr(RxBuf), fLen) 'Read zipfile If dwRet Then ' hFile = FreeFile 'Open output file Path = MCase$(Path) 'and write to it Open Path For Binary As hFile ' Put$ hFile, RxBuf ' hWinFile = FileAttr (hFile, 2) 'Set new filedate ST.wSecond = FIS.tmu_date.tm_sec 'same as stored date ST.wMinute = FIS.tmu_date.tm_min ' ST.wHour = FIS.tmu_date.tm_hour ' ST.wDay = FIS.tmu_date.tm_mday ' ST.wMonth = FIS.tmu_date.tm_mon + 1 ' ST.wYear = FIS.tmu_date.tm_year ' SystemTimeToFileTime ST, FT ' LocalFileTimeToFileTime FT, FT ' SetFileTime hWinFile, FT, FT, FT ' Close hFile ' Incr fCnt ' UnzCloseCurrentFile hZip ' End If ' End If ' End If ' dwRet = UnzGotoNextFile(hZip) ' Wend ' UnzClose hZip ' End If ' Function = fCnt ' End Function ' '================================================================================================== ' AddToZip (SourceFileName, ZipFileName) ' On first call, zipfile is created. ' On next call, file is added to it ' On last call, set sourcefilename empty, and zipfile is closed ' Example: If test1 and test2 must be put into testzip.zip, use: ' AddToZip "test1", "testzip.zip" ' AddToZip "test2", "testzip.zip" ' AddToZip "", "" '-------------------------------------------------------------------------------------------------- Function AddToZip (ZipSource As String, ZipDest As String) As Long Local LBuff$, LRet&, LBlockSize&, LhFile&, LSource As Asciiz * 128, LDest As Asciiz * 128 Local ZipFi As ZIP_FILEINFO_S, LSys As SYSTEMTIME, LLoFTime As FILETIME Local OpenBuff As OFSTRUCT, CFTime As FILETIME, LAFTime As FILETIME, LWFTime As FILETIME Static LhZip As Dword If ZipSource <> "" Then LSource = ZipSource LDest = ZipDest If LhZip = 0 Then LhZip = ZipOpen (LDest,0) If LhZip Then LhFile = OpenFile(LSource, OpenBuff, %OF_READWRITE) 'Retrieve date/time GetFileTime LhFile, CFTime, LAFTime, LWFTime 'from original file FileTimeToLocalFileTime LWFTime, LLOFTime FileTimeToSystemTime LLOFTime, LSys zipfi.tm_zip.tm_sec = LSys.wSecond zipfi.tm_zip.tm_min = LSys.wMinute zipfi.tm_zip.tm_hour = LSys.wHour zipfi.tm_zip.tm_mday = LSys.wDay zipfi.tm_zip.tm_mon = LSys.wMonth -1 zipfi.tm_zip.tm_year = LSys.wYear zipfi.DosDate = 0 zipfi.internal_fa = 0 zipfi.external_fa = 0 CloseHandle LhFile While InStr(LSource,"\") : LSource = Right$(LSource,Len(LSource)-1) : Wend LRet = ZipOpenNewFileInZip (LhZip, LSource, zipfi, ByVal 0,0,ByVal 0,0, ByVal 0, _ %Z_DEFLATED, %Z_DEFAULT_COMPRESSION) End If LhFile = FreeFile Open ZipSource For Binary As LhFile While Not (Eof(LhFile)) LBlockSize = Lof(LhFile) - Loc(LhFile) If LBlockSize > 32768 Then LBlockSize = 32768 Else Incr LBlockSize Get$ LhFile, LBlockSize, LBuff LRet = ZipWriteInFileInZip (LhZip, ByVal StrPtr(LBuff), Len(LBuff)) If Seek(LhFile) => Lof(LhFile) Then Exit Loop Wend Close LhFile ZipCloseFileInZip LhZip Else ZipClose LhZip, ByVal 0 : LhZip = 0 End If End Function '================================================================================================== ' FindFileInZip (FileName, ZipName) ' Tests for presence of file in archive ' Returns 0 if found, else non zero '================================================================================================== Function FindFileInZip (FileName As String, ZipName As String) As Long Local hUnzip As Dword, ZSrc As Asciiz * %MAX_PATH, ZDest As Asciiz * %MAX_PATH Local FIS As Unz_File_Info_s, dwRet As Dword, lRet As Long, szPath As Asciiz * %MAX_PATH Local Extra As Asciiz * %MAX_PATH, szStr As Asciiz * %MAX_PATH If Dir$(ZipName) = "" Then Function = -2 : Exit Function ZSrc = FileName ZDest = ZipName lRet = InStr(zSrc,":") If lRet > 0 Then zSrc = StrDelete$(zSrc, 1, lRet) While (Left$(zSrc,1) = "\") zsrc = StrDelete$(zSrc, 1, 1) Wend hUnzip = UnzOpen(ZDest) If IsFalse hUnzip Then Function = -3 : Exit Function dwRet = UnzGoToFirstFile(hUnzip) While IsFalse(dwRet) UnzGetCurrentFileInfo hUnzip, FIS, szPath, SizeOf(szPath), Extra, SizeOf(Extra), szStr, SizeOf(szStr) If UCase$(szPath) = UCase$(zSrc) Then Function = 0 : UnzClose hUnzip : Exit Function dwRet = UnzGoToNextFile(hUnzip) Wend Function = -1 UnzClose hUnzip End Function '================================================================================================== ' AddToZipEx (SourceFileName, ZipFileName, Flags) ' Does the same as AddToZip, with the following additions: ' - If Zip archive exists, file is added to it. ' - Possibility to in/exclude path info for added files. ' Added parameter Flags, which can be combination of: ' %ZIP_CLOSE : Archive is closed after file is added ' %ZIP_PATH : Add path to file (On unzipping path will be recreated) ' %ZIP_REL_PATH : Save this as relative basepath ' (Relative root is taken from first call to function) ' Example: If test1 and test2 must be put into testzip.zip, use: ' AddToZip "test1", "testzip.zip" ' AddToZip "test2", "testzip.zip", %ZIP_CLOSE ' ' Returns: > 0 if OK (Value is number of files in archive) ' ' '================================================================================================== Function AddToZipEx (ZipSource As String, ZipName As String, ByVal Flags As Dword) As Long Local LRet As Long, BlkSize As Long, zBuff As String, ZipFi As ZIP_FILEINFO_S Local hFile As Dword, dwRet As Dword, zLen As Dword, LSys As SYSTEMTIME, OpenBuff As OFSTRUCT Local ZSrc As Asciiz * %MAX_PATH, ZDest As Asciiz * %MAX_PATH, FIS As Unz_File_Info_s Local szFilePath As Asciiz * %MAX_PATH, Extra As Asciiz * %MAX_PATH, szStr As Asciiz * %MAX_PATH Local LLoFTime As FILETIME, CFTime As FILETIME, LAFTime As FILETIME, LWFTime As FILETIME Static hZip As Dword, hUnZip As Dword,ZipCnt As Long, szBasePath As Asciiz * %MAX_PATH '-------------------------------------------------------------------------------------------------- ZSrc = ZipSource ZDest = ZipName If (Dir$(ZipSource) <> "") And (hZip = 0) Then 'First loop? If Dir$("Tmp$Zip") <> "" Then Kill "Tmp$Zip" 'Kill existing tmpzip hZip = ZipOpen ("Tmp$Zip", 0) 'and open new one If (Dir$(ZipName) <> "") Then hUnZip = UnzOpen (ZDest) 'Open archive if exists ZipCnt = 0 If (Flags And %ZIP_REL_PATH) Then szBasePath = Left$(ZipSource,InStr(-1, ZipSource, Any "\")) 'Strip filename lRet = InStr(szBasePath,":") 'Strip driveletter If lRet > 0 Then szBasePath = StrDelete$(szBasePath, 1, lRet) ' While (Left$(szBasePath,1) = "\") 'Strip leading '\' szBasePath = StrDelete$(szBasePath, 1, 1) Wend End If End If ' If hUnzip Then 'File existed, process dwRet = UnzGoToFirstFile(hUnzip) While IsFalse (dwRet) UnzGetCurrentFileInfo hUnzip, _ FIS, _ szFilePath, _ SizeOf(szFilePath), _ Extra, _ SizeOf(Extra), _ szStr, _ SizeOf(szStr) dwRet = UnzOpenCurrentFile(hUnzip) If IsFalse(dwRet) Then ZBuff = Space$(FIS.Uncompressed_Size) ZLen = Len(ZBuff) + 10 dwRet = UnzReadCurrentFile(hUnzip, ByVal StrPtr(ZBuff), ZLen) If dwRet Then UnzCloseCurrentFile hUnzip ZipFi.tm_zip = FIS.tmu_Date ZipFi.DosDate = 0 ZipFi.internal_fa = 0 ZipFi.external_fa = 0 dwRet = ZipOpenNewFileInZip(hzip, szFilePath, ZipFi, ByVal 0, 0, ByVal 0, 0, ByVal 0,%Z_DEFLATED, %Z_DEFAULT_COMPRESSION) dwRet = ZipWriteInFileInZip(hZip, ByVal StrPtr(ZBuff), Len(ZBuff)) dwRet = ZipCloseFileInZip(hZip) Incr ZipCnt End If End If dwRet = UnzGoToNextFile(hUnzip) Wend UnzClose hUnzip hUnzip = 0 End If If Dir$(ZipSource) <> "" Then If hZip = 0 Then hZip = ZipOpen (ZDest,0) If hZip Then hFile = OpenFile(ZSrc, OpenBuff, %OF_READWRITE) 'Retrieve date/time GetFileTime hFile, CFTime, LAFTime, LWFTime 'from original file FileTimeToLocalFileTime LWFTime, LLOFTime FileTimeToSystemTime LLOFTime, LSys zipfi.tm_zip.tm_sec = LSys.wSecond zipfi.tm_zip.tm_min = LSys.wMinute zipfi.tm_zip.tm_hour = LSys.wHour zipfi.tm_zip.tm_mday = LSys.wDay zipfi.tm_zip.tm_mon = LSys.wMonth -1 zipfi.tm_zip.tm_year = LSys.wYear zipfi.DosDate = 0 zipfi.internal_fa = 0 zipfi.external_fa = 0 CloseHandle hFile lRet = InStr(zSrc,":") If lRet > 0 Then zSrc = StrDelete$(zSrc, 1, lRet) While (Left$(zSrc,1) = "\") zsrc = StrDelete$(zSrc, 1, 1) Wend If IsFalse(Flags And (%ZIP_PATH Or %ZIP_REL_PATH)) Then While InStr(ZSrc,"\") : ZSrc = Right$(ZSrc,Len(ZSrc)-1) : Wend End If If (Flags And %ZIP_REL_PATH) Then If Remain$(ZSrc, szBasePath) <> "" Then ZSrc = Remain$(ZSrc, szBasePath) End If LRet = ZipOpenNewFileInZip (hZip, ZSrc, zipfi, ByVal 0,0,ByVal 0,0, ByVal 0, _ %Z_DEFLATED, %Z_DEFAULT_COMPRESSION) End If hFile = FreeFile Open ZipSource For Binary As hFile While Not (Eof(hFile)) BlkSize = Lof(hFile) - Loc(hFile) If BlkSize > 32768 Then BlkSize = 32768 Else Incr BlkSize Get$ hFile, BlkSize, zBuff LRet = ZipWriteInFileInZip (hZip, ByVal StrPtr(zBuff), Len(zBuff)) If Seek(hFile) => Lof(hFile) Then Exit Loop Wend Incr ZipCnt Close hFile ZipCloseFileInZip hZip End If If (Flags And %ZIP_CLOSE) Then ZipClose hZip, ByVal 0 Kill ZipName FileCopy "Tmp$Zip", ZipName Kill "Tmp$Zip" szBasePath = "" hZip = 0 End If Function = ZipCnt End Function '==================================================================================================
Peter.
mailto

[This message has been edited by Peter Lameijn (edited September 08, 2004).]
Comment