1,488
社区成员
发帖
与我相关
我的任务
分享
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Declare Function InitCompression Lib "gzip.dll" () As Long
Private Declare Function CreateCompression Lib "gzip.dll" (ByRef context As Long, ByVal flags As Long) As Long
Private Declare Function DestroyCompression Lib "gzip.dll" (ByRef context As Long) As Long
Private Declare Function Compress Lib "gzip.dll" (ByVal context As Long, ByVal inBytes As Any, ByVal input_size As Long, ByVal outBytes As Long, ByVal output_size As Long, ByRef input_used As Long, ByRef output_used As Long, ByVal compressionLevel As Long) As Long
Private Const GZIP_LVL As Integer = 1
Public Function GZipCompress(ByteArray() As Byte) As Boolean
Dim hContext As Long '正文句柄
Dim output_used As Long '压缩后尺寸
Dim input_used As Long '未压缩尺寸
Dim buffer() As Byte
context = 0
output_used = 0
input_used = 0
InitCompression ' // 初始化压缩
CreateCompression hContext, GZIP_LVL
ReDim buffer(UBound(In_bytes))
If (Compress(hContext, ByteArray, UBound(ByteArray), buffer, UBound(buttfer), input_used, output_used, GZIP_LVL) <> 0) Then
GZipCompress=false
exit function
End If
GZipCompress=true
end function
开始的320字节,当然一直是“不够用”。
微软的程序猿也是,脑壳是不是进水太多了……
空间不足,偏偏弄个“参数错误”的错误代码来返回!
以前用的ZLib的,同样的问题就有一个明确的错误代码:Z_BUF_ERROR。
看看“别人的”函数库,如果出错,原因直观得多!
Shell "c:\progra~1\winrar\rar a -r d:\mydir\myrar.rar d:\mydata\*.*",vbHide
原来那“200字节”随机数经“压缩”后,需要的空间是400到410字节左右。
(压缩后,占用的数据空间增加了200字节左右…………)
我给的512字节的缓冲区,执行就正确了。
(反正即使产生200字节的字母编码,“输出”的数据也是“乱码”了)

好多都在说用Zlib实现gzip的压缩。
你的dll是从哪下载的?
有没有这个dll的函数原型的资料?
Private Declare Function InitCompression Lib "gzip.dll" () As Long
Private Declare Function CreateCompression Lib "gzip.dll" (ByRef context As Long, ByVal flags As Long) As Long
Private Declare Function DestroyCompression Lib "gzip.dll" (ByRef context As Long) As Long
Private Declare Function Compress Lib "gzip.dll" (ByVal context As Long, ByRef inBytes As Any, ByVal input_size As Long, ByRef outBytes As Any, ByVal output_size As Long, ByRef input_used As Long, ByRef output_used As Long, ByVal compressionLevel As Long) As Long
Private Const GZIP_LVL As Integer = 1
Public Function GZipCompress(ByteArray() As Byte) As Boolean
Dim hContext As Long '正文句柄
Dim output_used As Long '压缩后尺寸
Dim input_used As Long '未压缩尺寸
Dim buffer() As Byte
Dim iReturn As long
context = 0
output_used = 0
input_used = 0
InitCompression ' // 初始化压缩
CreateCompression hContext, GZIP_LVL
ReDim buffer(UBound(ByteArray) + 255)
lReturn = Compress(ByVal hContext, ByteArray(0), UBound(ByteArray) + 1, buffer(0), UBound(buffer), input_used, output_used, GZIP_LVL)
End Function
已经不报错了,但是数据没有被压缩。 IReturn 值是 -2147024809
我怀疑还是 Compress 函数中的参数没写好。
Function Compress( ) 的API声明形式不正确。
第2个、第4个参数,应该用“ByRef xxxx As Any”,或者用“ByVal xxxx As Long”。
然后在调用时,如果是ByRef的参数,就用 ByteArray(0)和buffer(0);
如果是ByVal As long的参数,就要用 VarPtr(ByteArray(0))和VarPtr(buffer(0))。
另外,必须注意的是:在调用 Compress( )之前,你的buffer()必须先用redim 给它分配足够大的数据空间!
否则会造成你的进程崩溃。