Home > other >  How to uncompress a file originally compressed in a C# application?
How to uncompress a file originally compressed in a C# application?

Time:06-06

I need to uncompress files that were originally compressed with a C# app, using the SharpZipLib library. I'm using the Delphi zlib implementation, but I must be missing something, because when decompressing something that wasn't originally compressed by myself using zlib I get:

Data Error

I'm using:

procedure TfrmDownload.DescompactaArquivo(aNomeArquivo: string);
var
  _ArqCompact,
  _ArqDescompact: TFileStream;
  _Unzip: TDecompressionStream;

  function _GetNomeArquivoDesc: string;
  begin
    result := StringReplace(aNomeArquivo, '.zip', '', [rfIgnoreCase]);
  end;
begin
  if ((FileExists(aNomeArquivo)) and (Pos('.zip', aNomeArquivo) > 0)) then
  begin
    _ArqCompact := TFileStream.Create(aNomeArquivo, fmOpenRead);
    try
      _ArqDescompact := TFileStream.Create(_GetNomeArquivoDesc, fmCreate);
      try
        _Unzip := TDecompressionStream.Create(_ArqCompact, 31);
        try
          _ArqDescompact.CopyFrom(_Unzip, 0);
        finally
          FreeAndNil(_Unzip);
        end;
      finally
        FreeAndNil(_ArqDescompact);
      end;
      DeleteFile(aNomeArquivo);
    finally
      FreeAndNil(_ArqCompact);
    end;
  end;
end;

Following AmigoJack's tip, I made an attempt to decompress using System.Zip:

procedure TForm11.DecompactaZip(aNomeArquivo: string);
var
  _Zip: TZipFile;
begin
  _Zip := TZipFile.Create;
  try
    _Zip.Open(aNomeArquivo, zmRead);
    _Zip.ExtractAll(ExtractFilePath(aNomeArquivo));
    _Zip.Close;
  finally
    _Zip.Free;
  end;
end;

...but I get:

stream read error

The file's content:

Hex view of file

CodePudding user response:

The screenshot ends too early but looking at the "local file header"

  • a filesize of $ffffffff and
  • an extraction version of $2d = decimal 45 = PK version 4.5

indicate we're having a Zip64 file, which is yet another thing: not directly a ZIP file, not zlib data. Although it exists since 2001 by far not every implementation is able to properly read/support it.

This addition looks like it adds Zip64 support, so try that. As a counter test set in your C# app the UseZip64 property to false and create a new file - that should then be able to opened and extracted by Delphi's default implementation. As per formats you might want your Zip64 file to have a filename extension of .zipx and the normal ZIP file to have a filename extension of .zip.

If you come to the point where you're able to parse the ZIP/Zip64 file yourself, so you're able to spot each compressed file, and verify that the method is Deflate, you may use zlib on that compressed data stream to see if that single extracted file then has the same content as if 7-zip has unpacked it.

  • Related