我有一个在磁盘上加密的 zip 存档。我将文件读入缓冲区,解密它(使用一些自定义算法),然后在内存中压缩它。
现在我可以修改存档,但如何将更改写回磁盘?我错过了什么吗?存档是在调用时创建的,zip_source_buffer_create(...)
并且在调用时zip_close(...)
会对内存进行更改。
麦克维:
#include <zip.h>
#include <fstream>
#include <filesystem>
#include <vector>
#include <string>
int main(int argc, char** argv) {
std::string zip_file = "test.zip";
size_t file_sz = std::filesystem::file_size("test.zip");
std::vector<char> buf(file_sz);
std::ifstream ifs{ zip_file, std::ios::binary };
ifs.read(buf.data(), file_sz);
/*
buffer is decrypted here
*/
zip_error_t ze;
zip_error_init(&ze);
zip_source_t* zip_source = zip_source_buffer_create(buf.data(), buf.size(), 1, &ze);
zip_t* zip = zip_open_from_source(zip_source, NULL, &ze);
zip_source_t* file_source = zip_source_buffer(zip, "hello", 6, 0);
zip_file_add(zip, "hello.txt", file_source, ZIP_FL_ENC_GUESS);
zip_source_free(file_source);
zip_error_fini(&ze);
zip_close(zip); //modifications are written to memory but how do I retrieve the data?
/*
zip data should be acquired here, encrypted, and written to disk
*/
return 0;
}