gpt4 book ai didi

c++ - 使用Poco::Zip将文件附加到现有的zip文件中

转载 作者:行者123 更新时间:2023-12-02 10:06:59 25 4
gpt4 key购买 nike

成功压缩文件夹后,这是我的情况:

如果是append = trueoverWrite = false,我必须检查目标zip文件是否存在,如果存在,我将检查不存在的zip文件,并将源文件夹中的新文件追加到该文件中。

我的问题是:

  • 如何打开zip文件并将其放入compress对象?还是应该使用Poco中的其他哪个库打开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允许您get the contents of an archiveadd files to it

    首先,打开目标存档以检查其中已经存在哪些文件:

    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);

    我没有机会对此进行测试。因此,尝试一下,看看需要做些什么才能使其工作。

    关于c++ - 使用Poco::Zip将文件附加到现有的zip文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59762384/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com