sigpatch-updater/source/unzip.c
2024-04-27 04:27:28 -06:00

68 lines
1.9 KiB
C

#include <stdio.h>
#include <minizip/unzip.h>
#include <string.h>
#include <dirent.h>
#include <switch.h>
#include "unzip.h"
#define WRITEBUFFERSIZE 0x1000 // 4KiB
#define MAXFILENAME 0x301
bool unzip(const char *output) {
unzFile zfile = unzOpen(output);
if (!zfile) {
return false;
}
unz_global_info gi = {0};
if (UNZ_OK != unzGetGlobalInfo(zfile, &gi)) {
return false;
}
for (int i = 0; i < gi.number_entry; i++) {
char filename_inzip[MAXFILENAME] = {0};
unz_file_info file_info = {0};
unzOpenCurrentFile(zfile);
unzGetCurrentFileInfo(zfile, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);
// check if the string ends with a /, if so, then its a directory.
if ((filename_inzip[strlen(filename_inzip) - 1]) == '/') {
// check if directory exists
DIR *dir = opendir(filename_inzip);
if (dir) {
closedir(dir);
} else {
printf("creating directory: %s\n", filename_inzip);
mkdir(filename_inzip, 0777);
}
} else {
const char *write_filename = filename_inzip;
FILE *outfile = fopen(write_filename, "wb");
void *buf = malloc(WRITEBUFFERSIZE);
printf("writing file: %s\n", filename_inzip);
consoleUpdate(NULL);
for (int j = unzReadCurrentFile(zfile, buf, WRITEBUFFERSIZE); j > 0; j = unzReadCurrentFile(zfile, buf, WRITEBUFFERSIZE)) {
fwrite(buf, 1, j, outfile);
}
fclose(outfile);
free(buf);
}
unzCloseCurrentFile(zfile);
unzGoToNextFile(zfile);
consoleUpdate(NULL);
}
unzClose(zfile);
remove(output);
// todo: i assume everything above works fine
// this might not always be the case, add error handling!
return true;
}