内存中压缩数组的二种办法

原创 2007年10月15日 14:48:00

到底压缩数组有什么好处我就不多说了,直接看代码吧

1 利用zlibwapi.dll(网上到处都有下载的,还有C的源码)
注:利用代码 转自“http://blog.csdn.net/modest/archive/2006/04/13/662072.aspx”
魏滔序原创
提示:DLL记得要先注册哦
'-------------------------------------------代码如下------------------------------------------------------------

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

'-----------------------------------------------------------------------------------------------------------------------------

2 改良一个俄罗斯的算法,是一位CSDN的朋友改的,我偶然得之。
效率一般,速度可以接受,唯一的缺点是不支持已压缩或加壳的EXE,处理单纯的BYTE()肯定是没问题

'-------------------------------------------代码如下------------------------------------------------------------

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

将RGB数组在内存中压缩成JPEG文件

将RGB数组在内存中压缩成JPEG文件 0.   环境 1)       jpeg库: jpegsr9a  下载地址:http://www.ijg.org/ 2)       编译环境: vs2...
  • zzzzzqf
  • zzzzzqf
  • 2014-07-08 20:10:48
  • 1395

使用Zlib对内存流及标准输入流进行压缩与解压缩

使用Zlib对内存流及标准输入流进行压缩与解压缩,以下是拼接官网zlib源代码文件的顺序。  zconf.h  zlib.h  zutil.h  gzguts.h  zutil.c  adler32....
  • i7thTool
  • i7thTool
  • 2017-01-06 09:07:44
  • 1054

java从数组中删除数组元素,数组与内存控制

通过从一个数组中删除数组元素到java数组与内存的控制。一、删除数组中的元素 这里有个很重要的概念:java数组一旦初始化完成,该数组长度将不可改变。...
  • smartdt
  • smartdt
  • 2017-12-01 16:47:01
  • 221

内存压缩是什么?有什么用?

知识重点:请问...和那个内存清理有什么不同?回答列表1 内存压缩技术介绍为节省存储空间或传输带宽,人们已经在计算机系统中广泛地使用了数据压缩技术。在磁介质存储数据或网络传输数据时,人们使用基于硬.....
  • abc470337944
  • abc470337944
  • 2011-10-13 17:00:00
  • 6682

使用SharpZipLib压缩打包多个内存中的文件

SharpZipLib是C#写的开源压缩解压缩组件,最近项目上遇到一个需求:根据用户选择的项目生成CSV文件并下载,后来改为同时生成2个CSV文件下载下来。想到的解决办法就是将2个CSV文件打包成一个...
  • studyzy
  • studyzy
  • 2013-09-10 14:23:07
  • 1149

1-byte array(byte[], boolean[])图片导致的内存过大

Android-视图显示大量图片时的内存问题  昨天优化一个项目(基本上每个ACTIVITY都会有大量图片),查了很多资料,借鉴了一些做法,网上的说法有对有错,这里总结一下。  android3.0默...
  • nnmmbb
  • nnmmbb
  • 2014-01-22 14:12:08
  • 2422

C#对byte数组压缩和解压

直接上代码 public class ByteHelper { public const ushort COMPRESSION_FORMAT_LZNT1 = 2; ...
  • zy332719794
  • zy332719794
  • 2014-06-05 16:13:29
  • 2923

字符串的压缩与解压 java

  • 2011年10月28日 11:27
  • 3KB
  • 下载

gzip压缩的数据如何在内存中解压

http://bbs.chinaunix.net/thread-1361609-1-1.html http://hispider.googlecode.com/svn/trunk/devel/his...
  • m372897500
  • m372897500
  • 2012-05-10 11:36:18
  • 842

Dell R2950内存接法

  • 2014年10月20日 22:55
  • 210KB
  • 下载
收藏助手
不良信息举报
您举报文章:内存中压缩数组的二种办法
举报原因:
原因补充:

(最多只允许输入30个字)