I'm using the following for having gzip 'compressed' data, but it seems that there is no compression done and I can't figure out why..
When I test the file TestData.gz with WinRar it says it's a valid GZip, but the original file is only stored and not compressed..
(I'm using the zlibwapi.dll 1.2.3.0 which was renamed to zlib.dll because of compatibility with an older application)
The following code is based on C-based example..
When I test the file TestData.gz with WinRar it says it's a valid GZip, but the original file is only stored and not compressed..
(I'm using the zlibwapi.dll 1.2.3.0 which was renamed to zlib.dll because of compatibility with an older application)
The following code is based on C-based example..
Code:
Private Type zStream
next_in As Long '/* next input byte */
avail_in As Long '/* number of bytes available at next_in */
total_in As Long '/* count of input bytes read so far */
next_out As Long '/* next output byte should be put there */
avail_out As Long '/* remaining free space at next_out */
total_out As Long '/* count of bytes output so far */
Msg As Long '/* last error message, NULL if no error */
State As Long '/* not visible by applications*/
zalloc As Long '/* used to allocate the internal state*/
zfree As Long '/* used to free the internal state */
OPAQUE As Long '/* private data passed to zalloc and zfree*/
data_type As Long '/* best guess about the data: ascii or binary*/
adler As Long '/* adler32 value of the uncompressed data */
Reserved As Long '/* reserved for future use */
End Type
Private Const DEF_MEM_LEVEL As Long = 8
Private Const MAX_WBITS As Long = 15
Private Const Z_OK As Long = 0
Private Const Z_STREAM_END As Long = 1
Private Const Z_FINISH As Long = 4
Private Const Z_DEFAULT_COMPRESSION As Long = -1
Private Const Z_DEFAULT_STRATEGY As Long = 0
Private Declare Function deflate Lib "zlib.dll" (vStream As zStream, Optional ByVal vflush As Long = Z_FINISH) As Long
Private Declare Function deflateBound Lib "zlib.dll" (vStream As zStream, ByVal srcLen As Long) As Long
Private Declare Function deflateEnd Lib "zlib.dll" (vStream As zStream) As Long
Private Declare Function deflateInit2 Lib "zlib.dll" Alias "deflateInit2_" (strm As zStream, ByVal Level As Long, ByVal method As Long, ByVal windowBits As Long, ByVal memLevel As Long, ByVal strategy As Long, ByVal Version As String, ByVal stream_size As Long) As Long
Public Function CompressData_GZip(ByRef abData() As Byte) As Boolean
Dim abBuffer() As Byte
Dim lFile As Long
Dim lLengthBuffer As Long
Dim lLengthData As Long
Dim lReturn As Long
Dim tStream As zStream
'--------------------------------------------------------------
'Default CompressData failed
CompressData_GZip = False
'--------------------------------------------------------------
'Get Length of data
lLengthData = UBound(abData()) + 1
'--------------------------------------------------------------
'Is there any data to compress?
If lLengthData > 0 Then
With tStream
'--------------------------------------------------------------
'Init Stream
.zalloc = Z_NULL
.zfree = Z_NULL
.OPAQUE = Z_NULL
.next_in = VarPtr(abData(0))
.avail_in = lLengthData
'.data_type = Z_TEXT
'--------------------------------------------------------------
'Start
lReturn = deflateInit2(tStream, _
Z_DEFAULT_COMPRESSION, _
Z_DEFLATED, _
MAX_WBITS + 16, _
DEF_MEM_LEVEL, _
Z_DEFAULT_STRATEGY, _
"1.2.3", _
LenB(tStream))
If lReturn = Z_OK Then
'--------------------------------------------------------------
'Get the total needed buffersize
lLengthBuffer = deflateBound(tStream, lLengthData)
If lLengthBuffer > 0 Then
'--------------------------------------------------------------
'Add 12 to the length as 1.2.3 doesn't take the GZIP magic header into account (it's fixed with 1.2.3.1 beta)
lLengthBuffer = lLengthBuffer + 12
'--------------------------------------------------------------
'Create buffer for retrieving the compressed data
ReDim abBuffer(lLengthBuffer - 1)
'--------------------------------------------------------------
'Set pointer and size of destination buffer
.next_out = VarPtr(abBuffer(0))
.avail_out = lLengthBuffer
'--------------------------------------------------------------
'Get compressed data
lReturn = deflate(tStream, vflush:=Z_FINISH)
If lReturn = Z_STREAM_END Then
lFile = FreeFile
Open "D:\TestData.gz" For Binary Access Write As #lFile
Put #lFile, , abBuffer()
Close #lFile
CompressData_GZip = True
Else
'OHNO....
End If
End If 'If lReturn>0
End If 'If lReturn=Z_OK
'--------------------------------------------------------------
'Cleanup
lReturn = deflateEnd(tStream)
End With
End If
End Function