Working with ZIP/archives in C++: libraries 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++: overview of libraries and practical examples



Data archiving is one of the key tasks in software development. C++ does not have built-in support for the ZIP format, however, there are several mature and proven libraries that allow you to easily pack, unpack, and modify ZIP archives. In this article, we will look at the main tools: zlib, minizip, and libzip, and also provide practical code examples.



1. Basic concepts of working with ZIP in C++



ZIP is a lossless compression format that supports several algorithms (Deflate, BZip2, LZMA). To work with it in C++, three approaches are typically used:



  • Direct work with zlib — low-level compression/decompression of data streams, but without support for the ZIP file structure.
  • minizip — an add-on over zlib that provides functions for creating and reading ZIP archives.
  • libzip — a full-featured library with support for editing, deleting, and adding files to existing archives.


2. The zlib library



zlib is a fundamental compression library on which many ZIP solutions are built. It provides the compress() and uncompress() functions for working with raw data.



Example: compressing a string using zlib



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

int main() { std::string input = "Example text for compression in C++!"; uLong srcLen = input.size(); uLong destLen = compressBound(srcLen); char* compressed = new char[destLen]; if (compress((Bytef*)compressed, &destLen, (const Bytef*)input.data(), srcLen) == Z_OK) { std::cout << "Compressed: " << srcLen << " -> " << destLen << " bytes"; } delete[] compressed; return 0;}


Note: To work with zlib, you need to include the header file zlib.h and link with the library -lz.



3. The minizip library



minizip is a lightweight wrapper over zlib that allows you to create and extract ZIP archives. It is distributed as part of the zlib source code (contrib/minizip folder).



Example: creating a ZIP archive with one file



#include <iostream>#include <zip.h>  // minizip

int main() { zipFile zf = zipOpen("example.zip", APPEND_STATUS_CREATE); if (!zf) { std::cerr << "Error creating archive"; return 1; } zip_fileinfo info = {0}; const char* filename = "test.txt"; const char* content = "Hello from minizip!"; if (zipOpenNewFileInZip(zf, filename, &info, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, Z_DEFAULT_COMPRESSION) == ZIP_OK) { zipWriteInFileInZip(zf, content, strlen(content)); zipCloseFileInZip(zf); } zipClose(zf, nullptr); std::cout << "Archive created: example.zip"; return 0;}


Example: extracting all files from a ZIP



#include <iostream>#include <unzip.h>

int main() { unzFile uf = unzOpen("example.zip"); if (!uf) { std::cerr << "Failed to open archive"; return 1; } unz_global_info global_info; unzGetGlobalInfo(uf, &global_info); for (uLong i = 0; i < global_info.number_entry; ++i) { char filename[256]; unz_file_info file_info; unzGetCurrentFileInfo(uf, &file_info, filename, sizeof(filename), nullptr, 0, nullptr, 0); if (unzOpenCurrentFile(uf) == UNZ_OK) { c

Recommendations