Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: zlib gz file format problem



Permlink Replies: 1 - Last Post: Sep 11, 2015 7:31 AM Last Post By: Lajos Juhasz
Ole Rössel

Posts: 3
Registered: 4/9/98
zlib gz file format problem
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 10, 2015 6:38 AM
I get problems with a gz-file which was created with the following algorithm:

var Buffer: array [1..20000] of ANSIchar;
nRead: integer;
CompressedFile: TFileStream;
UnCompressedFile: TFileStream;
Compressor: TZCompressionStream;
DeCompressedFile: TFileStream;
UnCompressedFileName, CompressedFileName: ANSIstring;

procedure TForm1.ConvertClick(Sender: TObject);
begin
UnCompressedFile:=TFileStream.Create(UnCompressedFileName,fmOpenRead);
CompressedFile:=TFileStream.Create(CompressedFileName,fmCreate);
Compressor:=TCompressionStream.Create(clMax,CompressedFile);

Form1.Memo2.Lines.Add(DateTimeToStr(Now));

repeat
nRead:=UnCompressedFile.Read(Buffer,SizeOf(Buffer));
Compressor.Write(Buffer,SizeOf(Buffer));
until nRead = 0;

FreeAndNil(UnCompressedFile);
FreeAndNil(DeCompressor);
FreeAndNil(CompressedFile);

Form1.Memo2.Lines.Add(DateTimeToStr(Now));
end;

I can not read it with the following code:

'g','G': begin // .etx.gz file
CompressedFile:=TFileStream.Create(InFileName,fmOpenRead);
DeCompressor:=TDecompressionStream.Create(CompressedFile,15+16);

repeat
nRead:=DeCompressor.Read(Buffer,SizeOf(Buffer));
StrBuffer:=StrBuffer+string(Buffer);
(* *)
StartIndex:=PosEx('<'>',StrBuffer,StartIndex);
while (StopIndex > 0) and (StartIndex < StopIndex) do
begin
Msg:=Trim(System.Copy(StrBuffer,StartIndex,StopIndex));
System.Delete(StrBuffer,1,StopIndex);
if IsValidMsgType(Msg)
then SourceList.Add(Msg);
StartIndex:=PosEx('<',StrBuffer);
StopIndex:=PosEx('>',StrBuffer,StartIndex);
end;

until nRead = 0;
FreeAndNil(CompressedFile);
FreeAndNil(DeCompressor);
end

I get Debugger exception:
Project xxx raised exceptionclass EZDecompressionError with message 'data error'

Files created with gzip etc are readable with this algorithm without errors.

I can not unpack it with WinZip, 7Zip etc.

WinZip says: Invalid archive directory.

Seems like the gz header is incorrect as byte 1 and 2 are: 78,DA

According to the spec it should be: 1F,8B

Is there a bug or do I use the methods incorrectly?

Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: zlib gz file format problem
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 11, 2015 7:31 AM   in response to: Ole Rössel in response to: Ole Rössel
Ole Rössel wrote:

Is there a bug or do I use the methods incorrectly?

You didn't gave enough source snippets to be 100% sure. However you are
using String and Ansistring to hold binary data (buffers). That's a bad
idea with D2009+ where string is unicodestring and ansi string is code
page aware. When you convert that data the compiler can change the
content.

Also you are using Ansistring to store file names instead of string.
That will not work correctly for files with unicode names.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02