Working with ZIP/archives in C++: libzip library and code examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with ZIP archives in C++: from simple to complex



In modern C++ development, the need to pack and unpack data often arises. Whether it's saving configurations, transferring logs, or creating backups — working with ZIP archives remains one of the most in-demand skills. In this article, we will thoroughly examine two popular libraries: libzip (official, powerful) and miniz (lightweight, single-header). You will learn how to create, read, and modify ZIP archives, and also get ready-made code examples.



Why ZIP?

The ZIP format is the de facto standard for lossless compression. It is supported by all operating systems, allows storing multiple files and folders, and supports encryption and streaming. C++ has no built-in ZIP support (except for the experimental <filesystem>), so we use third-party libraries.



libzip library: a powerful tool for serious tasks

libzip is a cross-platform C library with an excellent C++-compatible API. It provides full control over archives: creating, reading, modifying, deleting files, working with comments and metadata.



Installing libzip (Linux/macOS)

# Ubuntu/Debiansudo apt-get install libzip-dev

# macOS (Homebrew)brew install libzip

# Windows — via vcpkg or download from libzip.org


Example 1: Creating a ZIP archive

Let's create an example.zip archive and add a text file to it.

#include <zip.h>#include <iostream>#include <string>

int main() { // Open the archive for writing (APPEND_CREATE will create a new one) int err = 0; zip_t *archive = zip_open("example.zip", ZIP_CREATE | ZIP_TRUNCATE, &err); if (!archive) { std::cerr << "Error creating archive: " << err << std::endl; return 1; }

// Data for the file const char *content = "Hello from PythonLib!"; zip_source_t *source = zip_source_buffer(archive, content, strlen(content), 0); if (!source) { std::cerr << "Failed to create data source" << std::endl; zip_close(archive); return 1; }

// Add the file to the archive zip_int64_t index = zip_file_add(archive, "hello.txt", source, ZIP_FL_OVERWRITE); if (index < 0) { std::cerr << "Failed to add file: " << zip_strerror(archive) << std::endl; zip_source_free(source); zip_close(archive); return 1; }

// Close the archive zip_close(archive); std::cout << "Archive created successfully!" << std::endl; return 0;}


Example 2: Extracting all files from an archive

Let's read the archive and extract all files to the current directory.

#include <zip.h>#include <iostream>#include <fstream>

int main() { int err = 0; zip_t *archive = zip_open("example.zip", ZIP_RDONLY, &err); if (!archive) { std::cerr << "Error opening archive" << std::endl; return 1; }

zip_int64_t num_entries = zip_get_num_entries(archive, 0); for (zip_int64_t i = 0; i < num_entries; ++i) { // Get the file name const char *filename = zip_get_name(archive, i, 0); if (!filename) continue;

// Open the file inside the archive zip_file_t *zf = zip_fopen_index(archive, i, 0); if (!zf) { std::cerr << "Failed to open: " << filename << std::endl; continue; }

// Read data and write to file char buffer[1024]; std::ofstream out(filename, std::ios::binary); zip_int64_t bytes_read; while ((bytes_read = zip_fread(zf, buffer, sizeof(buffer))) > 0) { out.write(b

Recommendations