diff --git a/src/myunzip/myunzip.c b/src/myunzip/myunzip.c index bb8d64e25d2da52227fb9dfa0a7a5c54bda15c6c..b07cee661a6963393a236b31f35f9516d9e51f15 100644 --- a/src/myunzip/myunzip.c +++ b/src/myunzip/myunzip.c @@ -2,6 +2,7 @@ #include <string.h> #include <stdlib.h> #include <stdint.h> +#include <sys/stat.h> #include <inflate.h> @@ -30,6 +31,17 @@ typedef struct file_record file_record_t; const int UNCOMPRESSED = 0; const int DEFLATE = 8; +void make_directory_structure(char *fname) { + char *slash = strchr(fname, '/'); + + while (slash != NULL) { + *slash = '\0'; + mkdir(fname, 0777); + *slash = '/'; + slash = strchr(slash + 1, '/'); + } +} + void unzip(FILE *fp) { file_record_t *record = (file_record_t *) malloc(sizeof(file_record_t)); if (!record) { @@ -43,6 +55,8 @@ void unzip(FILE *fp) { char *fname = (char *) malloc(record->file_name_len); fread(fname, 1, record->file_name_len, fp); + make_directory_structure(fname); + /* Move pointer to start of file data */ fseek(fp, record->extra_len, SEEK_CUR); diff --git a/src/myunzip0/myunzip0.c b/src/myunzip0/myunzip0.c index aca88e7e6fc0231aa0f052906c7ff376f44b19e6..ee85b6e0ee5a36df8c8fbd1a8a0e96cf51e45054 100644 --- a/src/myunzip0/myunzip0.c +++ b/src/myunzip0/myunzip0.c @@ -3,6 +3,14 @@ #include <stdlib.h> #include <stdint.h> +/* TODO is it okay to use this? + * The default behavior of the normal zip programs is to save the + * relative paths of the zipped file. + * Then, when unzipping, it creates that path if it doesn't exist. + * Need this to call mkdir(). + */ +#include <sys/stat.h> + struct file_record { uint32_t signature; uint16_t version; @@ -28,6 +36,17 @@ typedef struct file_record file_record_t; const int UNCOMPRESSED = 0; const int DEFLATE = 8; +void make_directory_structure(char *fname) { + char *slash = strchr(fname, '/'); + + while (slash != NULL) { + *slash = '\0'; + mkdir(fname, 0777); + *slash = '/'; + slash = strchr(slash + 1, '/'); + } +} + void unzip(FILE *fp) { file_record_t *record = (file_record_t *) malloc(sizeof(file_record_t)); if (!record) { @@ -54,7 +73,9 @@ void unzip(FILE *fp) { fprintf(stderr, "error: invalid compression\n"); exit(1); } - + + make_directory_structure(fname); + FILE *out = fopen(fname, "wb"); char *buf = (char *) malloc(size * sizeof(char)); diff --git a/test_myunzip0 b/test_myunzip0 deleted file mode 100755 index 6c1be68077365f2dc4381b19d4a631d16bf1199c..0000000000000000000000000000000000000000 Binary files a/test_myunzip0 and /dev/null differ diff --git a/tests/__pycache__/test_myunzip0.cpython-38.pyc b/tests/__pycache__/test_myunzip0.cpython-38.pyc index fecba02928f82db37eaa8b7325d43518570350bf..57fcf59b5c9612017570bfcaa29b4c847018b212 100644 Binary files a/tests/__pycache__/test_myunzip0.cpython-38.pyc and b/tests/__pycache__/test_myunzip0.cpython-38.pyc differ