|
wcymiss 发表于 2015-3-9 17:06
嗯,确实有错,而且错得离谱,不知道第一个写这段代码是根据什么写的。
错误1:压缩前数据长度获取错误。 ...
公司电脑网络限制,登录不了本论坛了,也下载不了附件,所以帖上老师的代码,回公司后再慢慢研究:
方法一gzip_dll(Module)
Option Explicit
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
'======================解压API==========================
Private Declare Function InitDecompression Lib "D:\gzip.dll" () As Long
Private Declare Function CreateDecompression Lib "D:\gzip.dll" (ByRef context As Long, ByVal flags As Long) As Long
Private Declare Function DestroyDecompression Lib "D:\gzip.dll" (ByVal context As Long) As Long
Private Declare Function Decompress Lib "D:\gzip.dll" ( _
ByVal context As Long, _
ByRef input_buffer As Any, ByVal input_buffer_size As Long, _
ByRef output_buffer As Any, ByVal Output_buffer_size As Long, _
ByRef input_used As Long, ByRef output_used As Long) As Long
Public Function UnCompressByGzipDLL(arrByte() As Byte, Optional Flag As Long = 1) As Boolean
Dim contextHandle As Long
Dim InputBufferSize As Long
Dim OutputBuffer() As Byte
Dim OutputBufferSize As Long
Dim outUsed As Long
Dim inUsed As Long
Dim ReturnValue As Long
UnCompressByGzipDLL = False
ReDim Result(0)
InputBufferSize = UBound(arrByte) + 1
CopyMemory OutputBufferSize, arrByte(UBound(arrByte) - 3), 4
ReDim OutputBuffer(OutputBufferSize - 1) As Byte
InitDecompression
CreateDecompression contextHandle, Flag
ReturnValue = Decompress( _
contextHandle, _
arrByte(0), InputBufferSize, _
OutputBuffer(0), OutputBufferSize + 1, _
inUsed, outUsed)
DestroyDecompression contextHandle
If ReturnValue = 0 Then
UnCompressByGzipDLL = True
arrByte = OutputBuffer
End If
End Function
方法二winrar(Module)
Option Explicit
Public Function UnCompressByWinrar(arrByte() As Byte) As Boolean
Const WinrarFullName As String = "C:\Program Files\WinRAR\WinRAR.exe"
Const TempPath As String = "D:\"
Const TempGzipFile As String = "GzipTest"
Dim GzFile As String
Dim UnGzFile As String
If Dir(WinrarFullName) = "" Then MsgBox "请写入winrar.exe文件的正确路径": Exit Function
GzFile = TempPath & TempGzipFile & ".gz"
UnGzFile = TempPath & TempGzipFile
If Dir(GzFile) <> "" Then Kill GzFile
Open GzFile For Binary As #1
Put #1, , arrByte
Close #1
CreateObject("wscript.shell").Run """" & WinrarFullName & """ x " & GzFile & " " & TempGzipFile & " " & TempPath & " -y -ibck", 0, 1
Open UnGzFile For Binary As #1
ReDim arrByte(LOF(1) - 1)
Get #1, , arrByte
Close #1
Kill TempPath & TempGzipFile & ".gz"
Kill TempPath & TempGzipFile
UnCompressByWinrar = True
End Function
网站测试(Module)
Option Explicit
Sub URLTest1() '用方法1
Dim arrByte() As Byte
Dim arrByteGzip() As Byte
Dim strText As String
Dim strTextGzip As String
Dim ISize As Long
Dim CanCompare As Boolean
Dim i As Long
i = 4
CanCompare = True
With CreateObject("WinHttp.WinHttpRequest.5.1")
.Open "GET", Cells(i, 1).Value, False
.Send
If .getallresponseheaders Like "*Content-Encoding: gzip*" Then
CanCompare = False
Cells(i, 6).Value = "无非GZIP数据"
Else
arrByte = .responseBody
Cells(i, 2).Value = UBound(arrByte) + 1
strText = byteToStr(arrByte, "utf-8")
End If
End With
With CreateObject("WinHttp.WinHttpRequest.5.1")
.Open "GET", Cells(i, 1).Value, False
.setRequestHeader "Accept-Encoding", "gzip, deflate"
.Send
If .getallresponseheaders Like "*Content-Encoding: gzip*" Then
arrByteGzip = .responseBody
Cells(i, 3).Value = UBound(arrByteGzip) + 1
CopyMemory ISize, arrByteGzip(UBound(arrByteGzip) - 3), 4
Cells(i, 4).Value = ISize
Call UnCompressByGzipDLL(arrByteGzip)
Cells(i, 5).Value = UBound(arrByteGzip) + 1
strTextGzip = byteToStr(arrByteGzip, "utf-8")
Else
CanCompare = False
Cells(i, 6).Value = "无GZIP数据"
End If
End With
If CanCompare Then
Cells(i, 6).Value = (strTextGzip = strText)
End If
End Sub
Sub URLTest2() '用方法2
Dim arrByte() As Byte
Dim arrByteGzip() As Byte
Dim strText As String
Dim strTextGzip As String
Dim ISize As Long
Dim CanCompare As Boolean
Dim i As Long
i = 4
CanCompare = True
Range(Cells(i, 2), Cells(i, 8)).ClearContents
With CreateObject("WinHttp.WinHttpRequest.5.1")
.Open "GET", Cells(i, 1).Value, False
.Send
If .getallresponseheaders Like "*Content-Encoding: gzip*" Then
CanCompare = False
Cells(i, 6).Value = "无非GZIP数据"
Else
arrByte = .responseBody
Cells(i, 2).Value = UBound(arrByte) + 1
strText = byteToStr(arrByte, "utf-8")
End If
End With
With CreateObject("WinHttp.WinHttpRequest.5.1")
.Open "GET", Cells(i, 1).Value, False
.setRequestHeader "Accept-Encoding", "gzip, deflate"
.Send
If .getallresponseheaders Like "*Content-Encoding: gzip*" Then
arrByteGzip = .responseBody
Cells(i, 3).Value = UBound(arrByteGzip) + 1
CopyMemory ISize, arrByteGzip(UBound(arrByteGzip) - 3), 4
Cells(i, 4).Value = ISize
Call UnCompressByWinrar(arrByteGzip)
Cells(i, 5).Value = UBound(arrByteGzip) + 1
strTextGzip = byteToStr(arrByteGzip, "utf-8")
Else
CanCompare = False
Cells(i, 6).Value = "无GZIP数据"
End If
End With
If CanCompare Then
Cells(i, 6).Value = (strTextGzip = strText)
End If
End Sub
Private Function byteToStr(arrByte, strCharset As String) As String
With CreateObject("Adodb.Stream")
.Type = 1 'adTypeBinary
.Open
.Write arrByte
.Position = 0
.Type = 2 'adTypeText
.Charset = strCharset
byteToStr = .Readtext
.Close
End With
End Function
压缩(Module)
Option Explicit
======================压缩API==========================
Private Declare Function InitCompression Lib "D:\gzip.dll" () As Long
Private Declare Function CreateCompression Lib "D:\gzip.dll" (ByRef context As Long, ByVal flags As Long) As Long
Private Declare Function DestroyCompression Lib "gD:\zip.dll" (ByVal context As Long) As Long
Private Declare Function Compress Lib "D:\gzip.dll" ( _
ByVal context As Long, _
ByRef input_buffer As Any, ByVal input_buffer_size As Long, _
ByRef output_buffer As Any, ByVal Output_buffer_size As Long, _
ByRef input_used As Long, ByRef output_used As Long, _
ByVal compression_level As Long) As Long
Public Function CompressByGzipDLL(arrByte() As Byte, Optional Flag As Long = 1) As Boolean
Dim contextHandle As Long
Dim InputBufferSize As Long
Dim OutputBuffer() As Byte
Dim outUsed As Long
Dim inUsed As Long
Dim ReturnValue As Long
Dim Result() As Byte
Dim TempLength As Long
Const MaxBufferSize As Long = 1000
CompressByGzipDLL = False
ReDim Result(0)
InputBufferSize = UBound(arrByte) + 1
ReDim OutputBuffer(MaxBufferSize - 1) As Byte
InitCompression
CreateCompression contextHandle, Flag
Do
ReturnValue = Compress( _
contextHandle, _
arrByte(0), InputBufferSize, _
OutputBuffer(0), MaxBufferSize, _
inUsed, outUsed, 1)
If outUsed <> 0 Then
TempLength = UBound(Result)
ReDim Preserve Result(TempLength + outUsed)
CopyMemory Result(TempLength), OutputBuffer(0), outUsed
InputBufferSize = InputBufferSize - inUsed
End If
Loop While ReturnValue = 0
DestroyCompression contextHandle
If UBound(Result) > 0 Then
CompressByGzipDLL = True
ReDim arrByte(UBound(Result) - 1)
CopyMemory arrByte(0), Result(0), UBound(Result)
End If
End Function
字符串测试(Module)
Option Explicit
Sub StringTest1() '用方法1
Dim strText As String
Dim arrByte() As Byte
Dim arrByte0() As Byte
Dim i As Long
strText = String(1000, "a")
arrByte0 = StrConv(strText, vbFromUnicode)
arrByte = arrByte0
Debug.Print "长度:" & UBound(arrByte) + 1
Debug.Print "压缩执行:" & CompressByGzipDLL(arrByte, 1)
Debug.Print "解压执行:" & UnCompressByGzipDLL(arrByte, 1)
If UBound(arrByte) = UBound(arrByte0) Then
Debug.Print "解压正确"
Else
Debug.Print "解压错误"
End If
For i = 0 To UBound(arrByte)
If arrByte(i) <> arrByte0(i) Then Stop
Next
End Sub
Sub StringTest2() '用方法2
Dim strText As String
Dim arrByte() As Byte
Dim arrByte0() As Byte
Dim i As Long
strText = String(1000, "a")
arrByte0 = strText
arrByte = arrByte0
Debug.Print "长度:" & UBound(arrByte) + 1
Debug.Print "压缩执行:" & CompressByGzipDLL(arrByte, 1)
Debug.Print "解压执行:" & UnCompressByWinrar(arrByte)
If UBound(arrByte) = UBound(arrByte0) Then
Debug.Print "解压正确"
Else
Debug.Print "解压错误"
End If
For i = 0 To UBound(arrByte)
If arrByte(i) <> arrByte0(i) Then Stop
Next
End Sub
|
|