使用Poco :: ZIP压缩/解压缩zip文件

问题描述 投票:0回答:1

我是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();


}

上面的代码是我到目前为止所实现的,因此我可以将1个单个文件压缩为.zip文件。如果我想将文件夹/目录压缩到.zip文件中,有人可以帮我吗?如果有人告诉我使用另一个库,我将无法使用,因为Poco库也用于我当前项目的其他部分。非常感谢,在此先感谢。

c++ poco-libraries
1个回答
0
投票

您需要添加一种递归方式来搜索文件夹,以下是我能想到的:

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);
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.