Delphi XE和ZLib问题(II)

时间:2014-02-25 08:02:28

标签: delphi delphi-xe zlib

我都是, 我记得你关于ZLib问题的问题....  Delphi XE and ZLib Problems

David Heffernan带着他出色的回答让我顺道(再次感谢@David)......

总结答案...... “压缩器的这种流程如下所示:字符串 - > UTF-8字节 - >压缩字节 - > base64字符串。 显然你反转箭头来解压缩。“

我不知道我必须在同一篇文章中发帖,否则我必须附上这样的新问题......

好吧,上周末我上班了......

我按照流程字符串 - > UTF-8字节 - >压缩字节 - > base64字符串

这是压缩功能,它可以工作......

function CompressStrToCode64Str(const aText: string; aCompressionLevel: TZCompressionLevel): string;
var
  strStreamIN,
  strStreamOUT: TStringStream;
begin
  result := '';
  /// Putting the string received to a Stream.
  strStreamIN := TStringStream.Create(aText, TEncoding.UTF8);
  try
    /// Creating the output stream for compression.
    strStreamOUT := TStringStream.Create('', TEncoding.UTF8);
    try
      /// Compressing streamIN to streamOUT
      ZCompressStream(strStreamIN, strStreamOUT, aCompressionLevel);
      /// Encoding to base64 for string handling.
      result := string(EncodeBase64(strStreamOUT, strStreamOUT.Size));
    finally
      strStreamOUT.Free;
    end;
  finally
    strStreamIN.Free;
  end;
end;

这是解压缩功能......但它不起作用......(返回空字符串)

function TForm1.Code64StrToUncompressStr(Const aText: string): string;
var
  strStreamIN,
  strStreamOUT: TStringStream;
  data: TBytes;
begin
  result := '';
  /// Creating the input stream.
  strStreamIN := TStringStream.Create('', TEncoding.UTF8);
  try
    /// Decoding base64 of received string to a TBytes
    data := DecodeBase64(ansistring(aText));
    /// Putting the TBytes to a Stream
    strStreamIN.Write(data[0], Length(data));
    /// Creating uncompressed stream
    strStreamOUT := TStringStream.Create('', TEncoding.UTF8);
    try
      /// Decompressing streamIN to StreamOUT
      ZDeCompressStream(strStreamIN, strStreamOUT);
      result := strStreamOUT.DataString;
    finally
      strStreamOUT.Free;
    end;
  finally
    strStreamIN.Free;
  end;
end;

一些想法为什么不能解压缩功能。它返回一个空字符串。 TIA耐心等待。

1 个答案:

答案 0 :(得分:2)

这就是我的想法:

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes, ZLib, EncdDecd;

function CompressAndEncodeString(const Str: string): string;
var
  Utf8Stream: TStringStream;
  Compressed: TMemoryStream;
  Base64Stream: TStringStream;
begin
  Utf8Stream := TStringStream.Create(Str, TEncoding.UTF8);
  try
    Compressed := TMemoryStream.Create;
    try
      ZCompressStream(Utf8Stream, Compressed);
      Compressed.Position := 0;
      Base64Stream := TStringStream.Create('', TEncoding.ASCII);
      try
        EncodeStream(Compressed, Base64Stream);
        Result := Base64Stream.DataString;
      finally
        Base64Stream.Free;
      end;
    finally
      Compressed.Free;
    end;
  finally
    Utf8Stream.Free;
  end;
end;

function DecodeAndDecompressString(const Str: string): string;
var
  Utf8Stream: TStringStream;
  Compressed: TMemoryStream;
  Base64Stream: TStringStream;
begin
  Base64Stream := TStringStream.Create(Str, TEncoding.ASCII);
  try
    Compressed := TMemoryStream.Create;
    try
      DecodeStream(Base64Stream, Compressed);
      Compressed.Position := 0;
      Utf8Stream := TStringStream.Create('', TEncoding.UTF8);
      try
        ZDecompressStream(Compressed, Utf8Stream);
        Result := Utf8Stream.DataString;
      finally
        Utf8Stream.Free;
      end;
    finally
      Compressed.Free;
    end;
  finally
    Base64Stream.Free;
  end;
end;

var
  EncodedAndCompressed: AnsiString;

begin
  EncodedAndCompressed := CompressAndEncodeString('ZLib, Utf-8, compression test');
  Writeln(EncodedAndCompressed);
  Writeln(DecodeAndDecompressString(EncodedAndCompressed));
  Readln;
end.

<强>输出

eJyL8slM0lEILUnTtdBRSM7PLShKLS7OzM9TKEktLgEAjjwKMA==
ZLib, Utf-8, compression test

正如您所看到的,当您包含base64时,它并不是真正的压缩。它可能是使用更大的输入字符串进行压缩。这就是为什么我认为你更好地传输二进制文件,正如我在上一个问题中所解释的那样。