Fórum Zlib - Comprimindo Strings #297850
30/09/2005
0
Eu adaptei um código do Zlib de comprimir arquivos para comprimir strings.. Não sei se fiz certo, por que a maioria das strings comprimidas tem mais caracteres que a string original, o que há de errado?
//Comprimir dados function Tform1.C(texto : string):string; var temp : TStringStream; temp2 : TStringStream; Zip: TCompressionStream; t : string; begin temp := TStringStream.Create(texto); temp2 := TStringStream.Create(´´); Zip:=TCompressionStream.Create(clMax, temp2); Zip.CopyFrom(temp, temp.Size); Zip.Free; temp.Free; t := temp2.DataString; temp2.Free; Result :=t; end; //Descomprimir dados function Tform1.D(texto:string):string; var temp : TStringStream; temp2 : TStringStream; DeZip: TDecompressionStream; i: Integer; Buf: array[0..1023]of Byte; t : string; begin temp := TStringStream.Create(texto); temp2 := TStringStream.Create(´´); DeZip:=TDecompressionStream.Create(temp); repeat i:=DeZip.Read(Buf, SizeOf(Buf)); if i <> 0 then temp2.Write(Buf, i); until i <= 0; DeZip.Free; temp.Free; t := temp2.DataString; temp2.Free; Result := t; end;
Amon-ra
Curtir tópico
+ 0Posts
30/09/2005
Massuda
Gostei + 0
30/09/2005
Amon-ra
Em todo o caso.. estou enviando 1 caracter a mais.. não sei se é bom...
Alguém têm um jeito menos burro de fazer isso? hehehe
//Comprimir dados function Tlincenet.C(texto : string):string; var temp : TStringStream; temp2 : TStringStream; Zip: TCompressionStream; t : string; begin temp := TStringStream.Create(texto); temp2 := TStringStream.Create(´´); Zip:=TCompressionStream.Create(clMax, temp2); Zip.CopyFrom(temp, temp.Size); Zip.Free; temp.Free; t := temp2.DataString; temp2.Free; if Length(t)>Length(texto)then begin Result :=´!´+texto; end else begin Result :=´$´+t; end; end; //Descomprimir dados function Tlincenet.D(texto:string):string; var temp : TStringStream; temp2 : TStringStream; DeZip: TDecompressionStream; i: Integer; Buf: array[0..1023]of Byte; t : string; a : string; begin a := texto; if Copy(a,0,1) = ´!´ then begin Result := Copy(a,2,Length(a)-1); end else begin a := Copy(a,2,Length(a)-1); // temp := TStringStream.Create(a); temp2 := TStringStream.Create(´´); DeZip:=TDecompressionStream.Create(temp); i:=DeZip.Read(Buf, SizeOf(Buf)); repeat if i <> 0 then temp2.Write(Buf, i); until i <= 0; DeZip.Free; temp.Free; t := temp2.DataString; temp2.Free; Result := t; end; // end;
Gostei + 0
08/02/2006
Joananjr
Ex: se vc pede pra ele compactar jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
Quando pede pra desconpactar ele traz jj
Coloquei tb o conteudi de um txt de 2k e na descompactacao so me retornou a primeira linha do texto.
Alguem sabe como consertar o código pra ele funcionar corretamente?
Acho que tem algo a ver com essa variavem Buf ...
Gostei + 0
26/12/2019
Alexandre
Ex: se vc pede pra ele compactar jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
Quando pede pra desconpactar ele traz jj
Coloquei tb o conteudi de um txt de 2k e na descompactacao so me retornou a primeira linha do texto.
Alguem sabe como consertar o código pra ele funcionar corretamente?
Acho que tem algo a ver com essa variavem Buf ...
Joananjr:
Segue as funções para compactar e descompactar textos longos, com ou sem imagens:
function CompressString(Source: WideString): WideString;
var
ms: TMemoryStream;
stSource : TStringStream;
CompStream: TCompressionStream;
begin
Result := EmptyStr;
if (Source <> EmptyStr) then
begin
ms := tMemoryStream.Create;
stSource := TStringStream.Create(Source);
CompStream := TCompressionStream.Create (clMax, ms);
CompStream.CopyFrom(stSource, Length(Source));
CompStream.Free;
ms.position := 0;
SetLength(Result,Ms.size );
ms.read ( Result[1],ms.size);
ms.Free;
stSource.Free;
CompStream := nil;
ms := nil;
end
else
begin
Result := Source;
end;
end;
function DeCompressString (Source: WideString): WideString;
var
ms: tMemoryStream;
Decomp: TDecompressionStream;
C: Char;
begin
Result := EmptyStr;
if (Source <> EmptyStr) and (Pos('{\\rtf',Source) = 0) and (Source <> #0) then
begin
ms := tMemoryStream.Create;
ms.Write ( Source[1],Length(Source));
ms.position := 0;
DeComp := TDecompressionStream.Create (ms);
try
try
While deComp.Read ( c ,1 )<> 0 DO
begin
Result := Result + c;
end;
except
result := Source;
end;
finally
decomp.free;
ms.free;
Decomp := nil;
ms := nil;
end;
end
else Result := Source;
end;
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)