unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Zlib, ComCtrls, DbiTypes, DbiProcs, DbiErrs;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
RichEdit1: TRichEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure CompressStream(inpStream, outStream: TStream);
var
InpBuf, OutBuf: Pointer;
InpBytes, OutBytes: Integer;
begin
InpBuf := nil;
OutBuf := nil;
try
GetMem(InpBuf, inpStream.Size);
inpStream.Position := 0;
InpBytes := inpStream.Read(InpBuf^, inpStream.Size);
CompressBuf(InpBuf, InpBytes, OutBuf, OutBytes);
outStream.Write(OutBuf^, OutBytes);
finally
if InpBuf <> nil then FreeMem(InpBuf);
if OutBuf <> nil then FreeMem(OutBuf);
end;
end;
procedure DecompressStream(inpStream, outStream: TStream);
var
InpBuf, OutBuf: Pointer;
OutBytes, sz: Integer;
begin
InpBuf := nil;
OutBuf := nil;
sz := inpStream.Size - inpStream.Position;
if sz > 0 then
try
GetMem(InpBuf, sz);
inpStream.Read(InpBuf^, sz);
DecompressBuf(InpBuf, sz, 0, OutBuf, OutBytes);
outStream.Write(OutBuf^, OutBytes);
finally
if InpBuf <> nil then FreeMem(InpBuf);
if OutBuf <> nil then FreeMem(OutBuf);
end;
outStream.Position := 0;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ms1, ms2: TMemoryStream;
begin
ms1 := TMemoryStream.Create;
try
ms2 := TMemoryStream.Create;
try
RichEdit1.Lines.SaveToFile('C:\zzz\original.dat'); // ¿øº»
RichEdit1.Lines.SaveToStream(ms1);
CompressStream(ms1, ms2);
ShowMessage(Format('¾ÐÃâÀ²: %d %%', [round(100 / ms1.Size * ms2.Size)]));
ms2.SaveToFile('C:\zzz\compressed.dat');
RichEdit1.Clear;
finally
ms1.Free;
end;
finally
ms2.Free;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
ms1, ms2: TMemoryStream;
begin
ms1 := TMemoryStream.Create;
try
ms2 := TMemoryStream.Create;
try
ms1.LoadFromFile('C:\zzz\compressed.dat');
DecompressStream(ms1, ms2);
RichEdit1.Lines.LoadFromStream(ms2);
finally
ms1.Free;
end;
finally
ms2.Free;
end;
end;
// ¾Æ·¡´Â ¿¹Á¦ ¹®ÀÚ¿À» ¸¸µé±â À©ÇÑ °ÍÀÔ´Ï´Ù
procedure TForm1.FormCreate(Sender: TObject);
var
Category: byte;
Code: byte;
ResultCode: word;
ErrorString: array[0..DBIMAXMSGLEN + 1] of char;
OutString: string;
begin
// BDE ȯ°æÀ» ÃʱâÈ ÇÑ´Ù
DbiInit(nil);
RichEdit1.Lines.Clear;
for Category := ERRCAT_NONE to ERRCAT_RC do
for Code := 0 to 255 do
begin
ResultCode := (Category shl 8) + Code;
// error code ¿¡ ÇØ´çÇÏ´Â ¸Þ½ÃÁö¸¦ ±¸ÇÑ´Ù
DbiGetErrorString(ResultCode, ErrorString);
if StrLen(ErrorString) > 0 then // ÁÖ¾îÁø ¿¡·¯ÄÚµåÀÇ ¼³¸íÀÌ Àִ°͸¸ ¹ßÃë
begin
OutString := Format('%6d %0.4x %s', [ResultCode, ResultCode, ErrorString]);
RichEdit1.Lines.Add(OutString);
end;
end;
// BDE ·Î ºÎÅÍ Á¢¼ÓÀ» ÇØÁ¦ÇÏ¿© »ç¿ëÇÑ ÀÚ¿øÀ» ¹Ý³³ÇÑ´Ù
DbiExit;
end;
end. |
|