libzippp

repository·master·Indexed 19 days ago

https://github.com/ctabin/libzippp

A portable C++ wrapper around the libzip library providing an easy-to-use interface for ZIP file handling. It supports reading and writing archives, in-memory buffers, progress monitoring, and optional encryption. Requires C++11 and dependencies including zlib and libzip.

Tokens
4.4K
Snippets
14
Records
19
Agent score
16%

What's inside libzippp

  1. Enable encryption in libzippp

    master

    To use encryption features, two conditions must be met:

    1. The underlying libzip library must be compiled with encryption support.
    2. libzippp must be built with the LIBZIPPP_ENABLE_ENCRYPTION CMake flag set to ON.

    When compiling tests, libzippp defaults to using OpenSSL (-lssl -lcrypto). You can specify a different cryptographic library using the CRYPTO_FLAGS variable during the make process:

    make CRYPTO_FLAGS="-lsome_lib" LIBZIP_CMAKE="" tests
  2. Reference libzippp in a CMake project

    master

    If libzippp is installed in a standard location or its path is provided via CMAKE_PREFIX_PATH, you can use it in your CMakeLists.txt as follows:

    find_package(libzippp 3.0 REQUIRED)
    target_link_libraries(your_target_name libzippp::libzippp)
  3. Install prerequisites for libzippp

    master

    libzippp requires at least C++11. Depending on your operating system, you may need to install specific development packages for its underlying dependencies (zlib, libzip, etc.).

    Linux

    Install development packages for the following:

    • zlib1g-dev
    • libzip-dev
    • liblzma-dev
    • libbz2-dev

    You can also use the provided Makefile by executing make libraries.

    Windows

    • Use the precompiled libraries from libzippp-<version>-windows-ready_to_compile.zip.
    • Or, install from source via CMake.
  4. Compile libzippp on Linux

    master

    If you have libzip installed in a non-standard location, you can specify the path during compilation using the LIBZIP variable:

    make LIBZIP=path/to/libzip

    On Debian-based systems, ensure zlib1g-dev is installed to avoid manual zlib installation.

  5. Compile libzippp from source

    master

    To compile libzippp, you need a C++ compiler (like g++ or MSVC) and CMake installed.

    Quick Start (Linux/macOS)

    mkdir build
    cd build
    cmake ..
    make
    make install

    Step-by-Step (Cross-platform)

    1. Create a build directory: mkdir build && cd build.
    2. Configure with CMake:
      • Command line: cmake .. -DCMAKE_BUILD_TYPE=Release
      • CMake GUI: Set source and build folders. If not using MSVC, add a cache entry for CMAKE_BUILD_TYPE.
      • Dependency Paths: If CMake cannot find zlib or libzip, set CMAKE_PREFIX_PATH to the directories containing their lib and include folders. Example: -DCMAKE_PREFIX_PATH=/path/to/libzip:/path/to/zlib.
    3. Compile:
      • Linux: make && make install
      • Windows: Open the generated project in MSVC and build the INSTALL target.
    mkdir build
    cd build
    cmake ..
    make
    make install
  6. Reference libzippp without CMake

    master

    If you are not using CMake, you must manually pass the include directory to your compiler and link against libzippp and its dependencies (libzip and zlib).

    Example 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
  7. Compile libzippp on Windows

    master

    When using Windows, be aware that non-virtual-only classes are shared within the libzippp DLL. To avoid compatibility issues between the DLL and your application, you should link the library statically.

    Note: The default installation path for MS Visual Studio 2012 is C:\Program Files (x86)\Microsoft Visual Studio 11.0\.

  8. Build libzippp on Windows using the prepared environment

    master

    The easiest way to build on Windows is to use the pre-packaged environment provided in the releases.

    1. Download libzippp-<version>-windows-ready_to_compile.zip and extract it.
    2. Ensure cmake 3.20 is in your PATH and MS Visual Studio is installed.
    3. (Optional) Check for any necessary patches in the lib folder.
    4. Execute compile.bat. This script automates the compilation of zlib, libzip, and finally libzippp.
    5. The resulting binaries will be in a dist folder containing release and debug subdirectories.
  9. Configure libzippp build with CMake variables

    master

    You can customize the build of libzippp using the following CMake variables via the command line (-DNAME=VALUE) or the CMake GUI:

    VariableDescription
    LIBZIPPP_ENABLE_ENCRYPTIONEnable/Disable building with encryption capabilities. Default is OFF. (Note: Requires libzip to be compiled with encryption support)
    LIBZIPPP_INSTALLEnable/Disable installation of libzippp. Default is OFF when using via add_subdirectory, else ON
    LIBZIPPP_INSTALL_HEADERSEnable/Disable installation of libzippp headers. Default is OFF when using via add_subdirectory, else ON
    LIBZIPPP_BUILD_TESTSEnable/Disable building libzippp tests. Default is OFF when using via add_subdirectory, else ON
    LIBZIPPP_CMAKE_CONFIG_MODEEnable/Disable building with libzip installed cmake config files. Default is OFF
    LIBZIPPP_GNUINSTALLDIRSEnable/Disable building with install directories taken from GNUInstallDirs. Default is OFF
    CMAKE_INSTALL_PREFIXThe installation destination
    CMAKE_BUILD_TYPESet to Release or Debug
    CMAKE_PREFIX_PATHColon-separated list of prefix paths for installed dependencies
    BUILD_SHARED_LIBSSet to ON or OFF to build shared or static libs
  10. Remove data from an archive

    master

    To delete files or directories from an archive, open the archive in ZipArchive::Write mode and use zf.deleteEntry(path).

    #include "libzippp.h"
    using namespace libzippp;
    
    int main(int argc, char** argv) {
      ZipArchive zf("archive.zip");
      zf.open(ZipArchive::Write);
      zf.deleteEntry("myFile.txt");
      zf.deleteEntry("myDir/subDir/");
      zf.close();
      
      return 0;
    }
  11. Read a specific entry from an archive

    master

    To access a specific file within an archive, you can use zf.getEntry(name) to get a ZipEntry object, or use zf.readEntry(name, bool) for raw access. readEntry returns a char* that must be manually deleted using delete[] after use.

    #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;
    }