From 1a24a5157a2c06793b071ac54ced033f0d722509 Mon Sep 17 00:00:00 2001 From: WCX Date: Thu, 27 Jul 2023 09:17:06 +0000 Subject: [PATCH 1/3] =?UTF-8?q?[apps][minizip]=20add=20minizip=20app=20=20?= =?UTF-8?q?&=20=E5=A2=9E=E5=8A=A0=20minizip=20=E5=BA=94=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/minizip/miniunz.c | 551 +++++++++++++++++++++ apps/minizip/minizip.c | 372 ++++++++++++++ apps/minizip/minizip_test.c | 51 ++ apps/minizip/xmake.lua | 49 ++ repo/packages/m/minizip/scripts/deploy.lua | 30 ++ repo/packages/m/minizip/scripts/export.lua | 42 ++ repo/packages/m/minizip/xmake.lua | 72 +++ 7 files changed, 1167 insertions(+) create mode 100644 apps/minizip/miniunz.c create mode 100644 apps/minizip/minizip.c create mode 100644 apps/minizip/minizip_test.c create mode 100644 apps/minizip/xmake.lua create mode 100644 repo/packages/m/minizip/scripts/deploy.lua create mode 100644 repo/packages/m/minizip/scripts/export.lua create mode 100644 repo/packages/m/minizip/xmake.lua diff --git a/apps/minizip/miniunz.c b/apps/minizip/miniunz.c new file mode 100644 index 0000000..b293396 --- /dev/null +++ b/apps/minizip/miniunz.c @@ -0,0 +1,551 @@ +/* + miniunz.c + Version 1.01e, February 12th, 2005 + + Copyright (C) 1998-2005 Gilles Vollant +*/ + +#include +#include +#include +#include +#include +#include + +#ifdef unix +#include +#include +#endif + +#include "minizip/unzip.h" +#include "dfs_posix.h" + +#define CASESENSITIVITY (0) +#define WRITEBUFFERSIZE (8192) +#define MAXFILENAME (256) + +/* + mini unzip, demo of unzip package + + usage : + Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir] + + list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT + if it exists +*/ + +/* change_file_date : change the date/time of a file + filename : the filename of the file where date/time must be modified + dosdate : the new date at the MSDos format (4 bytes) + tmu_date : the SAME new date at the tm_unz format */ +static void change_file_date(filename, dosdate, tmu_date) + const char *filename; +uLong dosdate; +tm_unz tmu_date; +{ +#ifdef unix + struct utimbuf ut; + struct tm newdate; + newdate.tm_sec = tmu_date.tm_sec; + newdate.tm_min = tmu_date.tm_min; + newdate.tm_hour = tmu_date.tm_hour; + newdate.tm_mday = tmu_date.tm_mday; + newdate.tm_mon = tmu_date.tm_mon; + if (tmu_date.tm_year > 1900) + newdate.tm_year = tmu_date.tm_year - 1900; + else + newdate.tm_year = tmu_date.tm_year; + newdate.tm_isdst = -1; + + ut.actime = ut.modtime = mktime(&newdate); + utime(filename, &ut); +#endif +} + +/* mymkdir and change_file_date are not 100 % portable + As I don't know well Unix, I wait feedback for the unix portion */ + +static int mymkdir(dirname) + const char *dirname; +{ + int ret = 0; +#ifdef WIN32 + ret = mkdir(dirname); +#else +#ifdef unix + ret = mkdir(dirname, 0775); +#endif +#endif + return ret; +} + +static int makedir(newdir) +char *newdir; +{ + char *buffer; + char *p; + int len = (int)strlen(newdir); + + if (len <= 0) + return 0; + + buffer = (char *)malloc(len + 1); + strcpy(buffer, newdir); + + if (buffer[len - 1] == '/') + { + buffer[len - 1] = '\0'; + } + if (mymkdir(buffer) == 0) + { + free(buffer); + return 1; + } + + p = buffer + 1; + while (1) + { + char hold; + + while (*p && *p != '\\' && *p != '/') + p++; + hold = *p; + *p = 0; + if ((mymkdir(buffer) == -1) && (errno == ENOENT)) + { + printf("couldn't create directory %s\n", buffer); + free(buffer); + return 0; + } + if (hold == 0) + break; + *p++ = hold; + } + free(buffer); + return 1; +} + +static void do_banner() +{ + printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n"); + printf("more info at http://www.winimage.com/zLibDll/minizip.html\n\n"); +} + +static void do_help() +{ + printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" + " -e Extract without pathname (junk paths)\n" + " -x Extract with pathname\n" + " -v list files\n" + " -l list files\n" + " -d directory to extract into\n" + " -o overwrite files without prompting\n" + " -p extract crypted file using password\n\n"); +} + +static int do_list(uf) +unzFile uf; +{ + uLong i; + unz_global_info gi; + int err; + + err = unzGetGlobalInfo(uf, &gi); + if (err != UNZ_OK) + printf("error %d with zipfile in unzGetGlobalInfo \n", err); + printf(" Length Method Size Ratio Date Time CRC-32 Name\n"); + printf(" ------ ------ ---- ----- ---- ---- ------ ----\n"); + for (i = 0; i < gi.number_entry; i++) + { + char filename_inzip[256]; + unz_file_info file_info; + uLong ratio = 0; + const char *string_method; + char charCrypt = ' '; + err = unzGetCurrentFileInfo(uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0); + if (err != UNZ_OK) + { + printf("error %d with zipfile in unzGetCurrentFileInfo\n", err); + break; + } + if (file_info.uncompressed_size > 0) + ratio = (file_info.compressed_size * 100) / file_info.uncompressed_size; + + /* display a '*' if the file is crypted */ + if ((file_info.flag & 1) != 0) + charCrypt = '*'; + + if (file_info.compression_method == 0) + string_method = "Stored"; + else if (file_info.compression_method == Z_DEFLATED) + { + uInt iLevel = (uInt)((file_info.flag & 0x6) / 2); + if (iLevel == 0) + string_method = "Defl:N"; + else if (iLevel == 1) + string_method = "Defl:X"; + else if ((iLevel == 2) || (iLevel == 3)) + string_method = "Defl:F"; /* 2:fast , 3 : extra fast*/ + } + else + string_method = "Unkn. "; + + printf("%7lu %6s%c%7lu %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n", + file_info.uncompressed_size, string_method, + charCrypt, + file_info.compressed_size, + ratio, + (uLong)file_info.tmu_date.tm_mon + 1, + (uLong)file_info.tmu_date.tm_mday, + (uLong)file_info.tmu_date.tm_year % 100, + (uLong)file_info.tmu_date.tm_hour, (uLong)file_info.tmu_date.tm_min, + (uLong)file_info.crc, filename_inzip); + if ((i + 1) < gi.number_entry) + { + err = unzGoToNextFile(uf); + if (err != UNZ_OK) + { + printf("error %d with zipfile in unzGoToNextFile\n", err); + break; + } + } + } + + unzClose(uf); + + return 0; +} + +static int do_extract_currentfile(uf, popt_extract_without_path, popt_overwrite, password) +unzFile uf; +const int *popt_extract_without_path; +int *popt_overwrite; +const char *password; +{ + char filename_inzip[256]; + char *filename_withoutpath; + char *p; + int err = UNZ_OK; + FILE *fout = NULL; + void *buf; + uInt size_buf; + + unz_file_info file_info; + err = unzGetCurrentFileInfo(uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0); + + if (err != UNZ_OK) + { + printf("error %d with zipfile in unzGetCurrentFileInfo\n", err); + return err; + } + + size_buf = WRITEBUFFERSIZE; + buf = (void *)malloc(size_buf); + if (buf == NULL) + { + printf("Error allocating memory\n"); + return UNZ_INTERNALERROR; + } + + p = filename_withoutpath = filename_inzip; + while ((*p) != '\0') + { + if (((*p) == '/') || ((*p) == '\\')) + filename_withoutpath = p + 1; + p++; + } + + if ((*filename_withoutpath) == '\0') + { + if ((*popt_extract_without_path) == 0) + { + printf("creating directory: %s\n", filename_inzip); + mymkdir(filename_inzip); + } + } + else + { + const char *write_filename; + int skip = 0; + + if ((*popt_extract_without_path) == 0) + write_filename = filename_inzip; + else + write_filename = filename_withoutpath; + + err = unzOpenCurrentFilePassword(uf, password); + if (err != UNZ_OK) + { + printf("error %d with zipfile in unzOpenCurrentFilePassword\n", err); + } + + if (((*popt_overwrite) == 0) && (err == UNZ_OK)) + { + char rep = 0; + FILE *ftestexist; + ftestexist = fopen(write_filename, "rb"); + if (ftestexist != NULL) + { + fclose(ftestexist); + do + { + char answer[128]; + int ret; + + printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ", write_filename); + ret = scanf("%1s", answer); + if (ret != 1) + { + exit(EXIT_FAILURE); + } + rep = answer[0]; + if ((rep >= 'a') && (rep <= 'z')) + rep -= 0x20; + } while ((rep != 'Y') && (rep != 'N') && (rep != 'A')); + } + + if (rep == 'N') + skip = 1; + + if (rep == 'A') + *popt_overwrite = 1; + } + + if ((skip == 0) && (err == UNZ_OK)) + { + fout = fopen(write_filename, "wb"); + + /* some zipfile don't contain directory alone before file */ + if ((fout == NULL) && ((*popt_extract_without_path) == 0) && + (filename_withoutpath != (char *)filename_inzip)) + { + char c = *(filename_withoutpath - 1); + *(filename_withoutpath - 1) = '\0'; + makedir((char *)write_filename); + *(filename_withoutpath - 1) = c; + fout = fopen(write_filename, "wb"); + } + + if (fout == NULL) + { + printf("error opening %s\n", write_filename); + } + } + + if (fout != NULL) + { + printf(" extracting: %s\n", write_filename); + + do + { + err = unzReadCurrentFile(uf, buf, size_buf); + if (err < 0) + { + printf("error %d with zipfile in unzReadCurrentFile\n", err); + break; + } + if (err > 0) + if (fwrite(buf, err, 1, fout) != 1) + { + printf("error in writing extracted file\n"); + err = UNZ_ERRNO; + break; + } + } while (err > 0); + if (fout) + fclose(fout); + + if (err == 0) + change_file_date(write_filename, file_info.dosDate, + file_info.tmu_date); + } + + if (err == UNZ_OK) + { + err = unzCloseCurrentFile(uf); + if (err != UNZ_OK) + { + printf("error %d with zipfile in unzCloseCurrentFile\n", err); + } + } + else + unzCloseCurrentFile(uf); /* don't lose the error */ + } + + free(buf); + return err; +} + +static int do_extract(uf, opt_extract_without_path, opt_overwrite, password) +unzFile uf; +int opt_extract_without_path; +int opt_overwrite; +const char *password; +{ + uLong i; + unz_global_info gi; + int err; + + err = unzGetGlobalInfo(uf, &gi); + if (err != UNZ_OK) + printf("error %d with zipfile in unzGetGlobalInfo \n", err); + + for (i = 0; i < gi.number_entry; i++) + { + if (do_extract_currentfile(uf, &opt_extract_without_path, + &opt_overwrite, + password) != UNZ_OK) + break; + + if ((i + 1) < gi.number_entry) + { + err = unzGoToNextFile(uf); + if (err != UNZ_OK) + { + printf("error %d with zipfile in unzGoToNextFile\n", err); + break; + } + } + } + + unzClose(uf); + + return 0; +} + +static int do_extract_onefile(uf, filename, opt_extract_without_path, opt_overwrite, password) +unzFile uf; +const char *filename; +int opt_extract_without_path; +int opt_overwrite; +const char *password; +{ + if (unzLocateFile(uf, filename, CASESENSITIVITY) != UNZ_OK) + { + printf("file %s not found in the zipfile\n", filename); + return 2; + } + + if (do_extract_currentfile(uf, &opt_extract_without_path, + &opt_overwrite, + password) == UNZ_OK) + { + unzClose(uf); + return 0; + } + else + { + unzClose(uf); + return 1; + } +} + +int main(argc, argv) +int argc; +char *argv[]; +{ + const char *zipfilename = NULL; + const char *filename_to_extract = NULL; + const char *password = NULL; + char filename_try[MAXFILENAME + 16] = ""; + int i; + int opt_do_list = 0; + int opt_do_extract = 1; + int opt_do_extract_withoutpath = 0; + int opt_overwrite = 0; + int opt_extractdir = 0; + const char *dirname = NULL; + unzFile uf = NULL; + + do_banner(); + if (argc == 1) + { + do_help(); + return 0; + } + else + { + for (i = 1; i < argc; i++) + { + if ((*argv[i]) == '-') + { + const char *p = argv[i] + 1; + + while ((*p) != '\0') + { + char c = *(p++); + ; + if ((c == 'l') || (c == 'L')) + opt_do_list = 1; + if ((c == 'v') || (c == 'V')) + opt_do_list = 1; + if ((c == 'x') || (c == 'X')) + opt_do_extract = 1; + if ((c == 'e') || (c == 'E')) + opt_do_extract = opt_do_extract_withoutpath = 1; + if ((c == 'o') || (c == 'O')) + opt_overwrite = 1; + if ((c == 'd') || (c == 'D')) + { + opt_extractdir = 1; + dirname = argv[i + 1]; + } + + if (((c == 'p') || (c == 'P')) && (i + 1 < argc)) + { + password = argv[i + 1]; + i++; + } + } + } + else + { + if (zipfilename == NULL) + zipfilename = argv[i]; + else if ((filename_to_extract == NULL) && (!opt_extractdir)) + filename_to_extract = argv[i]; + } + } + } + + if (zipfilename != NULL) + { + strncpy(filename_try, zipfilename, MAXFILENAME - 1); + /* strncpy doesnt append the trailing NULL, of the string is too long. */ + filename_try[MAXFILENAME] = '\0'; + + uf = unzOpen(zipfilename); + if (uf == NULL) + { + strcat(filename_try, ".zip"); + uf = unzOpen(filename_try); + } + } + + if (uf == NULL) + { + printf("Cannot open %s or %s.zip\n", zipfilename, zipfilename); + return 1; + } + printf("%s opened\n", filename_try); + + if (opt_do_list == 1) + return do_list(uf); + else if (opt_do_extract == 1) + { + if (opt_extractdir && chdir(dirname)) + { + printf("Error changing into %s, aborting\n", dirname); + exit(-1); + } + + if (filename_to_extract == NULL) + return do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password); + else + return do_extract_onefile(uf, filename_to_extract, + opt_do_extract_withoutpath, opt_overwrite, password); + } + unzClose(uf); + + return 0; +} diff --git a/apps/minizip/minizip.c b/apps/minizip/minizip.c new file mode 100644 index 0000000..996173f --- /dev/null +++ b/apps/minizip/minizip.c @@ -0,0 +1,372 @@ +/* + minizip.c + Version 1.01e, February 12th, 2005 + + Copyright (C) 1998-2005 Gilles Vollant +*/ + +#include +#include +#include +#include +#include +#include + +#include "dfs_posix.h" + +// # include +// # include +// # include +// # include + +#include "minizip/zip.h" + +#define WRITEBUFFERSIZE (16384) +#define MAXFILENAME (256) + +#ifdef unix +static uLong filetime(f, tmzip, dt) +char *f; /* name of file to get info on */ +tm_zip *tmzip; /* return value: access, modific. and creation times */ +uLong *dt; /* dostime */ +{ + int ret = 0; + struct stat s; /* results of stat() */ + struct tm *filedate; + time_t tm_t = 0; + + if (strcmp(f, "-") != 0) + { + char name[MAXFILENAME + 1]; + int len = strlen(f); + if (len > MAXFILENAME) + len = MAXFILENAME; + + strncpy(name, f, MAXFILENAME - 1); + /* strncpy doesnt append the trailing NULL, of the string is too long. */ + name[MAXFILENAME] = '\0'; + + if (name[len - 1] == '/') + name[len - 1] = '\0'; + /* not all systems allow stat'ing a file with / appended */ + if (stat(name, &s) == 0) + { + tm_t = s.st_mtime; + ret = 1; + } + } + filedate = localtime(&tm_t); + + tmzip->tm_sec = filedate->tm_sec; + tmzip->tm_min = filedate->tm_min; + tmzip->tm_hour = filedate->tm_hour; + tmzip->tm_mday = filedate->tm_mday; + tmzip->tm_mon = filedate->tm_mon; + tmzip->tm_year = filedate->tm_year; + + return ret; +} +#else +static uLong filetime(f, tmzip, dt) +char *f; /* name of file to get info on */ +tm_zip *tmzip; /* return value: access, modific. and creation times */ +uLong *dt; /* dostime */ +{ + return 0; +} +#endif + +static int check_exist_file(filename) + const char *filename; +{ + FILE *ftestexist; + int ret = 1; + ftestexist = fopen(filename, "rb"); + if (ftestexist == NULL) + ret = 0; + else + fclose(ftestexist); + return ret; +} + +static void do_banner() +{ + printf("MiniZip 1.01b, demo of zLib + Zip package written by Gilles Vollant\n"); + printf("more info at http://www.winimage.com/zLibDll/minizip.html\n\n"); +} + +static void do_help() +{ + printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] file.zip [files_to_add]\n\n" + " -o Overwrite existing file.zip\n" + " -a Append to existing file.zip\n" + " -0 Store only\n" + " -1 Compress faster\n" + " -9 Compress better\n\n"); +} + +/* calculate the CRC32 of a file, + because to encrypt a file, we need known the CRC32 of the file before */ +static int getFileCrc(const char *filenameinzip, void *buf, unsigned long size_buf, unsigned long *result_crc) +{ + unsigned long calculate_crc = 0; + int err = ZIP_OK; + FILE *fin = fopen(filenameinzip, "rb"); + unsigned long size_read = 0; + unsigned long total_read = 0; + if (fin == NULL) + { + err = ZIP_ERRNO; + } + + if (err == ZIP_OK) + do + { + err = ZIP_OK; + size_read = (int)fread(buf, 1, size_buf, fin); + if (size_read < size_buf) + if (feof(fin) == 0) + { + printf("error in reading %s\n", filenameinzip); + err = ZIP_ERRNO; + } + + if (size_read > 0) + calculate_crc = crc32(calculate_crc, buf, size_read); + total_read += size_read; + + } while ((err == ZIP_OK) && (size_read > 0)); + + if (fin) + fclose(fin); + + *result_crc = calculate_crc; + printf("file %s crc %lx\n", filenameinzip, calculate_crc); + return err; +} + +int main(argc, argv) +int argc; +char *argv[]; +{ + int i; + int opt_overwrite = 0; + int opt_compress_level = Z_DEFAULT_COMPRESSION; + int zipfilenamearg = 0; + char filename_try[MAXFILENAME + 16]; + int zipok; + int err = 0; + int size_buf = 0; + void *buf = NULL; + const char *password = NULL; + + do_banner(); + if (argc == 1) + { + do_help(); + return 0; + } + else + { + for (i = 1; i < argc; i++) + { + if ((*argv[i]) == '-') + { + const char *p = argv[i] + 1; + + while ((*p) != '\0') + { + char c = *(p++); + ; + if ((c == 'o') || (c == 'O')) + opt_overwrite = 1; + if ((c == 'a') || (c == 'A')) + opt_overwrite = 2; + if ((c >= '0') && (c <= '9')) + opt_compress_level = c - '0'; + + if (((c == 'p') || (c == 'P')) && (i + 1 < argc)) + { + password = argv[i + 1]; + i++; + } + } + } + else if (zipfilenamearg == 0) + zipfilenamearg = i; + } + } + + size_buf = WRITEBUFFERSIZE; + buf = (void *)malloc(size_buf); + if (buf == NULL) + { + printf("Error allocating memory\n"); + return ZIP_INTERNALERROR; + } + + if (zipfilenamearg == 0) + zipok = 0; + else + { + int i, len; + int dot_found = 0; + + zipok = 1; + strncpy(filename_try, argv[zipfilenamearg], MAXFILENAME - 1); + /* strncpy doesnt append the trailing NULL, of the string is too long. */ + filename_try[MAXFILENAME] = '\0'; + + len = (int)strlen(filename_try); + for (i = 0; i < len; i++) + if (filename_try[i] == '.') + dot_found = 1; + + if (dot_found == 0) + strcat(filename_try, ".zip"); + + if (opt_overwrite == 2) + { + /* if the file don't exist, we not append file */ + if (check_exist_file(filename_try) == 0) + opt_overwrite = 1; + } + else if (opt_overwrite == 0) + if (check_exist_file(filename_try) != 0) + { + char rep = 0; + do + { + char answer[128]; + int ret; + printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ", filename_try); + ret = scanf("%1s", answer); + if (ret != 1) + { + exit(EXIT_FAILURE); + } + rep = answer[0]; + if ((rep >= 'a') && (rep <= 'z')) + rep -= 0x20; + } while ((rep != 'Y') && (rep != 'N') && (rep != 'A')); + if (rep == 'N') + zipok = 0; + if (rep == 'A') + opt_overwrite = 2; + } + } + + if (zipok == 1) + { + zipFile zf; + int errclose; + zf = zipOpen(filename_try, (opt_overwrite == 2) ? 2 : 0); + + if (zf == NULL) + { + printf("error opening %s\n", filename_try); + err = ZIP_ERRNO; + } + else + printf("creating %s\n", filename_try); + + for (i = zipfilenamearg + 1; (i < argc) && (err == ZIP_OK); i++) + { + if (!((((*(argv[i])) == '-') || ((*(argv[i])) == '/')) && + ((argv[i][1] == 'o') || (argv[i][1] == 'O') || + (argv[i][1] == 'a') || (argv[i][1] == 'A') || + (argv[i][1] == 'p') || (argv[i][1] == 'P') || + ((argv[i][1] >= '0') || (argv[i][1] <= '9'))) && + (strlen(argv[i]) == 2))) + { + FILE *fin; + int size_read; + const char *filenameinzip = argv[i]; + zip_fileinfo zi; + unsigned long crcFile = 0; + + zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour = + zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0; + zi.dosDate = 0; + zi.internal_fa = 0; + zi.external_fa = 0; + filetime(filenameinzip, &zi.tmz_date, &zi.dosDate); + + /* + err = zipOpenNewFileInZip(zf,filenameinzip,&zi, + NULL,0,NULL,0,NULL / * comment * /, + (opt_compress_level != 0) ? Z_DEFLATED : 0, + opt_compress_level); + */ + if ((password != NULL) && (err == ZIP_OK)) + err = getFileCrc(filenameinzip, buf, size_buf, &crcFile); + + err = zipOpenNewFileInZip3(zf, filenameinzip, &zi, + NULL, 0, NULL, 0, NULL /* comment*/, + (opt_compress_level != 0) ? Z_DEFLATED : 0, + opt_compress_level, 0, + /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */ + -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, + password, crcFile); + + if (err != ZIP_OK) + printf("error in opening %s in zipfile\n", filenameinzip); + else + { + fin = fopen(filenameinzip, "rb"); + if (fin == NULL) + { + err = ZIP_ERRNO; + printf("error in opening %s for reading\n", filenameinzip); + } + } + + if (err == ZIP_OK) + do + { + err = ZIP_OK; + size_read = (int)fread(buf, 1, size_buf, fin); + if (size_read < size_buf) + if (feof(fin) == 0) + { + printf("error in reading %s\n", filenameinzip); + err = ZIP_ERRNO; + } + + if (size_read > 0) + { + err = zipWriteInFileInZip(zf, buf, size_read); + if (err < 0) + { + printf("error in writing %s in the zipfile\n", + filenameinzip); + } + } + } while ((err == ZIP_OK) && (size_read > 0)); + + if (fin) + fclose(fin); + + if (err < 0) + err = ZIP_ERRNO; + else + { + err = zipCloseFileInZip(zf); + if (err != ZIP_OK) + printf("error in closing %s in the zipfile\n", + filenameinzip); + } + } + } + errclose = zipClose(zf, NULL); + if (errclose != ZIP_OK) + printf("error in closing %s\n", filename_try); + } + else + { + do_help(); + } + + free(buf); + return 0; +} diff --git a/apps/minizip/minizip_test.c b/apps/minizip/minizip_test.c new file mode 100644 index 0000000..48a6b76 --- /dev/null +++ b/apps/minizip/minizip_test.c @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: GPL-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-05-17 wcx1024979076 The first version + */ + +#include "stdio.h" +#include "minizip/zip.h" + +int main() +{ + // 文件名 + const char *zipfile = "example.zip"; + // 需要压缩的文件 + const char *file = "example.txt"; + + zipFile zf = zipOpen(zipfile, APPEND_STATUS_CREATE); + if (zf == NULL) + { + printf("Error creating %s \n", zipfile); + return 1; + } + + // 压缩文件 + int err = zipOpenNewFileInZip(zf, file, NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_BEST_COMPRESSION); + if (err != ZIP_OK) + { + printf("Error adding %s to %s \n", file, zipfile); + return 1; + } + + // 读取文件并压缩 + FILE *f = fopen(file, "rb"); + char buf[1024]; + int len; + while ((len = fread(buf, 1, sizeof(buf), f)) > 0) + { + zipWriteInFileInZip(zf, buf, len); + } + fclose(f); + + zipCloseFileInZip(zf); + zipClose(zf, NULL); + + printf("Successfully created %s \n", zipfile); + return 0; +} diff --git a/apps/minizip/xmake.lua b/apps/minizip/xmake.lua new file mode 100644 index 0000000..1948901 --- /dev/null +++ b/apps/minizip/xmake.lua @@ -0,0 +1,49 @@ +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- You may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2022-2023 RT-Thread Development Team +-- +-- @author wcx1024979076 +-- @file xmake.lua +-- +-- Change Logs: +-- Date Author Notes +-- ------------ ---------- ----------------------------------------------- +-- 2023-07-26 wcx1024979076 initial version +-- +add_rules("mode.debug", "mode.release") + +add_requires("minizip") + +target("minizip") +do + add_rules("rt.sdk") + add_files("minizip.c") + add_packages("minizip") +end +target_end() + +target("miniunz") +do + add_rules("rt.sdk") + add_files("miniunz.c") + add_packages("minizip") +end +target_end() + +target("minizip_test") +do + add_rules("rt.sdk") + add_files("minizip_test.c") + add_packages("minizip") +end +target_end() diff --git a/repo/packages/m/minizip/scripts/deploy.lua b/repo/packages/m/minizip/scripts/deploy.lua new file mode 100644 index 0000000..d6b4c7e --- /dev/null +++ b/repo/packages/m/minizip/scripts/deploy.lua @@ -0,0 +1,30 @@ +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- You may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2023-2023 RT-Thread Development Team +-- +-- @author wcx1024979076 +-- @file deploy.lua +-- +-- Change Logs: +-- Date Author Notes +-- ------------ ---------- ----------------------------------------------- +-- 2023-07-26 wcx1024979076 initial version +-- +import("rt.rt_utils") + +function main(rootfs, installdir) + for _, filepath in ipairs(os.files(path.join(installdir, "lib") .. "/lib*.so*")) do + local filename = path.filename(filepath) + rt_utils.cp_with_symlink(filepath, path.join(rootfs, "lib", filename)) + end +end diff --git a/repo/packages/m/minizip/scripts/export.lua b/repo/packages/m/minizip/scripts/export.lua new file mode 100644 index 0000000..f852635 --- /dev/null +++ b/repo/packages/m/minizip/scripts/export.lua @@ -0,0 +1,42 @@ +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- You may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2023-2023 RT-Thread Development Team +-- +-- @author wcx1024979076 +-- @file export.lua +-- +-- Change Logs: +-- Date Author Notes +-- ------------ ---------- ----------------------------------------------- +-- 2023-07-26 wcx1024979076 initial version +-- +import("rt.rt_utils") + +function main(rootfs, installdir) + for _, filedir in ipairs(os.filedirs(path.join(installdir, "include", "minizip") .. "/*")) do + local inc = path.join(installdir, "include", "minizip") + local name = path.relative(filedir, inc) + rt_utils.cp_with_symlink(path.join(inc, name), path.join(rootfs, "include", "minizip", name), + {rootdir = inc, symlink = true}) + end + + for _, filepath in ipairs(os.files(path.join(installdir, "lib") .. "/lib*.a")) do + local filename = path.filename(filepath) + rt_utils.cp_with_symlink(filepath, path.join(rootfs, "lib", filename)) + end + + for _, filepath in ipairs(os.files(path.join(installdir, "lib") .. "/lib*.so*")) do + local filename = path.filename(filepath) + rt_utils.cp_with_symlink(filepath, path.join(rootfs, "lib", filename)) + end +end diff --git a/repo/packages/m/minizip/xmake.lua b/repo/packages/m/minizip/xmake.lua new file mode 100644 index 0000000..144df23 --- /dev/null +++ b/repo/packages/m/minizip/xmake.lua @@ -0,0 +1,72 @@ +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- You may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2023-2023 RT-Thread Development Team +-- +-- @author wcx1024979076 +-- @file xmake.lua +-- +-- Change Logs: +-- Date Author Notes +-- ------------ ---------- ----------------------------------------------- +-- 2023-07-26 wcx1024979076 initial version +-- + +package("minizip") + set_homepage("https://www.zlib.net/") + set_description("Mini zip and unzip based on zlib") + set_license("zlib") + + add_urls("https://github.com/madler/zlib/archive/$(version).tar.gz", + "https://github.com/madler/zlib.git") + add_versions("v1.2.10", "42cd7b2bdaf1c4570e0877e61f2fdc0bce8019492431d054d3d86925e5058dc5") + add_versions("v1.2.11", "629380c90a77b964d896ed37163f5c3a34f6e6d897311f1df2a7016355c45eff") + add_versions("v1.2.12", "d8688496ea40fb61787500e863cc63c9afcbc524468cedeb478068924eb54932") + add_versions("v1.2.13", "1525952a0a567581792613a9723333d7f8cc20b87a81f920fb8bc7e3f2251428") + + add_configs("shared", { + description = "Build shared library.", + default = os.getenv("RT_XMAKE_LINK_TYPE") ~= "static", + type = "boolean" + }) + + on_load(function(package) + package:add("deps", "zlib", {debug = package:config("debug"), configs = {shared = package:config("shared")}}) + end) + + on_install(function (package) + import("rt.private.build.rtflags") + local info = rtflags.get_package_info(package) + local host = info.host + local configs = {host = host} + local cc = info.cc + local ldflags = {} + os.setenv("PATH", path.directory(cc) .. ":" .. os.getenv("PATH")) + + table.insert(configs, "--enable-static=yes") + if package:config("shared") then + table.insert(configs, "--enable-shared=yes") + else + table.insert(configs, "--enable-shared=no") + end + + os.cd(path.join("contrib", "minizip")) + + local buildenvs = import("package.tools.autoconf").buildenvs(package, + {ldflags = ldflags, packagedeps = {"zlib"}}) + import("package.tools.autoconf").configure(package, configs, {envs = buildenvs}) + import("package.tools.make").install(package, {}, {envs = buildenvs}) + end) + + on_test(function (package) + assert(package:has_cfuncs("inflate", {includes = "minizip/zip.h"})) + end) From 2d6c550acbae44e47ea1a5ad05beab03bbc1ba72 Mon Sep 17 00:00:00 2001 From: WCX1024979076 Date: Sun, 30 Jul 2023 05:56:39 +0300 Subject: [PATCH 2/3] =?UTF-8?q?=E7=A7=BB=E9=99=A4minizip=5Ftest.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/minizip/minizip_test.c | 51 ------------------------------------- apps/minizip/xmake.lua | 7 ----- 2 files changed, 58 deletions(-) delete mode 100644 apps/minizip/minizip_test.c diff --git a/apps/minizip/minizip_test.c b/apps/minizip/minizip_test.c deleted file mode 100644 index 48a6b76..0000000 --- a/apps/minizip/minizip_test.c +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2006-2018, RT-Thread Development Team - * - * SPDX-License-Identifier: GPL-2.0 - * - * Change Logs: - * Date Author Notes - * 2023-05-17 wcx1024979076 The first version - */ - -#include "stdio.h" -#include "minizip/zip.h" - -int main() -{ - // 文件名 - const char *zipfile = "example.zip"; - // 需要压缩的文件 - const char *file = "example.txt"; - - zipFile zf = zipOpen(zipfile, APPEND_STATUS_CREATE); - if (zf == NULL) - { - printf("Error creating %s \n", zipfile); - return 1; - } - - // 压缩文件 - int err = zipOpenNewFileInZip(zf, file, NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_BEST_COMPRESSION); - if (err != ZIP_OK) - { - printf("Error adding %s to %s \n", file, zipfile); - return 1; - } - - // 读取文件并压缩 - FILE *f = fopen(file, "rb"); - char buf[1024]; - int len; - while ((len = fread(buf, 1, sizeof(buf), f)) > 0) - { - zipWriteInFileInZip(zf, buf, len); - } - fclose(f); - - zipCloseFileInZip(zf); - zipClose(zf, NULL); - - printf("Successfully created %s \n", zipfile); - return 0; -} diff --git a/apps/minizip/xmake.lua b/apps/minizip/xmake.lua index 1948901..cf8b94f 100644 --- a/apps/minizip/xmake.lua +++ b/apps/minizip/xmake.lua @@ -40,10 +40,3 @@ do end target_end() -target("minizip_test") -do - add_rules("rt.sdk") - add_files("minizip_test.c") - add_packages("minizip") -end -target_end() From 0fb55e478ea320f35c8556461fcd8df29782c9cf Mon Sep 17 00:00:00 2001 From: wcx1024979076 Date: Sat, 30 Sep 2023 15:15:57 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=88=A0=E9=99=A4apps/minizip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/minizip/miniunz.c | 551 ----------------------------------------- apps/minizip/minizip.c | 372 ---------------------------- apps/minizip/xmake.lua | 42 ---- 3 files changed, 965 deletions(-) delete mode 100644 apps/minizip/miniunz.c delete mode 100644 apps/minizip/minizip.c delete mode 100644 apps/minizip/xmake.lua diff --git a/apps/minizip/miniunz.c b/apps/minizip/miniunz.c deleted file mode 100644 index b293396..0000000 --- a/apps/minizip/miniunz.c +++ /dev/null @@ -1,551 +0,0 @@ -/* - miniunz.c - Version 1.01e, February 12th, 2005 - - Copyright (C) 1998-2005 Gilles Vollant -*/ - -#include -#include -#include -#include -#include -#include - -#ifdef unix -#include -#include -#endif - -#include "minizip/unzip.h" -#include "dfs_posix.h" - -#define CASESENSITIVITY (0) -#define WRITEBUFFERSIZE (8192) -#define MAXFILENAME (256) - -/* - mini unzip, demo of unzip package - - usage : - Usage : miniunz [-exvlo] file.zip [file_to_extract] [-d extractdir] - - list the file in the zipfile, and print the content of FILE_ID.ZIP or README.TXT - if it exists -*/ - -/* change_file_date : change the date/time of a file - filename : the filename of the file where date/time must be modified - dosdate : the new date at the MSDos format (4 bytes) - tmu_date : the SAME new date at the tm_unz format */ -static void change_file_date(filename, dosdate, tmu_date) - const char *filename; -uLong dosdate; -tm_unz tmu_date; -{ -#ifdef unix - struct utimbuf ut; - struct tm newdate; - newdate.tm_sec = tmu_date.tm_sec; - newdate.tm_min = tmu_date.tm_min; - newdate.tm_hour = tmu_date.tm_hour; - newdate.tm_mday = tmu_date.tm_mday; - newdate.tm_mon = tmu_date.tm_mon; - if (tmu_date.tm_year > 1900) - newdate.tm_year = tmu_date.tm_year - 1900; - else - newdate.tm_year = tmu_date.tm_year; - newdate.tm_isdst = -1; - - ut.actime = ut.modtime = mktime(&newdate); - utime(filename, &ut); -#endif -} - -/* mymkdir and change_file_date are not 100 % portable - As I don't know well Unix, I wait feedback for the unix portion */ - -static int mymkdir(dirname) - const char *dirname; -{ - int ret = 0; -#ifdef WIN32 - ret = mkdir(dirname); -#else -#ifdef unix - ret = mkdir(dirname, 0775); -#endif -#endif - return ret; -} - -static int makedir(newdir) -char *newdir; -{ - char *buffer; - char *p; - int len = (int)strlen(newdir); - - if (len <= 0) - return 0; - - buffer = (char *)malloc(len + 1); - strcpy(buffer, newdir); - - if (buffer[len - 1] == '/') - { - buffer[len - 1] = '\0'; - } - if (mymkdir(buffer) == 0) - { - free(buffer); - return 1; - } - - p = buffer + 1; - while (1) - { - char hold; - - while (*p && *p != '\\' && *p != '/') - p++; - hold = *p; - *p = 0; - if ((mymkdir(buffer) == -1) && (errno == ENOENT)) - { - printf("couldn't create directory %s\n", buffer); - free(buffer); - return 0; - } - if (hold == 0) - break; - *p++ = hold; - } - free(buffer); - return 1; -} - -static void do_banner() -{ - printf("MiniUnz 1.01b, demo of zLib + Unz package written by Gilles Vollant\n"); - printf("more info at http://www.winimage.com/zLibDll/minizip.html\n\n"); -} - -static void do_help() -{ - printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" - " -e Extract without pathname (junk paths)\n" - " -x Extract with pathname\n" - " -v list files\n" - " -l list files\n" - " -d directory to extract into\n" - " -o overwrite files without prompting\n" - " -p extract crypted file using password\n\n"); -} - -static int do_list(uf) -unzFile uf; -{ - uLong i; - unz_global_info gi; - int err; - - err = unzGetGlobalInfo(uf, &gi); - if (err != UNZ_OK) - printf("error %d with zipfile in unzGetGlobalInfo \n", err); - printf(" Length Method Size Ratio Date Time CRC-32 Name\n"); - printf(" ------ ------ ---- ----- ---- ---- ------ ----\n"); - for (i = 0; i < gi.number_entry; i++) - { - char filename_inzip[256]; - unz_file_info file_info; - uLong ratio = 0; - const char *string_method; - char charCrypt = ' '; - err = unzGetCurrentFileInfo(uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0); - if (err != UNZ_OK) - { - printf("error %d with zipfile in unzGetCurrentFileInfo\n", err); - break; - } - if (file_info.uncompressed_size > 0) - ratio = (file_info.compressed_size * 100) / file_info.uncompressed_size; - - /* display a '*' if the file is crypted */ - if ((file_info.flag & 1) != 0) - charCrypt = '*'; - - if (file_info.compression_method == 0) - string_method = "Stored"; - else if (file_info.compression_method == Z_DEFLATED) - { - uInt iLevel = (uInt)((file_info.flag & 0x6) / 2); - if (iLevel == 0) - string_method = "Defl:N"; - else if (iLevel == 1) - string_method = "Defl:X"; - else if ((iLevel == 2) || (iLevel == 3)) - string_method = "Defl:F"; /* 2:fast , 3 : extra fast*/ - } - else - string_method = "Unkn. "; - - printf("%7lu %6s%c%7lu %3lu%% %2.2lu-%2.2lu-%2.2lu %2.2lu:%2.2lu %8.8lx %s\n", - file_info.uncompressed_size, string_method, - charCrypt, - file_info.compressed_size, - ratio, - (uLong)file_info.tmu_date.tm_mon + 1, - (uLong)file_info.tmu_date.tm_mday, - (uLong)file_info.tmu_date.tm_year % 100, - (uLong)file_info.tmu_date.tm_hour, (uLong)file_info.tmu_date.tm_min, - (uLong)file_info.crc, filename_inzip); - if ((i + 1) < gi.number_entry) - { - err = unzGoToNextFile(uf); - if (err != UNZ_OK) - { - printf("error %d with zipfile in unzGoToNextFile\n", err); - break; - } - } - } - - unzClose(uf); - - return 0; -} - -static int do_extract_currentfile(uf, popt_extract_without_path, popt_overwrite, password) -unzFile uf; -const int *popt_extract_without_path; -int *popt_overwrite; -const char *password; -{ - char filename_inzip[256]; - char *filename_withoutpath; - char *p; - int err = UNZ_OK; - FILE *fout = NULL; - void *buf; - uInt size_buf; - - unz_file_info file_info; - err = unzGetCurrentFileInfo(uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0); - - if (err != UNZ_OK) - { - printf("error %d with zipfile in unzGetCurrentFileInfo\n", err); - return err; - } - - size_buf = WRITEBUFFERSIZE; - buf = (void *)malloc(size_buf); - if (buf == NULL) - { - printf("Error allocating memory\n"); - return UNZ_INTERNALERROR; - } - - p = filename_withoutpath = filename_inzip; - while ((*p) != '\0') - { - if (((*p) == '/') || ((*p) == '\\')) - filename_withoutpath = p + 1; - p++; - } - - if ((*filename_withoutpath) == '\0') - { - if ((*popt_extract_without_path) == 0) - { - printf("creating directory: %s\n", filename_inzip); - mymkdir(filename_inzip); - } - } - else - { - const char *write_filename; - int skip = 0; - - if ((*popt_extract_without_path) == 0) - write_filename = filename_inzip; - else - write_filename = filename_withoutpath; - - err = unzOpenCurrentFilePassword(uf, password); - if (err != UNZ_OK) - { - printf("error %d with zipfile in unzOpenCurrentFilePassword\n", err); - } - - if (((*popt_overwrite) == 0) && (err == UNZ_OK)) - { - char rep = 0; - FILE *ftestexist; - ftestexist = fopen(write_filename, "rb"); - if (ftestexist != NULL) - { - fclose(ftestexist); - do - { - char answer[128]; - int ret; - - printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ", write_filename); - ret = scanf("%1s", answer); - if (ret != 1) - { - exit(EXIT_FAILURE); - } - rep = answer[0]; - if ((rep >= 'a') && (rep <= 'z')) - rep -= 0x20; - } while ((rep != 'Y') && (rep != 'N') && (rep != 'A')); - } - - if (rep == 'N') - skip = 1; - - if (rep == 'A') - *popt_overwrite = 1; - } - - if ((skip == 0) && (err == UNZ_OK)) - { - fout = fopen(write_filename, "wb"); - - /* some zipfile don't contain directory alone before file */ - if ((fout == NULL) && ((*popt_extract_without_path) == 0) && - (filename_withoutpath != (char *)filename_inzip)) - { - char c = *(filename_withoutpath - 1); - *(filename_withoutpath - 1) = '\0'; - makedir((char *)write_filename); - *(filename_withoutpath - 1) = c; - fout = fopen(write_filename, "wb"); - } - - if (fout == NULL) - { - printf("error opening %s\n", write_filename); - } - } - - if (fout != NULL) - { - printf(" extracting: %s\n", write_filename); - - do - { - err = unzReadCurrentFile(uf, buf, size_buf); - if (err < 0) - { - printf("error %d with zipfile in unzReadCurrentFile\n", err); - break; - } - if (err > 0) - if (fwrite(buf, err, 1, fout) != 1) - { - printf("error in writing extracted file\n"); - err = UNZ_ERRNO; - break; - } - } while (err > 0); - if (fout) - fclose(fout); - - if (err == 0) - change_file_date(write_filename, file_info.dosDate, - file_info.tmu_date); - } - - if (err == UNZ_OK) - { - err = unzCloseCurrentFile(uf); - if (err != UNZ_OK) - { - printf("error %d with zipfile in unzCloseCurrentFile\n", err); - } - } - else - unzCloseCurrentFile(uf); /* don't lose the error */ - } - - free(buf); - return err; -} - -static int do_extract(uf, opt_extract_without_path, opt_overwrite, password) -unzFile uf; -int opt_extract_without_path; -int opt_overwrite; -const char *password; -{ - uLong i; - unz_global_info gi; - int err; - - err = unzGetGlobalInfo(uf, &gi); - if (err != UNZ_OK) - printf("error %d with zipfile in unzGetGlobalInfo \n", err); - - for (i = 0; i < gi.number_entry; i++) - { - if (do_extract_currentfile(uf, &opt_extract_without_path, - &opt_overwrite, - password) != UNZ_OK) - break; - - if ((i + 1) < gi.number_entry) - { - err = unzGoToNextFile(uf); - if (err != UNZ_OK) - { - printf("error %d with zipfile in unzGoToNextFile\n", err); - break; - } - } - } - - unzClose(uf); - - return 0; -} - -static int do_extract_onefile(uf, filename, opt_extract_without_path, opt_overwrite, password) -unzFile uf; -const char *filename; -int opt_extract_without_path; -int opt_overwrite; -const char *password; -{ - if (unzLocateFile(uf, filename, CASESENSITIVITY) != UNZ_OK) - { - printf("file %s not found in the zipfile\n", filename); - return 2; - } - - if (do_extract_currentfile(uf, &opt_extract_without_path, - &opt_overwrite, - password) == UNZ_OK) - { - unzClose(uf); - return 0; - } - else - { - unzClose(uf); - return 1; - } -} - -int main(argc, argv) -int argc; -char *argv[]; -{ - const char *zipfilename = NULL; - const char *filename_to_extract = NULL; - const char *password = NULL; - char filename_try[MAXFILENAME + 16] = ""; - int i; - int opt_do_list = 0; - int opt_do_extract = 1; - int opt_do_extract_withoutpath = 0; - int opt_overwrite = 0; - int opt_extractdir = 0; - const char *dirname = NULL; - unzFile uf = NULL; - - do_banner(); - if (argc == 1) - { - do_help(); - return 0; - } - else - { - for (i = 1; i < argc; i++) - { - if ((*argv[i]) == '-') - { - const char *p = argv[i] + 1; - - while ((*p) != '\0') - { - char c = *(p++); - ; - if ((c == 'l') || (c == 'L')) - opt_do_list = 1; - if ((c == 'v') || (c == 'V')) - opt_do_list = 1; - if ((c == 'x') || (c == 'X')) - opt_do_extract = 1; - if ((c == 'e') || (c == 'E')) - opt_do_extract = opt_do_extract_withoutpath = 1; - if ((c == 'o') || (c == 'O')) - opt_overwrite = 1; - if ((c == 'd') || (c == 'D')) - { - opt_extractdir = 1; - dirname = argv[i + 1]; - } - - if (((c == 'p') || (c == 'P')) && (i + 1 < argc)) - { - password = argv[i + 1]; - i++; - } - } - } - else - { - if (zipfilename == NULL) - zipfilename = argv[i]; - else if ((filename_to_extract == NULL) && (!opt_extractdir)) - filename_to_extract = argv[i]; - } - } - } - - if (zipfilename != NULL) - { - strncpy(filename_try, zipfilename, MAXFILENAME - 1); - /* strncpy doesnt append the trailing NULL, of the string is too long. */ - filename_try[MAXFILENAME] = '\0'; - - uf = unzOpen(zipfilename); - if (uf == NULL) - { - strcat(filename_try, ".zip"); - uf = unzOpen(filename_try); - } - } - - if (uf == NULL) - { - printf("Cannot open %s or %s.zip\n", zipfilename, zipfilename); - return 1; - } - printf("%s opened\n", filename_try); - - if (opt_do_list == 1) - return do_list(uf); - else if (opt_do_extract == 1) - { - if (opt_extractdir && chdir(dirname)) - { - printf("Error changing into %s, aborting\n", dirname); - exit(-1); - } - - if (filename_to_extract == NULL) - return do_extract(uf, opt_do_extract_withoutpath, opt_overwrite, password); - else - return do_extract_onefile(uf, filename_to_extract, - opt_do_extract_withoutpath, opt_overwrite, password); - } - unzClose(uf); - - return 0; -} diff --git a/apps/minizip/minizip.c b/apps/minizip/minizip.c deleted file mode 100644 index 996173f..0000000 --- a/apps/minizip/minizip.c +++ /dev/null @@ -1,372 +0,0 @@ -/* - minizip.c - Version 1.01e, February 12th, 2005 - - Copyright (C) 1998-2005 Gilles Vollant -*/ - -#include -#include -#include -#include -#include -#include - -#include "dfs_posix.h" - -// # include -// # include -// # include -// # include - -#include "minizip/zip.h" - -#define WRITEBUFFERSIZE (16384) -#define MAXFILENAME (256) - -#ifdef unix -static uLong filetime(f, tmzip, dt) -char *f; /* name of file to get info on */ -tm_zip *tmzip; /* return value: access, modific. and creation times */ -uLong *dt; /* dostime */ -{ - int ret = 0; - struct stat s; /* results of stat() */ - struct tm *filedate; - time_t tm_t = 0; - - if (strcmp(f, "-") != 0) - { - char name[MAXFILENAME + 1]; - int len = strlen(f); - if (len > MAXFILENAME) - len = MAXFILENAME; - - strncpy(name, f, MAXFILENAME - 1); - /* strncpy doesnt append the trailing NULL, of the string is too long. */ - name[MAXFILENAME] = '\0'; - - if (name[len - 1] == '/') - name[len - 1] = '\0'; - /* not all systems allow stat'ing a file with / appended */ - if (stat(name, &s) == 0) - { - tm_t = s.st_mtime; - ret = 1; - } - } - filedate = localtime(&tm_t); - - tmzip->tm_sec = filedate->tm_sec; - tmzip->tm_min = filedate->tm_min; - tmzip->tm_hour = filedate->tm_hour; - tmzip->tm_mday = filedate->tm_mday; - tmzip->tm_mon = filedate->tm_mon; - tmzip->tm_year = filedate->tm_year; - - return ret; -} -#else -static uLong filetime(f, tmzip, dt) -char *f; /* name of file to get info on */ -tm_zip *tmzip; /* return value: access, modific. and creation times */ -uLong *dt; /* dostime */ -{ - return 0; -} -#endif - -static int check_exist_file(filename) - const char *filename; -{ - FILE *ftestexist; - int ret = 1; - ftestexist = fopen(filename, "rb"); - if (ftestexist == NULL) - ret = 0; - else - fclose(ftestexist); - return ret; -} - -static void do_banner() -{ - printf("MiniZip 1.01b, demo of zLib + Zip package written by Gilles Vollant\n"); - printf("more info at http://www.winimage.com/zLibDll/minizip.html\n\n"); -} - -static void do_help() -{ - printf("Usage : minizip [-o] [-a] [-0 to -9] [-p password] file.zip [files_to_add]\n\n" - " -o Overwrite existing file.zip\n" - " -a Append to existing file.zip\n" - " -0 Store only\n" - " -1 Compress faster\n" - " -9 Compress better\n\n"); -} - -/* calculate the CRC32 of a file, - because to encrypt a file, we need known the CRC32 of the file before */ -static int getFileCrc(const char *filenameinzip, void *buf, unsigned long size_buf, unsigned long *result_crc) -{ - unsigned long calculate_crc = 0; - int err = ZIP_OK; - FILE *fin = fopen(filenameinzip, "rb"); - unsigned long size_read = 0; - unsigned long total_read = 0; - if (fin == NULL) - { - err = ZIP_ERRNO; - } - - if (err == ZIP_OK) - do - { - err = ZIP_OK; - size_read = (int)fread(buf, 1, size_buf, fin); - if (size_read < size_buf) - if (feof(fin) == 0) - { - printf("error in reading %s\n", filenameinzip); - err = ZIP_ERRNO; - } - - if (size_read > 0) - calculate_crc = crc32(calculate_crc, buf, size_read); - total_read += size_read; - - } while ((err == ZIP_OK) && (size_read > 0)); - - if (fin) - fclose(fin); - - *result_crc = calculate_crc; - printf("file %s crc %lx\n", filenameinzip, calculate_crc); - return err; -} - -int main(argc, argv) -int argc; -char *argv[]; -{ - int i; - int opt_overwrite = 0; - int opt_compress_level = Z_DEFAULT_COMPRESSION; - int zipfilenamearg = 0; - char filename_try[MAXFILENAME + 16]; - int zipok; - int err = 0; - int size_buf = 0; - void *buf = NULL; - const char *password = NULL; - - do_banner(); - if (argc == 1) - { - do_help(); - return 0; - } - else - { - for (i = 1; i < argc; i++) - { - if ((*argv[i]) == '-') - { - const char *p = argv[i] + 1; - - while ((*p) != '\0') - { - char c = *(p++); - ; - if ((c == 'o') || (c == 'O')) - opt_overwrite = 1; - if ((c == 'a') || (c == 'A')) - opt_overwrite = 2; - if ((c >= '0') && (c <= '9')) - opt_compress_level = c - '0'; - - if (((c == 'p') || (c == 'P')) && (i + 1 < argc)) - { - password = argv[i + 1]; - i++; - } - } - } - else if (zipfilenamearg == 0) - zipfilenamearg = i; - } - } - - size_buf = WRITEBUFFERSIZE; - buf = (void *)malloc(size_buf); - if (buf == NULL) - { - printf("Error allocating memory\n"); - return ZIP_INTERNALERROR; - } - - if (zipfilenamearg == 0) - zipok = 0; - else - { - int i, len; - int dot_found = 0; - - zipok = 1; - strncpy(filename_try, argv[zipfilenamearg], MAXFILENAME - 1); - /* strncpy doesnt append the trailing NULL, of the string is too long. */ - filename_try[MAXFILENAME] = '\0'; - - len = (int)strlen(filename_try); - for (i = 0; i < len; i++) - if (filename_try[i] == '.') - dot_found = 1; - - if (dot_found == 0) - strcat(filename_try, ".zip"); - - if (opt_overwrite == 2) - { - /* if the file don't exist, we not append file */ - if (check_exist_file(filename_try) == 0) - opt_overwrite = 1; - } - else if (opt_overwrite == 0) - if (check_exist_file(filename_try) != 0) - { - char rep = 0; - do - { - char answer[128]; - int ret; - printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ", filename_try); - ret = scanf("%1s", answer); - if (ret != 1) - { - exit(EXIT_FAILURE); - } - rep = answer[0]; - if ((rep >= 'a') && (rep <= 'z')) - rep -= 0x20; - } while ((rep != 'Y') && (rep != 'N') && (rep != 'A')); - if (rep == 'N') - zipok = 0; - if (rep == 'A') - opt_overwrite = 2; - } - } - - if (zipok == 1) - { - zipFile zf; - int errclose; - zf = zipOpen(filename_try, (opt_overwrite == 2) ? 2 : 0); - - if (zf == NULL) - { - printf("error opening %s\n", filename_try); - err = ZIP_ERRNO; - } - else - printf("creating %s\n", filename_try); - - for (i = zipfilenamearg + 1; (i < argc) && (err == ZIP_OK); i++) - { - if (!((((*(argv[i])) == '-') || ((*(argv[i])) == '/')) && - ((argv[i][1] == 'o') || (argv[i][1] == 'O') || - (argv[i][1] == 'a') || (argv[i][1] == 'A') || - (argv[i][1] == 'p') || (argv[i][1] == 'P') || - ((argv[i][1] >= '0') || (argv[i][1] <= '9'))) && - (strlen(argv[i]) == 2))) - { - FILE *fin; - int size_read; - const char *filenameinzip = argv[i]; - zip_fileinfo zi; - unsigned long crcFile = 0; - - zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour = - zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0; - zi.dosDate = 0; - zi.internal_fa = 0; - zi.external_fa = 0; - filetime(filenameinzip, &zi.tmz_date, &zi.dosDate); - - /* - err = zipOpenNewFileInZip(zf,filenameinzip,&zi, - NULL,0,NULL,0,NULL / * comment * /, - (opt_compress_level != 0) ? Z_DEFLATED : 0, - opt_compress_level); - */ - if ((password != NULL) && (err == ZIP_OK)) - err = getFileCrc(filenameinzip, buf, size_buf, &crcFile); - - err = zipOpenNewFileInZip3(zf, filenameinzip, &zi, - NULL, 0, NULL, 0, NULL /* comment*/, - (opt_compress_level != 0) ? Z_DEFLATED : 0, - opt_compress_level, 0, - /* -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, */ - -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, - password, crcFile); - - if (err != ZIP_OK) - printf("error in opening %s in zipfile\n", filenameinzip); - else - { - fin = fopen(filenameinzip, "rb"); - if (fin == NULL) - { - err = ZIP_ERRNO; - printf("error in opening %s for reading\n", filenameinzip); - } - } - - if (err == ZIP_OK) - do - { - err = ZIP_OK; - size_read = (int)fread(buf, 1, size_buf, fin); - if (size_read < size_buf) - if (feof(fin) == 0) - { - printf("error in reading %s\n", filenameinzip); - err = ZIP_ERRNO; - } - - if (size_read > 0) - { - err = zipWriteInFileInZip(zf, buf, size_read); - if (err < 0) - { - printf("error in writing %s in the zipfile\n", - filenameinzip); - } - } - } while ((err == ZIP_OK) && (size_read > 0)); - - if (fin) - fclose(fin); - - if (err < 0) - err = ZIP_ERRNO; - else - { - err = zipCloseFileInZip(zf); - if (err != ZIP_OK) - printf("error in closing %s in the zipfile\n", - filenameinzip); - } - } - } - errclose = zipClose(zf, NULL); - if (errclose != ZIP_OK) - printf("error in closing %s\n", filename_try); - } - else - { - do_help(); - } - - free(buf); - return 0; -} diff --git a/apps/minizip/xmake.lua b/apps/minizip/xmake.lua deleted file mode 100644 index cf8b94f..0000000 --- a/apps/minizip/xmake.lua +++ /dev/null @@ -1,42 +0,0 @@ --- Licensed under the Apache License, Version 2.0 (the "License"); --- You may not use this file except in compliance with the License. --- You may obtain a copy of the License at --- --- http://www.apache.org/licenses/LICENSE-2.0 --- --- Unless required by applicable law or agreed to in writing, software --- distributed under the License is distributed on an "AS IS" BASIS, --- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. --- See the License for the specific language governing permissions and --- limitations under the License. --- --- Copyright (C) 2022-2023 RT-Thread Development Team --- --- @author wcx1024979076 --- @file xmake.lua --- --- Change Logs: --- Date Author Notes --- ------------ ---------- ----------------------------------------------- --- 2023-07-26 wcx1024979076 initial version --- -add_rules("mode.debug", "mode.release") - -add_requires("minizip") - -target("minizip") -do - add_rules("rt.sdk") - add_files("minizip.c") - add_packages("minizip") -end -target_end() - -target("miniunz") -do - add_rules("rt.sdk") - add_files("miniunz.c") - add_packages("minizip") -end -target_end() -