From 2e2ef0cd71bad7fb2ef4a5e5caede3d5cc005bf4 Mon Sep 17 00:00:00 2001 From: Chee Yee Date: Mon, 6 Feb 2023 02:00:58 -0800 Subject: [PATCH] add zip library --- CMakeLists.txt | 1 + source/installer.cpp | 68 +++++++++++++++++++++++++++++++++++++------- source/installer.h | 5 ++-- 3 files changed, 62 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ab9e6b..771452e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,7 @@ target_link_libraries(ezremote_client curl lexbor smb2 + minizip kernel SceShellCoreUtil SceSysmodule diff --git a/source/installer.cpp b/source/installer.cpp index 6e5318c..b3cc28c 100644 --- a/source/installer.cpp +++ b/source/installer.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -96,9 +97,9 @@ namespace INSTALLER s_bgft_initialized = false; } - int InstallRemotePkg(const char *filename, pkg_header *header) + int InstallRemotePkg(const std::string &filename, pkg_header *header) { - std::string full_url = remote_settings->server + std::string(filename); + std::string full_url = remote_settings->server + filename; size_t scheme_pos = full_url.find("://"); size_t root_pos = full_url.find("/", scheme_pos+3); std::string host = full_url.substr(0, root_pos); @@ -188,22 +189,22 @@ namespace INSTALLER return 0; } - int InstallLocalPkg(const char *filename, pkg_header *header, bool remove_after_install) + int InstallLocalPkg(const std::string &filename, pkg_header *header, bool remove_after_install) { int ret; - if (strncmp(filename, "/data/", 6) != 0 && - strncmp(filename, "/user/data/", 11) != 0 && - strncmp(filename, "/mnt/usb", 8) != 0) + if (strncmp(filename.c_str(), "/data/", 6) != 0 && + strncmp(filename.c_str(), "/user/data/", 11) != 0 && + strncmp(filename.c_str(), "/mnt/usb", 8) != 0) return -1; char filepath[1024]; - snprintf(filepath, 1023, "%s", filename); - if (strncmp(filename, "/data/", 6) == 0) - snprintf(filepath, 1023, "/user%s", filename); + snprintf(filepath, 1023, "%s", filename.c_str()); + if (strncmp(filename.c_str(), "/data/", 6) == 0) + snprintf(filepath, 1023, "/user%s", filename.c_str()); char titleId[18]; memset(titleId, 0, sizeof(titleId)); int is_app = -1; - ret = sceAppInstUtilGetTitleIdFromPkg(filename, titleId, &is_app); + ret = sceAppInstUtilGetTitleIdFromPkg(filename.c_str(), titleId, &is_app); if (ret) { return 0; @@ -265,4 +266,51 @@ namespace INSTALLER err: return 0; } + + void Extract(const std::string &file, const std::string &dir) + { + unz_global_info global_info; + unz_file_info file_info; + unzFile zipfile = unzOpen(file.c_str()); + unzGetGlobalInfo(zipfile, &global_info); + unzGoToFirstFile(zipfile); + uint64_t curr_extracted_bytes = 0; + uint64_t curr_file_bytes = 0; + int num_files = global_info.number_entry; + char fname[512]; + char ext_fname[512]; + char read_buffer[8192]; + + for (int zip_idx = 0; zip_idx < num_files; ++zip_idx) + { + unzGetCurrentFileInfo(zipfile, &file_info, fname, 512, NULL, 0, NULL, 0); + sprintf(ext_fname, "%s%s", dir.c_str(), fname); + const size_t filename_length = strlen(ext_fname); + if (ext_fname[filename_length - 1] != '/') + { + snprintf(activity_message, 255, "Extracting %s", fname); + curr_file_bytes = 0; + unzOpenCurrentFile(zipfile); + FS::MkDirs(ext_fname, true); + FILE *f = fopen(ext_fname, "wb"); + while (curr_file_bytes < file_info.uncompressed_size) + { + int rbytes = unzReadCurrentFile(zipfile, read_buffer, 8192); + if (rbytes > 0) + { + fwrite(read_buffer, 1, rbytes, f); + curr_extracted_bytes += rbytes; + curr_file_bytes += rbytes; + } + } + fclose(f); + unzCloseCurrentFile(zipfile); + } + if ((zip_idx + 1) < num_files) + { + unzGoToNextFile(zipfile); + } + } + unzClose(zipfile); + } } \ No newline at end of file diff --git a/source/installer.h b/source/installer.h index 4cfb337..e75577d 100644 --- a/source/installer.h +++ b/source/installer.h @@ -110,6 +110,7 @@ namespace INSTALLER int Init(void); void Exit(void); - int InstallRemotePkg(const char *filename, pkg_header *header); - int InstallLocalPkg(const char *filename, pkg_header *header, bool remove_after_install=false); + void Extract(const std::string &file, const std::string &dir); + int InstallRemotePkg(const std::string &filename, pkg_header *header); + int InstallLocalPkg(const std::string &filename, pkg_header *header, bool remove_after_install=false); } \ No newline at end of file