压缩流
压缩流
支持DELPHI7及以上版本。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | unit zip; interface {$ if CompilerVersion>= 21} {$define NEWZLib} {$IFEND} uses Classes, Zlib, SysUtils; type {$ if CompilerVersion< 18.5} TBytes = array of Byte; {$IFEND} TZip = class (TObject) public /// <summary> /// 解压 /// </summary> class procedure UnZipStream( const pvInStream, pvOutStream: TStream); /// <summary> /// 压缩 /// </summary> class procedure ZipStream( const pvInStream, pvOutStream: TStream); class function verifyData( const buf; len:Cardinal): Cardinal; class function verifyStream(pvStream:TStream; len:Cardinal): Cardinal; end; implementation class procedure TZip.UnZipStream( const pvInStream, pvOutStream: TStream); var l:Integer; {$IFDEF POSIX} var lvBytes, lvOutBytes:TBytes; {$ELSE} var lvBytes:TBytes; OutBuf: Pointer; OutBytes: Integer; {$ENDIF} begin if pvInStream= nil then exit; l := pvInStream.Size; if l = 0 then Exit; {$IFDEF POSIX} SetLength(lvBytes, l); pvInStream.Position := 0; pvInStream.Read(lvBytes[0], pvInStream.Size); ZLib.ZDecompress(lvBytes, lvOutBytes); //POSIX下,只支持该方式 pvOutStream.Size := Length(lvOutBytes); pvOutStream.Position := 0; pvOutStream.Write(lvOutBytes[0], Length(lvOutBytes)); {$ELSE} setLength(lvBytes, l); pvInStream.Position := 0; pvInStream.ReadBuffer(lvBytes[0], l); {$ if defined(NEWZLib)} ZLib.ZDecompress(@lvBytes[0], l, OutBuf, OutBytes); {$ELSE} Zlib.DecompressBuf(@lvBytes[0], l, 0, OutBuf, OutBytes); {$ifend} try pvOutStream.Size := OutBytes; pvOutStream.Position := 0; pvOutStream.WriteBuffer(OutBuf^, OutBytes); finally FreeMem(OutBuf, OutBytes); end; {$ENDIF} end; class function TZip.verifyData( const buf; len: Cardinal): Cardinal; var i:Cardinal; p:PByte; begin i := 0; Result := 0; p := PByte(@buf); while i < len do begin Result := Result + p^; Inc(p); Inc(i); end; end; class function TZip.verifyStream(pvStream:TStream; len:Cardinal): Cardinal; var l, j:Cardinal; lvBytes:TBytes; begin SetLength(lvBytes, 1024); if len = 0 then begin j := pvStream.Size - pvStream.Position; end else begin j := len; end; Result := 0; while j > 0 do begin if j <1024 then l := j else l := 1024; pvStream.ReadBuffer(lvBytes[0], l); Result := Result + verifyData(lvBytes[0], l); Dec(j, l); end; end; class procedure TZip.ZipStream( const pvInStream, pvOutStream: TStream); {$IFDEF POSIX} var lvBytes, lvOutBytes:TBytes; {$ELSE} var lvInBuf: TBytes; OutBuf: Pointer; OutBytes: Integer; {$ENDIF} var l: Integer; begin if pvInStream= nil then exit; l := pvInStream.Size; if l = 0 then Exit; {$IFDEF POSIX} SetLength(lvBytes, pvInStream.Size); pvInStream.Position := 0; pvInStream.Read(lvBytes[0], pvInStream.Size); ZLib.ZCompress(lvBytes, lvOutBytes); // POSIX下只支持该中方式的压缩 pvOutStream.Size := Length(lvOutBytes); pvOutStream.Position := 0; pvOutStream.Write(lvOutBytes[0], Length(lvOutBytes)); {$ELSE} SetLength(lvInBuf, l); pvInStream.Position := 0; pvInStream.ReadBuffer(lvInBuf[0], l); {$ if defined(NEWZLib)} ZLib.ZCompress(@lvInBuf[0], l, OutBuf, OutBytes); {$ELSE} ZLib.CompressBuf(@lvInBuf[0], l, OutBuf, OutBytes); {$ifend} try pvOutStream.Size := OutBytes; pvOutStream.Position := 0; pvOutStream.WriteBuffer(OutBuf^, OutBytes); finally FreeMem(OutBuf, OutBytes); end; {$ENDIF} end; end. |
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/11797094.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?