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

流的壓縮和解壓

編輯:Delphi
(*//
標題:流的壓縮和解壓
說明:適用文件壓縮、圖象壓縮等;調用ZLib單元的方法實現
設計:Zswang
支持:[email protected]
日期:2004-03-24
//*)
(*//============================================================================
設計思路:
創建TCompressionStream、TDecompressionStream實例進行壓縮和解壓
============================================================================//*)
uses ZLib;
const cBufferSize = $4096;
function StreamCompression(mInputStream: TStream; mOutputStream: TStream): Integer;
var
  I: Integer;
  vBuffer: array[0..cBufferSize]of Char;
begin
  Result := -1;
  if not (Assigned(mInputStream) and Assigned(mOutputStream)) then Exit;
  with TCompressionStream.Create(clMax, mOutputStream) do try
    for I := 1 to mInputStream.Size div cBufferSize do begin
      mInputStream.Read(vBuffer, cBufferSize);
      Write(vBuffer, cBufferSize);
    end;
    I := mInputStream.Size mod cBufferSize;
    if I > 0 then begin
      mInputStream.Read(vBuffer, I);
      Write(vBuffer, I);
    end;
  finally
    Free;
  end;
end; { StreamCompression }
function StreamDecompression(mInputStream: TStream; mOutputStream: TStream): Integer;
var
  vBuffer: array[0..cBufferSize]of Char;
  I: Integer;
begin
  Result := -1;
  if not (Assigned(mInputStream) and Assigned(mOutputStream)) then Exit;
  with TDecompressionStream.Create(mInputStream) do try
    repeat
      I := Read(vBuffer, cBufferSize);
      mOutputStream.Write(vBuffer, I);
    until I = 0;
    Result := mOutputStream.Size;
  finally
    Free;
  end;
end; { StreamDecompression }
//Example
procedure TForm1.Button1Click(Sender: TObject);
var
  vInputStream: TFileStream;
  vOutputStream: TFileStream;
begin
  vInputStream := TFileStream.Create('c:	emp	emp.exe', fmOpenRead);
  vOutputStream := TFileStream.Create('c:	emp	emp.exe.z', fmCreate);
  try
    StreamCompression(vInputStream, vOutputStream);
  finally
    vInputStream.Free;
    vOutputStream.Free;
  end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
  vInputStream: TFileStream;
  vOutputStream: TFileStream;
begin
  vInputStream := TFileStream.Create('c:	emp	emp.exe.z', fmOpenRead);
  vOutputStream := TFileStream.Create('c:	emp	emp~.exe', fmCreate);
  try
    StreamDecompression(vInputStream, vOutputStream);
  finally
    vInputStream.Free;
    vOutputStream.Free;
  end;
end;
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved