1aaf4ece6SchristosSee below some functions declarations for Visual Basic. 2aaf4ece6Schristos 3aaf4ece6SchristosFrequently Asked Question: 4aaf4ece6Schristos 5aaf4ece6SchristosQ: Each time I use the compress function I get the -5 error (not enough 6aaf4ece6Schristos room in the output buffer). 7aaf4ece6Schristos 8aaf4ece6SchristosA: Make sure that the length of the compressed buffer is passed by 9aaf4ece6Schristos reference ("as any"), not by value ("as long"). Also check that 10aaf4ece6Schristos before the call of compress this length is equal to the total size of 11aaf4ece6Schristos the compressed buffer and not zero. 12aaf4ece6Schristos 13aaf4ece6Schristos 14aaf4ece6SchristosFrom: "Jon Caruana" <jon-net@usa.net> 15aaf4ece6SchristosSubject: Re: How to port zlib declares to vb? 16aaf4ece6SchristosDate: Mon, 28 Oct 1996 18:33:03 -0600 17aaf4ece6Schristos 18aaf4ece6SchristosGot the answer! (I haven't had time to check this but it's what I got, and 19aaf4ece6Schristoslooks correct): 20aaf4ece6Schristos 21aaf4ece6SchristosHe has the following routines working: 22aaf4ece6Schristos compress 23aaf4ece6Schristos uncompress 24aaf4ece6Schristos gzopen 25aaf4ece6Schristos gzwrite 26aaf4ece6Schristos gzread 27aaf4ece6Schristos gzclose 28aaf4ece6Schristos 29aaf4ece6SchristosDeclares follow: (Quoted from Carlos Rios <c_rios@sonda.cl>, in Vb4 form) 30aaf4ece6Schristos 31aaf4ece6Schristos#If Win16 Then 'Use Win16 calls. 32aaf4ece6SchristosDeclare Function compress Lib "ZLIB.DLL" (ByVal compr As 33aaf4ece6Schristos String, comprLen As Any, ByVal buf As String, ByVal buflen 34aaf4ece6Schristos As Long) As Integer 35aaf4ece6SchristosDeclare Function uncompress Lib "ZLIB.DLL" (ByVal uncompr 36aaf4ece6Schristos As String, uncomprLen As Any, ByVal compr As String, ByVal 37aaf4ece6Schristos lcompr As Long) As Integer 38aaf4ece6SchristosDeclare Function gzopen Lib "ZLIB.DLL" (ByVal filePath As 39aaf4ece6Schristos String, ByVal mode As String) As Long 40aaf4ece6SchristosDeclare Function gzread Lib "ZLIB.DLL" (ByVal file As 41aaf4ece6Schristos Long, ByVal uncompr As String, ByVal uncomprLen As Integer) 42aaf4ece6Schristos As Integer 43aaf4ece6SchristosDeclare Function gzwrite Lib "ZLIB.DLL" (ByVal file As 44aaf4ece6Schristos Long, ByVal uncompr As String, ByVal uncomprLen As Integer) 45aaf4ece6Schristos As Integer 46aaf4ece6SchristosDeclare Function gzclose Lib "ZLIB.DLL" (ByVal file As 47aaf4ece6Schristos Long) As Integer 48aaf4ece6Schristos#Else 49aaf4ece6SchristosDeclare Function compress Lib "ZLIB32.DLL" 50aaf4ece6Schristos (ByVal compr As String, comprLen As Any, ByVal buf As 51aaf4ece6Schristos String, ByVal buflen As Long) As Integer 52aaf4ece6SchristosDeclare Function uncompress Lib "ZLIB32.DLL" 53aaf4ece6Schristos (ByVal uncompr As String, uncomprLen As Any, ByVal compr As 54aaf4ece6Schristos String, ByVal lcompr As Long) As Long 55aaf4ece6SchristosDeclare Function gzopen Lib "ZLIB32.DLL" 56aaf4ece6Schristos (ByVal file As String, ByVal mode As String) As Long 57aaf4ece6SchristosDeclare Function gzread Lib "ZLIB32.DLL" 58aaf4ece6Schristos (ByVal file As Long, ByVal uncompr As String, ByVal 59aaf4ece6Schristos uncomprLen As Long) As Long 60aaf4ece6SchristosDeclare Function gzwrite Lib "ZLIB32.DLL" 61aaf4ece6Schristos (ByVal file As Long, ByVal uncompr As String, ByVal 62aaf4ece6Schristos uncomprLen As Long) As Long 63aaf4ece6SchristosDeclare Function gzclose Lib "ZLIB32.DLL" 64aaf4ece6Schristos (ByVal file As Long) As Long 65aaf4ece6Schristos#End If 66aaf4ece6Schristos 67aaf4ece6Schristos-Jon Caruana 68aaf4ece6Schristosjon-net@usa.net 69aaf4ece6SchristosMicrosoft Sitebuilder Network Level 1 Member - HTML Writer's Guild Member 70aaf4ece6Schristos 71aaf4ece6Schristos 72aaf4ece6SchristosHere is another example from Michael <michael_borgsys@hotmail.com> that he 73aaf4ece6Schristossays conforms to the VB guidelines, and that solves the problem of not 74aaf4ece6Schristosknowing the uncompressed size by storing it at the end of the file: 75aaf4ece6Schristos 76aaf4ece6Schristos'Calling the functions: 77aaf4ece6Schristos'bracket meaning: <parameter> [optional] {Range of possible values} 78aaf4ece6Schristos'Call subCompressFile(<path with filename to compress> [, <path with 79aaf4ece6Schristosfilename to write to>, [level of compression {1..9}]]) 80aaf4ece6Schristos'Call subUncompressFile(<path with filename to compress>) 81aaf4ece6Schristos 82aaf4ece6SchristosOption Explicit 83aaf4ece6SchristosPrivate lngpvtPcnSml As Long 'Stores value for 'lngPercentSmaller' 84aaf4ece6SchristosPrivate Const SUCCESS As Long = 0 85aaf4ece6SchristosPrivate Const strFilExt As String = ".cpr" 86aaf4ece6SchristosPrivate Declare Function lngfncCpr Lib "zlib.dll" Alias "compress2" (ByRef 87aaf4ece6Schristosdest As Any, ByRef destLen As Any, ByRef src As Any, ByVal srcLen As Long, 88aaf4ece6SchristosByVal level As Integer) As Long 89aaf4ece6SchristosPrivate Declare Function lngfncUcp Lib "zlib.dll" Alias "uncompress" (ByRef 90aaf4ece6Schristosdest As Any, ByRef destLen As Any, ByRef src As Any, ByVal srcLen As Long) 91aaf4ece6SchristosAs Long 92aaf4ece6Schristos 93aaf4ece6SchristosPublic Sub subCompressFile(ByVal strargOriFilPth As String, Optional ByVal 94aaf4ece6SchristosstrargCprFilPth As String, Optional ByVal intLvl As Integer = 9) 95aaf4ece6Schristos Dim strCprPth As String 96aaf4ece6Schristos Dim lngOriSiz As Long 97aaf4ece6Schristos Dim lngCprSiz As Long 98aaf4ece6Schristos Dim bytaryOri() As Byte 99aaf4ece6Schristos Dim bytaryCpr() As Byte 100aaf4ece6Schristos lngOriSiz = FileLen(strargOriFilPth) 101aaf4ece6Schristos ReDim bytaryOri(lngOriSiz - 1) 102aaf4ece6Schristos Open strargOriFilPth For Binary Access Read As #1 103aaf4ece6Schristos Get #1, , bytaryOri() 104aaf4ece6Schristos Close #1 105aaf4ece6Schristos strCprPth = IIf(strargCprFilPth = "", strargOriFilPth, strargCprFilPth) 106aaf4ece6Schristos'Select file path and name 107aaf4ece6Schristos strCprPth = strCprPth & IIf(Right(strCprPth, Len(strFilExt)) = 108aaf4ece6SchristosstrFilExt, "", strFilExt) 'Add file extension if not exists 109aaf4ece6Schristos lngCprSiz = (lngOriSiz * 1.01) + 12 'Compression needs temporary a bit 110aaf4ece6Schristosmore space then original file size 111aaf4ece6Schristos ReDim bytaryCpr(lngCprSiz - 1) 112aaf4ece6Schristos If lngfncCpr(bytaryCpr(0), lngCprSiz, bytaryOri(0), lngOriSiz, intLvl) = 113aaf4ece6SchristosSUCCESS Then 114aaf4ece6Schristos lngpvtPcnSml = (1# - (lngCprSiz / lngOriSiz)) * 100 115aaf4ece6Schristos ReDim Preserve bytaryCpr(lngCprSiz - 1) 116aaf4ece6Schristos Open strCprPth For Binary Access Write As #1 117aaf4ece6Schristos Put #1, , bytaryCpr() 118*b175d1c2Schristos Put #1, , lngOriSiz 'Add the original size value to the end 119aaf4ece6Schristos(last 4 bytes) 120aaf4ece6Schristos Close #1 121aaf4ece6Schristos Else 122aaf4ece6Schristos MsgBox "Compression error" 123aaf4ece6Schristos End If 124aaf4ece6Schristos Erase bytaryCpr 125aaf4ece6Schristos Erase bytaryOri 126aaf4ece6SchristosEnd Sub 127aaf4ece6Schristos 128aaf4ece6SchristosPublic Sub subUncompressFile(ByVal strargFilPth As String) 129aaf4ece6Schristos Dim bytaryCpr() As Byte 130aaf4ece6Schristos Dim bytaryOri() As Byte 131aaf4ece6Schristos Dim lngOriSiz As Long 132aaf4ece6Schristos Dim lngCprSiz As Long 133aaf4ece6Schristos Dim strOriPth As String 134aaf4ece6Schristos lngCprSiz = FileLen(strargFilPth) 135aaf4ece6Schristos ReDim bytaryCpr(lngCprSiz - 1) 136aaf4ece6Schristos Open strargFilPth For Binary Access Read As #1 137aaf4ece6Schristos Get #1, , bytaryCpr() 138aaf4ece6Schristos Close #1 139aaf4ece6Schristos 'Read the original file size value: 140aaf4ece6Schristos lngOriSiz = bytaryCpr(lngCprSiz - 1) * (2 ^ 24) _ 141aaf4ece6Schristos + bytaryCpr(lngCprSiz - 2) * (2 ^ 16) _ 142aaf4ece6Schristos + bytaryCpr(lngCprSiz - 3) * (2 ^ 8) _ 143aaf4ece6Schristos + bytaryCpr(lngCprSiz - 4) 144aaf4ece6Schristos ReDim Preserve bytaryCpr(lngCprSiz - 5) 'Cut of the original size value 145aaf4ece6Schristos ReDim bytaryOri(lngOriSiz - 1) 146aaf4ece6Schristos If lngfncUcp(bytaryOri(0), lngOriSiz, bytaryCpr(0), lngCprSiz) = SUCCESS 147aaf4ece6SchristosThen 148aaf4ece6Schristos strOriPth = Left(strargFilPth, Len(strargFilPth) - Len(strFilExt)) 149aaf4ece6Schristos Open strOriPth For Binary Access Write As #1 150aaf4ece6Schristos Put #1, , bytaryOri() 151aaf4ece6Schristos Close #1 152aaf4ece6Schristos Else 153aaf4ece6Schristos MsgBox "Uncompression error" 154aaf4ece6Schristos End If 155aaf4ece6Schristos Erase bytaryCpr 156aaf4ece6Schristos Erase bytaryOri 157aaf4ece6SchristosEnd Sub 158aaf4ece6SchristosPublic Property Get lngPercentSmaller() As Long 159aaf4ece6Schristos lngPercentSmaller = lngpvtPcnSml 160aaf4ece6SchristosEnd Property 161