功能简介:本文转载自 Thinkai 博客(AHK v1),提供 GZIP_DecompressFile() 函数,用于读取二进制文件中的网页 GZIP 压缩数据并解压为可读文本(支持指定编码,默认 UTF-8)。实现上通过 ADODB.Stream 以二进制方式读取文件,配合 SafeArrayAccessData 取得缓冲区指针,再借助 GZIP DLL 完成解压,适合处理抓取网页时返回的 gzip 内容。
使用提示(AHK v1 / v2):原文为 AHK v1 语法(ComObjCreate、MsgBox % ret 等)。迁移到 AHK v2 时需调整:ComObjCreate → ComObject;MsgBox % ret → MsgBox(ret);变量展开与表达式写法相应改为 v2 的对象化风格。另注意函数依赖 GZIP_DecompressBuffer 及 GZIP DLL 的引入,独立复用时需一并包含相关依赖与 32 位兼容性说明。
thinkai的博客简洁、有趣,即便是转载过来我也尽量保持作者原有的风格,主要是方便大家查阅。
兼容性仅测试通过在32位Autohotkey版本
GZIP_DecompressFile("test.txt", ret)
MsgBox % ret
GZIP_DecompressFile(file, ByRef ret, encoding:="utf-8"){
body := BinArr_FromFile(file)
VarSetCapacity(data, size := body.MaxIndex() + 1)
DllCall("oleaut32SafeArrayAccessData", "ptr", ComObjValue(body), "ptr*", pdata)
DllCall("RtlMoveMemory", "ptr", &data, "ptr", pdata, "ptr", size)
DllCall("oleaut32SafeArrayUnaccessData", "ptr", ComObjValue(body))
size := GZIP_DecompressBuffer(data, size)
ret := StrGet(&data, size, encoding)
}
BinArr_FromFile(FileName) {
oADO := ComObjCreate("ADODB.Stream")
oADO.Type := 1 ; adTypeBinary
oADO.Open
oADO.LoadFromFile(FileName)
return oADO.Read, oADO.Close
}
GZIP_DecompressBuffer( ByRef var, nSz ) { ; 'Microsoft GZIP Compression DLL' SKAN 20-Sep-2010
; Decompress routine for 'no-name single file GZIP', available in process memory.
; Forum post : www.autohotkey.com/forum/viewtopic.php?p=384875#384875
; Modified by Lexikos 25-Apr-2015 to accept the data size as a parameter.
; ---------- Added by tmplinshi ----------
static hModule, _
If !hModule {
hModule := DllCall("LoadLibrary", "Str", "gzip.dll", "Ptr")
_ := { base: {__Delete: "GZIP_DecompressBuffer"} }
}
If !_
Return DllCall("FreeLibrary", "Ptr", hModule)
; ---------- / Added by tmplinshi ----------
vSz := NumGet( var,nsz-4 ), VarSetCapacity( out,vsz,0 )
DllCall( "GZIPInitDecompression" )
DllCall( "GZIPCreateDecompression", UIntP,CTX, UInt,1 )
If ( DllCall( "GZIPDecompress", UInt,CTX, UInt,&var, UInt,nsz, UInt,&Out, UInt,vsz
, UIntP,input_used, UIntP,output_used ) = 0 && ( Ok := ( output_used = vsz ) ) )
VarSetCapacity( var,64 ), VarSetCapacity( var,0 ), VarSetCapacity( var,vsz,32 )
, DllCall( "RtlMoveMemory", UInt,&var, UInt,&out, UInt,vsz )
DllCall( "GZIPDestroyDecompression", UInt,CTX ), DllCall( "GZIPDeInitDecompression" )
Return Ok ? vsz : 0
}
GZIP_CompressBuffer(ByRef var, nSz ){
static hModule, _
If !hModule {
hModule := DllCall("LoadLibrary", "Str", "gzip.dll", "Ptr")
_ := { base: {__Delete: "GZIP_CompressBuffer"} }
}
If !_
Return DllCall("FreeLibrary", "Ptr", hModule)
vSz := NumGet( var,nsz-4 ), VarSetCapacity( out,vsz,0 )
DllCall( "GZIPInitCompression" )
DllCall( "GZIPCreateCompression", UIntP,CTX, UInt,1 )
If ( DllCall( "GZIPCompress", UInt,CTX, UInt,&var, UInt,nsz, UInt,&Out, UInt,vsz
, UIntP,input_used, UIntP,output_used ) = 0 && ( Ok := ( output_used = vsz ) ) )
VarSetCapacity( var,64 ), VarSetCapacity( var,0 ), VarSetCapacity( var,vsz,32 )
, DllCall( "RtlMoveMemory", UInt,&var, UInt,&out, UInt,vsz )
DllCall( "GZIPDestroyCompression", UInt,CTX ), DllCall( "GZIPDeInitCompression" )
Return Ok ? vsz : 0
}

