rikyoz / bit7z

A C++ static library offering a clean and simple interface to the 7-zip shared libraries.
https://rikyoz.github.io/bit7z
Mozilla Public License 2.0
627 stars 114 forks source link

Auto detection of (input) archive file format #16

Closed rikyoz closed 5 years ago

rikyoz commented 6 years ago

How to determine the archive file format (i.e. detect whether a .rar file is RAR or RAR5)? Unfortunately using bit7z there is no direct method to detect the archive file format. I think that there are two alternative ways that can be used to overcome this: 1) You can read the file signature (e.g. as it can be seen here, RAR and RAR5 formats have slightly different file signatures) or 2) If you're using the latest version of bit7z (v3.0.0), you can "indirectly" use the new BitArchiveInfo class. In fact, since its constructor throws an exception if the specified file format is not the one of the input file, you could, for example, write a function like the following one:

/* it detects the RAR archive format version of the input file
   (or it throws a BitException if it is not a RAR archive at all) */
const BitInFormat& detectRAR( const wstring& in_file ) {
    Bit7zLibrary lib( L"7z.dll" );
    try {
        BitArchiveInfo info( lib, in_file, BitFormat::Rar );
        //if BitArchiveInfo constructor did not throw an exception, the archive is RAR (< 5.0)!
        return BitFormat::Rar;
    } catch ( const BitException& ) {
        /* the archive is not a RAR and if it is not even a RAR5, 
           the following line will throw an exception (not catched)! */
        BitArchiveInfo info( lib, in_file, BitFormat::Rar5 );
        return BitFormat::Rar5;
    }
}

I think that a more generic (i.e. not only RAR) archive format detection is possible using the same concept, even though it could be kind of tricky, since we cannot iterate over the many format constant objects (BitFormat::...). Anyway, I hope to implement a more easy to use format detection in the next stable version (v3.1), maybe using the first method or exploiting some (still unknown, at least to me) functionality of 7-zip.