Giter VIP home page Giter VIP logo

ziparchive's Introduction

cZipArchive

A single-class pure VB6 library for zip archives management

Usage

Just include cZipArchive.cls to your project and start using instances of the class like this:

Simple compression

With New cZipArchive
    .AddFile App.Path & "\your_file"
    .CompressArchive App.Path & "\test.zip"
End With

Compress all files and sub-folders

With New cZipArchive
    .AddFromFolder "C:\Path\To\*.*", Recursive:=True
    .CompressArchive App.Path & "\archive.zip"
End With

Decompress all files from archive

With New cZipArchive
    .OpenArchive App.Path & "\test.zip"
    .Extract "C:\Path\To\extract_folder"
End With

Method Extract can optionally filter on file mask (e.g. Filter:="*.doc"), file index (e.g. Filter:=15) or array of booleans with each entry to decompress index set to True.

Extract single file to target filename

OutputTarget can include a target new_filename to be used when extracting a specific file from the archive.

With New cZipArchive
    .OpenArchive App.Path & "\test.zip"
    .Extract "C:\Path\To\extract_folder\new_filename", Filter:="your_file"
End With

Get archive entry uncompressed size

By using FileInfo property keyed on entry filename in first parameter and zipIdxSize like this

With New cZipArchive
    .OpenArchive App.Path & "\test.zip"
    Debug.Print .FileInfo("report.pdf", zipIdxSize)
End With

List files in zip archive

By using FileInfo propery keyed on entry numeric index in first parameter like this

Dim lIdx            As Long
With New cZipArchive
    .OpenArchive App.Path & "\test.zip"
    For lIdx = 0 To .FileCount - 1
        Debug.Print "FileName=" & .FileInfo(lIdx, zipIdxFileName) & ", Size=" & .FileInfo(lIdx, zipIdxSize)
    Next
End With

Here is a list of available values for the second parameter of FileInfo:

Name
0 zipIdxFileName
1 zipIdxAttributes
2 zipIdxCrc32
3 zipIdxSize
4 zipIdxCompressedSize
5 zipIdxComment
6 zipIdxLastModified
7 zipIdxMethod
8 zipIdxOffset
9 zipIdxFlags

Encryption support

Make sure to set Conditional Compilation in Make tab in project's properties dialog to include ZIP_CRYPTO = 1 setting for crypto support to get compiled from sources. By default crypto support is not compiled to reduce footprint on the final executable size.

With New cZipArchive
    .OpenArchive App.Path & "\test.zip"
    .Extract App.Path & "\test", Password:="123456"
End With

Use Password parameter on AddFile method together with EncrStrength parameter to set crypto used when creating archive.

EncrStrength Mode
0 ZipCrypto (default)
1 AES-128
2 AES-192
3 AES-256 (recommended)

Note that default ZipCrypto encryption is weak but this is the only option which is compatible with Windows Explorer built-in zipfolders support.

In-memory operations

Sample utility function ReadBinaryFile in /test/basic/Form1.frm returns byte array with file's content.

Dim baZip() As Byte
With New cZipArchive
    .AddFile ReadBinaryFile("sample.pdf"), "report.pdf"
    .CompressArchive baZip
End With
WriteBinaryFile "test.zip", baZip

Method Extract accepts byte array target too.

Dim baOutput() As Byte
With New cZipArchive
    .OpenArchive ReadBinaryFile("test.zip")
    .Extract baOutput, Filter:=0    '--- archive's first file only
End With

ToDo (not supported yet)

- Deflate64 (de)compressor
- VBA7 (x64) support

ziparchive's People

Contributors

tannerhelland avatar wqweto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ziparchive's Issues

Unzip with password not working

Hi
Please check and guide me on below code

With m_oZip
.OpenArchive "c:\temp\crdata.zip"
Set m_oExtractInMemory = m_oZip
bResult = .Extract("c:\temp", , "1111")
Set m_oExtractInMemory = Nothing
sLastError = .LastError
End With

Get file zip from zip archive ?

Hi (wqweto),

Using this model :

.OpenArchive "C:\TEMP\aaa.zip"
.Extract baOutput, "mem/report.pdf"
WriteBinaryFile "C:\TEMP\report.pdf", baOutput

Is there a way to know the size of "mem/report.pdf" before writing it in "C:\TEMP" ?

Tried by search in the doc but "size" word appears too much times to makes searching revelant.

Thanks :)

Modified date issue

hi nice to meet you I'm back after a long time.
A problem occurred while using "ZipArchive" again. :)

I have a file where the file date in the FileInfo method is about 1 second greater than the file date before compression.
Can you make the actual file and date the same?
And is it possible to keep the same date as the actual file when unpacking?

Include PuTTY license (just to be safe)

The ever-paranoid users at vbforums have raised concerns about PuTTY's licensing model as used in this project:

http://www.vbforums.com/showthread.php?848511-Is-there-a-way-to-work-around-the-Resource-Editor

I tried to keep things civil, but you know how it gets over there...

Anyway, to keep them happy, maybe it's worthwhile to include an explicit copy of PuTTY's license in the /lib/sshzlib/ folder, since PuTTY's developers are sloppy about including copyright and licensing details in each source file.

(Thanks again for sharing this project! I hope to test it on parsing ORA files in the near future...)

Encryption under VBA

Hi ,

I've tested under VB6 so far and here your class works well - but the actual goal is, to include the class in an Excel marco: here the archives are created, but they are not encrypted.
I am using Excel 2010 32bit under Windows 10 - but the marco should work for 32 and 64 bit Excel version and under all Windows OS from XP on (lower encryption strength for XP and Vista is ok, because these operating systems are also not really secure anymore).
Is it possible to make encryption work under VBA too?

Thanks for you work,
Peter

EDIT: It is working - my bad! In the Excel macro I forgot to add the new ZIP_CRYPTO = 1 arguement...

Unzip a file from subfolder of zip, in a folder without subfolder

Hi wqweto,

Trying to run your app :) Successfully ziped the 3 example files from C:\TEMP (replaces D:\TEMP as well as on the VM I have only C:\ ) to aaa.zip (removed level in the zip filename), with Command_3 sub.
So the aaa.zip has inside:

mem/enwik8.txt
mem/report.pdf
mem/report2.pdf
mem/thunk.bin

Now trying to extract a file (for example mem/report2.pdf) to C:\TEMP\Unzip but without creating the "mem" subfolder.
The goal is to get C:\TEMP\Unzip\report.pdf .

Tried Command_4 sub, where I changed bResult for :
.bResult = .Extract ("C:\TEMP\Unzip\", "mem/report2.pdf"

I get nothing.

Commented the lines :

Set m_oExtractInMemory = m_oZip
Set m_oExtractInMemory = Nothing

I get file extracted but it is here C:\TEMP\Unzip\mem\report.pdf

Is there a way to ride of the "mem" subfolder ?

Thanks :)
Couin

Source of the thunk code

This seem to use thunking to enable some of the functionality. I am assuming this is only going to work on 32-bit and a different thunking code (if even possible?) would be needed to run on 64-bit? If so , how was that derived?

Extract File From Resource File

I have tried extracting the test.zip file using the file path, and it succeeded in extracting all the files with all their contents,

With New cZipArchive
    .OpenArchive Text1.Text 'test.zip
    .Extract App.Path & "\test", Password:="test123"
End With

but when I put the test.zip file into the resource file, and tried to extract it, only one file was extracted, how do I solve this?

Dim bytResourceData() As Byte
    bytResourceData = LoadResData(404, "CUSTOM")
  
With New cZipArchive
    .OpenArchive bytResourceData
    .Extract App.Path & "\test", Password:="test123"
End With

Thanks @wqweto

Compression file size

Thank you for the good code.

I tested it with the attached sample program.

The compressed file is slightly larger than the Windows default compression.

Is there a reason?

Size of archive exceeds 2GB

Thank you for the code.

I tested it with the attached sample program with a different .addfromfolder-command in command1 to my testdata (folder with subfolders, 3.5gb of about 3k documents).

When compressing this folder I get an error -1 "overflow" and the compressing stops.
Before that error 131 in pvVfsSeek is raised because of a negative lPosition value.

Do you have an idea to solve this issue to compress to archives larger than 2 gb?

Small changes/features to support OpenRaster spec

Hi @wqweto. I'm looking at adding OpenRaster file format support to PhotoDemon. The spec is very straightforward, basically constructed around existing OpenDocument rules. cZipArchive makes my job much easier!

I haven't studied everything in great detail, but I notice a few odd requirements in the spec that (perhaps?) are not covered by ZipArchive at present. These are independent from things like Zip64 support, which are a totally different beast - and not what I'd dare ask for, since PhotoDemon couldn't handle 4gb+ files cleanly anyway.

  • The required mimetype file must be uncompressed (e.g. STORED in zip parlance). I wonder if ZipArchive could expose a flag for adding individual entries as STORED vs DEFLATED.
  • UTF-8 filenames are required by the spec. I'm not sure how much work it is to get/set the UTF-8 flag for zip filenames (or if this is tied to Zip64 format), but the spec mentions that the flag is important e.g.:

The file names within OpenRaster "zip files" are case-sensitive and must be UTF-8 encoded. If you decide to write non-ascii file names, be aware that the Zip format has an UTF-8 flag for each file name. Make sure this flag is set, otherwise the content might get decoded as cp437.

  • As an unrelated convenience, it would be very helpful to retrieve a list of filenames as perhaps a string array, to validate ORA file format correctness before attempting actual extraction of individual files (which could be extremely large). I'm using my own (ugly) code to do this at present, and could send a PR if you don't mind some potentially hideous code... :)

Apologies if any of these items are already covered and I just didn't review the code carefully enough! TIA

UAC

I created a very small project that uses this class but the project icon has the small shield of the UAC and, on startup, UAC windows appears.
Is there any way to solve the problem?
Or should I try to solve the UAC problem in general?

Size limits ? Not all files are archived

Hi :)

I'm near from achieve backup and restore feature, looks running well with Jingle Palette's sample files.
And I tried with my jingles set (folder size is 788 MB but not all files are used), and on restoring , wow, a lot of jingles missing.
Started to investigate, files are listed, but archive is corrupted, I can not read or extract any file in Windows explorer. Zip file size in 262 144 kB (in Size column of Explorer)
Tried with removing files to archive, zip is no long corrupted since its under 262 144 kB.

Tried with some files with the test/basic project to rid of an eventually wrong code in my project.
image

Here is the co, based on existing Command3_click :


Private Sub Command3_Click()
    Dim dblTimer        As Double
    Dim bResult         As Boolean
    Dim sLastError      As String
    Dim oOutput         As cMemoryStream
    Dim lLevel          As Long

    dblTimer = Timer
    Set m_oZip = New cZipArchive
    Set oOutput = New cMemoryStream
    m_bCancel = False
    lLevel = 8
    With m_oZip
        .AddFile pvInitMemStream("C:\TEMP\Ep.2 2017-06-04 (Export FB mono).mp3"), "Ep.2 2017-06-04 (Export FB mono).mp3"
        .AddFile pvInitMemStream("C:\TEMP\Ep.5 2017-06-25 (via E90).mp3"), "Ep.5 2017-06-25 (via E90).mp3"
        .AddFile pvInitMemStream("C:\TEMP\Ep.7 2017-07-09 (via E90).mp3"), "Ep.7 2017-07-09 (via E90).mp3"
        .AddFile pvInitMemStream("C:\TEMP\Ep.8 2017-07-16 (via E90).mp3"), "Ep.8 2017-07-16 (via E90).mp3"
        .AddFile pvInitMemStream("C:\TEMP\Ep.10 2017-07-30 (via E90).mp3"), "Ep.10 2017-07-30 (via E90).mp3"
        .AddFile pvInitMemStream("C:\TEMP\Ep.11 2017-08-13 (via E90).mp3"), "Ep.11 2017-08-13 (via E90).mp3"
        .AddFile pvInitMemStream("C:\TEMP\Ep.12 2017-08-20 (via E90).mp3"), "Ep.12 2017-08-20 (via E90).mp3"
        .AddFile pvInitMemStream("C:\TEMP\Ep.13 2017-08-27 (via E90).mp3"), "Ep.13 2017-08-27 (via E90).mp3"
        .AddFile pvInitMemStream("C:\TEMP\Ep.14 2017-09-03 (via E90).mp3"), "Ep.14 2017-09-03 (via E90).mp3"
        bResult = .CompressArchive(oOutput, Level:=lLevel)
        sLastError = .LastError
    End With
    Set m_oZip = Nothing
    WriteBinaryFile "C:\TEMP\Podcasts.zip", oOutput.Contents
    labProgress.Caption = IIf(bResult, "Done. ", sLastError & ". ") & Format(Timer - dblTimer, "0.000") & " elapsed"
End Sub

The result is that only the Ep.2 2017-06-04 (Export FB mono).mp3 is in the zip file.
About progress, I see that the other files are displayed for a short time than first , and percentage not goes to 100% (or near) for each file, but only around 30% max.

Some idea of what I do wrong ? (Of course, files are present and not bad).

Thanks :)
Couin

cZipArchive.Extract: support OutputTarget as vbLong, treat as naked pointer

This is an esoteric request, so feel free to disregard and I will simply muck up my own cZipArchive copy to implement. ;)

Because large array allocations are especially expensive in VB, I would prefer to simply reuse a single large destination buffer when extracting a series of sequential files. This is especially useful in my OpenRaster use-case, as I just need to extract PNG bytes via cZipArchive, then immediately decode it into a usable image buffer (and discard the original PNG stream). It would save me a ton of allocation time to take the approach of, "find largest extracted size, prep single buffer at that size, re-use for all PNG extractions" vs the current approach of allocating new memory for each temporary Extract() output buffer.

Obviously, cZipArchive supporting a naked long as extraction target would require some amount of trust that the caller has properly read .Size property and allocated accordingly. Not sure if this is desirable or not in a class designed for general use.

An alternate implementation could be a new optional parameter in Extract(), something to the effect of "only resize OutputTarget array if it's too small for this extraction", but this adds complexity on cZipArchive's end and the same problems persist - e.g. the caller still needs to confirm .Size to understand the output. That's why I thought this implementation (passing a naked pointer) may be preferable.

(Actually there are other use-cases too, e.g. the caller could pass a pointer to something like a memory-mapped file view, which otherwise would require extra steps to "wrap" that bare pointer in a VB array.)

Zip with password is failed

I have create zip file from folder like code below, the zip file is successfully created, but with no password

Dim Zip As New cZipArchive
With Zip
    .AddFromFolder App.Path & "\test\*.*", Recursive:=True, TargetFolder:="test", IncludeEmptyFolders:=True, Password:="test123"
    .CompressArchive App.Path & "\test.zip"
End With

Please help @wqweto

Regards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.