### Configure, Build, Install, and Test QuaZip Source: https://github.com/stachenov/quazip/blob/master/README.md A typical workflow for configuring, building, installing, and testing QuaZip using CMake. Ensure you have the necessary build tools and Qt installed. ```bash cmake -B build -DQUAZIP_QT_MAJOR_VERSION=6 -DBUILD_SHARED_LIBS=ON -DQUAZIP_ENABLE_TESTS=ON cmake --build build --config Release sudo cmake --install build cd build ctest --verbose -C Release cmake --build . --target clean ``` -------------------------------- ### Install QuaZip Library Source: https://github.com/stachenov/quazip/blob/master/quazip/doc/index.dox Installs the built QuaZip library. The library is installed as a CMake package, e.g., QuaZip-Qt5. ```bash cmake --build wherever/you/want/your/build/to/be --target install ``` -------------------------------- ### Install Doxygen and Graphviz Source: https://github.com/stachenov/quazip/blob/master/README_pages.md Installs the Doxygen documentation generator and Graphviz for diagram rendering. These are prerequisites for generating project documentation. ```bash sudo apt-get install doxygen graphviz ``` -------------------------------- ### Flatpak Manifest for QuaZip Source: https://github.com/stachenov/quazip/blob/master/quazip/doc/usage.dox Example configuration for using QuaZip within a Flatpak application's manifest. This specifies build system, options, and sources. ```yaml modules: - name: quazip buildsystem: cmake-ninja builddir: true config-opts: - -DCMAKE_BUILD_TYPE=MinSizeRel sources: - type: archive url: https://github.com/stachenov/quazip/archive/v1.1.tar.gz sha256: 54edce9c11371762bd4f0003c2937b5d8806a2752dd9c0fd9085e90792612ad0 - type: shell commands: - sed -i 's|${CMAKE_ROOT}/Modules|share/cmake|' CMakeLists.txt ``` -------------------------------- ### Build QuaZip on Linux Source: https://github.com/stachenov/quazip/blob/master/README.md Installs dependencies and builds QuaZip using CMake on Linux. Ensure zlib1g-dev and libbz2-dev are installed. ```bash sudo apt-get install zlib1g-dev libbz2-dev cmake -B build cmake --build build ``` -------------------------------- ### Build QuaZip on Windows with Conan v2 (x64) Source: https://github.com/stachenov/quazip/blob/master/README.md Builds QuaZip using CMake and Conan v2 for x64 systems on Windows. Installs dependencies via Conan. ```bash conan install . -of build -s build_type=Release -o *:shared=False --build=missing cmake --preset conan cmake --build build --config Release ``` -------------------------------- ### Build QuaZip on Windows with Conan v2 (x86) Source: https://github.com/stachenov/quazip/blob/master/README.md Builds QuaZip using CMake and Conan v2 for x86 systems on Windows. Installs dependencies via Conan and specifies x86 architecture. ```bash conan install . -of build -s build_type=Release -s:h arch=x86 -o *:shared=False --build=missing cmake --preset conan_x86 cmake --build build --config Release ``` -------------------------------- ### Link QuaZip with CMake Source: https://github.com/stachenov/quazip/blob/master/quazip/doc/usage.dox Include this in your CMakeLists.txt to find and link the QuaZip library to your target. Ensure QuaZip is installed or available in your build environment. ```cmake find_package(QuaZip-Qt5) target_link_libraries(whatever-your-target-is QuaZip::QuaZip) ``` -------------------------------- ### Get File List with Fluent API Source: https://github.com/stachenov/quazip/blob/master/EXAMPLES.md Use the fluent API (QuaExtract) to retrieve a list of all file names contained within a ZIP archive without extracting them. This is useful for inspecting archive contents. ```cpp #include // Get file list QStringList fileNames = QuaExtract() .getFileList("archive.zip"); ``` -------------------------------- ### Build QuaZip on Windows with vcpkg (x64) Source: https://github.com/stachenov/quazip/blob/master/README.md Builds QuaZip using CMake and vcpkg for x64 systems on Windows. Requires vcpkg to be set up. ```bash cmake --preset vcpkg cmake --build build --config Release ``` -------------------------------- ### Prepare and Generate Documentation Source: https://github.com/stachenov/quazip/blob/master/README_pages.md Copies project files, switches to the gh-pages branch, configures Doxygen, generates HTML documentation, and commits the updated documentation to the repository. ```bash cp -r quazip quazip-docs cd quazip-docs git checkout gh-pages cd ../quazip ( cat Doxyfile ; printf "OUTPUT_DIRECTORY=../quazip-docs\nHTML_OUTPUT=.\n" ) | doxygen - cd ../quazip-docs git add -A git commit -m "Update docs" git push ``` -------------------------------- ### Build QuaZip on Windows with vcpkg (x86) Source: https://github.com/stachenov/quazip/blob/master/README.md Builds QuaZip using CMake and vcpkg for x86 systems on Windows. Note: Only Qt5 is tested on x86. ```bash cmake --preset vcpkg_x86 cmake --build build --config Release ``` -------------------------------- ### Log Build Information Source: https://github.com/stachenov/quazip/blob/master/CMakeLists.txt Outputs the currently set Qt major version and C++ standard to the build log for verification. ```cmake message(STATUS "QUAZIP_QT_MAJOR_VERSION set to ${QUAZIP_QT_MAJOR_VERSION}") message(STATUS "CMAKE_CXX_STANDARD set to ${CMAKE_CXX_STANDARD}") ``` -------------------------------- ### QuaZipFile - Read and write individual entries as QIODevice Source: https://context7.com/stachenov/quazip/llms.txt Shows how to use `QuaZipFile` to read archive entries as a `QIODevice` (e.g., with `QTextStream`), write new entries into a ZIP archive, and handle encrypted reading. ```APIDOC ## QuaZipFile — Read and write individual entries as QIODevice `QuaZipFile` subclasses `QIODevice`, making archive entries usable with any Qt I/O consumer. Reading is done against the current file of an associated `QuaZip` instance or by providing the archive path plus entry name directly. Writing requires an associated `QuaZip` opened in `mdCreate` or `mdAdd`. ```cpp #include #include #include #include // --- Reading with QTextStream --- QuaZipFile readFile("archive.zip", "notes.txt", QuaZip::csDefault); if (readFile.open(QIODevice::ReadOnly)) { QTextStream ts(&readFile); qDebug() << ts.readAll(); readFile.close(); } // --- Writing new entries into a ZIP --- QuaZip zip("output.zip"); zip.setZip64Enabled(true); // support files > 4 GB zip.setUtf8Enabled(true); // UTF-8 filenames if (!zip.open(QuaZip::mdCreate)) return; // Entry 1: read metadata (timestamp, permissions) from the source file QFile srcA("report.pdf"); srcA.open(QIODevice::ReadOnly); QuaZipFile entryA(&zip); QuaZipNewInfo infoA("docs/report.pdf", "report.pdf"); // name-in-zip, source-path if (entryA.open(QIODevice::WriteOnly, infoA, /*password=*/nullptr, /*crc=*/0, Z_DEFLATED, Z_DEFAULT_COMPRESSION)) { entryA.write(srcA.readAll()); entryA.close(); } srcA.close(); // Entry 2: in-memory content, store without compression QuaZipFile entryB(&zip); QuaZipNewInfo infoB("VERSION"); infoB.dateTime = QDateTime::currentDateTimeUtc(); if (entryB.open(QIODevice::WriteOnly, infoB, nullptr, 0, /*method=*/0, /*level=*/0)) { entryB.write("1.5.0\n"); entryB.close(); } zip.setComment("Created by MyApp"); zip.close(); // --- Encrypted reading --- QuaZipFile secureFile("secret.zip", "data.bin"); if (secureFile.open(QIODevice::ReadOnly, "p4ssw0rd")) { QByteArray raw = secureFile.readAll(); secureFile.close(); } ``` ``` -------------------------------- ### QuaZip - Open and inspect a ZIP archive Source: https://context7.com/stachenov/quazip/llms.txt Demonstrates how to open a ZIP archive in read mode, iterate through its entries, retrieve file information, and access global archive metadata. ```APIDOC ## QuaZip — Open and inspect a ZIP archive `QuaZip` represents an open ZIP file and manages the central directory. It supports four open modes (`mdUnzip`, `mdCreate`, `mdAppend`, `mdAdd`) and provides iteration, file lookup, and metadata access for every entry. ```cpp #include #include #include QuaZip zip("archive.zip"); zip.setFileNameCodec("UTF-8"); // needed for non-ASCII names on Windows if (!zip.open(QuaZip::mdUnzip)) { qWarning() << "Cannot open archive, error:" << zip.getZipError(); return; } qDebug() << "Entries:" << zip.getEntriesCount(); qDebug() << "Global comment:" << zip.getComment(); // Iterate all entries for (bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) { QuaZipFileInfo64 info; if (zip.getCurrentFileInfo(&info)) { qDebug() << info.name << "compressed:" << info.compressedSize << "uncompressed:" << info.uncompressedSize; } } // Jump directly to a named file (case-insensitive on Windows by default) if (zip.setCurrentFile("README.txt", QuaZip::csInsensitive)) { qDebug() << "Found:" << zip.getCurrentFileName(); } // Full file list without manual iteration QStringList names = zip.getFileNameList(); QList infos = zip.getFileInfoList64(); zip.close(); if (zip.getZipError() != UNZ_OK) qWarning() << "Close error:" << zip.getZipError(); ``` ``` -------------------------------- ### Encrypted Reading of ZIP Entries with QuaZipFile Source: https://context7.com/stachenov/quazip/llms.txt Demonstrates how to open and read an encrypted ZIP entry by providing the password during the `open` call. The password should be passed as a `const char*`. ```cpp #include #include #include #include // --- Encrypted reading --- QuaZipFile secureFile("secret.zip", "data.bin"); if (secureFile.open(QIODevice::ReadOnly, "p4ssw0rd")) { QByteArray raw = secureFile.readAll(); secureFile.close(); } ``` -------------------------------- ### Open and Inspect a ZIP Archive with QuaZip Source: https://context7.com/stachenov/quazip/llms.txt Demonstrates how to open a ZIP archive, iterate through its entries, retrieve file information, and access specific files. Ensure UTF-8 encoding is set for non-ASCII filenames on Windows. ```cpp #include #include #include QuaZip zip("archive.zip"); zip.setFileNameCodec("UTF-8"); // needed for non-ASCII names on Windows if (!zip.open(QuaZip::mdUnzip)) { qWarning() << "Cannot open archive, error:" << zip.getZipError(); return; } qDebug() << "Entries:" << zip.getEntriesCount(); qDebug() << "Global comment:" << zip.getComment(); // Iterate all entries for (bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) { QuaZipFileInfo64 info; if (zip.getCurrentFileInfo(&info)) { qDebug() << info.name << "compressed:" << info.compressedSize << "uncompressed:" << info.uncompressedSize; } } // Jump directly to a named file (case-insensitive on Windows by default) if (zip.setCurrentFile("README.txt", QuaZip::csInsensitive)) { qDebug() << "Found:" << zip.getCurrentFileName(); } // Full file list without manual iteration QStringList names = zip.getFileNameList(); QList infos = zip.getFileInfoList64(); zip.close(); if (zip.getZipError() != UNZ_OK) qWarning() << "Close error:" << zip.getZipError(); ``` -------------------------------- ### Write New Entries to a ZIP Archive with QuaZipFile Source: https://context7.com/stachenov/quazip/llms.txt Demonstrates creating a new ZIP archive and adding entries, including files from disk and in-memory content. Supports Zip64 for large files and UTF-8 filenames. Compression can be disabled for specific entries. ```cpp #include #include #include #include // --- Writing new entries into a ZIP --- QuaZip zip("output.zip"); zip.setZip64Enabled(true); // support files > 4 GB zip.setUtf8Enabled(true); // UTF-8 filenames if (!zip.open(QuaZip::mdCreate)) return; // Entry 1: read metadata (timestamp, permissions) from the source file QFile srcA("report.pdf"); srcA.open(QIODevice::ReadOnly); QuaZipFile entryA(&zip); QuaZipNewInfo infoA("docs/report.pdf", "report.pdf"); // name-in-zip, source-path if (entryA.open(QIODevice::WriteOnly, infoA, /*password=*/nullptr, /*crc=*/0, Z_DEFLATED, Z_DEFAULT_COMPRESSION)) { entryA.write(srcA.readAll()); entryA.close(); } srcA.close(); // Entry 2: in-memory content, store without compression QuaZipFile entryB(&zip); QuaZipNewInfo infoB("VERSION"); infoB.dateTime = QDateTime::currentDateTimeUtc(); if (entryB.open(QIODevice::WriteOnly, infoB, nullptr, 0, /*method=*/0, /*level=*/0)) { entryB.write("1.5.0\n"); entryB.close(); } zip.setComment("Created by MyApp"); zip.close(); ``` -------------------------------- ### Fetch and Configure BZip2 Dependency Source: https://github.com/stachenov/quazip/blob/master/CMakeLists.txt This snippet uses CMake's FetchContent module to download and build the BZip2 library. It handles different CMake versions and specifies source and build directories. The BZip2 library is then added as a static library. ```cmake include(FetchContent) set(_bzip2_primary_url "https://sourceware.org/pub/bzip2/bzip2-1.0.8.tar.gz") set(_bzip2_fallback_url "https://mirror.bazel.build/sourceware.org/pub/bzip2/bzip2-1.0.8.tar.gz") set(_bzip2_hash "SHA256=ab5a03176ee106d3f0fa90e381da478ddae405918153cca248e682cd0c4a2269") if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.24") FetchContent_Declare(bzip2 URL "${_bzip2_primary_url}" "${_bzip2_fallback_url}" URL_HASH ${_bzip2_hash} SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/bzip2) else() FetchContent_Declare(bzip2 URL "${_bzip2_primary_url}" URL_HASH ${_bzip2_hash} SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/bzip2) endif() FetchContent_GetProperties(bzip2 POPULATED bzip2_POPULATED) if(NOT bzip2_POPULATED) FetchContent_MakeAvailable(bzip2) endif() set(BZIP2_SOURCE_DIR ${bzip2_SOURCE_DIR}) set(BZIP2_BINARY_DIR ${bzip2_BINARY_DIR}) ``` ```cmake set(BZIP2_SRC ${BZIP2_SOURCE_DIR}/blocksort.c ${BZIP2_SOURCE_DIR}/bzlib.c ${BZIP2_SOURCE_DIR}/compress.c ${BZIP2_SOURCE_DIR}/crctable.c ${BZIP2_SOURCE_DIR}/decompress.c ${BZIP2_SOURCE_DIR}/huffman.c ${BZIP2_SOURCE_DIR}/randtable.c) set(BZIP2_HDR ${BZIP2_SOURCE_DIR}/bzlib.h ${BZIP2_SOURCE_DIR}/bzlib_private.h) add_library(bzip2 STATIC ${BZIP2_SRC} ${BZIP2_HDR}) ``` ```cmake set_target_properties(bzip2 PROPERTIES PUBLIC_HEADER "${BZIP2_SOURCE_DIR}/bzlib.h" ) target_include_directories(bzip2 PUBLIC $ $ ) ``` ```cmake if(NOT QUAZIP_BZIP2_STDIO) target_compile_definitions(bzip2 PRIVATE -DBZ_NO_STDIO) endif() list(APPEND QUAZIP_DEP bzip2) list(APPEND QUAZIP_LIB bzip2) list(APPEND QUAZIP_INC ${BZIP2_SOURCE_DIR}) set(QUAZIP_BZIP2_BUILD ON) ``` -------------------------------- ### Find System ZLIB for Emscripten Source: https://github.com/stachenov/quazip/blob/master/CMakeLists.txt Handles finding and including system ZLIB for Emscripten builds when Qt's bundled ZLIB is not used. Includes warnings if ZLIB paths are not defined. ```cmake if(NOT QUAZIP_QT_ZLIB_USED) if(EMSCRIPTEN) if(NOT DEFINED ZLIB_LIBRARY) message(WARNING "ZLIB_LIBRARY is not set") endif() if(NOT DEFINED ZLIB_INCLUDE) message(WARNING "ZLIB_INCLUDE is not set") else() include_directories(${ZLIB_INCLUDE}) endif() if(NOT DEFINED ZCONF_INCLUDE) message(WARNING "ZCONF_INCLUDE is not set") else() include_directories(${ZCONF_INCLUDE}) endif() set(QUAZIP_LIB_LIBRARIES ${QUAZIP_LIB_LIBRARIES} ${ZLIB_LIBRARY}) else() find_package(ZLIB REQUIRED) set(QUAZIP_LIB_LIBRARIES ${QUAZIP_LIB_LIBRARIES} ZLIB::ZLIB) endif() endif() ``` -------------------------------- ### Configure QuaZip Instance for Zip64 and UTF-8 Source: https://context7.com/stachenov/quazip/llms.txt Enable Zip64 mode for files larger than 4 GB and UTF-8 encoding for filenames on a per-instance basis. Disable data descriptor writing for better compatibility with older readers. ```cpp #include // --- Instance-level flags --- QuaZip zip("large.zip"); zip.setZip64Enabled(true); // write zip64 local/central headers zip.setUtf8Enabled(true); // encode filenames in UTF-8 (sets flag in header) zip.setDataDescriptorWritingEnabled(false); // improve compat with old readers zip.open(QuaZip::mdCreate); // ... add files ... zip.close(); ``` -------------------------------- ### Define Source and Binary Directories Source: https://github.com/stachenov/quazip/blob/master/CMakeLists.txt Sets variables for the source and binary directories of Quazip. Also defines the library file name based on version information. ```cmake set(QUAZIP_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) set(QUAZIP_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) if(NOT QUAZIP_LIB_FILE_NAME) set(QUAZIP_LIB_FILE_NAME quazip${QuaZip_VERSION_MAJOR}-qt${QUAZIP_QT_MAJOR_VERSION}) endif() set(QUAZIP_LIB_TARGET_NAME QuaZip) set(QUAZIP_DIR_NAME QuaZip-Qt${QUAZIP_QT_MAJOR_VERSION}-${QUAZIP_LIB_VERSION}) set(QUAZIP_PACKAGE_NAME QuaZip-Qt${QUAZIP_QT_MAJOR_VERSION}) ``` -------------------------------- ### Build QuaZip Library Source: https://github.com/stachenov/quazip/blob/master/quazip/doc/index.dox Builds the QuaZip library using CMake. Specify the Qt major version with QUAZIP_QT_MAJOR_VERSION. Assumes a Unix-like environment. ```bash cd /wherever/quazip/source/is/quazip-x.y.z cmake -B wherever/you/want/your/build/to/be -D QUAZIP_QT_MAJOR_VERSION=6 or 5 cmake --build wherever/you/want/your/build/to/be ``` -------------------------------- ### Create ZIP Entry Metadata with QuaZipNewInfo Source: https://context7.com/stachenov/quazip/llms.txt Use QuaZipNewInfo to set metadata for new ZIP entries, including name, timestamp, permissions, comments, and extra fields. You can copy metadata from a real file or set values explicitly. This is useful for controlling entry details before writing to a QuaZipFile. ```cpp #include #include // Copy metadata from the real file QuaZipNewInfo info("subdir/image.png", "/home/user/Pictures/image.png"); // Override timestamp to a fixed value (useful for reproducible builds) info.dateTime = QDateTime(QDate(2024, 1, 1), QTime(0, 0, 0), Qt::UTC); // Set Unix permissions explicitly info.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup | QFile::ReadOther); // Add NTFS high-precision timestamps to the extra field info.setFileNTFSTimes("/home/user/Pictures/image.png"); // Add a per-entry comment info.comment = "Profile picture"; QuaZipFile entry(&zip); // zip already opened in mdCreate/mdAdd entry.open(QIODevice::WriteOnly, info); entry.write(imageData); entry.close(); ``` -------------------------------- ### Compress File with Low-Level API Source: https://github.com/stachenov/quazip/blob/master/EXAMPLES.md Use the low-level API for fine-grained control when creating a ZIP archive and adding a single file. Ensure proper error handling for archive and file operations. ```cpp #include #include #include // Create a ZIP archive and add a file QuaZip zip("archive.zip"); if (!zip.open(QuaZip::mdCreate)) { // Handle error return; } QFile inFile("document.txt"); if (!inFile.open(QIODevice::ReadOnly)) { return; } QuaZipFile outFile(&zip); // QuaZipNewInfo(name, file): name in archive, file to read metadata from if (!outFile.open(QIODevice::WriteOnly, QuaZipNewInfo(inFile.fileName(), inFile.fileName()))) { return; } // Copy data outFile.write(inFile.readAll()); outFile.close(); inFile.close(); zip.close(); ``` -------------------------------- ### Initialize Dependency Variables Source: https://github.com/stachenov/quazip/blob/master/CMakeLists.txt Initializes variables used to store paths and libraries for Quazip dependencies. ```cmake set(QUAZIP_DEP) set(QUAZIP_INC) set(QUAZIP_LIB) set(QUAZIP_LBD) ``` -------------------------------- ### Find Qt6 or Qt5 Source: https://github.com/stachenov/quazip/blob/master/CMakeLists.txt Searches for Qt6 and Qt5 in that order. Sets QT_VERSION_MAJOR to 6 if neither is found. ```cmake find_package( QT NAMES Qt6 Qt5 QUIET COMPONENTS Core ) if (NOT QT_FOUND) set(QT_VERSION_MAJOR 6) endif() ``` -------------------------------- ### Find Qt6 Components Source: https://github.com/stachenov/quazip/blob/master/CMakeLists.txt Finds required and optional components for Qt6, including Core, Network, and Test. Sets library targets and pkg-config requirements. ```cmake find_package(Qt6 REQUIRED COMPONENTS Core OPTIONAL_COMPONENTS Network Test) message(STATUS "Found Qt version ${Qt6_VERSION} at ${Qt6_DIR}") set(QUAZIP_QT_ZLIB_COMPONENT BundledZLIB) set(QUAZIP_QT_ZLIB_HEADER_COMPONENT ZlibPrivate) set(QUAZIP_LIB_LIBRARIES Qt6::Core ) set(QUAZIP_TEST_QT_LIBRARIES Qt6::Core Qt6::Network Qt6::Test) set(QUAZIP_PKGCONFIG_REQUIRES "zlib, Qt6Core") ``` -------------------------------- ### Read Individual Entries as QIODevice with QuaZipFile Source: https://context7.com/stachenov/quazip/llms.txt Shows how to read the content of a specific ZIP entry using QTextStream. Ensure the QuaZipFile is opened in ReadOnly mode. ```cpp #include #include #include #include // --- Reading with QTextStream --- QuaZipFile readFile("archive.zip", "notes.txt", QuaZip::csDefault); if (readFile.open(QIODevice::ReadOnly)) { QTextStream ts(&readFile); qDebug() << ts.readAll(); readFile.close(); } ``` -------------------------------- ### Extract All Files with Low-Level API Source: https://github.com/stachenov/quazip/blob/master/EXAMPLES.md Use the low-level API to open a ZIP archive and extract all its contents. This method iterates through each file, creates necessary directories, and copies the data. ```cpp #include #include #include #include // Open ZIP archive and extract all files QuaZip zip("archive.zip"); if (!zip.open(QuaZip::mdUnzip)) { return; } // Loop through all files in the archive for (bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) { QuaZipFile inFile(&zip); if (!inFile.open(QIODevice::ReadOnly)) { return; } // Get current file info to get the filename QuaZipFileInfo64 fileInfo; if (!zip.getCurrentFileInfo(&fileInfo)) { return; } // Create output file QString outPath = "output/" + fileInfo.name; QDir().mkpath(QFileInfo(outPath).absolutePath()); // Create directories if needed QFile outFile(outPath); if (!outFile.open(QIODevice::WriteOnly)) { return; } // Copy data outFile.write(inFile.readAll()); outFile.close(); inFile.close(); } zip.close(); ``` -------------------------------- ### Read and Write GZIP Files with QuaGzipFile Source: https://context7.com/stachenov/quazip/llms.txt QuaGzipFile provides a QIODevice interface for GZIP compression/decompression, compatible with Qt's I/O streams. It can be opened for writing or reading, and also supports opening directly from a file descriptor. ```cpp #include #include // --- Write a GZIP file --- QuaGzipFile writer("data.gz"); if (writer.open(QIODevice::WriteOnly)) { QTextStream out(&writer); out << "Hello, GZIP!\n"; out << "Line 2\n"; writer.flush(); writer.close(); } // --- Read a GZIP file --- QuaGzipFile reader("data.gz"); if (reader.open(QIODevice::ReadOnly)) { QTextStream in(&reader); while (!in.atEnd()) qDebug() << in.readLine(); reader.close(); } // --- Open by file descriptor --- int fd = ::open("data.gz", O_RDONLY); QuaGzipFile fdReader; if (fdReader.open(fd, QIODevice::ReadOnly)) { QByteArray raw = fdReader.readAll(); fdReader.close(); } ``` -------------------------------- ### Find Qt5 Components Source: https://github.com/stachenov/quazip/blob/master/CMakeLists.txt Finds required and optional components for Qt5, including Core, Network, and Test. Sets library targets and pkg-config requirements. ```cmake find_package(Qt5 REQUIRED COMPONENTS Core OPTIONAL_COMPONENTS Network Test) message(STATUS "Found Qt version ${Qt5_VERSION} at ${Qt5_DIR}") set(QUAZIP_QT_ZLIB_COMPONENT Zlib) set(QUAZIP_LIB_LIBRARIES Qt5::Core) set(QUAZIP_TEST_QT_LIBRARIES Qt5::Core Qt5::Network Qt5::Test) set(QUAZIP_PKGCONFIG_REQUIRES "zlib, Qt5Core") ``` -------------------------------- ### Compress File with Fluent API Options Source: https://github.com/stachenov/quazip/blob/master/EXAMPLES.md Chain multiple options using the fluent API (QuaCompress) for compressing a file, including UTF-8 filename support, best compression strategy, and password encryption. ```cpp #include // Chain multiple options ok = QuaCompress() .withUtf8Enabled() .withStrategy(QuaCompress::Best) .withPassword(QByteArray("password123")) .compressFile("archive.zip", "document.txt"); ``` -------------------------------- ### Configure Qt6 Core5Compat for QTextCodec Source: https://github.com/stachenov/quazip/blob/master/CMakeLists.txt This section checks if Qt6 is available and if the Core5Compat module is found. If both are true, it enables QTextCodec support by adding the necessary library and compile definitions. ```cmake if(QUAZIP_ENABLE_QTEXTCODEC) if(${QT_VERSION} VERSION_GREATER_EQUAL 6.0.0) find_package(Qt6 OPTIONAL_COMPONENTS Core5Compat) if(Qt6Core5Compat_FOUND) set(QUAZIP_LIB_LIBRARIES ${QUAZIP_LIB_LIBRARIES} Qt6::Core5Compat) set(QUAZIP_TEST_QT_LIBRARIES ${QUAZIP_TEST_QT_LIBRARIES} Qt6::Core5Compat) add_compile_definitions(QUAZIP_CAN_USE_QTEXTCODEC) message("-- Quazip can use QTextCodec") else() message("-- Core5Compat not found, QTextCodec support disabled") endif() else() add_compile_definitions(QUAZIP_CAN_USE_QTEXTCODEC) message("-- Quazip can use QTextCodec") endif() else() message("-- QTextCodec explicitly disabled by QUAZIP_ENABLE_QTEXTCODEC=OFF") endif() ``` -------------------------------- ### Compress Directory with Fluent API Filters Source: https://github.com/stachenov/quazip/blob/master/EXAMPLES.md Compress a directory using the fluent API (QuaCompress) with custom filters to include only files and exclude directory entries like '.' and '..'. Supports UTF-8 and custom compression strategy. ```cpp #include // Compress directory with custom filters ok = QuaCompress() .withUtf8Enabled() .withStrategy(QuaCompress::Better) .compressDir("archive.zip", "my_folder", true, QDir::Files | QDir::NoDotAndDotDot); ``` -------------------------------- ### Extract Directory with Password using Fluent API Source: https://github.com/stachenov/quazip/blob/master/EXAMPLES.md Extract all contents of an encrypted ZIP archive into a specified output directory using the fluent API (QuaExtract) by providing the password. Returns a list of extracted file names. ```cpp #include // Extract with password files = QuaExtract() .withPassword(QByteArray("password123")) .extractDir("archive.zip", "output_dir"); ``` -------------------------------- ### Compress Files and Directories with QuaCompress Source: https://context7.com/stachenov/quazip/llms.txt Use QuaCompress for fluent, chainable compression operations. Supports various compression strategies, UTF-8 filenames, custom timestamps, passwords, and directory compression with exclusions. Ensure UTF-8 encoding is consistent if adding to existing archives. ```cpp #include // Compress a single file with best compression and UTF-8 filenames bool ok = QuaCompress() .withUtf8Enabled() .withStrategy(QuaCompress::Best) .compressFile("archive.zip", "document.txt"); // Compress multiple files with a fixed timestamp (reproducible builds) ok = QuaCompress() .withUtf8Enabled() .withDateTime(QDateTime(QDate(2024, 6, 1), QTime(0,0,0), Qt::UTC)) .withStrategy(QuaCompress::Standard) .compressFiles("bundle.zip", {"a.cpp", "b.cpp", "CMakeLists.txt"}); // Compress a directory, excluding hidden files, with encryption ok = QuaCompress() .withUtf8Enabled() .withStrategy(QuaCompress::Better) .withPassword("p4ssw0rd") .compressDir("release.zip", "dist/", /*recursive=*/true, QDir::Files | QDir::NoDotAndDotDot); // Add files to an existing archive ok = QuaCompress() .withUtf8Enabled() // must match the archive's original encoding .addFile("existing.zip", "patch.txt"); ok = QuaCompress() .withUtf8Enabled() .addDir("existing.zip", "extra/", true); ``` -------------------------------- ### QuaExtract - Fluent builder API for extraction Source: https://context7.com/stachenov/quazip/llms.txt QuaExtract provides the extraction counterpart to QuaCompress, supporting file path extraction, full archive extraction, QIODevice sources, and optional codec override for legacy encodings. ```APIDOC ## QuaExtract — Fluent builder API for extraction `QuaExtract` provides the extraction counterpart to `QuaCompress`, supporting file path extraction, full archive extraction, QIODevice sources, and optional codec override for legacy encodings. ```cpp #include // Extract entire archive QStringList files = QuaExtract() .extractDir("archive.zip", "output/"); debug() << "Extracted:" << files; // Extract with password files = QuaExtract() .withPassword("p4ssw0rd") .extractDir("secret.zip", "output/"); // Extract a single named file QString path = QuaExtract() .extractFile("archive.zip", "docs/README.md", "out/README.md"); // Extract a subset of files files = QuaExtract() .extractFiles("archive.zip", {"src/main.cpp", "CMakeLists.txt"}, "workspace/"); // Extract from a QBuffer (e.g. a downloaded ZIP in memory) QBuffer buf(&zipBytes); files = QuaExtract() .withPassword("secret") .extractDir(&buf, "output/"); // Extract archive with a legacy DOS/Windows filename codec QuazipTextCodec *ibm866 = QuazipTextCodec::codecForName("IBM866"); files = QuaExtract() .extractDir("legacy_windows.zip", ibm866, "output/"); // Just list contents without extracting QStringList names = QuaExtract().getFileList("archive.zip"); ``` ``` -------------------------------- ### Simple Compression with Fluent API Source: https://github.com/stachenov/quazip/blob/master/EXAMPLES.md Use the fluent API (QuaCompress) for a readable, type-safe way to compress a single file. This method chains operations for clarity. ```cpp #include // Simple compression with fluent API bool ok = QuaCompress() .compressFile("archive.zip", "document.txt"); ``` -------------------------------- ### Set Default OS Code for New Entries Source: https://context7.com/stachenov/quazip/llms.txt Define the default operating system code for new archive entries. Use 3 for Unix-like systems and 0 for DOS/Windows. ```cpp // --- OS code for new entries (3 = Unix, 0 = DOS/Windows) --- QuaZip::setDefaultOsCode(3); ``` -------------------------------- ### Extract Directory with Fluent API Source: https://github.com/stachenov/quazip/blob/master/EXAMPLES.md Use the fluent API (QuaExtract) to extract all contents of a ZIP archive into a specified output directory. This method returns a list of extracted file names. ```cpp #include // Simple extraction QStringList files = QuaExtract() .extractDir("archive.zip", "output_dir"); ``` -------------------------------- ### Include QuaZip Subdirectory Source: https://github.com/stachenov/quazip/blob/master/CMakeLists.txt This command includes the main QuaZip source directory into the build, typically containing the core library implementation. ```cmake add_subdirectory(quazip) ``` -------------------------------- ### Handle Unsupported Qt Versions Source: https://github.com/stachenov/quazip/blob/master/CMakeLists.txt Generates a fatal error if an unsupported Qt major version is detected. ```cmake else() message(FATAL_ERROR "Qt version ${QUAZIP_QT_MAJOR_VERSION} is not supported") endif() ``` -------------------------------- ### QuaGzipFile - Read and write GZIP files via QIODevice Source: https://context7.com/stachenov/quazip/llms.txt QuaGzipFile wraps the zlib GZIP API as a QIODevice, enabling GZIP-compressed streams to be used with QTextStream, QDataStream, or any other Qt I/O consumer. ```APIDOC ## QuaGzipFile — Read and write GZIP files via QIODevice `QuaGzipFile` wraps the zlib GZIP API as a `QIODevice`, enabling GZIP-compressed streams to be used with `QTextStream`, `QDataStream`, or any other Qt I/O consumer. ```cpp #include #include // --- Write a GZIP file --- QuaGzipFile writer("data.gz"); if (writer.open(QIODevice::WriteOnly)) { QTextStream out(&writer); out << "Hello, GZIP!\n"; out << "Line 2\n"; writer.flush(); writer.close(); } // --- Read a GZIP file --- QuaGzipFile reader("data.gz"); if (reader.open(QIODevice::ReadOnly)) { QTextStream in(&reader); while (!in.atEnd()) debug() << in.readLine(); reader.close(); } // --- Open by file descriptor --- int fd = ::open("data.gz", O_RDONLY); QuaGzipFile fdReader; if (fdReader.open(fd, QIODevice::ReadOnly)) { QByteArray raw = fdReader.readAll(); fdReader.close(); } ``` ``` -------------------------------- ### Enable QuaZip Tests Source: https://github.com/stachenov/quazip/blob/master/CMakeLists.txt This snippet enables CMake's testing infrastructure and includes the subdirectory containing the QuaZip unit tests if the QUAZIP_ENABLE_TESTS option is set. ```cmake if(QUAZIP_ENABLE_TESTS) message(STATUS "Building QuaZip tests") enable_testing() add_subdirectory(qztest) endif() ``` -------------------------------- ### QuaCompress - Fluent builder API for compression Source: https://context7.com/stachenov/quazip/llms.txt QuaCompress provides a chainable builder interface over JlCompress compression operations. Separate classes for compression and extraction prevent accidentally mixing options. ```APIDOC ## QuaCompress — Fluent builder API for compression `QuaCompress` provides a chainable builder interface over `JlCompress` compression operations. Separate classes for compression and extraction prevent accidentally mixing options. ```cpp #include // Compress a single file with best compression and UTF-8 filenames bool ok = QuaCompress() .withUtf8Enabled() .withStrategy(QuaCompress::Best) .compressFile("archive.zip", "document.txt"); // Compress multiple files with a fixed timestamp (reproducible builds) ok = QuaCompress() .withUtf8Enabled() .withDateTime(QDateTime(QDate(2024, 6, 1), QTime(0,0,0), Qt::UTC)) .withStrategy(QuaCompress::Standard) .compressFiles("bundle.zip", {"a.cpp", "b.cpp", "CMakeLists.txt"}); // Compress a directory, excluding hidden files, with encryption ok = QuaCompress() .withUtf8Enabled() .withStrategy(QuaCompress::Better) .withPassword("p4ssw0rd") .compressDir("release.zip", "dist/", /*recursive=*/true, QDir::Files | QDir::NoDotAndDotDot); // Add files to an existing archive ok = QuaCompress() .withUtf8Enabled() // must match the archive's original encoding .addFile("existing.zip", "patch.txt"); ok = QuaCompress() .withUtf8Enabled() .addDir("existing.zip", "extra/", true); ``` ``` -------------------------------- ### QuaZipNewInfo Source: https://context7.com/stachenov/quazip/llms.txt QuaZipNewInfo carries the name, timestamp, permissions, comment, and extra fields for a file being written to a ZIP archive. It allows copying metadata from existing files or overriding it with custom values. ```APIDOC ## QuaZipNewInfo — Metadata for new ZIP entries `QuaZipNewInfo` carries the name, timestamp, permissions, comment, and extra fields for a file being written. The two-argument constructor (`name`, `file`) copies timestamp and permissions directly from a file on disk. ```cpp #include #include // Copy metadata from the real file QuaZipNewInfo info("subdir/image.png", "/home/user/Pictures/image.png"); // Override timestamp to a fixed value (useful for reproducible builds) info.dateTime = QDateTime(QDate(2024, 1, 1), QTime(0, 0, 0), Qt::UTC); // Set Unix permissions explicitly info.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup | QFile::ReadOther); // Add NTFS high-precision timestamps to the extra field info.setFileNTFSTimes("/home/user/Pictures/image.png"); // Add a per-entry comment info.comment = "Profile picture"; QuaZipFile entry(&zip); // zip already opened in mdCreate/mdAdd entry.open(QIODevice::WriteOnly, info); entry.write(imageData); entry.close(); ``` ``` -------------------------------- ### Handle BZIP2 Dependency Source: https://github.com/stachenov/quazip/blob/master/CMakeLists.txt Checks for and configures BZIP2 support if QUAZIP_BZIP2 is enabled. It attempts to find BZIP2 and sets include directories, libraries, and library directories accordingly. ```cmake if(QUAZIP_BZIP2) # Check if bzip2 is present set(QUAZIP_BZIP2 ON) if(NOT QUAZIP_FORCE_FETCH_LIBS) find_package(BZip2 QUIET) endif() if(BZIP2_FOUND AND NOT QUAZIP_FORCE_FETCH_LIBS) message(STATUS "Using BZIP2 ${BZIP2_VERSION_STRING}") list(APPEND QUAZIP_INC ${BZIP2_INCLUDE_DIRS}) list(APPEND QUAZIP_LIB ${BZIP2_LIBRARIES}) list(APPEND QUAZIP_LBD ${BZIP2_LIBRARY_DIRS}) set(PC_PRIVATE_LIBS "${PC_PRIVATE_LIBS} -lbzip2") set(QUAZIP_BZIP2_BUILD FALSE) ``` -------------------------------- ### QuaZipDir Source: https://context7.com/stachenov/quazip/llms.txt QuaZipDir mirrors the QDir API to let you browse a ZIP archive by directory. Navigation, existence checks, and entry listing all work relative to the current path within the archive. ```APIDOC ## QuaZipDir — Navigate archive contents like QDir `QuaZipDir` mirrors the `QDir` API to let you browse a ZIP archive by directory. The root is an empty string (not `/`). Navigation, existence checks, and entry listing all work relative to the current path. ```cpp #include #include QuaZip zip("project.zip"); zip.open(QuaZip::mdUnzip); QuaZipDir dir(&zip); // start at archive root // List top-level entries QStringList entries = dir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot, QDir::Name); qDebug() << "Root:" << entries; // e.g. ["docs/", "src/", "README.md"] // Navigate into a subdirectory if (dir.cd("src")) { qDebug() << "In src/ :" << dir.entryList(QDir::Files); dir.cdUp(); } // Check existence qDebug() << dir.exists("README.md"); // true qDebug() << dir.exists("missing.txt"); // false // Filtered listing with zip64 info dir.cd("docs"); QList docInfos = dir.entryInfoList64({"*.pdf", "*.md"}, QDir::Files, QDir::Name); for (const auto &fi : docInfos) qDebug() << fi.name << fi.uncompressedSize; zip.close(); ``` ``` -------------------------------- ### Add QuaZip Sources Directly with CMake Source: https://github.com/stachenov/quazip/blob/master/quazip/doc/usage.dox If you are adding QuaZip sources directly to your project, use this CMake configuration. It's recommended to set BUILD_SHARED_LIBS to NO for static linking. ```cmake add_subdirectory(quazip) target_link_libraries(whatever-your-target-is QuaZip::QuaZip) ``` -------------------------------- ### Log Final Qt Version Source: https://github.com/stachenov/quazip/blob/master/CMakeLists.txt Logs the final determined Qt major version being used for the build. ```cmake message(STATUS "Using Qt version ${QUAZIP_QT_MAJOR_VERSION}") ``` -------------------------------- ### Conditional Qt ZLIB Component Handling Source: https://github.com/stachenov/quazip/blob/master/CMakeLists.txt Conditionally finds and uses Qt's bundled ZLIB component if QUAZIP_USE_QT_ZLIB is enabled. Includes error handling for missing components. ```cmake set(QUAZIP_QT_ZLIB_USED OFF) if(QUAZIP_USE_QT_ZLIB) find_package(Qt${QUAZIP_QT_MAJOR_VERSION} OPTIONAL_COMPONENTS ${QUAZIP_QT_ZLIB_COMPONENT}) set(QUAZIP_QT_ZLIB_COMPONENT_FOUND Qt${QUAZIP_QT_MAJOR_VERSION}${QUAZIP_QT_ZLIB_COMPONENT}_FOUND) if (DEFINED QUAZIP_QT_ZLIB_HEADER_COMPONENT) find_package(Qt${QUAZIP_QT_MAJOR_VERSION} OPTIONAL_COMPONENTS ${QUAZIP_QT_ZLIB_HEADER_COMPONENT}) set(QUAZIP_QT_ZLIB_HEADER_COMPONENT_FOUND Qt${QUAZIP_QT_MAJOR_VERSION}${QUAZIP_QT_ZLIB_HEADER_COMPONENT}_FOUND) else() set(QUAZIP_QT_ZLIB_HEADER_COMPONENT_FOUND ON) endif() if(QUAZIP_QT_ZLIB_COMPONENT_FOUND AND QUAZIP_QT_ZLIB_HEADER_COMPONENT_FOUND) message(STATUS "Qt component ${QUAZIP_QT_ZLIB_COMPONENT} found") set(QUAZIP_LIB_LIBRARIES ${QUAZIP_LIB_LIBRARIES} Qt${QUAZIP_QT_MAJOR_VERSION}::${QUAZIP_QT_ZLIB_COMPONENT}) if(DEFINED QUAZIP_QT_ZLIB_HEADER_COMPONENT) message(STATUS "Qt component ${QUAZIP_QT_ZLIB_HEADER_COMPONENT} found") set(QUAZIP_LIB_LIBRARIES ${QUAZIP_LIB_LIBRARIES} Qt${QUAZIP_QT_MAJOR_VERSION}::${QUAZIP_QT_ZLIB_HEADER_COMPONENT}) endif() set(QUAZIP_QT_ZLIB_USED ON) else() message(FATAL_ERROR "QUAZIP_USE_QT_ZLIB was set but bundled zlib was not found. Terminating to prevent accidental linking to system libraries.") endif() endif() ``` -------------------------------- ### Set Default Filename Codec Source: https://context7.com/stachenov/quazip/llms.txt Set the default filename codec for all subsequent QuaZip instances. Use this sparingly, especially in library code. ```cpp // --- Process-wide default codec (use sparingly in libraries) --- QuaZip::setDefaultFileNameCodec("UTF-8"); ```