Skip to content

Commit 13dc85b

Browse files
Add ability to extract contents of zip archive to a file
This can be used to extract nested jar files to a temporary folder.
1 parent 018e3c7 commit 13dc85b

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

jbmc/src/java_bytecode/mz_zip_archive.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,16 @@ std::string mz_zip_archivet::extract(const size_t index)
103103
}
104104
throw std::runtime_error("Could not extract the file");
105105
}
106+
107+
void mz_zip_archivet::extract_to_file(
108+
const size_t index,
109+
const std::string &path)
110+
{
111+
const auto id = static_cast<mz_uint>(index);
112+
if(
113+
mz_zip_reader_extract_to_file(m_state.get(), id, path.c_str(), 0) !=
114+
MZ_TRUE)
115+
{
116+
throw std::runtime_error("Could not extract the file");
117+
}
118+
}

jbmc/src/java_bytecode/mz_zip_archive.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ class mz_zip_archivet final
5151
/// \throw Throws std::runtime_error if file cannot be extracted
5252
/// \return Contents of the file in the archive
5353
std::string extract(size_t index);
54+
/// Write contents of nth file in the archive to a file
55+
/// \param index: id of the file in the archive
56+
/// \param path: path to which to write the contents of the file
57+
/// \throw Throws std::runtime_error if file cannot be written
58+
void extract_to_file(size_t index, const std::string &path);
59+
5460
private:
5561
std::unique_ptr<mz_zip_archive_statet> m_state;
5662
};

0 commit comments

Comments
 (0)