Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I new to C++ especially in compress/decompress. My current project is using the Poco Library and the Zip extensions are used to compress or decompress the file/directory to a Zip file.

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

The above code is what I have so far and I can compress a single file into a .zip file.

How do I compress a folder/directory into a .zip file? I cannot use another library because Poco is used for other parts of my current project also.

You would need to add a recursive way to search through the folders.

This is what I thought of:

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);
                realFile.makeFile();
                renamedFile.makeFile();
                addFile(realFile, renamedFile, cm, cl);
                Okay, I will give it a try and if it help I would mark your answer as an acceptable answer. Thanks a lot Paul.
– Nguyễn Đức Tâm
                Dec 28, 2019 at 7:44
                if (!excludeRoot) @Paul can you help me explain about this line what is excludeRoot, I can't come up with anything.
– Nguyễn Đức Tâm
                Dec 30, 2019 at 2:16
                It adds a directory entry recursively to the zip file  excluding the parent directory.  Reference pocoproject.org/docs/Poco.Zip.Compress.html
– Paul
                Dec 30, 2019 at 2:59
                I see, but where does excludeRoot come from, I don't see any const or value that is excludeRoot
– Nguyễn Đức Tâm
                Dec 30, 2019 at 3:14
                You would require to pass excludeRoot(bool) to your function voidZipFile. It would be like voidZipFile (const Poco::Path &entry, ZipCommon::CompressionMethod cm, ZipCommon::CompressionLevel cl, bool excludeRoot, const Poco::Path& name)
– Paul
                Dec 30, 2019 at 3:41
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.