听说用ZLIB。PAS可进行数据压缩,哪位大侠告诉我如何使用?(100分)

  • 主题发起人 主题发起人 Hjking
  • 开始时间 开始时间
试一试 ASPACK.EXE 。
这个东西能将EXE、DLL、OCX等压缩很多(大约是30%)。
 
看看下面的代码!

unit MyCtrl;

interface

uses
Windows, SysUtils, Classes, Graphics, Forms, ZLib;

procedure GetScreen(var MyStream: TMemoryStream);
procedure UnCompressStream(var MyStream: TMemoryStream);
procedure CompressStream(var MyStream: TMemoryStream);

implementation

procedure GetScreen(var MyStream: TMemoryStream);
var
Dc: HDC;
MyCanvas: TCanvas;
MyRect: TRect;
Bmp: TBitmap;
begin
//捕捉全屏幕图像
Bmp:= TBitmap.Create;
Dc := GetWindowDC(0);
MyCanvas := TCanvas.Create;
try
MyCanvas.Handle := Dc;
MyRect:=Rect(0, 0,Screen.Width, Screen.Height);
//图像为 16位真彩色,也可根据实际需要调整
Bmp.PixelFormat := pf16bit;
Bmp.Width := MyRect.Right;
Bmp.Height := MyRect.Bottom;
//捕捉整个屏幕图像
Bmp.Canvas.CopyRect(MyRect, MyCanvas, MyRect);
Bmp.SaveToStream(MYStream);
finally
MyCanvas.Handle := 0;
MyCanvas.Free;
ReleaseDC(0, Dc);
Bmp.Free;
end;
end;

procedure CompressStream(var MyStream: TMemoryStream);
var
SourceStream: TCompressionStream;
DestStream: TMemoryStream;
Count: Integer;
Begin
//压缩图像
//获得图像流的原始尺寸
Count := MyStream.Size;
DestStream := TMemoryStream.Create;
SourceStream:=TCompressionStream.Create(clDefault, DestStream);
Try
//SourceStream中保存着原始的图像流
MyStream.SaveToStream(SourceStream);
//将原始图像流进行压缩, DestStream中保存着压缩后的图像流
SourceStream.Free;
MYStream.Clear;
//写入原始图像的尺寸
MyStream.WriteBuffer(Count, SizeOf(Count));
//写入经过压缩的图像流
MyStream.CopyFrom(DestStream, 0);
//复位流指针
MyStream.Position := 0;
finally
DestStream.Free;
end;
end;

procedure UnCompressStream(var MyStream: TMemoryStream);
var
SourceStream: TDecompressionStream;
Buffer: PChar;
Count: Integer;
Begin
//还原被压缩图像
//从被压缩的图像流中读出原始图像的尺寸
MyStream.ReadBuffer(Count, SizeOf(Count));
//根据图像尺寸大小为将要读入的原始图像流分配内存块
GetMem(Buffer, Count);
SourceStream := TDecompressionStream.Create(MyStream);
Try
//将被压缩的图像流解压缩,然后存入 Buffer内存块中
SourceStream.ReadBuffer(Buffer^, Count);
//将原始图像流保存至 DestStream流中
MyStream.Clear;
MyStream.WriteBuffer(Buffer^, Count);
//复位流指针
MyStream.Position := 0;
finally
FreeMem(Buffer);
end;
end;
end.
 
Thank you,panyongze.
如何压缩一般的文件呢,如.exe,.mdb...
 
procedure TForm1.Button1Click(Sender: TObject);
var
MS: TMemoryStream;
begin
MS:= TMemoryStream.Create;
ms.LoadFromFile('c:/aa.exe');
ms.SaveToFile('c:/bb.exe');
ms.Free;
end;
 
panyongze,你这样未达到压缩的目的。
 
你想压缩一般文件,ziptv是不错的选择,他支持很多格式:zip,arj,rar等

很多网站都有下载
 
我不想用第三方控件。
 
procedure TForm1.Button1Click(Sender: TObject);
var
MS: TMemoryStream;
begin
MS:= TMemoryStream.Create;
ms.LoadFromFile('c:/aa.exe');
//如果aa.exe是未压缩的文件,下面代码压缩文件。
CompressStream(My);
//如果aa.exe是已经压缩的文件,下面代码解压缩文件。
UnCompressStream(My);
ms.SaveToFile('c:/bb.exe');
ms.Free;
end;
 
接受答案了.
 
后退
顶部