字符串压缩与解压,请各位富翁帮忙? ( 积分: 80 )

  • 主题发起人 主题发起人 lieyan
  • 开始时间 开始时间
L

lieyan

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, Buttons;

type

TDataPacked = record
Name: array[0..49] of char;
age: WORD;
Remarks: array[0..255] of char;
end;

TForm1 = class(TForm)
BitBtn1: TBitBtn;
RichEdit1: TRichEdit;
BitBtn2: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;


implementation
uses zlib;
{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
var
dpMy: TDataPacked;
p: pointer;
nSize: integer;
s, sTmp: string;
buf: array[0..512] of char;
begin
FillChar(dpMy, sizeof(TDataPacked), 0);
StrPCopy(dpMy.Name, 'hly');
dpMy.age := 27;
StrPCopy(dpMy.Remarks, 'No married, Graduated form Shenyang Industry University.');

SetLength(sTmp, sizeof(buf));
CopyMemory(pchar(sTmp), @buf, Sizeof(buf));
Zlib.CompressBuf(pchar(sTmp), Length(sTmp), p, nSize);

SetLength(s, nSize);
Move(p^, s[1], nSize);
FreeMem(p, nSize);

RichEdit1.Text := s;
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
var
dpMy: TDataPacked;
p: pointer;
nSize: integer;
s: string;
begin
FillChar(dpMy, sizeof(TDataPacked), 0);
s := Trim(RichEdit1.Text);

Zlib.DecompressBuf(pchar(s), Length(s), 0, p, nSize);//<---EDecompressionError异常
if nSize > Sizeof(dpMy) then
Move(p^, dpMy, Sizeof(dpMy));
FreeMem(p, Sizeof(dpMy));
end;

end.
//以上通过CompressBuf可以对dpMy的记录进行压缩,但无法通过DecompressBuf进行解压缩,提示EDecompressionError异常。。。看了ZLIB源码,也查不出来,请各位帮忙...
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, Buttons;

type

TDataPacked = record
Name: array[0..49] of char;
age: WORD;
Remarks: array[0..255] of char;
end;

TForm1 = class(TForm)
BitBtn1: TBitBtn;
RichEdit1: TRichEdit;
BitBtn2: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;


implementation
uses zlib;
{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
var
dpMy: TDataPacked;
p: pointer;
nSize: integer;
s, sTmp: string;
buf: array[0..512] of char;
begin
FillChar(dpMy, sizeof(TDataPacked), 0);
StrPCopy(dpMy.Name, 'hly');
dpMy.age := 27;
StrPCopy(dpMy.Remarks, 'No married, Graduated form Shenyang Industry University.');

SetLength(sTmp, sizeof(buf));
CopyMemory(pchar(sTmp), @buf, Sizeof(buf));
Zlib.CompressBuf(pchar(sTmp), Length(sTmp), p, nSize);

SetLength(s, nSize);
Move(p^, s[1], nSize);
FreeMem(p, nSize);

RichEdit1.Text := s;
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
var
dpMy: TDataPacked;
p: pointer;
nSize: integer;
s: string;
begin
FillChar(dpMy, sizeof(TDataPacked), 0);
s := Trim(RichEdit1.Text);

Zlib.DecompressBuf(pchar(s), Length(s), 0, p, nSize);//<---EDecompressionError异常
if nSize > Sizeof(dpMy) then
Move(p^, dpMy, Sizeof(dpMy));
FreeMem(p, Sizeof(dpMy));
end;

end.
//以上通过CompressBuf可以对dpMy的记录进行压缩,但无法通过DecompressBuf进行解压缩,提示EDecompressionError异常。。。看了ZLIB源码,也查不出来,请各位帮忙...
 
已经搞定
 
后退
顶部