From ed011bed4cc555a8ab0c7bfee6bb46e08ae0fd0a Mon Sep 17 00:00:00 2001 From: Benau Date: Sun, 21 Jul 2019 16:37:11 +0800 Subject: [PATCH] Allow extract_zip to work recursively --- src/addons/zip.cpp | 31 +++++++++++++++++++------------ src/addons/zip.hpp | 2 +- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/addons/zip.cpp b/src/addons/zip.cpp index 96061ffff..d12ab3677 100644 --- a/src/addons/zip.cpp +++ b/src/addons/zip.cpp @@ -55,13 +55,13 @@ s32 IFileSystem_copyFileToFile(IWriteFile* dst, IReadFile* src) * \param to The destination directory. * \return True if successful. */ -bool extract_zip(const std::string &from, const std::string &to) +bool extract_zip(const std::string &from, const std::string &to, bool recursive) { //Add the zip to the file system IFileSystem *file_system = irr_driver->getDevice()->getFileSystem(); if(!file_system->addFileArchive(from.c_str(), /*ignoreCase*/false, - /*ignorePath*/true, io::EFAT_ZIP)) + /*ignorePath*/!recursive, io::EFAT_ZIP)) { return false; } @@ -75,27 +75,34 @@ bool extract_zip(const std::string &from, const std::string &to) bool error = false; for(unsigned int i=0; igetFileCount(); i++) { - const std::string current_file=zip_file_list->getFileName(i).c_str(); - Log::info("addons", "Unzipping file '%s'.", current_file.c_str()); if(zip_file_list->isDirectory(i)) continue; - if(current_file[0]=='.') continue; - const std::string base = StringUtils::getBasename(current_file); + if(zip_file_list->getFileName(i)[0]=='.') continue; + std::string base = zip_file_list->getFullFileName(i).c_str(); + if (!recursive) + base = StringUtils::getBasename(base); + + Log::info("addons", "Unzipping file '%s'.", base.c_str()); IReadFile* src_file = - zip_archive->createAndOpenFile(current_file.c_str()); + zip_archive->createAndOpenFile(base.c_str()); if(!src_file) { - Log::warn("addons", "Can't read file '%s'. This is ignored, but the addon might not work", current_file.c_str()); + Log::warn("addons", "Can't read file '%s'. This is ignored, but the addon might not work", base.c_str()); error = true; continue; } + std::string file_location = to + "/" + base; + if (recursive) + { + const std::string& dir = StringUtils::getPath(file_location); + file_manager->checkAndCreateDirectoryP(dir); + } IWriteFile* dst_file = - file_system->createAndWriteFile((to+"/"+base).c_str()); + file_system->createAndWriteFile(file_location.c_str()); if(dst_file == NULL) { - Log::warn("addons", "Couldn't create the file '%s'. The directory might not exist. This is ignored, but the addon might not work.", - (to+"/"+current_file).c_str()); + Log::warn("addons", "Couldn't create the file '%s'. The directory might not exist. This is ignored, but the addon might not work.", file_location.c_str()); error = true; continue; } @@ -103,7 +110,7 @@ bool extract_zip(const std::string &from, const std::string &to) if (IFileSystem_copyFileToFile(dst_file, src_file) < 0) { Log::warn("addons", "Could not copy '%s' from archive '%s'. This is ignored, but the addon might not work.", - current_file.c_str(), from.c_str()); + base.c_str(), from.c_str()); error = true; } dst_file->drop(); diff --git a/src/addons/zip.hpp b/src/addons/zip.hpp index dd2c20585..95143d917 100644 --- a/src/addons/zip.hpp +++ b/src/addons/zip.hpp @@ -22,6 +22,6 @@ * Extract a zip. * \ingroup addonsgroup */ -bool extract_zip(const std::string &from, const std::string &to); +bool extract_zip(const std::string &from, const std::string &to, bool recursive = false); #endif