Skip to content

Instantly share code, notes, and snippets.

@selfboot
Last active October 19, 2023 05:02
Show Gist options
  • Save selfboot/acda3473f687f610dc1f6230e555df03 to your computer and use it in GitHub Desktop.
Save selfboot/acda3473f687f610dc1f6230e555df03 to your computer and use it in GitHub Desktop.

Revisions

  1. selfboot revised this gist Oct 19, 2023. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions zip_demo.cpp
    Original file line number Diff line number Diff line change
    @@ -3,15 +3,14 @@
    #include <string>
    #include <vector>

    struct CopyrightInfo {
    struct FileInfo {
    std::string htmltemlate;
    std::string filename;
    };

    int main() {
    std::vector<CopyrightInfo> copyrightInfos = {
    {"<html>Content 1</html>", "file1"},
    {"<html>Content 2</html>", "file2"}
    std::vector<FileInfo> FileInfos = {
    {R"(<?xml version="1.0" encoding="utf-8" standalone="no"?>demo)", "file1"},
    };

    const char *tmpFile = "example.zip";
    @@ -24,7 +23,7 @@ int main() {
    }

    zip_source* s = NULL;
    for (auto item : copyrightInfos) {
    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);
  2. selfboot created this gist Oct 18, 2023.
    42 changes: 42 additions & 0 deletions zip_demo.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    #include <zip.h>
    #include <iostream>
    #include <string>
    #include <vector>

    struct CopyrightInfo {
    std::string htmltemlate;
    std::string filename;
    };

    int main() {
    std::vector<CopyrightInfo> copyrightInfos = {
    {"<html>Content 1</html>", "file1"},
    {"<html>Content 2</html>", "file2"}
    };

    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 : copyrightInfos) {
    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;
    }