gpt4 book ai didi

c++ - 尝试将 gzip 中的文件提取到 C++ 中的临时文件夹

转载 作者:行者123 更新时间:2023-11-30 04:58:43 25 4
gpt4 key购买 nike

我想使用 C++ 在 gzip 中提取文件。我正在使用 boost 库。这是我的代码:

//Read from the first command line argument, assume it's gzipped    
std::ifstream file(filepath, std::ios_base::in | std::ios_base::binary);
boost::iostreams::filtering_streambuf<boost::iostreams::input> inbuf;
inbuf.push(boost::iostreams::gzip_decompressor());
inbuf.push(file);
boost::iostreams::copy(inbuf, cout);

//Cleanup
file.close();

此代码正在将所有数据写入终端。但是,我想将所有文件分别提取到某个文件夹中。

最佳答案

我会说,照你说的做:

#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/filesystem.hpp>
#include <iostream>
#include <fstream>

int main(int argc, char** argv) {
auto dir = boost::filesystem::temp_directory_path() / "demo";
boost::filesystem::create_directories(dir);

for (boost::filesystem::path p : boost::make_iterator_range(argv+1, argv+argc)) {
std::ifstream file(p.c_str(), std::ios_base::in | std::ios_base::binary);
boost::iostreams::filtering_streambuf<boost::iostreams::input> inbuf;
inbuf.push(boost::iostreams::gzip_decompressor());
inbuf.push(file);

auto output = dir / p.filename();
std::cout << "Writing " << output << "\n";
std::ofstream ofs(output.c_str(), std::ios::binary);
boost::iostreams::copy(inbuf, ofs);
}
}

因此,使用一个参数运行,如 ./test test.cpp.gz 会将解压缩的输出写入临时目录中 sasme 名称的文件:

$ ../build/sotest test.cpp.gz 
Writing "/tmp/demo/test.cpp.gz"

如何获取原始文件名?

我之前在另一个答案中展示过:How to get gzip_params from boost's gzip_decompressor()

所以你会得到这个:

#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/filesystem.hpp>
#include <iostream>
#include <fstream>

namespace fs = boost::filesystem;
using gz_t = boost::iostreams::gzip_decompressor;

int main(int argc, char** argv) {
auto dir = fs::temp_directory_path() / "demo";
create_directories(dir);

for (fs::path p : boost::make_iterator_range(argv+1, argv+argc)) {
std::ifstream file(p.c_str(), std::ios_base::in | std::ios_base::binary);
boost::iostreams::filtering_streambuf<boost::iostreams::input> inbuf;
inbuf.push(gz_t());
inbuf.push(file);

if (gz_t* gz = inbuf.component<0, gz_t>()) {
auto output = dir / fs::unique_path(basename(p));
{
std::cout << "Writing " << output << "\n";
std::ofstream ofs(output.c_str(), std::ios::binary);
boost::iostreams::copy(inbuf, ofs);
}

std::cout << "Original filename: " << gz->file_name() << "\n";
std::cout << "Original mtime: " << gz->mtime() << "\n";
std::cout << "Zip comment: " << gz->comment() << "\n";
if (!gz->file_name().empty()) {
fs::rename(output, dir / fs::path(gz->file_name()).filename());
}
}

}
}

现在,如果你像这样运行它

$ gzip -k test.cpp
$ mv test.cpp. gz renamed.gz

$ ../build/sotest renamed.gz
Writing "/tmp/demo/renamed"
Original filename: test.cpp
Original mtime: 1532692097
Zip comment:

$ find /tmp/demo/
/tmp/demo/
/tmp/demo/test.cpp

关于c++ - 尝试将 gzip 中的文件提取到 C++ 中的临时文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51553266/

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