gpt4 book ai didi

delphi - Delphi 7和Delphi XE4中的解压缩流

转载 作者:行者123 更新时间:2023-12-03 18:27:16 26 4
gpt4 key购买 nike

以下是我的函数,该函数接受压缩文件,并通过一次读取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;,但这无效。

最佳答案

首先,让我们假设解压缩流类(无论它是什么)均已正确实现。在这种情况下,问题中的代码实际上很好。它成功解压缩了文件。虽然,这有点草率,因为它分配的缓冲区是您使用的缓冲区的两倍。缓冲区应该是字节数组,而不是char。使用SizeOf(Buffer)而不是重复该魔术1024常量。而Write调用最好是WriteBuffer来添加错误检查。

这两个输出之间的区别只是一个以8位编码进行编码,而另一个以16位编码进行编码,可能是UTF-16。

很难说这是否是故意的。人们将需要研究创建压缩文件的过程。例如,也许压缩文件是通过压缩Delphi字符串创建的。在D7中,该字符串是8位编码的,但是在DXE4中,它是16位编码的。

一个明显的步骤是比较两个输入文件,即D7与DXE4文件。您希望它们是相同的。但是,是吗?



然后,另一个可能的原因是您的解压缩流类已损坏。它看起来像ZLib类,众所周知。

关于delphi - Delphi 7和Delphi XE4中的解压缩流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20512880/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com