【发布时间】:2013-12-11 07:19:52
【问题描述】:
以下是我的函数,它接受压缩文件并通过一次读取 1024 个字符将其转换为 txt 文件。
procedure DecompressFile(const ACompressedFile, ADestinationFile : String);
var
SourceStream : TFileStream;
DestinationStream : TFileStream;
DecompressionStream : TDecompressionStream;
nRead : Integer;
Buffer: array [0..1023] of Char;
begin
SourceStream := TFileStream.Create(ACompressedFile, fmOpenRead);
try
DestinationStream := TFileStream.Create(ADestinationFile, fmCreate);
try
DecompressionStream := TDecompressionStream.Create(SourceStream);
try
repeat
nRead := DecompressionStream.Read(Buffer, 1024);
DestinationStream.Write(Buffer, nRead);
until nRead = 0;
finally
DecompressionStream.Free;
end;
finally
DestinationStream.Free;
end;
finally
SourceStream.Free;
end;
end;
我的问题是这会在 Delphi 7 的情况下生成正确的 txt 文件,但在 Delphi XE4 的情况下,它会在每个字符之间引入垃圾值。
例子:
Delphi 7: abcdedfgh
Delphi XE4: aNULbNULcNULdNULeNULfNULgNULhNUL
NUL 被插入到每个字符之间。我尝试更改声明
Buffer: array [0..1023] of Char;
到Buffer: array [0..1023] of AnsiChar;,但这不起作用。
【问题讨论】:
-
缓冲区最好使用Byte数组而不是Char,但这里没有问题。对于 XE4 文本看起来像 unicode 编码(UTF16?),对于拉丁符号,它总是对 - 0 + ascii 代码。你确定你在输入上有相同的 TXT 文件(也许你从代码创建测试文件)并且输出没有任何其他转换(比如将数据读入字符串或其他东西)?
-
在我看来,您好像直接从您移植的巨大应用程序中发布了代码。但是,如果您发布了 SSCCE,那就容易多了。如果你这样做了,你肯定会自己解决问题。你读过 Marco 的论文了吗?
-
@DavidHeffernan - 我正在并排阅读。
-
Delphi XE4 默认使用 Unicode 字符串。您的输出文本文件可能根本不是垃圾,您只需将它们作为 Utf-16 编码的文本文件打开。
标签: delphi delphi-7 delphi-xe4