'Poco addRecursive Not Returning to Normal Program Flow
I implemented Poco in my C++ application for Windows in VS 2019 using tips from this post: How to use Poco::ZIP to compress/decompress zip file
I have figured out how to recursively compress a folder with this code snippet:
// Now that we have an output file name, compress
std::ofstream fos(z_file, std::ios::binary);
Poco::Zip::Compress c(fos, true);
Poco::File aFile(file_path);
if (aFile.exists()) {
Poco::Path p(aFile.path());
if (aFile.isDirectory()) {
c.addRecursive(p, Poco::Zip::ZipCommon::CompressionMethod::CM_AUTO,
Poco::Zip::ZipCommon::CL_MAXIMUM, true);
}
}
else {
std::cout << "File not found... \r\n";
}
// Just in case these don't get called above.
c.close(); // MUST be done to finalize the Zip file
fos.close();
return z_file;
}
My question is that once the code gets to,
c.addRecursive(p, Poco::Zip::ZipCommon::CompressionMethod::CM_AUTO,
Poco::Zip::ZipCommon::CL_MAXIMUM, true);
the program continues to run normally, and does compress the folder just fine. However, the program does not return to normal execution flow. Is it going off on a thread and not coming back? I have researched that I can get an update about files being added to the archive through
Poco::FIFOEvent<const ZipLocalFileHeader> EDone;
It also says that closing the file is essential to creating the archive, but seems that with addRecursive my close() never gets called.
Any help would be appreciated.
Regards
Solution 1:[1]
Still getting back to C++ after a while off, but it was the way that I was calling the function where this code resides.
In the code that calls this, I was using:
std::string send_file { fh.compress_folder(home_dir) };
When I changed it to:
std::string send_file = fh.compress_folder(home_dir);
Then added a try/catch to the above code as:
std::ofstream fos(z_file, std::ios::binary);
Poco::Zip::Compress c(fos, true);
Poco::File aFile(file_path);
if (aFile.exists()) {
Poco::Path p(aFile.path());
if (aFile.isDirectory()) {
try {
c.addRecursive(p, Poco::Zip::ZipCommon::CompressionMethod::CM_AUTO,
Poco::Zip::ZipCommon::CL_MAXIMUM, true);
} catch(const Poco::Exception e) {
std::cout << e.what() << "/r/n";
}
}
}
else {
std::cout << "File not found... \r\n";
}
// Just in case these don't get called above.
c.close(); // MUST be done to finalize the Zip file
fos.close();
return z_file;
Now, the program properly returns and does not travel off into lala land any more.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | SysRisk |