Skip to content

Instantly share code, notes, and snippets.

@selfboot
Last active October 19, 2023 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save selfboot/acda3473f687f610dc1f6230e555df03 to your computer and use it in GitHub Desktop.
Save selfboot/acda3473f687f610dc1f6230e555df03 to your computer and use it in GitHub Desktop.
#include <zip.h>
#include <iostream>
#include <string>
#include <vector>
struct FileInfo {
std::string htmltemlate;
std::string filename;
};
int main() {
std::vector<FileInfo> FileInfos = {
{R"(<?xml version="1.0" encoding="utf-8" standalone="no"?>demo)", "file1"},
};
const char *tmpFile = "example.zip";
int error = 0;
zip* archive = zip_open(tmpFile, ZIP_CREATE | ZIP_TRUNCATE, &error);
if (archive == NULL) {
printf("fail to open %s err %d", tmpFile, error);
return 1;
}
zip_source* s = NULL;
for (auto item : FileInfos) {
if (NULL == (s = zip_source_buffer(archive, item.htmltemlate.c_str(), item.htmltemlate.size(), 0)) ||
zip_file_add(archive, (item.filename + "_temp.xhtml").c_str(), s, ZIP_FL_ENC_UTF_8 | ZIP_FL_OVERWRITE) < 0) {
zip_source_free(s);
printf("fail to add info.txt err %s", zip_strerror(archive));
error = -1;
}
}
if (zip_close(archive) < 0) {
printf("fail to close %s ret %d", tmpFile, error);
return 1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment