Last active
April 27, 2021 08:47
-
-
Save ITotalJustice/8c48e31469850f61a44180a26693d890 to your computer and use it in GitHub Desktop.
zip folder example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// example of how to zip entire folder (recursively) into mem, using minizip-1.2 | |
// MINZIP_SOURCE: https://github.com/zlib-ng/minizip-ng/tree/1.2 | |
// g++ main.cpp ./minizip/*.c -lz -DNOUNCRYPT -DNOCRYPT -std=c++17 | |
#include <cstdio> | |
#include <cstdint> | |
#include <cstdlib> | |
#include <vector> | |
#include <string> | |
#include <fstream> | |
#include <filesystem> | |
#include "minizip/zip.h" | |
#include "minizip/ioapi_mem.h" | |
auto zip(const char* folder_path) -> bool { | |
zlib_filefunc_def filefunc32{}; | |
ourmemory_t zipmem{}; | |
zipmem.grow = 1; // the memory buffer will grow | |
fill_memory_filefunc(&filefunc32, &zipmem); | |
auto zfile = zipOpen3("__notused__", APPEND_STATUS_CREATE, 0, 0, &filefunc32); | |
if (!zfile) { | |
std::printf("failed to zip open in memory\n"); | |
return false; | |
} | |
for (auto& entry : std::filesystem::recursive_directory_iterator{folder_path}) { | |
if (entry.is_regular_file()) { | |
// get the fullpath | |
const auto path = entry.path().string(); | |
std::ifstream fs{path, std::ios::binary}; | |
if (fs.good()) { | |
// read file into buffer | |
const auto file_size = entry.file_size(); | |
std::vector<char> buffer(file_size); | |
fs.read(buffer.data(), buffer.size()); | |
// open the file inside the zip | |
if (Z_OK != zipOpenNewFileInZip(zfile, | |
path.c_str(), // filepath | |
NULL, // info, optional | |
NULL, 0, // extrafield and size, optional | |
NULL, 0, // extrafield-global and size, optional | |
NULL, // comment, optional | |
Z_DEFLATED, // mode | |
Z_DEFAULT_COMPRESSION // level | |
)) { | |
std::printf("failed to open file in zip: %s\n", path.c_str()); | |
continue; | |
} | |
// write out the entire file | |
if (Z_OK != zipWriteInFileInZip(zfile, buffer.data(), buffer.size())) { | |
std::printf("failed to write file in zip: %s\n", path.c_str()); | |
} | |
// don't forget to close when done! | |
if (Z_OK != zipCloseFileInZip(zfile)) { | |
std::printf("failed to close file in zip: %s\n", path.c_str()); | |
} | |
} | |
else { | |
std::printf("failed to open file %s\n", path.c_str()); | |
} | |
} | |
} | |
zipClose(zfile, NULL); | |
// if grow is set, then it is allocated via the zip | |
// functions, so it should be manually freed after! | |
if (zipmem.grow && zipmem.base) { | |
// i'm writing the zip out just to test if everything worked. | |
std::ofstream fs{"out.zip", std::ios::binary}; | |
if (fs.good()) { | |
fs.write(static_cast<char*>(zipmem.base), zipmem.size); | |
} | |
std::free(zipmem.base); | |
zipmem.base = NULL; | |
} | |
return true; | |
} | |
auto main() -> int { | |
zip("minizip/"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
idk why github keeps changing the tabs from 4 to 8...