From 641bd7e140dfa4490f1d8ff880b3537390df5cac Mon Sep 17 00:00:00 2001 From: CompSciOrBust <54033033+CompSciOrBust@users.noreply.github.com> Date: Sun, 9 Feb 2020 23:38:53 +0000 Subject: [PATCH] Added zip support --- .gitmodules | 2 +- Makefile | 2 +- include/ExplorerUI.h | 4 +++- include/Utils.h | 3 ++- source/ExplorerUI.cpp | 11 ++++++++++- source/Utils.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ source/main.cpp | 1 + 7 files changed, 61 insertions(+), 5 deletions(-) diff --git a/.gitmodules b/.gitmodules index bb99122..72420da 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "SimpleIniParser"] path = SimpleIniParser - url = https://github.com/AtlasNX/SimpleIniParser + url = https://github.com/AtlasNX/SimpleIniParser \ No newline at end of file diff --git a/Makefile b/Makefile index 5b8126d..c83de9c 100644 --- a/Makefile +++ b/Makefile @@ -62,7 +62,7 @@ ASFLAGS := -g $(ARCH) LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) #Stolen from https://github.com/devolution2409/NX-input-recorder/blob/master/Makefile#L63 because SDL_Mixer is hard -LIBS := -lfreetype -lSDL2_mixer -lopusfile -lopus -lmodplug -lmpg123 -lvorbisidec -logg -lSDL2_ttf -lSDL2_gfx -lSDL2_image -lSDL2 -lEGL -lGLESv2 -lglapi -ldrm_nouveau -lwebp -lpng -ljpeg `sdl2-config --libs` `freetype-config --libs` -lnx -lSimpleIniParser +LIBS := -lfreetype -lSDL2_mixer -lopusfile -lopus -lmodplug -lmpg123 -lvorbisidec -logg -lSDL2_ttf -lSDL2_gfx -lSDL2_image -lSDL2 -lEGL -lGLESv2 -lglapi -ldrm_nouveau -lwebp -lpng -ljpeg `sdl2-config --libs` `freetype-config --libs` -lnx -lSimpleIniParser -lminizip -lz #--------------------------------------------------------------------------------- # list of directories containing libraries, this must be the top level containing diff --git a/include/ExplorerUI.h b/include/ExplorerUI.h index b2e755e..42e7dae 100644 --- a/include/ExplorerUI.h +++ b/include/ExplorerUI.h @@ -5,6 +5,7 @@ #include #include #include + class ExplorerUI : public UIWindow { private: @@ -28,6 +29,7 @@ class ExplorerUI : public UIWindow std::string DirPath = "mount:/"; std::string CurrentMount; std::string ClipBoardMount; + std::string *LongOpMessagePtr; int FileSortMode = 0; int HeaderColour_R = 94; int HeaderColour_G = 94; @@ -62,7 +64,6 @@ class MenuUI : public UIWindow //vars std::string ClipboardPath = ""; std::string ClipboardFileName = ""; - std::string LongOpMessage = ""; //functions void RecFileCopy(); public: @@ -78,6 +79,7 @@ class MenuUI : public UIWindow int LongOpMessageTextColour_R = 255; int LongOpMessageTextColour_G = 255; int LongOpMessageTextColour_B = 255; + std::string LongOpMessage = "Unset"; //Functions MenuUI(); void GetInput(); diff --git a/include/Utils.h b/include/Utils.h index 4e86e0f..46fba00 100644 --- a/include/Utils.h +++ b/include/Utils.h @@ -12,4 +12,5 @@ std::string GetKeyboardInput(std::string, std::string, std::string); std::string GetFileSize(std::string); void RecursiveFileCopy(std::string, std::string, std::string); std::string GetFileExtension(std::string); -bool GetParentalControl(void); \ No newline at end of file +bool GetParentalControl(void); +void UnzipFile(std::string, std::string); \ No newline at end of file diff --git a/source/ExplorerUI.cpp b/source/ExplorerUI.cpp index d1077bb..36f1962 100644 --- a/source/ExplorerUI.cpp +++ b/source/ExplorerUI.cpp @@ -35,6 +35,7 @@ class ExplorerUI : public UIWindow std::string DirPath = "mount:/"; std::string CurrentMount; std::string ClipBoardMount; + std::string *LongOpMessagePtr; int FileSortMode = 0; int HeaderColour_R = 94; int HeaderColour_G = 94; @@ -366,6 +367,14 @@ void ExplorerUI::OpenFile(string Path) //*WindowState = 4; *ChosenFile = Path; } + else if(FileSuffix == "zip") + { + *WindowState = 4; + *LongOpMessagePtr = "Unzipping " + FileNameList->ListingTextVec.at(FileNameList->SelectedIndex) + ". please wait!"; + UnzipFile(Path, DirPath); + *WindowState = 0; + LoadListDirs(DirPath); + } } std::vector ExplorerUI::GetSaveDataMounts() @@ -437,7 +446,6 @@ class MenuUI : public UIWindow //vars std::string ClipboardPath = ""; std::string ClipboardFileName = ""; - std::string LongOpMessage = ""; //functions void RecFileCopy(); public: @@ -453,6 +461,7 @@ class MenuUI : public UIWindow int LongOpMessageTextColour_R = 255; int LongOpMessageTextColour_G = 255; int LongOpMessageTextColour_B = 255; + std::string LongOpMessage = "Unset"; //Functions MenuUI(); void GetInput(); diff --git a/source/Utils.cpp b/source/Utils.cpp index fee7351..073c612 100644 --- a/source/Utils.cpp +++ b/source/Utils.cpp @@ -7,6 +7,7 @@ #include #include #include +#include std::vector SortFiles(std::string Path, std::vector FilesVec, int SortMode) { @@ -234,4 +235,46 @@ bool GetParentalControl() { return true; } +} + +//Based on https://github.com/ITotalJustice/atmosphere-updater/blob/master/source/unzip.c +void UnzipFile(std::string FileLocation, std::string LocationToUnzipTo) +{ + unzFile ZipFile = unzOpen(FileLocation.c_str()); + //Get info about the zip + unz_global_info ZipInfo; + unzGetGlobalInfo(ZipFile, &ZipInfo); + //Loop through every file + for(int i = 0; i < ZipInfo.number_entry; i++) + { + char FileNameInZip[256]; + unz_file_info FileInfo; + unzOpenCurrentFile(ZipFile); + unzGetCurrentFileInfo(ZipFile, &FileInfo, FileNameInZip, sizeof(FileNameInZip), NULL, 0, NULL, 0); + //Check if a dir + if ((FileNameInZip[strlen(FileNameInZip) - 1]) == '/') + { + std::string DirPa = LocationToUnzipTo.c_str(); + DirPa += FileNameInZip; + mkdir(DirPa.c_str(), 0); + } + else + { + const char *write_filename = FileNameInZip; + void *buf = malloc(500000); + FILE *outfile; + std::string FilePathOfFile = LocationToUnzipTo.c_str(); + FilePathOfFile += write_filename; + outfile = fopen(FilePathOfFile.c_str(), "wb"); + for (int j = unzReadCurrentFile(ZipFile, buf, 500000); j > 0; j = unzReadCurrentFile(ZipFile, buf, 500000)) + { + fwrite(buf, 1, j, outfile); + } + fclose(outfile); + free(buf); + } + unzCloseCurrentFile(ZipFile); + unzGoToNextFile(ZipFile); + } + unzClose(ZipFile); } \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index d49ae44..f2a613c 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -104,6 +104,7 @@ int main(int argc, char* argv[]) Menu->WindowState = WindowStatePtr; Menu->MenuList->Renderer = Renderer; Menu->Explorer = Explorer; + Explorer->LongOpMessagePtr = &Menu->LongOpMessage; //Init text editor TextUI *TextEditor = new TextUI();