从二进制文件解压网页GZIP数据GZIP_DecompressFile() Autohotkey

本文来自于Thinkai’s Blog,thinkai也是我在ahk上面的引路人,对thinkai感兴趣的朋友请关注他的博客。
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
}

给TA捐赠
共{{data.count}}人
人已捐赠
其他教程

通过文件对象复制二进制文件(读写) Autohotkey

2020-2-19 10:29:35

其他教程

Excel简单一键求和 Autohotkey

2020-2-19 10:30:55

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
有新私信 私信列表
搜索