SkillAgentSearch skills...

Libzippp

C++ wrapper for libzip

Install / Use

/learn @ctabin/Libzippp
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Build Status

LIBZIPPP

libzippp is a simple basic C++ wrapper around the libzip library. It is meant to be a portable and easy-to-use library for ZIP handling.

Compilation has been tested with:

  • GCC 9 (Travis CI)
  • GCC 11.2.0 (GNU/Linux Debian)
  • MS Visual Studio 2012 (Windows 7)

Underlying libraries:

For more info on available compression methods, see here.

Integration

libzippp has been ported to vcpkg and thus can be very easily integrated by running:

./vcpkg install libzippp

Compilation

Install Prerequisites

This library requires at least C++ 11 to be compiled.

  • Linux

    • Install the development packages for zlib and libzip (e.g. zlib1g-dev, libzip-dev, liblzma-dev, libbz2-dev).
    • It is possible to use the Makefile by executing make libraries.
  • Windows:

    • Use precompiled libraries from libzippp-<version>-windows-ready_to_compile.zip.
    • Install from source via CMake (similar to workflow below).
  • All Operating systems

    • If it is intended to be used with encryption it is necessary to compile libzip with any encryption and to enable it in libzippp through the cmake flag LIBZIPPP_ENABLE_ENCRYPTION.

Compile libzippp

Quick start

mkdir build
cd build
cmake ..
make
make install

Step by step

  • Make sure you have a compiler (MSVC, g++, ...) and CMake installed
  • Switch to the source folder
  • Create a build folder and switch to it, e.g.: mkdir build && cd build
  • Configure the build with cmake:
    • Commandline: cmake .. -DCMAKE_BUILD_TYPE=Release
    • Within the CMake GUI, set source and build folder accordingly
      • Click Add Cache Entry to add CMAKE_BUILD_TYPE if not building with MSVC
      • Click Configure & Generate
    • If CMake can't find zlib and/or libzip you need to set CMAKE_PREFIX_PATH to the directories where you installed those into (either via -DCMAKE_PREFIX_PATH=<...> or via the GUI)
      • Example: -DCMAKE_PREFIX_PATH=/home/user/libzip-1.11.4:/home/user/zlib-1.3.2
  • Compile as usual
    • Linux: make && make install
    • Windows: Open generated project in MSVC. Build the INSTALL target to install.

CMake variables of interest

Set via commandline as cmake -DNAME=VALUE <other opts> or via CMake GUI or CCMake Add Cache Entry.

  • LIBZIPPP_INSTALL: Enable/Disable installation of libzippp. Default is OFF when using via add_subdirectory, else ON
  • LIBZIPPP_INSTALL_HEADERS: Enable/Disable installation of libzippp headers. Default is OFF when using via add_subdirectory, else ON
  • LIBZIPPP_BUILD_TESTS: Enable/Disable building libzippp tests. Default is OFF when using via add_subdirectory, else ON
  • LIBZIPPP_ENABLE_ENCRYPTION: Enable/Disable building libzippp with encryption capabilities. Default is OFF.
  • LIBZIPPP_CMAKE_CONFIG_MODE: Enable/Disable building with libzip installed cmake config files. Default is OFF.
  • LIBZIPPP_GNUINSTALLDIRS: Enable/Disable building with install directories taken from GNUInstallDirs. Default is OFF.
  • CMAKE_INSTALL_PREFIX: Where to install the project to
  • CMAKE_BUILD_TYPE: Set to Release or Debug to build with or without optimizations
  • CMAKE_PREFIX_PATH: Colon-separated list of prefix paths (paths containing lib and include folders) for installed libs to be used by this
  • BUILD_SHARED_LIBS: Set to ON or OFF to build shared or static libs, uses platform default if not set

Referencing libzippp

Once installed libzippp can be used from any CMake project with ease:
Given that it was installed (via CMAKE_INSTALL_PREFIX) into a standard location or its install prefix is passed into your projects CMAKE_PREFIX_PATH you can simply call find_package(libzippp 3.0 REQUIRED) and link against libzippp::libzippp.

When not using CMake to consume libzippp you have to pass its include directory to your compiler and link against libzippp.{a,so}. Do not forget to also link against libzip libraries e.g. in lib/libzip-1.11.4/lib/.libs/). An example of compilation with g++:

g++ -I./src \
    -I./lib/libzip-1.11.4/lib I./lib/libzip-1.11.4/build \
    main.cpp libzippp.a \
    lib/libzip-1.11.4/lib/.libs/libzip.a \
    lib/zlib-1.3.2/libz.a

Encryption

Since version 1.5, libzip uses an underlying cryptographic library (OpenSSL, GNUTLS or CommonCrypto) that is necessary for static compilation. By default, libzippp will use -lssl -lcrypto (OpenSSL) as default flags to compile the tests. This can be changed by using make CRYPTO_FLAGS="-lsome_lib" LIBZIP_CMAKE="" tests.

Since libzip cmake's file detects automatically the cryptographic library to use, by default all the allowed libraries but OpenSSL are explicitely disabled in the LIBZIP_CMAKE variable in the Makefile.

See here for more information.

WINDOWS - Alternative way

The easiest way is to download zlib, libzip and libzippp sources and use CMake GUI to build each library in order:

  • Open CMake GUI
  • Point Source to the libraries source folder, Build to a new folder build inside it
  • Run Generate
  • Open the generated solution in MSVC and build & install it
  • Repeat for the next library

But there is also a prepared batch file to help automate this. It may need some adjusting though.

From Stage 1 - Use prepared environment

  1. Make sure you have cmake 3.20 (cmake.exe must be in the PATH) and MS Visual Studio.

  2. Download the libzippp-<version>-windows-ready_to_compile.zip file from the release and extract it somewhere on your system. This will create a prepared structure, so libzippp can be compiled along with the needed libraries.

  3. Check if there is any patch to apply in lib. Sometimes, some files are not compilable in C89 in libzip, depending on the version.

  4. Simply execute the compile.bat file. This will compile zlib, libzip and finally libzippp.

  5. You'll have a dist folder containing the release and debug folders where you can now execute the libzippp tests.

From Stage 0 - DIY

  1. Make sure you have cmake 3.10 (cmake.exe must be in the PATH) and MS Visual Studio 2012.

  2. Download libzip and zlib sources and extract them in the 'lib' folder. You should end up with the following structure:

libzippp/compile.bat
libzippp/lib/zlib-1.3.2
libzippp/lib/libzip-1.11.4
  1. Execute the compile.bat (simply double-click on it). The compilation should go without error.

  2. You'll have a dist folder containing the release and debug folders where you can now execute the libzippp tests.

  3. You can either use libzippp.dll and libzippp.lib to link dynamically the library or simply use libzippp_static.lib to link it statically. Unless you also link zlib and libzippp statically, you'll need the dll packaged with your executable.

Usage

The API is meant to be very straight forward. Some french explanations can be found here.

List and read files in an archive

#include "libzippp.h"
using namespace libzippp;

int main(int argc, char** argv) {
  ZipArchive zf("archive.zip");
  zf.open(ZipArchive::ReadOnly);

  vector<ZipEntry> entries = zf.getEntries();
  vector<ZipEntry>::iterator it;
  for(it=entries.begin() ; it!=entries.end(); ++it) {
    ZipEntry entry = *it;
    string name = entry.getName();
    int size = entry.getSize();

    //the length of binaryData will be given by 'size'
    void* binaryData = entry.readAsBinary();

    //the length of textData will be given by 'size'
    string textData = entry.readAsText();

    //...
  }

  zf.close();
  
  return 0;
}

You can also create an archive directly from a buffer

#include "libzippp.h"
using namespace libzippp;

int main(int argc, char** argv) {
  char* buffer = someData;
  uint32_t bufferSize = sizeOfBuffer;

  ZipArchive* zf = ZipArchive::fromBuffer(buffer, bufferSize);
  if(zf!=nullptr) {
    /* work with the ZipArchive instance */
    zf->close();
    delete zf;
  }
  
  return 0;
}

Read a specific entry from an archive

#include "libzippp.h"
using namespace libzippp;

int main(int argc, char** argv) {
  ZipArchive zf("archive.zip");
  zf.open(ZipArchive::ReadOnly);

  //raw access
  char* data = (char*)zf.readEntry("myFile.txt", true);
  ZipEntry entry1 = zf.getEntry("myFile.txt");
  string str1(data, entry1.getSize());
  delete[] data;

  //text access
  ZipEntry entry2 = zf.getEntry("myFile.txt");
  string str2 = entry2.readAsText();

  zf.close();
  
  return 0;
}

Read a large entry from an archive

#include "libzippp.h"
using namespace libzippp;

int main(int argc, char** argv) {
  ZipArchive zf("archive.zip");
  zf.open(ZipArchive::ReadOnly);

  ZipEntry largeEntry = z1.getEntry("largeentry");
  std::ofstream ofUnzippedFile("largeFileContent.data");
  largeEntry.readContent(ofUnzippedFile);
  ofUnzippedFile.close();

  zf.close();

  return 0;
}

Add data to an archive

#include "libzippp.h"
using namespace libzippp;

int main(int argc, char** argv) {
  ZipArchive zf("archive.zip");
  zf.open(ZipArchive::Write);

  
#ifdef LIBZIPPP_USE_BZIP2
  // Advanced usage : change the compression method. Default is DEFLATE.
  zf.setCompressionMethod(entry, CompressionMethod::BZIP2);
#endif

  zf.addEntry
View on GitHub
GitHub Stars449
CategoryDevelopment
Updated6d ago
Forks97

Languages

C++

Security Score

85/100

Audited on Mar 30, 2026

No findings