Skip to content

Commit

Permalink
Added method to ZipArchive to get list of files in the current archiv…
Browse files Browse the repository at this point in the history
…e, cribbed from richgel999/miniz#38
  • Loading branch information
lordofhyphens committed Sep 2, 2018
1 parent 880e9a5 commit 38b5418
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
26 changes: 26 additions & 0 deletions xs/src/Zip/ZipArchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ZipArchive::ZipArchive (std::string zip_archive_name, char zip_mode) : archive(m
stats = mz_zip_reader_init_file(&archive, zip_name.c_str(), 0);
} else {
std::cout << "Error:: Unknown zip mode" << std::endl;
stats = 0;
}
}

Expand Down Expand Up @@ -61,6 +62,31 @@ ZipArchive::finalize()
return stats;
}

std::vector<std::string>
ZipArchive::list_entries() {
std::vector<std::string> files = {};
stats = 0;
int file_count = (int)mz_zip_reader_get_num_files(&archive);
if (file_count == 0) {
return files;
std::cerr << "Error:: No files in zip archive" << std::endl;
}
mz_zip_archive_file_stat file_stat;
if (!mz_zip_reader_file_stat(&archive, 0, &file_stat))
{
std::cerr << "Error:: No files in zip archive or not a zip archive." << std::endl;
return files;
}

for (int i = 0; i < file_count; i++)
{
if (!mz_zip_reader_file_stat(&archive, i, &file_stat)) continue;
if (mz_zip_reader_is_file_a_directory(&archive, i)) continue; // skip directories for now

files.emplace_back(file_stat.m_filename);
}
}

ZipArchive::~ZipArchive() {
if(!finalized)
this->finalize();
Expand Down
5 changes: 5 additions & 0 deletions xs/src/Zip/ZipArchive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <string>
#include <iostream>
#include <vector>
#include "miniz/miniz.h"

namespace Slic3r {
Expand Down Expand Up @@ -40,6 +41,10 @@ class ZipArchive
/// \return mz_bool 0: failure 1: success.
mz_bool finalize();

/// Get list of file names contained in this archive.
/// On failure, sets stats to 0 and returns an empty vector
std::vector<std::string> list_entries();

~ZipArchive();

private:
Expand Down

0 comments on commit 38b5418

Please sign in to comment.