【问题标题】:Rewriting this simple function using c++ STL/Boost instead of system() calls?使用 c++ STL/Boost 而不是 system() 调用重写这个简单的函数?
【发布时间】:2013-06-15 20:56:55
【问题描述】:

我需要将 *.cpp 文件归档到 gzip 压缩包内的特定目录中。我需要在运行时发生这种情况,以准确记录哪些代码(例如,哪些源代码文件的哪些版本)从程序中产生了一组特定的结果。

所以,我编写了以下函数。它通常可以正常工作,但它过去曾出现过一次或两次错误。我不记得到底发生了什么,但我记得我认为这是由于使用 system() 而不是在 c++ 程序中实际执行文件删除和存档。

void saveSourceCode_TarGZ(string destinationFile) {
    system( ("rm -f " + destinationFile).c_str() );
    system( ("rm -f " + destinationFile + ".gz").c_str() );

    system( ("tar -cvf " + destinationFile + " ./*.cpp").c_str() );
    system( ("gzip " + destinationFile).c_str() );
}

上述函数的前两行只是删除与我正在尝试创建的存档 (destinationFile) 同名的现有文件(如果存在)。最后两行分别创建一个包含当前工作目录中所有 .cpp 文件的 tarball 并 gzip 该 tarball。

如何使用 STL 或 Boost 库重写此函数?

我在使用 Boost 库方面非常缺乏经验,而且对于 c++ 中的文件系统管理我完全一无所知。

【问题讨论】:

    标签: c++ boost gzip tar


    【解决方案1】:

    STL 中没有压缩和解压缩文件的功能(根据某些压缩方案)。

    也就是说,有一个名为 lz4 (link) 的小型库应该可以满足您的需求。 摘自lz4.h:

    //****************************
    // Simple Functions
    //****************************
    
    int LZ4_compress        (const char* source, char* dest, int inputSize);
    int LZ4_decompress_safe (const char* source, char* dest, int inputSize, int maxOutputSize);
    

    如果可以使用 boost::iostreams,还有内置的 gzip 功能,即boost::iostreams::gzip

    【讨论】:

      猜你喜欢
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      相关资源
      最近更新 更多