zlib is designed to be a free, general-purpose, legally unencumbered -- that is, not covered by any patents -- lossless data-compression library for use on virtually any computer hardware and operating system. The zlib data format is itself portable across platforms.
zlib was written by Jean-loup Gailly (compression) and Mark Adler (decompression). Jean-loup is also the primary author/maintainer of gzip(1), the author of the comp.compression FAQ list and the former maintainer of Info-ZIP's Zip; Mark is also the author of gzip's and UnZip's main decompression routines and was the original author of Zip. Not surprisingly, the compression algorithm used in zlib is essentially the same as that in gzip and Zip, namely, the `deflate' method that originated in PKWARE's PKZIP 2.x.
Requirements: AHK_L Unicode (Ansi will require minor modifications)
I have wrapped only the Utility and GZip compress/decompress functions.
Functions:
zlib_Compress(Byref Compressed, Byref Data, DataLen, level = -1) { nSize := DllCall("zlib1\compressBound", "UInt", DataLen, "Cdecl") VarSetCapacity(Compressed,nSize) ErrorLevel := DllCall("zlib1\compress2", "ptr", &Compressed, "UIntP", nSize, "ptr", &Data, "UInt", DataLen, "Int" , level ;level 0 (no compression), 1 (best speed) - 9 (best compression) , "Cdecl") ;0 means Z_OK return ErrorLevel ? 0 : nSize } ;http://www.autohotkey.com/forum/viewtopic.php?t=68170 zlib_Decompress(Byref Decompressed, Byref CompressedData, DataLen, OriginalSize = -1) { OriginalSize := (OriginalSize > 0) ? OriginalSize : DataLen*10 ;should be large enough for most cases VarSetCapacity(Decompressed,OriginalSize) ErrorLevel := DllCall("zlib1\uncompress", "Ptr", &Decompressed, "UIntP", OriginalSize, "Ptr", &CompressedData, "UInt", DataLen) return ErrorLevel ? 0 : OriginalSize } ;http://www.autohotkey.com/forum/viewtopic.php?t=68170 gz_compress(infilename, outfilename) { VarSetCapacity(sOutFileName, 260) DllCall("WideCharToMultiByte", "Uint", 0, "Uint", 0, "str", outfilename, "int", -1, "str", sOutFileName, "int", 260, "Uint", 0, "Uint", 0) infile := FileOpen(infilename, "r"), outfile := DllCall("zlib1\gzopen", "Str" , sOutFileName , "Str", "wb", "Cdecl") if (!infile || !outfile) return 0 nBufferLen := 8192 ; can be increased if gzbuffer function is called beforehand VarSetCapacity(inbuffer,nBufferLen) while ((num_read := infile.RawRead(inbuffer, nBufferLen)) > 0) DllCall("zlib1\gzwrite", "UPtr", outfile, "UPtr", &inbuffer, "UInt", num_read, "Cdecl") infile.Close() DllCall("zlib1\gzclose", "UPtr", outfile, "Cdecl") return 1 } ;http://www.autohotkey.com/forum/viewtopic.php?t=68170 gz_decompress(infilename, outfilename) { VarSetCapacity(sInFileName, 260) DllCall("WideCharToMultiByte", "Uint", 0, "Uint", 0, "str", infilename, "int", -1, "str", sInFileName, "int", 260, "Uint", 0, "Uint", 0) infile := DllCall("zlib1\gzopen", "Str" , sInFileName , "Str", "rb", "Cdecl"), outfile := FileOpen(outfilename, "w") if (!infile || !outfile) return 0 VarSetCapacity(buffer,8192) ;can be increased after calling gzbuffer beforehand num_read = 0 while ((num_read := DllCall("zlib1\gzread", "UPtr", infile, "UPtr", &buffer, "UInt", 8192, "Cdecl")) > 0) outfile.RawWrite(buffer, num_read) DllCall("zlib1\gzclose", "UPtr", infile, "Cdecl") infile.Close() return 1 } ;http://www.autohotkey.com/forum/viewtopic.php?t=68170 /* Return codes for the compression/decompression functions. Negative values are errors, positive values are used for special but normal events. #define Z_OK 0 #define Z_STREAM_END 1 #define Z_NEED_DICT 2 #define Z_ERRNO (-1) #define Z_STREAM_ERROR (-2) #define Z_DATA_ERROR (-3) #define Z_MEM_ERROR (-4) #define Z_BUF_ERROR (-5) #define Z_VERSION_ERROR (-6) Compression levels. #define Z_NO_COMPRESSION 0 #define Z_BEST_SPEED 1 #define Z_BEST_COMPRESSION 9 #define Z_DEFAULT_COMPRESSION (-1) */
Example:
; DATA COMPRESSION infile := FileOpen(A_ScriptFullPath, "r") len := infile.length infile.RawRead(MyData,len) if (r := zlib_Compress(CompressedData , MyData, len)) MsgBox % "Data compressed " . round((r/len)*100) . "%" else MsgBox Error Errorlevel %Errorlevel% infile.Close() ; DATA DECOMPRESSION if (t := zlib_Decompress(Inflated,CompressedData,r,len)) MsgBox % StrGet(&Inflated,len,"") else MsgBox Error Errorlevel %Errorlevel% ;COMPRESS TO GZIP FILE gz_compress(A_AhkPath,A_ScriptDir . "\test.gz") ;DECOMPRESS GZIP FILE gz_decompress(A_ScriptDir . "\test.gz", A_ScriptDir . "\extracted.exe") zlib_Compress(Byref Compressed, Byref Data, DataLen, level = -1) { nSize := DllCall("zlib1\compressBound", "UInt", DataLen, "Cdecl") VarSetCapacity(Compressed,nSize) ErrorLevel := DllCall("zlib1\compress2", "ptr", &Compressed, "UIntP", nSize, "ptr", &Data, "UInt", DataLen, "Int" , level ;level 0 (no compression), 1 (best speed) - 9 (best compression) , "Cdecl") ;0 means Z_OK return ErrorLevel ? 0 : nSize } ;http://www.autohotkey.com/forum/viewtopic.php?t=68170 zlib_Decompress(Byref Decompressed, Byref CompressedData, DataLen, OriginalSize = -1) { OriginalSize := (OriginalSize > 0) ? OriginalSize : DataLen*10 ;should be large enough for most cases VarSetCapacity(Decompressed,OriginalSize) ErrorLevel := DllCall("zlib1\uncompress", "Ptr", &Decompressed, "UIntP", OriginalSize, "Ptr", &CompressedData, "UInt", DataLen) return ErrorLevel ? 0 : OriginalSize } ;http://www.autohotkey.com/forum/viewtopic.php?t=68170 gz_compress(infilename, outfilename) { VarSetCapacity(sOutFileName, 260) DllCall("WideCharToMultiByte", "Uint", 0, "Uint", 0, "str", outfilename, "int", -1, "str", sOutFileName, "int", 260, "Uint", 0, "Uint", 0) infile := FileOpen(infilename, "r"), outfile := DllCall("zlib1\gzopen", "Str" , sOutFileName , "Str", "wb", "Cdecl") if (!infile || !outfile) return 0 nBufferLen := 8192 ; can be increased if gzbuffer function is called beforehand VarSetCapacity(inbuffer,nBufferLen) while ((num_read := infile.RawRead(inbuffer, nBufferLen)) > 0) DllCall("zlib1\gzwrite", "UPtr", outfile, "UPtr", &inbuffer, "UInt", num_read, "Cdecl") infile.Close() DllCall("zlib1\gzclose", "UPtr", outfile, "Cdecl") return 1 } ;http://www.autohotkey.com/forum/viewtopic.php?t=68170 gz_decompress(infilename, outfilename) { VarSetCapacity(sInFileName, 260) DllCall("WideCharToMultiByte", "Uint", 0, "Uint", 0, "str", infilename, "int", -1, "str", sInFileName, "int", 260, "Uint", 0, "Uint", 0) infile := DllCall("zlib1\gzopen", "Str" , sInFileName , "Str", "rb", "Cdecl"), outfile := FileOpen(outfilename, "w") if (!infile || !outfile) return 0 VarSetCapacity(buffer,8192) ;can be increased after calling gzbuffer beforehand num_read = 0 while ((num_read := DllCall("zlib1\gzread", "UPtr", infile, "UPtr", &buffer, "UInt", 8192, "Cdecl")) > 0) outfile.RawWrite(buffer, num_read) DllCall("zlib1\gzclose", "UPtr", infile, "Cdecl") infile.Close() return 1 } ;http://www.autohotkey.com/forum/viewtopic.php?t=68170 /* Return codes for the compression/decompression functions. Negative values are errors, positive values are used for special but normal events. #define Z_OK 0 #define Z_STREAM_END 1 #define Z_NEED_DICT 2 #define Z_ERRNO (-1) #define Z_STREAM_ERROR (-2) #define Z_DATA_ERROR (-3) #define Z_MEM_ERROR (-4) #define Z_BUF_ERROR (-5) #define Z_VERSION_ERROR (-6) Compression levels. #define Z_NO_COMPRESSION 0 #define Z_BEST_SPEED 1 #define Z_BEST_COMPRESSION 9 #define Z_DEFAULT_COMPRESSION (-1) */
Reference: <!-- m -->http://www.zlib.net/manual.html<!-- m -->