关于ZLIB对字符串压缩和解压缩的问题.(200)

  • 主题发起人 主题发起人 qn-wl
  • 开始时间 开始时间
Q

qn-wl

Unregistered / Unconfirmed
GUEST, unregistred user!
从网上找到这段代码.结果我对字符"123"压缩.但是解压缩后得出的不是123unit Zlib; interface uses Windows, Messages, SysUtils, Classes , Zlib; Function CompressStr(Const S : String) : String; Function DeCompressStr(Const S : String) : String; implementation Function CompressStr(Const S : String) : String; var CompressionStream : TCompressionStream; OutS , RetS : TMemoryStream; Count : Integer; begin Count := Length(S); OutS := TMemoryStream.Create; CompressionStream := TCompressionStream.Create(clDefault, OutS); Try Count := CompressionStream.Write(Pointer(S)^,Count); except Result := S; CompressionStream.Free; OutS.Free; Exit; end; CompressionStream.Free; OutS.Position := 0; RetS := TMemoryStream.Create; RetS.Write(Count , SizeOf(Count)); OutS.SaveToStream(RetS); OutS.Free; RetS.Position := 0; Count := RetS.Size; SetLength(Result , Count); ZeroMemory(@Result[1],Count); RetS.Read(Pointer(Result)^,Count); RetS.Free; end; Function DeCompressStr(Const S : String) : String; var nSize: integer; DecompressionStream: TDecompressionStream; InS : TMemoryStream; begin InS := TMemoryStream.Create; InS.Write(Pointer(S)^, Length(S)); InS.Position := 0; Ins.Read(nSize,SizeOf(nSize)); try DecompressionStream := TDecompressionStream.Create(InS); except end; try SetLength(Result, nSize); DecompressionStream.Read(Pointer(Result)^, nSize); except end; FreeAndNil(DecompressionStream); FreeAndNil(InS); end; end
 
你要用个字符串变量保存压缩后的结果,不要用文本框之类的控件去保存结果.var s:string;begin s:=CompressStr('123'); showmessage(DeCompressStr(s));end;
 
接受答案了.
 

Similar threads

I
回复
0
查看
498
import
I
I
回复
0
查看
646
import
I
I
回复
0
查看
588
import
I
I
回复
0
查看
582
import
I
I
回复
0
查看
689
import
I
后退
顶部