kusano fc6ab3
See below some functions declarations for Visual Basic.
kusano fc6ab3
kusano fc6ab3
Frequently Asked Question:
kusano fc6ab3
kusano fc6ab3
Q: Each time I use the compress function I get the -5 error (not enough
kusano fc6ab3
   room in the output buffer).
kusano fc6ab3
kusano fc6ab3
A: Make sure that the length of the compressed buffer is passed by
kusano fc6ab3
   reference ("as any"), not by value ("as long"). Also check that
kusano fc6ab3
   before the call of compress this length is equal to the total size of
kusano fc6ab3
   the compressed buffer and not zero.
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
From: "Jon Caruana" <jon-net@usa.net></jon-net@usa.net>
kusano fc6ab3
Subject: Re: How to port zlib declares to vb?
kusano fc6ab3
Date: Mon, 28 Oct 1996 18:33:03 -0600
kusano fc6ab3
kusano fc6ab3
Got the answer! (I haven't had time to check this but it's what I got, and
kusano fc6ab3
looks correct):
kusano fc6ab3
kusano fc6ab3
He has the following routines working:
kusano fc6ab3
        compress
kusano fc6ab3
        uncompress
kusano fc6ab3
        gzopen
kusano fc6ab3
        gzwrite
kusano fc6ab3
        gzread
kusano fc6ab3
        gzclose
kusano fc6ab3
kusano fc6ab3
Declares follow: (Quoted from Carlos Rios <c_rios@sonda.cl>, in Vb4 form)</c_rios@sonda.cl>
kusano fc6ab3
kusano fc6ab3
#If Win16 Then   'Use Win16 calls.
kusano fc6ab3
Declare Function compress Lib "ZLIB.DLL" (ByVal compr As
kusano fc6ab3
        String, comprLen As Any, ByVal buf As String, ByVal buflen
kusano fc6ab3
        As Long) As Integer
kusano fc6ab3
Declare Function uncompress Lib "ZLIB.DLL" (ByVal uncompr
kusano fc6ab3
        As String, uncomprLen As Any, ByVal compr As String, ByVal
kusano fc6ab3
        lcompr As Long) As Integer
kusano fc6ab3
Declare Function gzopen Lib "ZLIB.DLL" (ByVal filePath As
kusano fc6ab3
        String, ByVal mode As String) As Long
kusano fc6ab3
Declare Function gzread Lib "ZLIB.DLL" (ByVal file As
kusano fc6ab3
        Long, ByVal uncompr As String, ByVal uncomprLen As Integer)
kusano fc6ab3
        As Integer
kusano fc6ab3
Declare Function gzwrite Lib "ZLIB.DLL" (ByVal file As
kusano fc6ab3
        Long, ByVal uncompr As String, ByVal uncomprLen As Integer)
kusano fc6ab3
        As Integer
kusano fc6ab3
Declare Function gzclose Lib "ZLIB.DLL" (ByVal file As
kusano fc6ab3
        Long) As Integer
kusano fc6ab3
#Else
kusano fc6ab3
Declare Function compress Lib "ZLIB32.DLL"
kusano fc6ab3
        (ByVal compr As String, comprLen As Any, ByVal buf As
kusano fc6ab3
        String, ByVal buflen As Long) As Integer
kusano fc6ab3
Declare Function uncompress Lib "ZLIB32.DLL"
kusano fc6ab3
        (ByVal uncompr As String, uncomprLen As Any, ByVal compr As
kusano fc6ab3
        String, ByVal lcompr As Long) As Long
kusano fc6ab3
Declare Function gzopen Lib "ZLIB32.DLL"
kusano fc6ab3
        (ByVal file As String, ByVal mode As String) As Long
kusano fc6ab3
Declare Function gzread Lib "ZLIB32.DLL"
kusano fc6ab3
        (ByVal file As Long, ByVal uncompr As String, ByVal
kusano fc6ab3
        uncomprLen As Long) As Long
kusano fc6ab3
Declare Function gzwrite Lib "ZLIB32.DLL"
kusano fc6ab3
        (ByVal file As Long, ByVal uncompr As String, ByVal
kusano fc6ab3
        uncomprLen As Long) As Long
kusano fc6ab3
Declare Function gzclose Lib "ZLIB32.DLL"
kusano fc6ab3
        (ByVal file As Long) As Long
kusano fc6ab3
#End If
kusano fc6ab3
kusano fc6ab3
-Jon Caruana
kusano fc6ab3
jon-net@usa.net
kusano fc6ab3
Microsoft Sitebuilder Network Level 1 Member - HTML Writer's Guild Member
kusano fc6ab3
kusano fc6ab3
kusano fc6ab3
Here is another example from Michael <michael_borgsys@hotmail.com> that he</michael_borgsys@hotmail.com>
kusano fc6ab3
says conforms to the VB guidelines, and that solves the problem of not
kusano fc6ab3
knowing the uncompressed size by storing it at the end of the file:
kusano fc6ab3
kusano fc6ab3
'Calling the functions:
kusano fc6ab3
'bracket meaning: <parameter> [optional] {Range of possible values}</parameter>
kusano fc6ab3
'Call subCompressFile(<path compress="" filename="" to="" with=""> [, </path>
kusano fc6ab3
filename to write to>, [level of compression {1..9}]])
kusano fc6ab3
'Call subUncompressFile(<path compress="" filename="" to="" with="">)</path>
kusano fc6ab3
kusano fc6ab3
Option Explicit
kusano fc6ab3
Private lngpvtPcnSml As Long 'Stores value for 'lngPercentSmaller'
kusano fc6ab3
Private Const SUCCESS As Long = 0
kusano fc6ab3
Private Const strFilExt As String = ".cpr"
kusano fc6ab3
Private Declare Function lngfncCpr Lib "zlib.dll" Alias "compress2" (ByRef
kusano fc6ab3
dest As Any, ByRef destLen As Any, ByRef src As Any, ByVal srcLen As Long,
kusano fc6ab3
ByVal level As Integer) As Long
kusano fc6ab3
Private Declare Function lngfncUcp Lib "zlib.dll" Alias "uncompress" (ByRef
kusano fc6ab3
dest As Any, ByRef destLen As Any, ByRef src As Any, ByVal srcLen As Long)
kusano fc6ab3
As Long
kusano fc6ab3
kusano fc6ab3
Public Sub subCompressFile(ByVal strargOriFilPth As String, Optional ByVal
kusano fc6ab3
strargCprFilPth As String, Optional ByVal intLvl As Integer = 9)
kusano fc6ab3
    Dim strCprPth As String
kusano fc6ab3
    Dim lngOriSiz As Long
kusano fc6ab3
    Dim lngCprSiz As Long
kusano fc6ab3
    Dim bytaryOri() As Byte
kusano fc6ab3
    Dim bytaryCpr() As Byte
kusano fc6ab3
    lngOriSiz = FileLen(strargOriFilPth)
kusano fc6ab3
    ReDim bytaryOri(lngOriSiz - 1)
kusano fc6ab3
    Open strargOriFilPth For Binary Access Read As #1
kusano fc6ab3
        Get #1, , bytaryOri()
kusano fc6ab3
    Close #1
kusano fc6ab3
    strCprPth = IIf(strargCprFilPth = "", strargOriFilPth, strargCprFilPth)
kusano fc6ab3
'Select file path and name
kusano fc6ab3
    strCprPth = strCprPth & IIf(Right(strCprPth, Len(strFilExt)) =
kusano fc6ab3
strFilExt, "", strFilExt) 'Add file extension if not exists
kusano fc6ab3
    lngCprSiz = (lngOriSiz * 1.01) + 12 'Compression needs temporary a bit
kusano fc6ab3
more space then original file size
kusano fc6ab3
    ReDim bytaryCpr(lngCprSiz - 1)
kusano fc6ab3
    If lngfncCpr(bytaryCpr(0), lngCprSiz, bytaryOri(0), lngOriSiz, intLvl) =
kusano fc6ab3
SUCCESS Then
kusano fc6ab3
        lngpvtPcnSml = (1# - (lngCprSiz / lngOriSiz)) * 100
kusano fc6ab3
        ReDim Preserve bytaryCpr(lngCprSiz - 1)
kusano fc6ab3
        Open strCprPth For Binary Access Write As #1
kusano fc6ab3
            Put #1, , bytaryCpr()
kusano fc6ab3
            Put #1, , lngOriSiz 'Add the the original size value to the end
kusano fc6ab3
(last 4 bytes)
kusano fc6ab3
        Close #1
kusano fc6ab3
    Else
kusano fc6ab3
        MsgBox "Compression error"
kusano fc6ab3
    End If
kusano fc6ab3
    Erase bytaryCpr
kusano fc6ab3
    Erase bytaryOri
kusano fc6ab3
End Sub
kusano fc6ab3
kusano fc6ab3
Public Sub subUncompressFile(ByVal strargFilPth As String)
kusano fc6ab3
    Dim bytaryCpr() As Byte
kusano fc6ab3
    Dim bytaryOri() As Byte
kusano fc6ab3
    Dim lngOriSiz As Long
kusano fc6ab3
    Dim lngCprSiz As Long
kusano fc6ab3
    Dim strOriPth As String
kusano fc6ab3
    lngCprSiz = FileLen(strargFilPth)
kusano fc6ab3
    ReDim bytaryCpr(lngCprSiz - 1)
kusano fc6ab3
    Open strargFilPth For Binary Access Read As #1
kusano fc6ab3
        Get #1, , bytaryCpr()
kusano fc6ab3
    Close #1
kusano fc6ab3
    'Read the original file size value:
kusano fc6ab3
    lngOriSiz = bytaryCpr(lngCprSiz - 1) * (2 ^ 24) _
kusano fc6ab3
              + bytaryCpr(lngCprSiz - 2) * (2 ^ 16) _
kusano fc6ab3
              + bytaryCpr(lngCprSiz - 3) * (2 ^ 8) _
kusano fc6ab3
              + bytaryCpr(lngCprSiz - 4)
kusano fc6ab3
    ReDim Preserve bytaryCpr(lngCprSiz - 5) 'Cut of the original size value
kusano fc6ab3
    ReDim bytaryOri(lngOriSiz - 1)
kusano fc6ab3
    If lngfncUcp(bytaryOri(0), lngOriSiz, bytaryCpr(0), lngCprSiz) = SUCCESS
kusano fc6ab3
Then
kusano fc6ab3
        strOriPth = Left(strargFilPth, Len(strargFilPth) - Len(strFilExt))
kusano fc6ab3
        Open strOriPth For Binary Access Write As #1
kusano fc6ab3
            Put #1, , bytaryOri()
kusano fc6ab3
        Close #1
kusano fc6ab3
    Else
kusano fc6ab3
        MsgBox "Uncompression error"
kusano fc6ab3
    End If
kusano fc6ab3
    Erase bytaryCpr
kusano fc6ab3
    Erase bytaryOri
kusano fc6ab3
End Sub
kusano fc6ab3
Public Property Get lngPercentSmaller() As Long
kusano fc6ab3
    lngPercentSmaller = lngpvtPcnSml
kusano fc6ab3
End Property