From 01d319ba2d7f909a24286f932d4cac05761133d9 Mon Sep 17 00:00:00 2001 From: Alan Garny Date: Tue, 4 Jun 2024 12:27:52 +1200 Subject: [PATCH] Added extractEntryToMemory(). --- src/combine/combinearchive.cpp | 37 ++++++++++++++++++++++++++++++---- src/combine/combinearchive.h | 18 +++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/combine/combinearchive.cpp b/src/combine/combinearchive.cpp index 773b314..b2f3808 100644 --- a/src/combine/combinearchive.cpp +++ b/src/combine/combinearchive.cpp @@ -310,9 +310,8 @@ bool CombineArchive::writeToFile(const std::string &fileName) return true; } -bool -CombineArchive::getStream(const std::string &name, - std::ifstream &stream) +std::map::iterator +CombineArchive::mapIterator(const std::string &name) { std::map::iterator it = mMap.find(name); if (it == mMap.end()) @@ -324,9 +323,19 @@ CombineArchive::getStream(const std::string &name, if (name.find("/") == 0) it = mMap.find(name.substr(1)); if (it == mMap.end()) - return false; + return mMap.end(); } } + return it; +} + +bool +CombineArchive::getStream(const std::string &name, + std::ifstream &stream) +{ + auto it = mapIterator(name); + if (it == mMap.end()) + return false; std::string filename = (*it).second; if (filename.find("unzipper://") == 0) @@ -576,3 +585,23 @@ CombineArchive::extractEntryToString(const std::string& name) extractEntryToStream(name, stream); return stream.str(); } + +std::vector +CombineArchive::extractEntryToBuffer(const std::string& name) +{ + auto it = mapIterator(name); + if (it == mMap.end()) + return {}; + + std::string filename = (*it).second; + if (filename.find("unzipper://") == 0) + filename = filename.substr(std::string("unzipper://").length()); + + if (mpUnzipper == NULL) + return {}; + + std::vector res; + mpUnzipper->extractEntryToMemory(filename, res); + + return res; +} diff --git a/src/combine/combinearchive.h b/src/combine/combinearchive.h index fffccde..dbfee93 100644 --- a/src/combine/combinearchive.h +++ b/src/combine/combinearchive.h @@ -165,6 +165,15 @@ class LIBCOMBINE_EXTERN CombineArchive */ std::string extractEntryToString(const std::string& name); + /** + * extracts the given entry and returns the contents as a buffer. + * + * @param name the entry to be extracted + * + * @return the content of the entry, or empty if not found. + */ + std::vector extractEntryToBuffer(const std::string& name); + /** * extracts all entries in this archive into the given directory. * @@ -402,6 +411,15 @@ class LIBCOMBINE_EXTERN CombineArchive * */ int addMetadataToArchive(OmexDescription& desc, zipper::Zipper* zipper); + + /** + * returns the map iterator for the given file. + * + * @param name the name that should be in the current map of files + * + * @return the map iterator + */ + std::map::iterator mapIterator(const std::string &name); }; LIBCOMBINE_CPP_NAMESPACE_END