找回密码
 加入
搜索
查看: 2906|回复: 3

[网络通信] 提个较有技术含量的问题!有什么函数可以能对数据包或者变量里的数据进行压缩吗?

[复制链接]
发表于 2011-4-14 14:46:24 | 显示全部楼层 |阅读模式
我发现富文本编辑器上的内容占空间很大,每次将这些数据通过tcp发给客户端,效率太低了。
希望大牛能给段压缩数据的代码,将数据压缩后发送,客户端再解压还原数据
 楼主| 发表于 2011-4-14 14:52:02 | 显示全部楼层
网上搜了一个vb的代码,看不懂,不知道能不能利用和改写?
ption 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
发表于 2011-4-14 16:31:37 | 显示全部楼层
http://www.autoitx.com/thread-14245-1-1.html
看看这个不知道有没有用,一直没搞懂是什么意思,只知道大概和数据压缩有关
 楼主| 发表于 2011-4-14 17:03:50 | 显示全部楼层
谢谢楼上!解决了!压缩后大概只有原来的数据包大小的十分之一!
不过就是解压速度慢。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-11-15 07:17 , Processed in 0.076279 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表