Replies: 2 comments 9 replies
-
Hi! Anyway, the progress callback is a Usually, it is used together with the total callback to calculate, for example, the current progress percentage: std::uint64_t totalSize = 0;
writer.setTotalCallback([&]( std::uint64_t total ) {
totalSize = total;
});
writer.setProgressCallback([&total]( std::uint64_t progress ) -> bool {
const auto percentage = static_cast<int>(
100.0 * (static_cast<double>(progress) / static_cast<double>(total))
);
std::cout << "Compression progress: " << percentage << "%" << std::endl;
return true;
}); Please note that 7-Zip callbacks are blocking, so if you do some heavy operations in the body of the callback, it might slow down the compression (or extraction) operation. |
Beta Was this translation helpful? Give feedback.
-
im trying to make a wrapper for my c# applications all other callbacks works except for this progressCallback. both methods in the code reaching catch (const bit7z::BitException& ex) Why? //pch.h
// void(const char*)
typedef void (__stdcall *FileCallback)(const char* file);
// bool(uint64)
typedef bool (__stdcall *ProgressCallback)(uint64_t progress);
// void(uint64, uint64)
typedef void (__stdcall *RatioCallback)(uint64_t input, uint64_t output);
// void(uint64)
typedef void (__stdcall *TotalCallback)(uint64_t total);
#ifdef __cplusplus
extern "C" {
#endif
// Function to initialize the bit7z library
__declspec(dllexport) void Bit7zInitialize(const char* sevenZipDllPath);
// Extracts the archive at `archive_file` to `to_folder`.
__declspec(dllexport) int Bit7zExtract(
const char* src_file,
const char* dst_folder,
FileCallback file_callback,
ProgressCallback progress_callback,
RatioCallback ratio_callback,
TotalCallback total_callback);
// Function to release resources (important for cleanup)
__declspec(dllexport) void Bit7zShutdown();
#ifdef __cplusplus
}
#endif
//pch.cpp
namespace _bit7z
{
std::mutex g_mutex;
std::unique_ptr<bit7z::Bit7zLibrary> g_lib = nullptr;
std::unique_ptr<bit7z::BitFileExtractor> g_extractor = nullptr;
}
int Bit7zExtract(
const char* src_file,
const char* dst_folder,
FileCallback file_callback,
ProgressCallback progress_callback,
RatioCallback ratio_callback,
TotalCallback total_callback)
{
std::lock_guard<std::mutex> lock(_bit7z::g_mutex);
if (!_bit7z::g_lib)
{
std::cerr << "Error: bit7z library not initialized." << '\n';
return -4;
}
_bit7z::g_extractor = std::make_unique<bit7z::BitFileExtractor>(*_bit7z::g_lib, bit7z::BitFormat::Zip);
if (!_bit7z::g_extractor)
{
std::cerr << "Error: bit7z extractor not initialized." << '\n';
return -1;
}
_bit7z::g_extractor->setFileCallback([&](const bit7z::tstring &file) {
file_callback(file.c_str());
});
_bit7z::g_extractor->setRatioCallback([&](const uint64_t input, const uint64_t output) {
ratio_callback(input, output);
});
_bit7z::g_extractor->setTotalCallback([&](const uint64_t total) {
total_callback(total);
});
//method 1
//_bit7z::g_extractor->setProgressCallback(progress_callback);
//method 2
//_bit7z::g_extractor->setProgressCallback([&](const uint64_t progress) {
// return progress_callback(progress)
//});
try
{
_bit7z::g_extractor->setOverwriteMode(bit7z::OverwriteMode::Overwrite);
_bit7z::g_extractor->extract(src_file, dst_folder);
return 0; // Success
}
catch (const bit7z::BitException& ex)
{
std::cerr << "Error creating archive: " << ex.what() << " Code: " << ex.hresultCode() << '\n';
return -2;
}
catch (const std::exception& ex)
{
std::cerr << "Standard Exception: " << ex.what() << '\n';
return -3;
}
} solve by assign first to a local variable _bit7z::g_extractor->setProgressCallback([&](const uint64_t progress) -> bool
{
const auto result = progress;
return progress_callback(result);
}); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Is there an example on how to use the progress callback from BitArchiveWriter, BitArchiveEditor, and BitArchiveReader?
Beta Was this translation helpful? Give feedback.
All reactions