Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long) Private Declare Function InitDecompression Lib "gzip.dll" () As Long Private Declare Function CreateDecompression Lib "gzip.dll" (ByRef context As Long, ByVal Flags As Long) As Long Private Declare Function DestroyDecompression Lib "gzip.dll" (ByRef context As Long) As Long Private Declare Function Decompress Lib "gzip.dll" (ByVal context As Long, inBytes As Any, ByVal input_size As Long, outBytes As Any, ByVal output_size As Long, ByRef input_used As Long, ByRef output_used As Long) As Long Private Const OFFSET As Long = &H8 '解压缩数组 Public Function UnCompressByte(ByteArray() As Byte) As Boolean Dim BufferSize As Long Dim buffer() As Byte Dim lReturn As Long Dim outUsed As Long Dim inUsed As Long '创建解压缩后的缓存 CopyMemory BufferSize, ByteArray(0), OFFSET BufferSize = BufferSize + (BufferSize * 0.01) + 12 ReDim buffer(BufferSize) As Byte '创建解压缩进程 Dim contextHandle As Long: InitDecompression CreateDecompression contextHandle, 1 '创建 '解压缩数据 lReturn = Decompress(ByVal contextHandle, ByteArray(0), UBound(ByteArray) + 1, buffer(0), BufferSize, inUsed, outUsed) DestroyDecompression contextHandle '删除重复的数据 ReDim Preserve ByteArray(0 To outUsed - 1) CopyMemory ByteArray(0), buffer(0), outUsed End Function
压缩
Option Explicit 'Declares Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long) Private Declare Function Compress Lib "zlibwapi.dll" Alias "compress" (dest As Any, destLen As Any, src As Any, ByVal srcLen As Long) As Long Private Declare Function uncompress Lib "zlibwapi.dll" (dest As Any, destLen As Any, src As Any, ByVal srcLen As Long) As Long Private Const OFFSET As Long = &H8 '压缩数组 Public Function CompressByte(ByteArray() As Byte) As Boolean Dim BufferSize As Long Dim TempBuffer() As Byte 'Create a buffer to hold the compressed data BufferSize = UBound(ByteArray) + 1 BufferSize = BufferSize + (BufferSize * 0.01) + 12 ReDim TempBuffer(BufferSize) 'Compress byte array (data) CompressByte = (Compress(TempBuffer(0), BufferSize, ByteArray(0), UBound(ByteArray) + 1) = 0) 'Add the size of the original data Call CopyMemory(ByteArray(0), CLng(UBound(ByteArray) + 1), OFFSET) 'Remove redundant data ReDim Preserve ByteArray(0 To BufferSize + OFFSET - 1) CopyMemory ByteArray(OFFSET), TempBuffer(0), BufferSize End Function '解压缩数组 Public Function UnCompressByte(ByteArray() As Byte) As Boolean Dim OrigLen As Long Dim BufferSize As Long Dim TempBuffer() As Byte 'Get the original size Call CopyMemory(OrigLen, ByteArray(0), OFFSET) 'Create a buffer to hold the uncompressed data BufferSize = OrigLen BufferSize = BufferSize + (BufferSize * 0.01) + 12 ReDim TempBuffer(BufferSize) 'Decompress data UnCompressByte = (uncompress(TempBuffer(0), BufferSize, ByteArray(OFFSET), UBound(ByteArray) - OFFSET + 1) = 0) 'Remove redundant data ReDim Preserve ByteArray(0 To BufferSize - 1) CopyMemory ByteArray(0), TempBuffer(0), BufferSize End Function