Results 1 to 8 of 8

Thread: VB6 - Zipper & ZipWriter, Zipping from VB programs

Hybrid View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Lightbulb VB6 - Zipper & ZipWriter, Zipping from VB programs

    Background

    A lot of us find the need to create ZIP archives programmatically from time to time. There are a number of techniques we can use, from spawning external utility programs to 3rd party components to Shell automation.

    Here is yet another way to accomplish this: using the free, open source zlibwapi.dll


    Minizip

    In addition to providing a STDCALL version of ZLib that we can call easily from VB6 programs zlibwapi.dll includes the Minizip project code as well.

    Like a lot of open source hacked out by C coders this can be rough in many places, but it is widely used and well proven. It should have few if any bugs in the most recent version.

    I'm using version 1.25 here. You can get this from:

    Minizip: Zip and UnZip additionnal library

    See the typo there? This is just one symptom of some of the issues, but fortunately the code at least seems to work fine even if its source is wonky with lots of flaws in comments and general documentation issues.

    This DLL is not included in the attachment. You must download it yourself.

    In zlib125dll.zip there is the Win32 Windows DLL of my Windows DLL named Zlibwapi.dll that contains both zLib and Minilib.
    The file you want from this ZIP archive is:

    zlib125dll.zip\dll32\zlibwapi.dll


    ZipWriter

    ZipWriter is a VB6 Class that wraps zlibwapi.dll to provide you with a way to create a ZIP archive and actually write data into it as archived files with no intermediate disk I/O steps.

    You can use this as-is in many cases without the other code offered here.


    Zipper

    Zipper is a VB6 UserControl that wraps ZipWriter and a small helper ZipFile Class to give you the ability to create a ZIP archive (or add to an existing one) and add a list of disk files to it.

    Zipper.Zip is an async operation and reports back progress, errors, and completion through several events. There is a Zipper.Cancel method if you need that.


    ZipDemo

    ZipDemo is a VB6 project that demonstrates use of the items described above.

    You must download zlibwapi.dll and copy it into this project folder to run the program.

    Much of the bulk of this attachment consists of the sample files in the "samples2" folder included.


    There is a "ZipWriter" button that does a simple test of ZipWriter, creating a new ZIP archive "test.zip" with two files written to it. When that step completes the "Zipper" button is enabled.

    The "Zipper" button tests Zipper, adding the files it finds in the "samples2" folder to the "test.zip" created in the first stage of the demo. While running a progressbar is updated and a "Cancel" button is enabled.

    The "samples2" folder as supplied has just a few small files. Be sure to copy some larger files into it and rerun the program to see how things go with large files. The performance is fairly good.


    Remarks

    While you need to deploy zlibwapi.dll with your programs this is a standard DLL that you can feel free to place next to your EXE. No registration is required.

    The results are better than those achieved using most other common techniques. Progress/Cancel/Complete can be really nice to have. There is no need for the shaky, convoluted, hackish window spelunking people often resort to when automating Shell objects.

    There is a lot more you can do with zlibwapi.dll too. You can read from ZIP archives, unzip them, compress separate files outside of ZIP archives, compress/expand data in memory, etc. Even the huge-file Zip64 format is supported.

    All you need is to write additional wrappers or just make the calls directly.
    Attached Files Attached Files

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - Zipper & ZipWriter, Zipping from VB programs

    Here is another demo. This one just shows creating a ZIP archive from a folder, but it also includes subfolders if any are present.

    That is something not spelled out in the previous example.


    Name:  sshot.png
Views: 2099
Size:  5.5 KB

    Demo upon completion


    The sample data is included.
    Attached Files Attached Files

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - Zipper & ZipWriter, Zipping from VB programs

    This is an update: Zipper 2 (version 2).

    This expands the ZipWriter class to handle password encryption of archived files, and the ability to specify the timestamp to be recorded in the archive for each file.

    The Zipper UserControl is expanded to accept file passwords and offers the option to use the disk file's LastUpdate timestamp instead of the result of the Now() function. This means Zipper has two helper classes now: ZipFile as before as well as the new ZipDateTimes. Nothing prevents the code in ZipDateTimes from being folded into Zipper itself, but keeping it separate helps keep Zipper at least a little easier to understand.


    Once again, the point of Zipper is to make it easy to ZIP up one or more files asynchronously with progress updates and completion notification. The result is far cleaner than using hacks to try to detect when (and even if) a Shell32 ZIP process completes and completes properly.

    A further enhancement might be the option to use the ZIP64 format to allow you to automate archiving of huge files. Note that until Windows Vista there was no File Explorer (Shell32) support for ZIP64.
    Attached Files Attached Files

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - Zipper & ZipWriter, Zipping from VB programs

    Version 2.1, which adds ZipperSync.

    ZipperSync is a synchronous "clone" (very close copy) of the Zipper UserControl. ZipperSync may be a little easier to read because it doesn't have to use a time-slicing Timer to drive the I/O and processing.

    If your files to ZIP up into an archive are fairly small so that you do not need async operation to remain responsive you might choose ZipperSync instead of Zipper.

    The demo is very similar to the Zipper 2 Demo above.


    Edit:

    While hilarious if nobody had already downloaded it (not so funny since several people had), the attachment was a bit of a mess. It had a broken version of the new ZipperSync and was still using the Zipper control.

    Attachment replaced. Sorry for the error.
    Attached Files Attached Files
    Last edited by dilettante; Dec 19th, 2014 at 03:01 PM. Reason: replaced attachment

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - Zipper & ZipWriter, Zipping from VB programs

    ZipperSync posted above is full of serious bugs!

    These have been corrected, and a new AddBLOB() method has been added in version 2.2 posted here. This is being posted in a minimal project showing how easy it can be to use.

    Note that the new AddBLOB() means I had to extend the ZipFile class. While Zipper.ctl has not had a similar AddBLOB() method added it should still work with the new ZipFile 2.2 as well.

    AddBLOB() means you can add any arbitrary Byte array (BLOB) data as a "file" in the archive without writing it out as a temporary disk file.


    A little confusing, but:

    You can take (1.) the previous code posted above as version 2.1, (2.) replace all of the Zip*.* files by the new ones from this archive, and then (3.) everything should work fine (both Zipper 2 and ZipperSync 2.2).

    In other words... I have not included Zipper.cls this time since it did not change. If I add BLOB support to it later I'll repost all of the Zip*.* files in a future attachment.


    This ZipperSync class is very easy to use:

    Code:
    Private Sub cmdZipIt_Click()
        Dim TextBLOB() As Byte
    
        Text1.Enabled = False
        cmdZipIt.Enabled = False
    
        'Zip Text1.Text as "Text.txt" in "Sample.zip" archive:
        With New ZipperSync
            TextBLOB = StrConv(Text1.Text, vbFromUnicode) 'Store as ANSI.
            .AddBLOB TextBLOB, "Text.txt"
            .Zip App.Path & "\Sample.zip"
        End With
    
        lblStatus.Caption = "Complete"
    End Sub
    Attached Files Attached Files

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - Zipper & ZipWriter, Zipping from VB programs

    A reminder:

    See post #1 above, since you need a recent version of zlibwapi.dll for this to work. As mentioned above this is a "flat DLL" i.e. not an ActiveX DLL so it does not require registration.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - Zipper & ZipWriter, Zipping from VB programs

    BTW:

    I you can't or don't want to compile it yourself, there is a precompiled zlibwapi.dll version 1.2.8 at:

    GlobalPlatform

    This has zlib fixes beyond 1.2.5, and seems to work fine with my wrapper classes posted above.


    Edit:

    The page at that link no longer appears to offer precompiled binaries.
    Last edited by dilettante; Sep 30th, 2016 at 03:01 AM.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: VB6 - Zipper & ZipWriter, Zipping from VB programs

    In case it isn't clear, you can just skip the additional classes and use ZipWriter.cls alone if you are doing something very simple. Those other classes are used to abstract and "batch" the process of zipping multiple files. For a single file or two they may be more than you need.


    Here I'm using the same ZipWriter version 2 from the last few uploads (again without error handling for simplification - don't do that!) to accomplish the same thing as the example in post #5 above:

    Code:
    Private Sub cmdZipIt_Click()
        Dim TextBLOB() As Byte
    
        Text1.Enabled = False
        cmdZipIt.Enabled = False
    
        'Zip Text1.Text as "Text.txt" in "Sample.zip" archive:
        With New ZipWriter
            TextBLOB = StrConv(Text1.Text, vbFromUnicode) 'Store as ANSI.
            .OpenZip App.Path & "\Sample.zip"
            .OpenFileInZip "Text.txt"
            .WriteBytes TextBLOB
            .CloseFileInZip
            .CloseZip
        End With
    
        lblStatus.Caption = "Complete"
    End Sub
    So you can just bypass the extra classes that wrap ZipWriter, write your own wrapper, or pull the definitions and logic from ZipWriter and use them inline within some other module (.BAS, .FRM, etc.).

    ZipWriter itself contains the zlibwapi.dll definitions and wrapper logic, i.e. a "VB6 API binding" for the subset of zlibwapi.dll functionality required to create/write Zip archives.
    Attached Files Attached Files

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width