[英]Append files to an existing zip file with Poco::Zip
成功壓縮文件夾后,這是我的情況:
如果append = true
和overWrite = false
我必須檢查目標 zip 文件是否存在如果存在我將檢查現有的 zip 文件它不包含哪些文件並將新文件從源文件夾附加到它。
我的問題是:
std::ifstream
但 Poco::zip::Compress 似乎沒有收到std::ifstream
我當然必須修改 Poco 源代碼本身以符合我的要求。 提前致謝。
void ZipFile(string source, string target, List extensions, bool append, bool overWrite)
{
Poco::File tempFile(source);
if (tempFile.exists())
{
if (Poco::File(target).exists() && append && !overWrite) {
fs::path targetPath = fs::path(target);
std::ifstream targetFileStream(targetPath.string(), std::ios::binary);
std::ofstream outStream(target, ios::binary);
CompressEx compress(outStream, false, false);
if (tempFile.isDirectory())
{
Poco::Path sourceDir(source);
sourceDir.makeDirectory();
compress.addRecursive(sourceDir, Poco::Zip::ZipCommon::CompressionMethod::CM_AUTO,
Poco::Zip::ZipCommon::CL_NORMAL, false);
}
else if (tempFile.isFile())
{
Poco::Path path(tempFile.path());
compress.addFile(path, path.getFileName(), Poco::Zip::ZipCommon::CompressionMethod::CM_AUTO,
Poco::Zip::ZipCommon::CL_NORMAL);
}
compress.close(); // MUST be done to finalize the Zip file
outStream.close();
}
}
無需修改 Poco 源代碼。 Poco 允許您獲取存檔的內容並向其中添加文件。
首先,打開目標存檔以檢查其中已經存在哪些文件:
Poco::ZipArchive archive(targetFileStream);
然后收集您要添加的所有文件,這些文件不在存檔中,但:
std::vector<fs::path> files;
if (fs::is_directory(source)) {
for(auto &entry : fs::recursive_directory_iterator())
// if entry is file and not in zip
if (fs::is_regular_file(entry)
&& archive.findHeader(fs::relative(entry.path, source)) == archive.headerEnd()) {
files.push_back(entry.path);
}
} else if (fs::is_regular_file(entry)
&& archive.findHeader(source) == archive.headerEnd()) {
files.push_back(source);
}
最后,將文件添加到您的 zip
Poco::Zip::ZipManipulator manipulator(target, false);
for(auto &file : files)
manipulator.addFile(fs::relative(file, source), file,
Poco::Zip::ZipCommon::CompressionMethod::CM_AUTO,
Poco::Zip::ZipCommon::CL_NORMAL);
我沒有機會對此進行測試。 所以嘗試一下,看看需要做些什么才能讓它發揮作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.