bit7z: Auto-detect archive format fails if the file extension does not match the actual file format

I have a real use case where my legacy application produces an archive with 7z format but gives it a .zip extension!
Sorry, but this is beyond my control 😦

Observations:

  1. If I set the archive format as bit7z::BitFormat::Auto, then the BitArchiveReader is not able to read items from this file.
    auto entries = reader.items(); fails.
  2. If the file’s extension is changed to something unknown e.g. .xyz, then it works!

It appears that the Reader tries to find the format from the file extension (.zip) and then gets confused along the way!
ā“ Would it be possible for the Reader to solely use the actual signature of the file format instead of the extension?

Here is a sample archive file with actual format of 7z, but extension .zip:
TestMismatchOfFileExtensionAndActualFormat.7z.zip

In some cases it was observed that some such files (actual format = 7z, and extension = .zip) actually get opened, but wrong entries are returned. This could be similar to the issue: https://github.com/rikyoz/bit7z/issues/235

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (9 by maintainers)

Most upvoted comments

Support added in v4.0.9.

Hi! The problem is always the same as in https://github.com/rikyoz/bit7z/issues/235: 7-Zip tries to find the beginning of a zip archive inside the 7z file, and it manages to find it, which is why it doesn’t throw immediately.

image

In the picture, I have highlighted the signature of the 7z archive in red, and that of the zip archive in blue. Since bit7z recognised the zip format from the file extension, it tries to open it using such format. Then, 7-Zip searches for the archive start and detects the signature highlighted in blue, confirming that it has found a zip archive, and opening it successfully (for some reason).

As for why the extraction fails and some of the extracted files are corrupted, my hunch is that it fails because the parent 7z file is compressed (unlike that other tar archive): basically, the zip file is compressed twice (once zip and then 7z), but 7-Zip doesn’t know this and just tries to extract it using the zip format.

As I said in the other issue, this is not a problem with format detection by extension per se, but rather the default behaviour of 7-Zip. This can be turned off, and I’m currently working on an API that will do just that. The API will probably look something like this:

enum struct ArchiveStart : std::uint8_t {
    ScanFile, ///< Search the whole input file for the archive's start (if the file format supports it).
    FileStart ///< Check only the file start for the archive's start.
};
//...
// Throwing an error if the file is not a zip archive from the start.
BitArchiveReader( lib, "path-to-archive.zip", ArchiveStart::FileStart, BitFormat::Zip );

To keep the backward compatibility with older versions of bit7z, the default behavior will still be ArchiveStart::ScanFile:

// Searching for the archive's start by seeking through the input file.
BitArchiveReader( lib, "path-to-archive.zip", BitFormat::Zip );

I’ll keep this issue opened, and close it once this feature will ship. After testing it, I plan to release it in a next patch version v4.0.9.