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
623 stars 113 forks source link

Total file size wrong when updating an archive #38

Closed fabri9532 closed 4 years ago

fabri9532 commented 4 years ago

When I add a file or directory to an existing archive, the total size reported by the CallBack "TotalCallback" is wrong.

See the following code. When I add the file "b.txt", the total size has an incorrect value (not equal to size of "b.txt"). The problem also occurs when I use "compressDirectory".

//Code ----------

include "bitcompressor.hpp"

include "bitexception.hpp"

include "bitformat.hpp"

void TotalCallbackProc(uint64_t total_size); void ProgressCallbackProc(uint64_t progress_size);

using namespace bit7z;

int main() { try { Bit7zLibrary lib{ L"7z.dll" };

    BitCompressor compressor{ lib, BitFormat::SevenZip };
    compressor.setCompressionMethod(BitCompressionMethod::Lzma2);
    compressor.setCompressionLevel(BitCompressionLevel::NORMAL);

    compressor.setTotalCallback(TotalCallbackProc);
    compressor.setProgressCallback(ProgressCallbackProc);

    compressor.compressFile(L"C:\\a.txt", L"Output_archive.7z");
    compressor.setUpdateMode(true);
    compressor.compressFile(L"C:\\b.txt", L"Output_archive.7z");
}
catch (const BitException& ex) {
    //do something with ex.what()...
}

}

void TotalCallbackProc(uint64_t total_size) { //When compress "b.txt" the parameter total_size has a wrong value. }

void ProgressCallbackProc(uint64_t progress_size) { } // End Code ----------

rikyoz commented 4 years ago

Hi! First of all, thank you for using bit7z!

When I add a file or directory to an existing archive, the total size reported by the CallBack "TotalCallback" is wrong.

See the following code. When I add the file "b.txt", the total size has an incorrect value (not equal to size of "b.txt"). The problem also occurs when I use "compressDirectory".

Actually, in this specific case the total size reported by TotalCallback is correct, since it must not be equal to the size of b.txt. Such "size", in fact, refers to the total number of bytes going to be processed by the compression operation. Now, since you are updating an existing archive, the total size also includes the compressed size of this latter (in your case, of a.txt). As a further explanation, if you add a third file c.txt, the total size passed to the callback will be: compressed size of a.txt and b.txt + size of c.txt. Note: the compressed size of the archive does not consider its metadata, hence generally compressed size != archive size (more precisely, <=).