【问题标题】:Poco addRecursive Not Returning to Normal Program FlowPoco addRecursive 不返回正常程序流程
【发布时间】:2021-08-24 01:23:06
【问题描述】:

我在 VS 2019 的 Windows 的 C++ 应用程序中实现了 Poco,使用了这篇文章中的提示:How to use Poco::ZIP to compress/decompress zip file

我已经想出了如何使用此代码 sn-p 递归压缩文件夹:

    // Now that we have an output file name, compress
    std::ofstream fos(z_file, std::ios::binary);
    Poco::Zip::Compress c(fos, true);
    Poco::File aFile(file_path);
    if (aFile.exists()) {
        Poco::Path p(aFile.path());
        if (aFile.isDirectory()) {
            c.addRecursive(p, Poco::Zip::ZipCommon::CompressionMethod::CM_AUTO,
                Poco::Zip::ZipCommon::CL_MAXIMUM, true);
        }
    }
    else {
        std::cout << "File not found... \r\n";
    }
    // Just in case these don't get called above.
    c.close(); // MUST be done to finalize the Zip file
    fos.close();
    return z_file;
}

我的问题是,一旦代码到达,

c.addRecursive(p, Poco::Zip::ZipCommon::CompressionMethod::CM_AUTO,
                Poco::Zip::ZipCommon::CL_MAXIMUM, true);

程序继续正常运行,并且压缩文件夹就好了。但是,程序不会返回到正常的执行流程。它会在线程上消失并且不会回来吗?我研究过我可以通过

获取有关添加到存档中的文件的更新
Poco::FIFOEvent<const ZipLocalFileHeader> EDone;

它还说关闭文件对于创建存档至关重要,但似乎使用 addRecursive 我的 close() 永远不会被调用。

任何帮助将不胜感激。

问候

【问题讨论】:

    标签: c++ directory compression


    【解决方案1】:

    一段时间后仍然回到 C++,但这是我调用此代码所在函数的方式。

    在调用它的代码中,我使用的是:

    std::string send_file { fh.compress_folder(home_dir) };
    

    当我把它改成:

    std::string send_file = fh.compress_folder(home_dir);
    

    然后在上面的代码中添加一个try/catch:

        std::ofstream fos(z_file, std::ios::binary);
        Poco::Zip::Compress c(fos, true);
        Poco::File aFile(file_path);
        if (aFile.exists()) {
            Poco::Path p(aFile.path());
            if (aFile.isDirectory()) {
                try {
                    c.addRecursive(p, Poco::Zip::ZipCommon::CompressionMethod::CM_AUTO,
                        Poco::Zip::ZipCommon::CL_MAXIMUM, true);
                } catch(const Poco::Exception e) {
                    std::cout << e.what() << "/r/n";
                }
            }
        }
        else {
            std::cout << "File not found... \r\n";
        }
        // Just in case these don't get called above.
        c.close(); // MUST be done to finalize the Zip file
        fos.close();
        return z_file;
    

    现在,程序正常返回,不再进入拉拉地。

    【讨论】:

      猜你喜欢
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-08
      • 2019-11-03
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多