A standalone, pure VB6 Class for GZIP Compression and Decompression.
This class provides a simple and effective way to handle GZIP-formatted data directly in memory or for file-to-file operations, without requiring any external DLLs like zlibwapi.dll
.
This class is a heavily simplified adaptation of the brilliant and far more comprehensive cZipArchive.cls by wqweto.
The core DEFLATE algorithm, machine code thunk, and the ingenious method of executing it from VB6 were originally engineered by him. This cGZip.cls
was created by isolating the DEFLATE/GZIP logic from his larger ZIP archive project.
Full credit for the core engine belongs to him. You can find the original, full-featured project here:
- Original Repository: https://github.com/wqweto/ZipArchive
Just, add the cGZip.cls
file to your Visual Basic 6 project.
To use the class, create a new instance of it. All compression and decompression functions are available as methods of this object.
' Create an instance of the class
Dim GZip As New cGZip
' Define source and destination files
Dim sourceFile As String
sourceFile = App.Path & "\MyDocument.txt"
Dim compressedFile As String
compressedFile = App.Path & "\MyDocument.txt.gz"
' Create a dummy source file for the example
Dim hFile As Integer
hFile = FreeFile
Open sourceFile For Output As #hFile
Print #hFile, "This is a test of the cGZip class using custom enum flags."
Close #hFile
' Compress the file using the ULTRA compression level from your enum
Dim bOK As Boolean
bOK = GZip.CompressFileToGz(sourceFile, compressedFile, GZ_ULTRA_COMPRESSION)
If bOK Then
MsgBox "File compressed successfully!", vbInformation
Else
MsgBox "Compression failed: " & GZip.LastError, vbCritical
End If
LastError As String
(Read-Only) Returns a description of the last error that occurred.
The class and its helper functions use the GzipCompressionLevel enumeration to set the compression level. This provides readable names for the available settings.
-
GZ_NO_COMPRESSION
(Value: 0) - Store only, no compression. -
GZ_FASTEST_COMPRESSION
(Value: 1) - Fastest speed, minimal compression. -
GZ_NORMAL_COMPRESSION
(Value: 5) - A standard compression level. -
GZ_MAX_COMPRESSION
(Value: 7) - (Default) A great balance between speed and size. This level is used if no other level is specified. -
GZ_ULTRA_COMPRESSION
(Value: 9) - Best possible compression, but the slowest process.
-
Compress(sourceBuffer() As Byte, [Optional Level As GzipCompressionLevel = GZ_DEFAULT_COMPRESSION]) As Byte()
Compresses a source byte array into a new GZ-formatted byte array. -
Decompress(gzBuffer() As Byte) As Byte()
Decompresses a GZ-formatted byte array into the original byte array.
-
CompressFileToGz(sourceFilePath As String, destGzPath As String, [Optional Level As GzipCompressionLevel = GZ_DEFAULT_COMPRESSION]) As Boolean
Compresses a source file on disk to a new .gz file on disk. Returns True on success. -
DecompressGzToFile(sourceGzPath As String, destFilePath As String) As Boolean
Decompresses a .gz file from disk to a new, uncompressed file on disk. Returns True on success. -
CompressFileToBuffer(sourceFilePath As String, [Optional Level As GzipCompressionLevel = GZ_DEFAULT_COMPRESSION]) As Byte()
Reads a file from disk and returns its GZ-compressed content as a byte array. -
DecompressBufferToFile(gzBuffer() As Byte, destFilePath As String) As Boolean
Decompresses a GZ-formatted byte array and writes the result to a file on disk. Returns True on success.