客户专线:400-641-8864 / 153-2383-7083 | 在线QQ:987837344
首页 易当家ERP 鼎捷ERP 行业方案 实施方法 联系我们  
 

Delphi7不用第三方控件实现文件压缩


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, StrUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, zlib, Zlibconst;

type
  TForm1 = class(TForm)
    dlgOpen1: TOpenDialog;
    edt1: TEdit;
    btn1: TButton;
    btn2: TButton;
    btn3: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

procedure CompressIt(var CompressedStream: TMemoryStream; const CompressionLevel: TCompressionLevel);


var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure CompressIt(var CompressedStream: TMemoryStream; const CompressionLevel: TCompressionLevel);
// 参数是传递的流和压缩方式
var
  SourceStream: TCompressionStream;
  DestStream: TMemoryStream;
  Count: int64; //注意,此处修改了,原来是int
begin
  //获得流的原始尺寸
  Count := CompressedStream.Size;
  DestStream := TMemoryStream.Create;
  SourceStream := TCompressionStream.Create(CompressionLevel, DestStream);
  try
    //SourceStream中保存着原始的流
    CompressedStream.SaveToStream(SourceStream);
    //将原始流进行压缩, DestStream中保存着压缩后的流
    SourceStream.Free;
    CompressedStream.Clear;
    //写入原始图像的尺寸
    CompressedStream.WriteBuffer(Count, SizeOf(Count));
    //写入经过压缩的流
    CompressedStream.CopyFrom(DestStream, 0);
  finally
    DestStream.Free;
  end;
end;

procedure UnCompressit(const CompressedStream: TMemoryStream; var UnCompressedStream: TMemoryStream);
//参数    压缩过的流,解压后的流
var
  SourceStream: TDecompressionStream;
  DestStream: TMemoryStream;
  Buffer: PChar;
  Count: int64;
begin
  //从被压缩的图像流中读出原始的尺寸
  CompressedStream.ReadBuffer(Count, SizeOf(Count));
  //根据尺寸大小为将要读入的原始流分配内存块
  GetMem(Buffer, Count);
  DestStream := TMemoryStream.Create;
  SourceStream := TDecompressionStream.Create(CompressedStream);
  try
    //将被压缩的流解压缩,然后存入 Buffer内存块中
    SourceStream.ReadBuffer(Buffer^, Count);
    //将原始流保存至 DestStream流中
    DestStream.WriteBuffer(Buffer^, Count);
    DestStream.Position := 0; //复位流指针
    //从 DestStream流中载入图像流
    UnCompressedStream.LoadFromStream(DestStream);
  finally
    FreeMem(Buffer);
    DestStream.Free;
  end;
end;

procedure TForm1.btn1Click(Sender: TObject);
begin
  dlgOpen1.Execute;
  edt1.Text := dlgOpen1.FileName;
end;

procedure TForm1.btn2Click(Sender: TObject);
//把指定文件压缩然后保存为另外一个压缩包,
//呵呵,我使用的时候是把后缀改成cab,可以唬一些人吧?
var
  SM: TMemoryStream;
begin
  SM := TMemoryStream.Create;
  try
    Sm.LoadFromFile(edt1.Text);
    SM.Position := 0;
    Compressit(sm, clDefault);
    sm.SaveToFile(edt1.Text + '.cab');
  finally
    SM.Free;
  end;
end;


procedure TForm1.btn3Click(Sender: TObject);
//把指定的压缩包解成原来的文件。
var
  SM, DM: TMemoryStream;
begin
  SM := TMemoryStream.Create;
  DM := TMemoryStream.Create;
  try
    Sm.LoadFromFile(edt1.Text);
    SM.Position := 0;
    UnCompressit(sm, dm);
    dm.Position := 0;
    dm.SaveToFile(LeftStr(edt1.Text, Length(edt1.Text) - 4));
  finally
    SM.Free;
    DM.Free;
  end;
end;

end.



猜您喜欢:     易当家E9ERP     易当家E8ERP     微信公众号开发

ERP,ERP系统,ERP软件
ERP软件 | 鼎捷软件 | 神州数码 |
Copyright© 2008-2016 深圳市易当家软件开发有限公司. All Rights Reserved. 粤ICP备14043283号