| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#include "mz.h" |
| 14 |
#include "mz_os.h" |
| 15 |
#include "mz_strm.h" |
| 16 |
#include "mz_strm_buf.h" |
| 17 |
#include "mz_strm_split.h" |
| 18 |
#include "mz_zip.h" |
| 19 |
#include "mz_zip_rw.h" |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
typedef struct minizip_opt_s { |
| 26 |
int64_t disk_size; |
| 27 |
int32_t encoding; |
| 28 |
uint8_t include_path; |
| 29 |
int16_t compress_level; |
| 30 |
uint8_t compress_method; |
| 31 |
uint8_t overwrite; |
| 32 |
uint8_t append; |
| 33 |
uint8_t follow_links; |
| 34 |
uint8_t store_links; |
| 35 |
uint8_t zip_cd; |
| 36 |
uint8_t verbose; |
| 37 |
uint8_t aes; |
| 38 |
} minizip_opt; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
int32_t minizip_banner(void); |
| 43 |
int32_t minizip_help(void); |
| 44 |
|
| 45 |
int32_t minizip_list(const char *path, int32_t encoding); |
| 46 |
|
| 47 |
int32_t minizip_add_entry_cb(void *handle, void *userdata, mz_zip_file *file_info); |
| 48 |
int32_t minizip_add_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position); |
| 49 |
int32_t minizip_add_overwrite_cb(void *handle, void *userdata, const char *path); |
| 50 |
int32_t minizip_add(const char *path, const char *password, minizip_opt *options, int32_t arg_count, const char **args); |
| 51 |
|
| 52 |
int32_t minizip_extract_entry_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path); |
| 53 |
int32_t minizip_extract_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position); |
| 54 |
int32_t minizip_extract_overwrite_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path); |
| 55 |
int32_t minizip_extract(const char *path, const char *pattern, const char *destination, const char *password, |
| 56 |
minizip_opt *options); |
| 57 |
|
| 58 |
int32_t minizip_erase(const char *src_path, const char *target_path, int32_t arg_count, const char **args); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
int32_t minizip_banner(void) { |
| 63 |
printf("minizip-ng %s - https://github.com/zlib-ng/minizip-ng\n", MZ_VERSION); |
| 64 |
printf("---------------------------------------------------\n"); |
| 65 |
return MZ_OK; |
| 66 |
} |
| 67 |
|
| 68 |
int32_t minizip_help(void) { |
| 69 |
printf( |
| 70 |
"Usage: minizip [-x][-d dir|-l|-e][-o][-f][-y][-c cp][-a][-0 to -9][-b|-m|-t][-k 512][-p pwd][-s] file.zip " |
| 71 |
"[files]\n\n" |
| 72 |
" -x Extract files\n" |
| 73 |
" -l List files\n" |
| 74 |
" -d Destination directory\n" |
| 75 |
" -e Erase files\n" |
| 76 |
" -o Overwrite existing files\n" |
| 77 |
" -c File names use cp437 encoding (or specified codepage)\n" |
| 78 |
" -a Append to existing zip file\n" |
| 79 |
" -i Include full path of files\n" |
| 80 |
" -f Follow symbolic links\n" |
| 81 |
" -y Store symbolic links\n" |
| 82 |
" -v Verbose info\n" |
| 83 |
" -0 Store only\n" |
| 84 |
" -1 Compress faster\n" |
| 85 |
" -9 Compress better\n" |
| 86 |
" -k Disk size in KB\n" |
| 87 |
" -z Zip central directory\n" |
| 88 |
" -p Encryption password\n" |
| 89 |
" -s AES encryption\n" |
| 90 |
" -b BZIP2 compression\n" |
| 91 |
" -m LZMA compression\n" |
| 92 |
" -n XZ compression\n" |
| 93 |
" -t ZSTD compression\n\n"); |
| 94 |
return MZ_OK; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
int32_t minizip_list(const char *path, int32_t encoding) { |
| 100 |
mz_zip_file *file_info = NULL; |
| 101 |
uint32_t ratio = 0; |
| 102 |
int32_t err = MZ_OK; |
| 103 |
struct tm tmu_date; |
| 104 |
const char *method = NULL; |
| 105 |
char *utf8_string = NULL; |
| 106 |
char crypt = ' '; |
| 107 |
void *reader = NULL; |
| 108 |
|
| 109 |
reader = mz_zip_reader_create(); |
| 110 |
if (!reader) |
| 111 |
return MZ_MEM_ERROR; |
| 112 |
|
| 113 |
err = mz_zip_reader_open_file(reader, path); |
| 114 |
if (err != MZ_OK) { |
| 115 |
printf("Error %" PRId32 " opening archive %s\n", err, path); |
| 116 |
mz_zip_reader_delete(&reader); |
| 117 |
return err; |
| 118 |
} |
| 119 |
|
| 120 |
mz_zip_reader_set_encoding(reader, encoding); |
| 121 |
|
| 122 |
err = mz_zip_reader_goto_first_entry(reader); |
| 123 |
|
| 124 |
if (err != MZ_OK && err != MZ_END_OF_LIST) { |
| 125 |
printf("Error %" PRId32 " going to first entry in archive\n", err); |
| 126 |
mz_zip_reader_delete(&reader); |
| 127 |
return err; |
| 128 |
} |
| 129 |
|
| 130 |
printf(" Packed Unpacked Ratio Method Attribs Date Time CRC-32 Name\n"); |
| 131 |
printf(" ------ -------- ----- ------ ------- ---- ---- ------ ----\n"); |
| 132 |
|
| 133 |
|
| 134 |
while (err == MZ_OK) { |
| 135 |
err = mz_zip_reader_entry_get_info(reader, &file_info); |
| 136 |
|
| 137 |
if (err != MZ_OK) { |
| 138 |
printf("Error %" PRId32 " getting entry info in archive\n", err); |
| 139 |
break; |
| 140 |
} |
| 141 |
|
| 142 |
ratio = 0; |
| 143 |
if (file_info->uncompressed_size > 0) |
| 144 |
ratio = (uint32_t)((file_info->compressed_size * 100) / file_info->uncompressed_size); |
| 145 |
|
| 146 |
|
| 147 |
if (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) |
| 148 |
crypt = '*'; |
| 149 |
else |
| 150 |
crypt = ' '; |
| 151 |
|
| 152 |
method = mz_zip_get_compression_method_string(file_info->compression_method); |
| 153 |
mz_zip_time_t_to_tm(file_info->modified_date, &tmu_date); |
| 154 |
|
| 155 |
if ((encoding > 0) && (file_info->flag & MZ_ZIP_FLAG_UTF8) == 0) { |
| 156 |
utf8_string = mz_os_utf8_string_create(file_info->filename, encoding); |
| 157 |
if (!utf8_string) { |
| 158 |
err = MZ_MEM_ERROR; |
| 159 |
printf("Error %" PRId32 " creating UTF-8 string\n", err); |
| 160 |
break; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
printf("%12" PRId64 " %12" PRId64 " %3" PRIu32 "%% %6s%c %8" PRIx32 " %2.2" PRIu32 "-%2.2" PRIu32 |
| 166 |
"-%2.2" PRIu32 " %2.2" PRIu32 ":%2.2" PRIu32 " %8.8" PRIx32 " %s\n", |
| 167 |
file_info->compressed_size, file_info->uncompressed_size, ratio, method, crypt, file_info->external_fa, |
| 168 |
(uint32_t)tmu_date.tm_mon + 1, (uint32_t)tmu_date.tm_mday, (uint32_t)tmu_date.tm_year % 100, |
| 169 |
(uint32_t)tmu_date.tm_hour, (uint32_t)tmu_date.tm_min, file_info->crc, |
| 170 |
utf8_string ? utf8_string : file_info->filename); |
| 171 |
|
| 172 |
if (utf8_string) |
| 173 |
mz_os_utf8_string_delete(&utf8_string); |
| 174 |
|
| 175 |
err = mz_zip_reader_goto_next_entry(reader); |
| 176 |
|
| 177 |
if (err != MZ_OK && err != MZ_END_OF_LIST) { |
| 178 |
printf("Error %" PRId32 " going to next entry in archive\n", err); |
| 179 |
break; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
mz_zip_reader_delete(&reader); |
| 184 |
|
| 185 |
if (err == MZ_END_OF_LIST) |
| 186 |
return MZ_OK; |
| 187 |
|
| 188 |
return err; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
int32_t minizip_add_entry_cb(void *handle, void *userdata, mz_zip_file *file_info) { |
| 194 |
MZ_UNUSED(handle); |
| 195 |
MZ_UNUSED(userdata); |
| 196 |
|
| 197 |
|
| 198 |
printf("Adding %s\n", file_info->filename); |
| 199 |
return MZ_OK; |
| 200 |
} |
| 201 |
|
| 202 |
int32_t minizip_add_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position) { |
| 203 |
minizip_opt *options = (minizip_opt *)userdata; |
| 204 |
double progress = 0; |
| 205 |
uint8_t raw = 0; |
| 206 |
|
| 207 |
MZ_UNUSED(userdata); |
| 208 |
|
| 209 |
mz_zip_writer_get_raw(handle, &raw); |
| 210 |
|
| 211 |
if (raw && file_info->compressed_size > 0) |
| 212 |
progress = ((double)position / file_info->compressed_size) * 100; |
| 213 |
else if (!raw && file_info->uncompressed_size > 0) |
| 214 |
progress = ((double)position / file_info->uncompressed_size) * 100; |
| 215 |
|
| 216 |
|
| 217 |
if (options->verbose) { |
| 218 |
printf("%s - %" PRId64 " / %" PRId64 " (%.02f%%)\n", file_info->filename, position, |
| 219 |
file_info->uncompressed_size, progress); |
| 220 |
} |
| 221 |
return MZ_OK; |
| 222 |
} |
| 223 |
|
| 224 |
int32_t minizip_add_overwrite_cb(void *handle, void *userdata, const char *path) { |
| 225 |
minizip_opt *options = (minizip_opt *)userdata; |
| 226 |
|
| 227 |
MZ_UNUSED(handle); |
| 228 |
|
| 229 |
if (!options->overwrite) { |
| 230 |
|
| 231 |
char rep = 0; |
| 232 |
do { |
| 233 |
char answer[128]; |
| 234 |
printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ", path); |
| 235 |
if (scanf("%1s", answer) != 1) |
| 236 |
exit(EXIT_FAILURE); |
| 237 |
rep = answer[0]; |
| 238 |
|
| 239 |
if ((rep >= 'a') && (rep <= 'z')) |
| 240 |
rep -= 0x20; |
| 241 |
} while ((rep != 'Y') && (rep != 'N') && (rep != 'A')); |
| 242 |
|
| 243 |
if (rep == 'A') { |
| 244 |
return MZ_EXIST_ERROR; |
| 245 |
} else if (rep == 'N') { |
| 246 |
return MZ_INTERNAL_ERROR; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
return MZ_OK; |
| 251 |
} |
| 252 |
|
| 253 |
int32_t minizip_add(const char *path, const char *password, minizip_opt *options, int32_t arg_count, |
| 254 |
const char **args) { |
| 255 |
void *writer = NULL; |
| 256 |
int32_t err = MZ_OK; |
| 257 |
int32_t err_close = MZ_OK; |
| 258 |
int32_t i = 0; |
| 259 |
const char *filename_in_zip = NULL; |
| 260 |
|
| 261 |
printf("Archive %s\n", path); |
| 262 |
|
| 263 |
|
| 264 |
writer = mz_zip_writer_create(); |
| 265 |
if (!writer) |
| 266 |
return MZ_MEM_ERROR; |
| 267 |
|
| 268 |
mz_zip_writer_set_password(writer, password); |
| 269 |
mz_zip_writer_set_aes(writer, options->aes); |
| 270 |
mz_zip_writer_set_compress_method(writer, options->compress_method); |
| 271 |
mz_zip_writer_set_compress_level(writer, options->compress_level); |
| 272 |
mz_zip_writer_set_follow_links(writer, options->follow_links); |
| 273 |
mz_zip_writer_set_store_links(writer, options->store_links); |
| 274 |
mz_zip_writer_set_overwrite_cb(writer, options, minizip_add_overwrite_cb); |
| 275 |
mz_zip_writer_set_progress_cb(writer, options, minizip_add_progress_cb); |
| 276 |
mz_zip_writer_set_entry_cb(writer, options, minizip_add_entry_cb); |
| 277 |
mz_zip_writer_set_zip_cd(writer, options->zip_cd); |
| 278 |
|
| 279 |
err = mz_zip_writer_open_file(writer, path, options->disk_size, options->append); |
| 280 |
|
| 281 |
if (err == MZ_OK) { |
| 282 |
for (i = 0; i < arg_count; i += 1) { |
| 283 |
filename_in_zip = args[i]; |
| 284 |
|
| 285 |
|
| 286 |
err = mz_zip_writer_add_path(writer, filename_in_zip, NULL, options->include_path, 1); |
| 287 |
if (err != MZ_OK) |
| 288 |
printf("Error %" PRId32 " adding path to archive %s\n", err, filename_in_zip); |
| 289 |
} |
| 290 |
} else { |
| 291 |
printf("Error %" PRId32 " opening archive for writing\n", err); |
| 292 |
} |
| 293 |
|
| 294 |
err_close = mz_zip_writer_close(writer); |
| 295 |
if (err_close != MZ_OK) { |
| 296 |
printf("Error %" PRId32 " closing archive for writing %s\n", err_close, path); |
| 297 |
err = err_close; |
| 298 |
} |
| 299 |
|
| 300 |
mz_zip_writer_delete(&writer); |
| 301 |
return err; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
int32_t minizip_extract_entry_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path) { |
| 307 |
minizip_opt *options = (minizip_opt *)userdata; |
| 308 |
char *utf8_string = NULL; |
| 309 |
|
| 310 |
MZ_UNUSED(path); |
| 311 |
|
| 312 |
if ((options->encoding > 0) && (file_info->flag & MZ_ZIP_FLAG_UTF8) == 0) { |
| 313 |
utf8_string = mz_os_utf8_string_create(file_info->filename, options->encoding); |
| 314 |
if (!utf8_string) |
| 315 |
return MZ_MEM_ERROR; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
printf("Extracting %s\n", utf8_string ? utf8_string : file_info->filename); |
| 320 |
|
| 321 |
if (utf8_string) |
| 322 |
mz_os_utf8_string_delete(&utf8_string); |
| 323 |
|
| 324 |
return MZ_OK; |
| 325 |
} |
| 326 |
|
| 327 |
int32_t minizip_extract_progress_cb(void *handle, void *userdata, mz_zip_file *file_info, int64_t position) { |
| 328 |
minizip_opt *options = (minizip_opt *)userdata; |
| 329 |
double progress = 0; |
| 330 |
uint8_t raw = 0; |
| 331 |
|
| 332 |
mz_zip_reader_get_raw(handle, &raw); |
| 333 |
|
| 334 |
if (raw && file_info->compressed_size > 0) |
| 335 |
progress = ((double)position / file_info->compressed_size) * 100; |
| 336 |
else if (!raw && file_info->uncompressed_size > 0) |
| 337 |
progress = ((double)position / file_info->uncompressed_size) * 100; |
| 338 |
|
| 339 |
|
| 340 |
if (options->verbose) { |
| 341 |
printf("%s - %" PRId64 " / %" PRId64 " (%.02f%%)\n", file_info->filename, position, |
| 342 |
file_info->uncompressed_size, progress); |
| 343 |
} |
| 344 |
|
| 345 |
return MZ_OK; |
| 346 |
} |
| 347 |
|
| 348 |
int32_t minizip_extract_overwrite_cb(void *handle, void *userdata, mz_zip_file *file_info, const char *path) { |
| 349 |
minizip_opt *options = (minizip_opt *)userdata; |
| 350 |
|
| 351 |
MZ_UNUSED(handle); |
| 352 |
MZ_UNUSED(file_info); |
| 353 |
|
| 354 |
|
| 355 |
if (!options->overwrite) { |
| 356 |
char rep = 0; |
| 357 |
do { |
| 358 |
char answer[128]; |
| 359 |
printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ", path); |
| 360 |
if (scanf("%1s", answer) != 1) |
| 361 |
exit(EXIT_FAILURE); |
| 362 |
rep = answer[0]; |
| 363 |
if ((rep >= 'a') && (rep <= 'z')) |
| 364 |
rep -= 0x20; |
| 365 |
} while ((rep != 'Y') && (rep != 'N') && (rep != 'A')); |
| 366 |
|
| 367 |
if (rep == 'N') |
| 368 |
return MZ_EXIST_ERROR; |
| 369 |
if (rep == 'A') |
| 370 |
options->overwrite = 1; |
| 371 |
} |
| 372 |
|
| 373 |
return MZ_OK; |
| 374 |
} |
| 375 |
|
| 376 |
int32_t minizip_extract(const char *path, const char *pattern, const char *destination, const char *password, |
| 377 |
minizip_opt *options) { |
| 378 |
void *reader = NULL; |
| 379 |
int32_t err = MZ_OK; |
| 380 |
int32_t err_close = MZ_OK; |
| 381 |
|
| 382 |
printf("Archive %s\n", path); |
| 383 |
|
| 384 |
|
| 385 |
reader = mz_zip_reader_create(); |
| 386 |
if (!reader) |
| 387 |
return MZ_MEM_ERROR; |
| 388 |
|
| 389 |
mz_zip_reader_set_pattern(reader, pattern, 1); |
| 390 |
mz_zip_reader_set_password(reader, password); |
| 391 |
mz_zip_reader_set_encoding(reader, options->encoding); |
| 392 |
mz_zip_reader_set_entry_cb(reader, options, minizip_extract_entry_cb); |
| 393 |
mz_zip_reader_set_progress_cb(reader, options, minizip_extract_progress_cb); |
| 394 |
mz_zip_reader_set_overwrite_cb(reader, options, minizip_extract_overwrite_cb); |
| 395 |
|
| 396 |
err = mz_zip_reader_open_file(reader, path); |
| 397 |
|
| 398 |
if (err != MZ_OK) { |
| 399 |
printf("Error %" PRId32 " opening archive %s\n", err, path); |
| 400 |
} else { |
| 401 |
|
| 402 |
err = mz_zip_reader_save_all(reader, destination); |
| 403 |
|
| 404 |
if (err == MZ_END_OF_LIST) { |
| 405 |
if (pattern) { |
| 406 |
printf("Files matching %s not found in archive\n", pattern); |
| 407 |
} else { |
| 408 |
printf("No files in archive\n"); |
| 409 |
err = MZ_OK; |
| 410 |
} |
| 411 |
} else if (err != MZ_OK) { |
| 412 |
printf("Error %" PRId32 " saving entries to disk %s\n", err, path); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
err_close = mz_zip_reader_close(reader); |
| 417 |
if (err_close != MZ_OK) { |
| 418 |
printf("Error %" PRId32 " closing archive for reading\n", err_close); |
| 419 |
err = err_close; |
| 420 |
} |
| 421 |
|
| 422 |
mz_zip_reader_delete(&reader); |
| 423 |
return err; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
int32_t minizip_erase(const char *src_path, const char *target_path, int32_t arg_count, const char **args) { |
| 429 |
mz_zip_file *file_info = NULL; |
| 430 |
const char *filename_in_zip = NULL; |
| 431 |
const char *target_path_ptr = target_path; |
| 432 |
void *reader = NULL; |
| 433 |
void *writer = NULL; |
| 434 |
int32_t skip = 0; |
| 435 |
int32_t err = MZ_OK; |
| 436 |
int32_t i = 0; |
| 437 |
uint8_t zip_cd = 0; |
| 438 |
char bak_path[256]; |
| 439 |
char tmp_path[256]; |
| 440 |
|
| 441 |
if (!target_path) { |
| 442 |
|
| 443 |
strncpy(tmp_path, src_path, sizeof(tmp_path) - 1); |
| 444 |
tmp_path[sizeof(tmp_path) - 1] = 0; |
| 445 |
strncat(tmp_path, ".tmp.zip", sizeof(tmp_path) - strlen(tmp_path) - 1); |
| 446 |
target_path_ptr = tmp_path; |
| 447 |
} |
| 448 |
|
| 449 |
reader = mz_zip_reader_create(); |
| 450 |
if (!reader) |
| 451 |
return MZ_MEM_ERROR; |
| 452 |
writer = mz_zip_writer_create(); |
| 453 |
if (!writer) { |
| 454 |
mz_zip_reader_delete(&reader); |
| 455 |
return MZ_MEM_ERROR; |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
err = mz_zip_reader_open_file(reader, src_path); |
| 460 |
if (err != MZ_OK) { |
| 461 |
printf("Error %" PRId32 " opening archive for reading %s\n", err, src_path); |
| 462 |
mz_zip_reader_delete(&reader); |
| 463 |
mz_zip_writer_delete(&writer); |
| 464 |
return err; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
err = mz_zip_writer_open_file(writer, target_path_ptr, 0, 0); |
| 469 |
if (err != MZ_OK) { |
| 470 |
printf("Error %" PRId32 " opening archive for writing %s\n", err, target_path_ptr); |
| 471 |
mz_zip_reader_delete(&reader); |
| 472 |
mz_zip_writer_delete(&writer); |
| 473 |
return err; |
| 474 |
} |
| 475 |
|
| 476 |
err = mz_zip_reader_goto_first_entry(reader); |
| 477 |
|
| 478 |
if (err != MZ_OK && err != MZ_END_OF_LIST) |
| 479 |
printf("Error %" PRId32 " going to first entry in archive\n", err); |
| 480 |
|
| 481 |
while (err == MZ_OK) { |
| 482 |
err = mz_zip_reader_entry_get_info(reader, &file_info); |
| 483 |
if (err != MZ_OK) { |
| 484 |
printf("Error %" PRId32 " getting info from archive\n", err); |
| 485 |
break; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
for (i = 0, skip = 0; i < arg_count; i += 1) { |
| 491 |
filename_in_zip = args[i]; |
| 492 |
|
| 493 |
if (mz_path_compare_wc(file_info->filename, filename_in_zip, 1) == MZ_OK) |
| 494 |
skip = 1; |
| 495 |
} |
| 496 |
|
| 497 |
if (skip) { |
| 498 |
printf("Skipping %s\n", file_info->filename); |
| 499 |
} else { |
| 500 |
printf("Copying %s\n", file_info->filename); |
| 501 |
err = mz_zip_writer_copy_from_reader(writer, reader); |
| 502 |
} |
| 503 |
|
| 504 |
if (err != MZ_OK) { |
| 505 |
printf("Error %" PRId32 " copying entry into new zip\n", err); |
| 506 |
break; |
| 507 |
} |
| 508 |
|
| 509 |
err = mz_zip_reader_goto_next_entry(reader); |
| 510 |
|
| 511 |
if (err != MZ_OK && err != MZ_END_OF_LIST) |
| 512 |
printf("Error %" PRId32 " going to next entry in archive\n", err); |
| 513 |
} |
| 514 |
|
| 515 |
mz_zip_reader_get_zip_cd(reader, &zip_cd); |
| 516 |
mz_zip_writer_set_zip_cd(writer, zip_cd); |
| 517 |
|
| 518 |
mz_zip_reader_close(reader); |
| 519 |
mz_zip_reader_delete(&reader); |
| 520 |
|
| 521 |
mz_zip_writer_close(writer); |
| 522 |
mz_zip_writer_delete(&writer); |
| 523 |
|
| 524 |
if (err == MZ_END_OF_LIST) { |
| 525 |
if (!target_path) { |
| 526 |
|
| 527 |
strncpy(bak_path, src_path, sizeof(bak_path) - 1); |
| 528 |
bak_path[sizeof(bak_path) - 1] = 0; |
| 529 |
strncat(bak_path, ".bak", sizeof(bak_path) - strlen(bak_path) - 1); |
| 530 |
|
| 531 |
if (mz_os_file_exists(bak_path) == MZ_OK) |
| 532 |
mz_os_unlink(bak_path); |
| 533 |
|
| 534 |
if (mz_os_rename(src_path, bak_path) != MZ_OK) |
| 535 |
printf("Error backing up archive before replacing %s\n", bak_path); |
| 536 |
|
| 537 |
if (mz_os_rename(tmp_path, src_path) != MZ_OK) |
| 538 |
printf("Error replacing archive with temp %s\n", tmp_path); |
| 539 |
} |
| 540 |
|
| 541 |
return MZ_OK; |
| 542 |
} |
| 543 |
|
| 544 |
return err; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
#if !defined(MZ_ZIP_NO_MAIN) |
| 550 |
int main(int argc, const char *argv[]) { |
| 551 |
minizip_opt options; |
| 552 |
int32_t path_arg = 0; |
| 553 |
int32_t err = 0; |
| 554 |
int32_t i = 0; |
| 555 |
uint8_t do_list = 0; |
| 556 |
uint8_t do_extract = 0; |
| 557 |
uint8_t do_erase = 0; |
| 558 |
const char *path = NULL; |
| 559 |
const char *password = NULL; |
| 560 |
const char *destination = NULL; |
| 561 |
const char *filename_to_extract = NULL; |
| 562 |
|
| 563 |
minizip_banner(); |
| 564 |
if (argc == 1) { |
| 565 |
minizip_help(); |
| 566 |
return 0; |
| 567 |
} |
| 568 |
|
| 569 |
memset(&options, 0, sizeof(options)); |
| 570 |
|
| 571 |
options.compress_method = MZ_COMPRESS_METHOD_DEFLATE; |
| 572 |
options.compress_level = MZ_COMPRESS_LEVEL_DEFAULT; |
| 573 |
|
| 574 |
|
| 575 |
for (i = 1; i < argc; i += 1) { |
| 576 |
printf("%s ", argv[i]); |
| 577 |
if (argv[i][0] == '-') { |
| 578 |
char c = argv[i][1]; |
| 579 |
if ((c == 'l') || (c == 'L')) |
| 580 |
do_list = 1; |
| 581 |
else if ((c == 'x') || (c == 'X')) |
| 582 |
do_extract = 1; |
| 583 |
else if ((c == 'e') || (c == 'E')) |
| 584 |
do_erase = 1; |
| 585 |
else if ((c == 'a') || (c == 'A')) |
| 586 |
options.append = 1; |
| 587 |
else if ((c == 'o') || (c == 'O')) |
| 588 |
options.overwrite = 1; |
| 589 |
else if ((c == 'f') || (c == 'F')) |
| 590 |
options.follow_links = 1; |
| 591 |
else if ((c == 'y') || (c == 'Y')) |
| 592 |
options.store_links = 1; |
| 593 |
else if ((c == 'i') || (c == 'I')) |
| 594 |
options.include_path = 1; |
| 595 |
else if ((c == 'z') || (c == 'Z')) |
| 596 |
options.zip_cd = 1; |
| 597 |
else if ((c == 'v') || (c == 'V')) |
| 598 |
options.verbose = 1; |
| 599 |
else if ((c >= '0') && (c <= '9')) { |
| 600 |
options.compress_level = (int16_t)atoi(&argv[i][1]); |
| 601 |
if (options.compress_level == 0) |
| 602 |
options.compress_method = MZ_COMPRESS_METHOD_STORE; |
| 603 |
} else if ((c == 'b') || (c == 'B')) |
| 604 |
# ifdef HAVE_BZIP2 |
| 605 |
options.compress_method = MZ_COMPRESS_METHOD_BZIP2; |
| 606 |
# else |
| 607 |
err = MZ_SUPPORT_ERROR; |
| 608 |
# endif |
| 609 |
else if ((c == 'm') || (c == 'M')) |
| 610 |
# ifdef HAVE_LZMA |
| 611 |
options.compress_method = MZ_COMPRESS_METHOD_LZMA; |
| 612 |
# else |
| 613 |
err = MZ_SUPPORT_ERROR; |
| 614 |
# endif |
| 615 |
else if ((c == 'n') || (c == 'N')) |
| 616 |
# if defined(HAVE_LZMA) || defined(HAVE_LIBCOMP) |
| 617 |
options.compress_method = MZ_COMPRESS_METHOD_XZ; |
| 618 |
# else |
| 619 |
err = MZ_SUPPORT_ERROR; |
| 620 |
# endif |
| 621 |
else if ((c == 't') || (c == 'T')) |
| 622 |
# ifdef HAVE_ZSTD |
| 623 |
options.compress_method = MZ_COMPRESS_METHOD_ZSTD; |
| 624 |
# else |
| 625 |
err = MZ_SUPPORT_ERROR; |
| 626 |
# endif |
| 627 |
else if ((c == 's') || (c == 'S')) |
| 628 |
# ifdef HAVE_WZAES |
| 629 |
options.aes = 1; |
| 630 |
# else |
| 631 |
err = MZ_SUPPORT_ERROR; |
| 632 |
# endif |
| 633 |
else if (((c == 'c') || (c == 'C')) && (i + 1 < argc)) { |
| 634 |
options.encoding = (int32_t)atoi(argv[i + 1]); |
| 635 |
i += 1; |
| 636 |
} else if (((c == 'k') || (c == 'K')) && (i + 1 < argc)) { |
| 637 |
options.disk_size = (int64_t)atoi(argv[i + 1]) * 1024; |
| 638 |
printf("%s ", argv[i + 1]); |
| 639 |
i += 1; |
| 640 |
} else if (((c == 'd') || (c == 'D')) && (i + 1 < argc)) { |
| 641 |
destination = argv[i + 1]; |
| 642 |
printf("%s ", argv[i + 1]); |
| 643 |
i += 1; |
| 644 |
} else if (((c == 'p') || (c == 'P')) && (i + 1 < argc)) { |
| 645 |
# ifndef MZ_ZIP_NO_ENCRYPTION |
| 646 |
password = argv[i + 1]; |
| 647 |
printf("*** "); |
| 648 |
# else |
| 649 |
err = MZ_SUPPORT_ERROR; |
| 650 |
# endif |
| 651 |
i += 1; |
| 652 |
} |
| 653 |
} else if (path_arg == 0) |
| 654 |
path_arg = i; |
| 655 |
} |
| 656 |
printf("\n"); |
| 657 |
|
| 658 |
if (err == MZ_SUPPORT_ERROR) { |
| 659 |
printf("Feature not supported\n"); |
| 660 |
return err; |
| 661 |
} |
| 662 |
|
| 663 |
if (path_arg == 0) { |
| 664 |
minizip_help(); |
| 665 |
return 0; |
| 666 |
} |
| 667 |
|
| 668 |
path = argv[path_arg]; |
| 669 |
|
| 670 |
if (do_list) { |
| 671 |
|
| 672 |
err = minizip_list(path, options.encoding); |
| 673 |
} else if (do_extract) { |
| 674 |
if (argc > path_arg + 1) |
| 675 |
filename_to_extract = argv[path_arg + 1]; |
| 676 |
|
| 677 |
|
| 678 |
err = minizip_extract(path, filename_to_extract, destination, password, &options); |
| 679 |
} else if (do_erase) { |
| 680 |
|
| 681 |
err = minizip_erase(path, NULL, argc - (path_arg + 1), &argv[path_arg + 1]); |
| 682 |
} else { |
| 683 |
|
| 684 |
err = minizip_add(path, password, &options, argc - (path_arg + 1), &argv[path_arg + 1]); |
| 685 |
} |
| 686 |
|
| 687 |
return err; |
| 688 |
} |
| 689 |
#endif |