gpt4 book ai didi

c++ - 使用 c++ STL/Boost 而不是 system() 调用重写这个简单的函数?

转载 作者:行者123 更新时间:2023-11-28 00:44:21 25 4
gpt4 key购买 nike

我需要将 *.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 并对该 tarball 进行 gzip 压缩。

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

我在使用 Boost 库方面非常缺乏经验,而且在涉及到 C++ 文件系统管理时,我完全一无所知。

最佳答案

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

关于c++ - 使用 c++ STL/Boost 而不是 system() 调用重写这个简单的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17127812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com