#1 2013-01-09 11:54:54

Avemey
Member
Registered: 2013-01-09
Posts: 4
Website

TZipReader Unzip zip with empty folder trouble

Hi!

I have 1.zip file with some files and 1 empty (!) folder:
_1/1  (it is empty folder)
_1/file1.txt
_1/file2.txt
...
Archive is valid (created with PeaZip and successfully unpacked with 7z and PeaZip).
When i trying to unzip this zip, i take error -3. Undef USEINLINEASM not help (code=-3 gets in function inflate as i understood).
When zip archive created without empty folders (created with 7z, PeaZip or TZipWriter) there are no trouble to unpack.
In experiments i see, that this trouble is coming when empty folder in zip.

What i do wrong?
(Delphi XE, Win7 64bit)

//uses SynZip, SynZipFiles;
//  ZEZipUnpackSyn('c:\1.zip', 'c:\temp\1\'); //c:\temp\1\ is exist
function ZEZipUnpackSyn(ZipName: string; PathName: string): boolean;
var
  zr: TZipReader;
  i: integer;
  stream: TFileStream;
  s: string;
  t: integer;
  _fname: string;

begin
  result := false;
  if (not FileExists(ZipName)) then
    exit;

  t := Length(PathName);
  if (t > 0) then
    if (PathName[t] <> PathDelim) then
      PathName := PathName + PathDelim;

  if (not DirectoryExists(PathName)) then
    exit;

  zr := nil;
  try
    zr := TZipReader.Create(ZipName);
    for i := 0 to zr.Count - 1 do
    begin
      stream := nil;
      _fname := zr.Entry[i].ZipName;

      s := ExtractFilePath(_fname);

      if (length(s) > 0) then
        ForceDirectories(PathName + s);

      s := ExtractFileName(_fname);
      if (length(s) > 0) then
      try
        stream := TFileStream.Create(PathName + _fname, fmCreate);
        zr.GetData(i, stream);
      finally
        if (Assigned(stream)) then
          FreeAndNil(stream);
      end;
    end;

    result := true

  finally
    if (Assigned(zr)) then
      FreeAndNil(zr);
  end;
end; //ZEZipUnpackSyn

Offline

#2 2013-01-09 14:48:55

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: TZipReader Unzip zip with empty folder trouble

Could be an issue in our class, in fact.
It does not handle "empty folders".
It handle folders, as part of file names.

You can create a ticket at http://synopse.info/fossil

Offline

#3 2013-01-10 14:21:45

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: TZipReader Unzip zip with empty folder trouble

I've committed a potential fix.

Please try it.

You have to check Header.IsFolder to find out if the corresponding entry is a folder.

Offline

#4 2013-01-10 18:50:08

Avemey
Member
Registered: 2013-01-09
Posts: 4
Website

Re: TZipReader Unzip zip with empty folder trouble

Unfortunately this fix not work: zip file with empty folder unpack, but unpacked files are corrupted and zip without empty folder take error -3.

I make small test console application tstsynunpack.zip (~4Kb) with 3 zip files:
1.zip - have empty folder "1" and file "somefile.txt"
2.zip - have empty folder "SOME_DIR" and file "1.txt"
3.zip - no empty files, just 2 files: "1.txt" and "SOME_DIR/somefile.txt"

1.zip and 2.zip unpackeds, but unpacked files are corrupted. 3.zip take error -3.

Offline

#5 2013-01-10 20:03:40

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: TZipReader Unzip zip with empty folder trouble

The supplied file has a "extra field length" entry in its header not equal to zero, BUT it should be zero, according to the layout.
See http://www.pkware.com/documents/casestudies/APPNOTE.TXT and "local file header" definition.

If I recompress the file using TotalCommander, for instance, this TFileHeader.extraLen=0, and the decompression works as expected!
Are you sure PeaZip do not have a problem of file layout?

Offline

#6 2013-01-11 07:09:28

Avemey
Member
Registered: 2013-01-09
Posts: 4
Website

Re: TZipReader Unzip zip with empty folder trouble

It is strange. We use far manager + pkzip 4.0 (old, but work) and PeaZip 4.3 (for now migrate from pkzip to pea) and we have not troubles with zips (as i remember).
I repeat unpack experement tstsynunpack2.zip:
1.zip, 2.zip and 3.zip created with pkzip 4.0.
1pea.zip, 2pea.zip and 3pea.zip - with peazip.
1total.zip 2total.zip and 3total.zip - with TotalCommander.
Results:
1. TZipReader TotalCommander zips unpack successed!
2. TZipReader pkzip and peazip zips: files are corrupted.
3. TZipReader unpacked corrupted files from pkzip and peazip not equal.
4. pkzip successed unpack pea and totalcommander zips.
5. peazip successed unpack pkzip and totalcommander zips.
6. TotalCommander successed unpack pea and pkzip zips.

I am confused. It is seen like PeaZip and PkZip are similar and TotalCommander not similar.

Offline

#7 2013-01-11 08:08:09

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: TZipReader Unzip zip with empty folder trouble

I suspect our TZipRead class use the "extra len" field value from the directory entries, whereas it should use the value in the local header instead.

Most zippers have both fields equal 0, but your 1.zip file does not.

I'll check and fix that.

Offline

#8 2013-01-11 09:57:11

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: TZipReader Unzip zip with empty folder trouble

I've changed the code to use the local file header "extra len" field to position the reading cursor.
Some code refactoring was needed (some new methods added).
Seems to fix the issue.
See http://synopse.info/fossil/info/bb09abeb24

Thanks a lot for your feedback.

Offline

#9 2013-01-11 18:26:40

Avemey
Member
Registered: 2013-01-09
Posts: 4
Website

Re: TZipReader Unzip zip with empty folder trouble

Good news:
1. Previous zips unpacks successful!
2. Tested big (3Gb logs packed into ~200mb) zip unpacked successful!

But when i trying unpack chart1.ods (it is zip) created with LibreOffice calc 3.4.3 then got infinite loop: tstsynunpack3.zip (~24Kb).
Get infinite loop on 2 entry:
  Entry=2 ("ObjectReplacements\Object 1"):
    GetData (SynZipFiles.pas line 550) =>
       UnCompressStream (SynZip.pas line 4511) =>
          in repeat loop get code = Z_BUF_ERROR
May be it is happens because first entries have another compression?
Entry=0 ("mimetype") without compression
Entry=1 ("meta.xml") without compression

When this document uncompress by TotalCommander and repack then unpacks fine.

Offline

Board footer

Powered by FluxBB