Allow extract_zip to work recursively

This commit is contained in:
Benau 2019-07-21 16:37:11 +08:00
parent 35168d2ecb
commit ed011bed4c
2 changed files with 20 additions and 13 deletions

View File

@ -55,13 +55,13 @@ s32 IFileSystem_copyFileToFile(IWriteFile* dst, IReadFile* src)
* \param to The destination directory. * \param to The destination directory.
* \return True if successful. * \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 //Add the zip to the file system
IFileSystem *file_system = irr_driver->getDevice()->getFileSystem(); IFileSystem *file_system = irr_driver->getDevice()->getFileSystem();
if(!file_system->addFileArchive(from.c_str(), if(!file_system->addFileArchive(from.c_str(),
/*ignoreCase*/false, /*ignoreCase*/false,
/*ignorePath*/true, io::EFAT_ZIP)) /*ignorePath*/!recursive, io::EFAT_ZIP))
{ {
return false; return false;
} }
@ -75,27 +75,34 @@ bool extract_zip(const std::string &from, const std::string &to)
bool error = false; bool error = false;
for(unsigned int i=0; i<zip_file_list->getFileCount(); i++) for(unsigned int i=0; i<zip_file_list->getFileCount(); 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(zip_file_list->isDirectory(i)) continue;
if(current_file[0]=='.') continue; if(zip_file_list->getFileName(i)[0]=='.') continue;
const std::string base = StringUtils::getBasename(current_file); 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 = IReadFile* src_file =
zip_archive->createAndOpenFile(current_file.c_str()); zip_archive->createAndOpenFile(base.c_str());
if(!src_file) 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; error = true;
continue; continue;
} }
std::string file_location = to + "/" + base;
if (recursive)
{
const std::string& dir = StringUtils::getPath(file_location);
file_manager->checkAndCreateDirectoryP(dir);
}
IWriteFile* dst_file = IWriteFile* dst_file =
file_system->createAndWriteFile((to+"/"+base).c_str()); file_system->createAndWriteFile(file_location.c_str());
if(dst_file == NULL) 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.", 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());
(to+"/"+current_file).c_str());
error = true; error = true;
continue; continue;
} }
@ -103,7 +110,7 @@ bool extract_zip(const std::string &from, const std::string &to)
if (IFileSystem_copyFileToFile(dst_file, src_file) < 0) 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.", 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; error = true;
} }
dst_file->drop(); dst_file->drop();

View File

@ -22,6 +22,6 @@
* Extract a zip. * Extract a zip.
* \ingroup addonsgroup * \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 #endif