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:
- Include Necessary Libraries: The code begins by including the necessary headers. The
miniz.h
library is essential for handling zip files. - Function Definition: The
extractZip
function takes two parameters: the path to the zip file and the output directory where the files will be extracted. - Initialize Zip Archive: A
mz_zip_archive
structure is initialized to manage the zip file. Thememset
function is used to clear the structure. - 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. - File Count: The number of files in the zip archive is retrieved using
mz_zip_reader_get_num_files
. - 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.
- The file’s statistics are obtained using
- Close Zip Archive: Finally, the zip archive is closed using
mz_zip_reader_end
. - Main Function: The
main
function sets the paths for the zip file and output directory, then calls theextractZip
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… 🙂
I’ve been designing web applications—on and off—since 2001, back when animated GIFs were all the rage and ‘responsive design’ meant answering your client’s emails. Over the past 14 years, I’ve kept pace with the ever-evolving trends in PHP development, successfully delivering a variety of projects that made my clients happy (and kept me caffeinated).
This website serves as my soapbox—a place to share the insights I’ve picked up along the way with anyone curious enough to dive in. Welcome aboard!
Need some custom work done? Or, just want to reach out? Email: dan@danoriordan.com