是的,您可以使用POCO将原始内存缓冲区添加到 zip 文件中。
以下运行代码显示了如何在零拷贝的帮助下Poco::MemoryInputStream
。您可以std::istringstream
对std::stringbuf
.
#include <Poco/Exception.h>
#include <Poco/Zip/Compress.h>
#include <Poco/DateTime.h>
#include <Poco/MemoryStream.h>
#include <iostream>
#include <fstream>
using namespace std;
/*
* Compress multiple buffers to a zip archive, each buffer gets its filename.
* Raw data, you need to do the serialization yourself.
* To verify the data: unzip data.zip && hexdump -C buf1 && hexdump -C buf2
*/
void f(const char* buf1, size_t buf1len, const char* buf2, size_t buf2len) {
std::ofstream ostr("data.zip", std::ios::binary);
Poco::Zip::Compress compress(ostr, true);
// Poco::MemoryInputStream is a zero-copy implementation of std::istream :-)
Poco::MemoryInputStream mis1(buf1, buf1len);
compress.addFile(mis1, Poco::DateTime(), "buf1");
Poco::MemoryInputStream mis2(buf2, buf2len);
compress.addFile(mis2, Poco::DateTime(), "buf2");
compress.close();
ostr.close();
}
int main() {
const char buf1[] = "binary\0buffer"; size_t buf1len = sizeof(buf1);
const char buf2[] = "BIN\0BUF"; size_t buf2len = sizeof(buf2);
try {
f(buf1, buf1len, buf2, buf2len);
return 0;
} catch (Poco::Exception& e) {
cout << e.displayText() << endl;
return -1;
}
}
在我看来,POCO 是一个很棒的、轻量级的、现代的、易于理解的 C++ 库,你很快就会发现自己对它很放心。