Simple Zip File Extractor in C++

In this post, we will create a simple zip file extractor using C++. The program will utilize the miniz library, which is a lightweight and easy-to-use library for handling zip files. The extractor will read a zip file and extract its contents to a specified directory.

#include <iostream>
#include <string>
#include "miniz.h" // Ensure you have the miniz library included

void extractZip(const std::string& zipFilePath, const std::string& outputDir) {
    mz_zip_archive zipArchive;
    memset(&zipArchive, 0, sizeof(zipArchive));

    // Open the zip file
    if (!mz_zip_reader_init_file(&zipArchive, zipFilePath.c_str(), 0)) {
        std::cerr << "Failed to open zip file: " << zipFilePath << std::endl;
        return;
    }

    // Get the number of files in the zip
    int fileCount = (int)mz_zip_reader_get_num_files(&zipArchive);
    for (int i = 0; i < fileCount; ++i) {
        mz_zip_archive_file_stat fileStat;
        if (!mz_zip_reader_file_stat(&zipArchive, i, &fileStat)) {
            std::cerr << "Failed to get file stat for index: " << i << std::endl;
            continue;
        }

        // Create the output file path
        std::string outputFilePath = outputDir + "/" + fileStat.m_filename;

        // Extract the file
        if (!mz_zip_reader_extract_to_file(&zipArchive, i, outputFilePath.c_str(), 0)) {
            std::cerr << "Failed to extract file: " << fileStat.m_filename << std::endl;
        } else {
            std::cout << "Extracted: " << fileStat.m_filename << std::endl;
        }
    }

    // Close the zip archive
    mz_zip_reader_end(&zipArchive);
}

int main() {
    std::string zipFilePath = "example.zip"; // Path to your zip file
    std::string outputDir = "output"; // Directory to extract files to

    extractZip(zipFilePath, outputDir);
    return 0;
}

Code Explanation

The provided code implements a simple zip file extractor in C++. Here’s a breakdown of how it works:

  1. Include Necessary Libraries: The code begins by including the necessary headers. The miniz.h library is essential for handling zip files.
  2. Function Definition: The extractZip function takes two parameters: the path to the zip file and the output directory where the files will be extracted.
  3. Initialize Zip Archive: A mz_zip_archive structure is initialized to manage the zip file. The memset function is used to clear the structure.
  4. Open Zip File: The mz_zip_reader_init_file function attempts to open the specified zip file. If it fails, an error message is printed, and the function returns.
  5. File Count: The number of files in the zip archive is retrieved using mz_zip_reader_get_num_files.
  6. Iterate Through Files: A loop iterates through each file in the zip archive. For each file:
    • The file’s statistics are obtained using mz_zip_reader_file_stat.
    • The output file path is constructed by appending the file name to the output directory.
    • The file is extracted using mz_zip_reader_extract_to_file. If successful, a message is printed; otherwise, an error message is displayed.
  7. Close Zip Archive: Finally, the zip archive is closed using mz_zip_reader_end.
  8. Main Function: The main function sets the paths for the zip file and output directory, then calls the extractZip function to perform the extraction.

This code provides a straightforward way to extract files from a zip archive, demonstrating the utility of the miniz library in C++.

Happy coding… 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *