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