Announcement

Collapse
No announcement yet.

Zlib.dll - Can I zip a folder (only including the containing folder as a path)

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Zlib.dll - Can I zip a folder (only including the containing folder as a path)

    I can successfully use zlib to unzip "*.docx files" files and these contain several folders.

    After editing the files I cannot find by the zlib Header file how to re-zip the files including a single folder as the path

  • #2
    When I wrote a utility (called dirzip) to compress all subdirectories into a single zip file (each subdirectory in it's own zip file), I used a dll called litezip. I'm not sure exactly where I got it, but I'm pretty sure a post on the forums here pointed me in it's direction. I think you might have good luck trying that dll if the docx format uses actual zip files (I've never checked, so can't say for sure), though if it's using zlib compression like the pdf files do, then that won't help you any, but I thought I'd mention it here in case you could use it instead.
    http://www.softcon.com]


    for hosting/internet


    access.

    Comment


    • #3
      Sorry I forgot to reply with the solution:

      If the first file to be zipped contains a path whose final folder is the one that you wish use as the restore folder only for all the files then the first file:

      Code:
      AddToZipEx(inputpath & infilename, outputpath & outfilename, %ZIP_REL_PATH)
      However if the first file contains one or more sub folders then the first two files should be:

      Code:
      AddToZipEx(inputpath, outputpath & outfilename, %ZIP_REL_PATH)
      where "inputpath" is a path whose last folder is the required folder - there should be no filename.

      followed by the following for each subsequent file whose path may or may not contain subfolders.

      Code:
      AddToZipEx(inputpath & infilename, outputpath & outfilename, %ZIP_REL_PATH)
      The final file should be coded as:

      Code:
      AddToZipEx(inputpath & inline, outputpath & outfilename, %ZIP_REL_PATH + %ZIP_CLOSE)
      The current $INCLUDE "zlib.inc" will flag an error at one point (I think when closing the zip file) but I ignore it!

      Last edited by Robert Wallace; 14 Aug 2018, 07:21 AM. Reason: [\QUOTE] didn't display a Glyph.

      Comment


      • #4
        I don't seem to be able to edit the above post so the following corrections should be made:

        where "inputpath" is a path whose last folder is the required folder - there should be no filename.
        Add:

        The path must finish with a
        Code:
        "\"
        .

        You will also have to modify the "zlib.inc" as follows:

        Code:
         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
        '-----------------Added by Bob Wallace-----------------------------------------
                IF REMAIN$(ZSrc, szBasePath) = "" THEN EXIT FUNCTION
        '------------------------------------------------------------------------------
                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
        The advantage is that if you are zipping multiple files in different subfolders you don't need to look for the first file that is in the basic path.


        Comment


        • #5
          I don't seem to be able to edit the above post ...
          That post is a week old. I don't know how long the editable period is, but it is hours not days. Besides, I think it makes a better explanation this way.
          Dale

          Comment


          • #6
            Yes, Robert I agree. The fleeting time for editing posts is way too short.

            Comment


            • #7
              I wrote that updated include (pbzlib.inc) for ZLIB long ago, never use it now anymore...
              This was the latest version:

              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"
              $ZLIB_VERSION               = "1.1.4"
              
              '==================================================================================================
              '                                           ---- CONSTANTS ----
              '--------------------------------------------------------------------------------------------------
              %Z_NO_FLUSH                 =  0        'Allowed Flush values; see deflate() for details
              %Z_PARTIAL_FLUSH            =  1        '(Z_PARTIAL_FLUSH obsolete, use Z_SYNC_FLUSH instead)
              %Z_SYNC_FLUSH               =  2
              %Z_FULL_FLUSH               =  3
              %Z_FINISH                   =  4
              
              %Z_OK                       =  0        'Return codes for compression/decompression functions
              %Z_STREAM_END               =  1        'Negative values are errors, positive values are for
              %Z_NEED_DICT                =  2        'special but normal events.
              %Z_ERRNO                    = -1
              %Z_STREAM_ERROR             = -2
              %Z_DATA_ERROR               = -3
              %Z_MEM_ERROR                = -4
              %Z_BUF_ERROR                = -5
              %Z_VERSION_ERROR            = -6
              
              %Z_FILTERED                 =  1        'Compression strategy (see DeflateInit2)
              %Z_HUFFMAN_ONLY             =  2
              %Z_DEFAULT_STRATEGY         =  0
              
              %Z_BINARY                   =  0        'Possible values for the data_type field
              %Z_ASCII                    =  1
              %Z_UNKNOWN                  =  2
              
              %Z_NULL                     =  0        'For initializing zalloc, zfree, opaque
              
              %Z_NO_COMPRESSION           =  0        'Compression levels
              %Z_BEST_SPEED               =  1
              %Z_BEST_COMPRESSION         =  9
              %Z_DEFAULT_COMPRESSION      = -1
              
              %Z_DEFLATED                 =  8         'Deflate compression(only one supported in this version)
              
              %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
              
              '==================================================================================================
              '                                           ---- ADLER32 ----
              '..................................................................................................
              ' Adler32. Update a running Adler-32 checksum with the bytes buf[0..Len-1] and return the updated
              ' checksum. If buf Is Null, this function returns the required initial value for the checksum.
              ' An Adler-32 checksum is almost as reliable as a CRC32 but can be computed much faster. Example:
              '
              '   result = adler32(0, Z_NULL, 0)
              '   While (read_buffer(buffer, length) != EOF) {
              '       adler = adler32(adler, buffer, length);
              '     }
              '     If (adler != original_adler) error();
              '..................................................................................................
              Declare Function Adler32 Lib "ZLib.dll" Alias "adler32" (ByVal adler As Dword, _
                                                                       Buf As Asciiz, _
                                                                       ByVal BufLen As Dword) As Dword
              
              '==================================================================================================
              '                                           ---- COMPRESS ----
              '..................................................................................................
              ' Compresses the source buffer into the destination buffer. sourceLen is the byte length of the
              ' source buffer. Upon entry, destLen is the total size of the destination buffer, which must
              ' be at least 0.1% larger than sourceLen plus 12 bytes. Upon exit, destLen is the actual size of
              ' the compressed buffer.
              ' This function can be used to compress a whole file at once if the input file is mmap'ed.
              ' compress returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if
              ' there was not enough room in the output buffer.
              '..................................................................................................
              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
              
              '==================================================================================================
              '                                           ---- CRC32 ----
              '..................................................................................................
              ' Update a running crc with the bytes buf[0..Len-1] and return the updated crc. If buf is
              ' Null, the function returns required initial value for the crc. Pre- And Post-conditioning (one's
              ' complement) is done within the function so it shouldn't be done by the application. Example:
              '
              '   uLong crc = crc32(0L, Z_NULL, 0);
              '
              '     While (read_buffer(buffer, length) != EOF) {
              '       crc = crc32(crc, buffer, length);
              '     }
              '     If (crc != original_crc) error();
              '..................................................................................................
              Declare Function Crc32 Lib "ZLib.dll" Alias "crc32" (ByVal crc As Dword, _
                                                                   buf As Asciiz, _
                                                                   ByVal BufLen As Dword) As Dword
              
              '===================================================================================================
              '                                           ---- ZLIBVERSION ----
              '...................................................................................................
              ' The application can compare zlibVersion and ZLIB_VERSION for consistency. If the first character
              ' differs, the library code actually used is not compatible with the zlib.h header file used by the
              ' application. This check is automatically made by deflateInit and inflateInit.
              '...................................................................................................
              'Declare Function ZLibVersion Lib "ZLib.dll" Alias "zlibVersion" () As Dword' Asciiz
              Declare Function ZLibVers Lib "ZLib.dll" Alias "zlibVersion" () As Dword' Asciiz
              
              Function ZLibVersion () As String
                Local ZlvPtr As Asciiz Ptr
                ZlvPtr = ZLibVers ()
                Function = @ZlvPtr
              End Function
              '==================================================================================================
              '                                           ---- UNCOMPRESS ----
              '..................................................................................................
              ' Uncompress. Decompresses the source buffer into the destination buffer. sourceLen is the byte
              ' length of the source buffer. Upon entry, destLen is the total size of the destination buffer,
              ' which must be large enough to hold the entire uncompressed data. (The size of the uncompressed
              ' data must have been saved previously by the compressor and transmitted to the decompressor by
              ' some mechanism outside the scope of this compression library.)
              ' Upon exit, destLen is the actual size of the compressed buffer. This function can be used to
              ' decompress a whole file at once if the input file is mmap'ed.
              ' uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if
              ' there was not enough room in the output buffer, or Z_DATA_ERROR if the input data was corrupted.
              '..................................................................................................
              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
              '               (If %ZIP_REL_PATH is used, it must be used in first call only)
              ' 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
              
              '==================================================================================================
              Regards,
              Peter

              Comment

              Working...
              X