Following is my function which accepts a compressed file and converts it to a txt file by reading 1024 characters at a time.
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;
My problem is that this produces correct txt file in case of Delphi 7 but in case of Delphi XE4, it introduces garbage values between each and every character.
Example:
Delphi 7: abcdedfgh
Delphi XE4: aNULbNULcNULdNULeNULfNULgNULhNUL
NUL is inserted in between every character. I tried changing declaration
Buffer: array [0..1023] of Char;
to Buffer: array [0..1023] of AnsiChar;
but this did not work.
First let us assume that the decompression stream class, whatever it is, is implemented correctly. In which case, the code in the question is actually fine. It successfully decompresses the file. Although, it's a little sloppy because it allocates a buffer twice as large as you use. The buffer should be an array of byte rather than char. Use SizeOf(Buffer) rather than repeating that magic 1024 constant. And the Write call would better be WriteBuffer to add error checking.
The difference between the two outputs is simply that one is encoded with an 8 bit encoding, and the other is encoded with a 16 bit encoding, probably UTF-16.
Whether or not this is intentional is hard to say. One would need to look at the process that created the compressed file. For instance, perhaps the compressed file is created by compressing a Delphi string. In D7 that string is 8 bit encoded, but in DXE4 it is 16 bit encoded.
An obvious step is to compare the two input files, the D7 vs the DXE4 file. You expect them to be identical. But are they?
The other possible cause then is that your decompression stream class is broken. It looks like the ZLib class which is known to be good.