【问题标题】:How to use Poco::ZIP to compress/decompress zip file如何使用 Poco::ZIP 压缩/解压缩 zip 文件
【发布时间】:2019-12-27 11:02:34
【问题描述】:

我是 C++ 新手,尤其是在压缩/解压缩方面。我目前的项目正在使用 Poco 库,并且 Zip 扩展用于将文件/目录压缩或解压缩为 Zip 文件。

#include <Poco/FileStream.h>
#include <Poco/Zip/Decompress.h>
#include <Poco/Zip/Compress.h>
#include <Poco/Timestamp.h>
#include <Poco/File.h>
voidZipFile(string source, string target, List extensions, bool append, bool overWrite)
{
    Poco::DateTime(Timestamp);
    set <string> extensionsSet;
    std::ofstream fos(target, ios::binary);
    Poco::Zip::Compress c(fos, true);

    for (int i = 0; i < extensions.Size(); i++) {
        string ext = std::dynamic_pointer_cast<String>(extensions.operator[](i))->GetPrimitive();
        extensionsSet.insert(ext);
    }
    c.setStoreExtensions(extensionsSet);//set extensions List 

    Poco::File aFile(source);//This is where I start my compress action

    if (aFile.exists())
    {
        Poco::Path p(aFile.path());
        if (aFile.isDirectory())
        {
            if (p.isDirectory()) {
                c.addDirectory(p, Poco::DateTime());
            }
            else {

            }
        }
        else if (aFile.isFile())
        {
            c.addFile(p, p.getFileName());
        }
    }
    else {
        _log.EndMethod();
        throw new FileNotFoundException("File Not Found");
    }


    //Poco::FileOutputStream fos(target, std::ios::binary);

    c.close(); // MUST be done to finalize the Zip file
    fos.close();


}

以上代码是我目前所拥有的,我可以将单个文件压缩成.zip 文件。

如何将文件夹/目录压缩成 .zip 文件?我不能使用其他库,因为 Poco 也用于我当前项目的其他部分。

【问题讨论】:

    标签: c++ poco-libraries


    【解决方案1】:

    您需要添加一种递归方式来搜索文件夹。

    这是我想到的:

    Poco::File aFile(entry);
            if (!aFile.isDirectory())
                throw ZipException("Not a directory: "+ entry.toString());
            Poco::Path aName(name);
            aName.makeDirectory();
            if (!excludeRoot)
            {
                if (aName.depth() == 0)
                {
                    Poco::Path tmp(entry);
                    tmp.makeAbsolute(); // eliminate ../
                    aName = Poco::Path(tmp[tmp.depth()-1]);
                    aName.makeDirectory();
                }
    
                addDirectory(aName, aFile.getLastModified());
            }
        // iterate over children in the directory
            std::vector<std::string> children;
            aFile.list(children);
            std::vector<std::string>::const_iterator it = children.begin();
            std::vector<std::string>::const_iterator itEnd = children.end();
            for (; it != itEnd; ++it)
            {
                Poco::Path realFile(entry, *it);
                Poco::Path renamedFile(aName, *it);
                Poco::File aFile(realFile);
                if (aFile.isDirectory())
                {
                    realFile.makeDirectory();
                    renamedFile.makeDirectory();
                    addRecursive(realFile, cm, cl, false, renamedFile);
                }
                else
                {
                    realFile.makeFile();
                    renamedFile.makeFile();
                    addFile(realFile, renamedFile, cm, cl);
                }
            }
    

    【讨论】:

    • 好的,我会试一试,如果有帮助,我会将您的答案标记为可接受的答案。非常感谢保罗。
    • if (!excludeRoot)@Paul 你能帮我解释一下这行什么是 excludeRoot,我什么都想不出来。
    • 它递归地添加一个目录条目到 zip 文件中,不包括父目录。参考pocoproject.org/docs/Poco.Zip.Compress.html
    • 我明白了,但是excludeRoot 来自哪里,我没有看到任何 const 或值是 excludeRoot
    • 您需要将 excludeRoot(bool) 传递给您的函数 voidZipFile。就像 voidZipFile (const Poco::Path &entry, ZipCommon::CompressionMethod cm, ZipCommon::CompressionLevel cl, bool excludeRoot, const Poco::Path& name)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2018-06-19
    • 1970-01-01
    相关资源
    最近更新 更多