Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract all files from zip without knowing their names. #38

Closed
GoogleCodeExporter opened this issue May 21, 2015 · 1 comment
Closed

Comments

@GoogleCodeExporter
Copy link

 Hi.I wonder if it is possible to extract everything from the .zip without knowing compressed files names.
I mean,this method:

    mz_zip_reader_extract_file_to_file

asks for file names to extract.But if the .zip was created somewhere else I 
have no idea of those names.

Thanks.



Original issue reported on code.google.com by michael....@idomoo.com on 14 Oct 2014 at 3:02

@jkaster
Copy link

jkaster commented Oct 28, 2016

Everything you need is already in miniz. Here's some working code .. not including the custom called functions because those should be self-explanatory.

std::vector<std::string> os::file::unzip(std::string const &zipFile, std::string const &path, std::string const &password)
{
std::vector<std::string> files = {};
mz_zip_archive zip_archive;
memset(&zip_archive, 0, sizeof(zip_archive));

auto status = mz_zip_reader_init_file(&zip_archive, zipFile.c_str(), 0);
if (!status) return files;
int fileCount = (int)mz_zip_reader_get_num_files(&zip_archive);
if (fileCount == 0)
{
    mz_zip_reader_end(&zip_archive);
    return files;
}
mz_zip_archive_file_stat file_stat;
if (!mz_zip_reader_file_stat(&zip_archive, 0, &file_stat)) 
{
    mz_zip_reader_end(&zip_archive);
    return files;
}
// Get root folder
string lastDir = "";
string base = slash_path(get_path(file_stat.m_filename)); // path delim on end

// Get and print information about each file in the archive.
for (int i = 0; i < fileCount; i++)
{
    if (!mz_zip_reader_file_stat(&zip_archive, i, &file_stat)) continue;
    if (mz_zip_reader_is_file_a_directory(&zip_archive, i)) continue; // skip directories for now
    string fileName = relative_path(base, file_stat.m_filename); // make path relative
    string destFile = combine_path(path, fileName); // make full dest path
            auto newDir = get_path(fileName); // get the file's path
    if (newDir != lastDir)
    {
        if (!os::dir::make(combine_path(path, newDir))) // creates the directory
        {

        }
    }

    if (mz_zip_reader_extract_to_file(&zip_archive, i, destFile.c_str(), 0))
    {
        files.emplace_back(destFile);
    }
}

// Close the archive, freeing any resources it was using
mz_zip_reader_end(&zip_archive);
return files;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants