程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> delphi Zlib 流壓縮解壓

delphi Zlib 流壓縮解壓

編輯:Delphi

procedure UnCompressionStream(var ASrcStream:TMemoryStream); //解壓縮
var
nTmpStream:TDecompressionStream;
nDestStream:TMemoryStream;
nBuf: array[1..512] of Byte;
nSrcCount: integer;
begin
ASrcStream.Position := 0;
nDestStream := TMemoryStream.Create;
nTmpStream := TDecompressionStream.Create(ASrcStream);
try
    repeat
      //讀入實際大小
      nSrcCount := nTmpStream.Read(nBuf, SizeOf(nBuf));
      if nSrcCount > 0 then
        nDestStream.Write(nBuf, nSrcCount);
    until (nSrcCount = 0);
    ASrcStream.Clear;
    ASrcStream.LoadFromStream(nDestStream);
    ASrcStream.Position := 0;
finally
    nDestStream.Clear;
    nDestStream.Free;
    nTmpStream.Free;
end;
end;
procedure CompressionStream(var ASrcStream:TMemoryStream;ACompressionLevel:Integer = 2); //壓縮流
var
nDestStream:TMemoryStream;
nTmpStream:TCompressionStream;
nCompressionLevel:TCompressionLevel;
begin
ASrcStream.Position := 0;
nDestStream := TMemoryStream.Create;
try
    //級別
    case ACompressionLevel of
      0:nCompressionLevel := clNone;
      1:nCompressionLevel := clFastest;
      2:nCompressionLevel := clDefault;
      3:nCompressionLevel := clMax;
      else
        nCompressionLevel := clMax;
    end;
    //開始壓縮
    nTmpStream := TCompressionStream.Create(nCompressionLevel,nDestStream);
    try
      ASrcStream.SaveToStream(nTmpStream);
    finally
      nTmpStream.Free;//釋放後nDestStream才會有數據
    end;
    ASrcStream.Clear;
    ASrcStream.LoadFromStream(nDestStream);
    ASrcStream.Position := 0;
finally
    nDestStream.Clear;
    nDestStream.Free;
end;
end;

摘自 踏雪無痕
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved