字符串压缩问题~~(72分)

  • 主题发起人 主题发起人 Mclkings
  • 开始时间 开始时间
M

Mclkings

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何进行字符串的压缩~~

我不要对文件的压缩,我不想先建立文件,然后再进行相关的文件流进行操作

我想直接用内存操作数据,进行数据的压缩,是否有运行通过的实例代码??

谢~~~
 
可以用流来压缩的。有很多现成的控件可以用比如VCLZip等。
 
我也想要字符串压缩算法,最后有Delphi和java的[:D]
 
//压缩字符串

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

function zip(instr:string;var outstr:string):boolean;
var
CS:TCompressionStream;
buffer:pchar;
MemoStream:TmemoryStream;
strStream:TstringStream;
begin
MemoStream:=TMemoryStream.Create;
strStream:=Tstringstream.Create('');
try
buffer:=pchar(instr);
Cs:=TCompressionStream.Create(clFastest,MemoStream);
try
Cs.Write(buffer^,length(instr));
finally
Cs.Free;
end;
MemoStream.Position :=0;
MemoStream.SaveToStream(strStream);
outstr:=strStream.DataString;
result:=true;
finally
strstream.free;
MemoStream.Free;
end;
end;


function unzip(instr:string;var outstr:string):boolean;
var
c:integer;
DS:TDecompressionStream;
buffer:pchar;
outbuffer:array [1..512] of byte;
MemoStream:TmemoryStream;
strStream:TstringStream;
begin
MemoStream:=TMemoryStream.Create;
strStream:=Tstringstream.Create('');
DS:=TDecompressionStream.Create(MemoStream);
try
try
buffer:=pchar(instr);
MemoStream.Write(buffer^,length(instr));
MemoStream.Position :=0;
repeat
c:=DS.Read(outbuffer,512);
if c>0 then strStream.Write(outbuffer,c)
until c=0;
finally
ds.Free;
end;
outstr:=strStream.DataString;
result:=true;
finally
strstream.free;
MemoStream.Free;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
s,so:string;
begin
if zip(memo1.Text,s) then
if unzip(s,so) then
memo2.Lines.Text :=so;

end;

end.
 
好像没有压缩吧,更长了一点
 
多人接受答案了。
 
后退
顶部