forked from zlib-ng/minizip-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminizip.c
More file actions
762 lines (637 loc) · 21.1 KB
/
minizip.c
File metadata and controls
762 lines (637 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/* minizip.c
Version 2.2.7, January 30th, 2018
part of the MiniZip project
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 2009-2010 Mathias Svensson
Modifications for Zip64 support
http://result42.com
Copyright (C) 2007-2008 Even Rouault
Modifications of Unzip for Zip64
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <time.h>
#include <errno.h>
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_buf.h"
#include "mz_strm_split.h"
#include "mz_zip.h"
/***************************************************************************/
void minizip_banner()
{
printf("Minizip %s - https://github.com/nmoinvaz/minizip\n", MZ_VERSION);
printf("---------------------------------------------------\n");
}
void minizip_help()
{
printf("Usage : minizip [-x -d dir|-l] [-o] [-a] [-j] [-0 to -9] [-b|-m] [-k 512] [-p pwd] [-s] file.zip [files]\n\n" \
" -x Extract files\n" \
" -l List files\n" \
" -d Destination directory\n" \
" -o Overwrite existing files\n" \
" -a Append to existing zip file\n" \
" -u Buffered reading and writing\n" \
" -j Exclude path of files\n" \
" -0 Store only\n" \
" -1 Compress faster\n" \
" -9 Compress better\n" \
" -k Disk size in KB\n" \
" -p Encryption password\n");
#ifdef HAVE_AES
printf(" -s AES encryption\n");
#endif
#ifdef HAVE_BZIP2
printf(" -b BZIP2 compression\n");
#endif
#ifdef HAVE_LZMA
printf(" -m LZMA compression\n");
#endif
printf("\n");
}
/***************************************************************************/
typedef struct minizip_opt_s {
uint8_t exclude_path;
int16_t compress_level;
int16_t compress_method;
uint8_t overwrite;
#ifdef HAVE_AES
int16_t aes;
#endif
} minizip_opt;
/***************************************************************************/
int32_t minizip_add_file(void *handle, const char *path, const char *password, minizip_opt *options)
{
mz_zip_file file_info = { 0 };
int32_t read = 0;
int32_t written = 0;
int32_t err = MZ_OK;
int32_t err_close = MZ_OK;
void *stream = NULL;
const char *filenameinzip = NULL;
char buf[INT16_MAX];
// Construct the filename that our file will be stored in the zip as.
// The path name saved, should not include a leading slash.
// If it did, windows/xp and dynazip couldn't read the zip file.
filenameinzip = path;
while (filenameinzip[0] == '\\' || filenameinzip[0] == '/')
filenameinzip += 1;
// Should the file be stored with any path info at all?
if (options->exclude_path)
{
const char *match = NULL;
const char *last_slash = NULL;
for (match = filenameinzip; *match; match += 1)
{
if (*match == '\\' || *match == '/')
last_slash = match;
}
if (last_slash != NULL)
filenameinzip = last_slash + 1; // base filename follows last slash
}
// Get information about the file on disk so we can store it in zip
printf("Adding: %s\n", filenameinzip);
file_info.version_madeby = MZ_VERSION_MADEBY;
file_info.compression_method = options->compress_method;
file_info.filename = (char *)filenameinzip;
file_info.uncompressed_size = mz_os_get_file_size(path);
#ifdef HAVE_AES
if (options->aes)
file_info.aes_version = MZ_AES_VERSION;
#endif
mz_os_get_file_date(path, &file_info.modified_date, &file_info.accessed_date,
&file_info.creation_date);
// Add to zip
err = mz_zip_entry_write_open(handle, &file_info, options->compress_level, password);
if (err != MZ_OK)
{
printf("Error in opening %s in zip file (%d)\n", filenameinzip, err);
return err;
}
mz_stream_os_create(&stream);
err = mz_stream_os_open(stream, path, MZ_OPEN_MODE_READ);
if (err == MZ_OK)
{
// Read contents of file and write it to zip
do
{
read = mz_stream_os_read(stream, buf, sizeof(buf));
if (read < 0)
{
err = mz_stream_os_error(stream);
printf("Error %d in reading %s\n", err, filenameinzip);
break;
}
if (read == 0)
break;
written = mz_zip_entry_write(handle, buf, read);
if (written != read)
{
err = mz_stream_os_error(stream);
printf("Error in writing %s in the zip file (%d)\n", filenameinzip, err);
break;
}
}
while (err == MZ_OK);
mz_stream_os_close(stream);
}
else
{
printf("Error in opening %s for reading\n", path);
}
mz_stream_os_delete(&stream);
err_close = mz_zip_entry_close(handle);
if (err_close != MZ_OK)
{
printf("Error in closing %s in the zip file (%d)\n", filenameinzip, err_close);
err = err_close;
}
return err;
}
int32_t minizip_add(void *handle, const char *path, const char *password, minizip_opt *options, uint8_t recursive)
{
DIR *dir = NULL;
struct dirent *entry = NULL;
int32_t err = 0;
char full_path[320];
if (mz_os_is_dir(path) != MZ_OK)
return minizip_add_file(handle, path, password, options);
dir = mz_os_open_dir(path);
if (dir == NULL)
{
printf("Cannot enumerate directory %s\n", path);
return MZ_EXIST_ERROR;
}
while ((entry = mz_os_read_dir(dir)) != NULL)
{
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
full_path[0] = 0;
mz_path_combine(full_path, path, sizeof(full_path));
mz_path_combine(full_path, entry->d_name, sizeof(full_path));
if (!recursive && mz_os_is_dir(full_path))
continue;
err = minizip_add(handle, full_path, password, options, recursive);
if (err != MZ_OK)
return err;
}
mz_os_close_dir(dir);
return MZ_OK;
}
/***************************************************************************/
int32_t minizip_list(void *handle)
{
mz_zip_file *file_info = NULL;
uint32_t ratio = 0;
int16_t level = 0;
int32_t err = MZ_OK;
struct tm tmu_date = { 0 };
const char *string_method = NULL;
char crypt = ' ';
err = mz_zip_goto_first_entry(handle);
if (err != MZ_OK && err != MZ_END_OF_LIST)
{
printf("Error %d going to first entry in zip file\n", err);
return err;
}
printf(" Length Method Size Ratio Date Time CRC-32 Name\n");
printf(" ------ ------ ---- ----- ---- ---- ------ ----\n");
while (err == MZ_OK)
{
err = mz_zip_entry_get_info(handle, &file_info);
if (err != MZ_OK)
{
printf("Error %d getting entry info in zip file\n", err);
break;
}
if (file_info->uncompressed_size > 0)
ratio = (uint32_t)((file_info->compressed_size * 100) / file_info->uncompressed_size);
// Display a '*' if the file is encrypted
if (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED)
crypt = '*';
switch (file_info->compression_method)
{
case MZ_COMPRESS_METHOD_RAW:
string_method = "Stored";
break;
case MZ_COMPRESS_METHOD_DEFLATE:
level = (int16_t)((file_info->flag & 0x6) / 2);
if (level == 0)
string_method = "Defl:N";
else if (level == 1)
string_method = "Defl:X";
else if ((level == 2) || (level == 3))
string_method = "Defl:F"; // 2: fast , 3: extra fast
else
string_method = "Defl:?";
break;
case MZ_COMPRESS_METHOD_BZIP2:
string_method = "BZip2";
break;
case MZ_COMPRESS_METHOD_LZMA:
string_method = "LZMA";
break;
default:
string_method = "Unknwn";
}
mz_zip_time_t_to_tm(file_info->modified_date, &tmu_date);
printf(" %7"PRIu64" %6s%c %7"PRIu64" %3"PRIu32"%% %2.2"PRIu32"-%2.2"PRIu32\
"-%2.2"PRIu32" %2.2"PRIu32":%2.2"PRIu32" %8.8"PRIx32" %s\n",
file_info->uncompressed_size, string_method, crypt, file_info->compressed_size, ratio,
(uint32_t)tmu_date.tm_mon + 1, (uint32_t)tmu_date.tm_mday,
(uint32_t)tmu_date.tm_year % 100,
(uint32_t)tmu_date.tm_hour, (uint32_t)tmu_date.tm_min,
file_info->crc, file_info->filename);
err = mz_zip_goto_next_entry(handle);
if (err != MZ_OK && err != MZ_END_OF_LIST)
{
printf("Error %d going to next entry in zip file\n", err);
return err;
}
}
if (err == MZ_END_OF_LIST)
return MZ_OK;
return err;
}
/***************************************************************************/
int32_t minizip_extract_currentfile(void *handle, const char *destination, const char *password, minizip_opt *options)
{
mz_zip_file *file_info = NULL;
uint8_t buf[INT16_MAX];
int32_t read = 0;
int32_t written = 0;
int32_t err = MZ_OK;
int32_t err_close = MZ_OK;
uint8_t skip = 0;
void *stream = NULL;
char *match = NULL;
char *filename = NULL;
char out_path[512];
char directory[512];
err = mz_zip_entry_get_info(handle, &file_info);
if (err != MZ_OK)
{
printf("Error %d getting entry info in zip file\n", err);
return err;
}
match = filename = file_info->filename;
while (*match != 0)
{
if ((*match == '/') || (*match == '\\'))
filename = match + 1;
match += 1;
}
// Construct output path
out_path[0] = 0;
if (destination != NULL)
mz_path_combine(out_path, destination, sizeof(out_path));
if (options->exclude_path)
mz_path_combine(out_path, filename, sizeof(out_path));
else
mz_path_combine(out_path, file_info->filename, sizeof(out_path));
// If zip entry is a directory then create it on disk
if (*filename == 0)
{
if (options->exclude_path == 0)
{
printf("Creating directory: %s\n", out_path);
mz_make_dir(out_path);
}
return err;
}
err = mz_zip_entry_read_open(handle, 0, password);
if (err != MZ_OK)
{
printf("Error %d opening entry in zip file\n", err);
return err;
}
// Determine if the file should be overwritten or not and ask the user if needed
if ((err == MZ_OK) && (options->overwrite == 0) && (mz_os_file_exists(out_path) == MZ_OK))
{
char rep = 0;
do
{
char answer[128];
printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ", out_path);
if (scanf("%1s", answer) != 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')
options->overwrite = 1;
}
mz_stream_os_create(&stream);
// Create the file on disk so we can unzip to it
if ((skip == 0) && (err == MZ_OK))
{
// Some zips don't contain directory alone before file
if ((mz_stream_os_open(stream, out_path, MZ_OPEN_MODE_CREATE) != MZ_OK) &&
(options->exclude_path == 0) && (filename != file_info->filename))
{
// Create the directory of the output path
strncpy(directory, out_path, sizeof(directory));
match = directory + strlen(directory) - 1;
while (match > directory)
{
if ((*match == '/') || (*match == '\\'))
{
*match = 0;
break;
}
match -= 1;
}
mz_make_dir(directory);
mz_stream_os_open(stream, out_path, MZ_OPEN_MODE_CREATE);
}
}
// Read from the zip, unzip to buffer, and write to disk
if (mz_stream_os_is_open(stream) == MZ_OK)
{
printf(" Extracting: %s\n", out_path);
while (1)
{
read = mz_zip_entry_read(handle, buf, sizeof(buf));
if (read < 0)
{
err = read;
printf("Error %d reading entry in zip file\n", err);
break;
}
if (read == 0)
break;
written = mz_stream_os_write(stream, buf, read);
if (written != read)
{
err = mz_stream_os_error(stream);
printf("Error %d in writing extracted file\n", err);
break;
}
}
mz_stream_os_close(stream);
// Set the time of the file that has been unzipped
if (err == MZ_OK)
mz_os_set_file_date(out_path, file_info->modified_date, file_info->accessed_date,
file_info->creation_date);
}
else
{
printf("Error opening %s\n", out_path);
}
mz_stream_os_delete(&stream);
err_close = mz_zip_entry_close(handle);
if (err_close != MZ_OK)
{
printf("Error %d closing entry in zip file\n", err_close);
err = err_close;
}
return err;
}
int32_t minizip_extract_all(void *handle, const char *destination, const char *password, minizip_opt *options)
{
int32_t err = MZ_OK;
err = mz_zip_goto_first_entry(handle);
if (err == MZ_END_OF_LIST)
return MZ_OK;
if (err != MZ_OK)
printf("Error %d going to first entry in zip file\n", err);
while (err == MZ_OK)
{
err = minizip_extract_currentfile(handle, destination, password, options);
if (err != MZ_OK)
break;
err = mz_zip_goto_next_entry(handle);
if (err == MZ_END_OF_LIST)
return MZ_OK;
if (err != MZ_OK)
printf("Error %d going to next entry in zip file\n", err);
}
return err;
}
int32_t minizip_extract_onefile(void *handle, const char *filename, const char *destination, const char *password, minizip_opt *options)
{
int32_t err = mz_zip_locate_entry(handle, filename, NULL);
if (err != MZ_OK)
{
printf("File %s not found in the zip file\n", filename);
return err;
}
return minizip_extract_currentfile(handle, destination, password, options);
}
/***************************************************************************/
#ifndef NOMAIN
int main(int argc, char *argv[])
{
void *handle = NULL;
void *file_stream = NULL;
void *split_stream = NULL;
void *buf_stream = NULL;
char *path = NULL;
char *password = NULL;
char *destination = NULL;
char *filename_to_extract = NULL;
minizip_opt options;
int64_t disk_size = 0;
int32_t path_arg = 0;
uint8_t do_list = 0;
uint8_t do_extract = 0;
uint8_t buffered = 0;
int16_t mode = 0;
uint8_t append = 0;
int32_t err_close = 0;
int32_t err = 0;
int32_t i = 0;
minizip_banner();
if (argc == 1)
{
minizip_help();
return 0;
}
memset(&options, 0, sizeof(options));
options.compress_method = MZ_COMPRESS_METHOD_DEFLATE;
options.compress_level = MZ_COMPRESS_LEVEL_DEFAULT;
// Parse command line options
for (i = 1; i < argc; i += 1)
{
if ((*argv[i]) == '-')
{
const char *p = argv[i] + 1;
while ((*p) != '\0')
{
char c = *(p++);
if ((c == 'l') || (c == 'L'))
do_list = 1;
if ((c == 'x') || (c == 'X'))
do_extract = 1;
if ((c == 'a') || (c == 'A'))
append = 1;
if ((c == 'u') || (c == 'U'))
buffered = 1;
if ((c == 'o') || (c == 'O'))
options.overwrite = 1;
if ((c == 'j') || (c == 'J'))
options.exclude_path = 1;
if ((c >= '0') && (c <= '9'))
{
options.compress_level = (c - '0');
if (options.compress_level == 0)
options.compress_method = MZ_COMPRESS_METHOD_RAW;
}
#ifdef HAVE_BZIP2
if ((c == 'b') || (c == 'B'))
options.compress_method = MZ_COMPRESS_METHOD_BZIP2;
#endif
#ifdef HAVE_LZMA
if ((c == 'm') || (c == 'M'))
options.compress_method = MZ_COMPRESS_METHOD_LZMA;
#endif
#ifdef HAVE_AES
if ((c == 's') || (c == 'S'))
options.aes = 1;
#endif
if (((c == 'k') || (c == 'K')) && (i + 1 < argc))
{
disk_size = atoi(argv[i + 1]) * 1024;
i += 1;
}
if (((c == 'd') || (c == 'D')) && (i + 1 < argc))
{
destination = argv[i + 1];
i += 1;
}
if (((c == 'p') || (c == 'P')) && (i + 1 < argc))
{
password = argv[i + 1];
i += 1;
}
}
continue;
}
if (path_arg == 0)
path_arg = i;
}
if (path_arg == 0)
{
minizip_help();
return 0;
}
path = argv[path_arg];
mode = MZ_OPEN_MODE_READ;
if ((do_list == 0) && (do_extract == 0))
{
mode |= MZ_OPEN_MODE_WRITE;
if (mz_os_file_exists(path) != MZ_OK)
{
// If the file doesn't exist, we don't append file
mode |= MZ_OPEN_MODE_CREATE;
}
else if (append == 1)
{
mode |= MZ_OPEN_MODE_APPEND;
}
else if (options.overwrite == 0)
{
// If ask the user what to do because append and overwrite args not set
char rep = 0;
do
{
char answer[128];
printf("The file %s exists. Overwrite ? [y]es, [n]o, [a]ppend : ", path);
if (scanf("%1s", answer) != 1)
exit(EXIT_FAILURE);
rep = answer[0];
if ((rep >= 'a') && (rep <= 'z'))
rep -= 0x20;
}
while ((rep != 'Y') && (rep != 'N') && (rep != 'A'));
if (rep == 'A')
{
mode |= MZ_OPEN_MODE_APPEND;
}
else if (rep == 'Y')
{
mode |= MZ_OPEN_MODE_CREATE;
}
else if (rep == 'N')
{
minizip_help();
return 0;
}
}
}
mz_stream_os_create(&file_stream);
mz_stream_buffered_create(&buf_stream);
mz_stream_split_create(&split_stream);
if (buffered)
{
mz_stream_set_base(buf_stream, file_stream);
mz_stream_set_base(split_stream, buf_stream);
}
else
{
mz_stream_set_base(split_stream, file_stream);
}
mz_stream_split_set_prop_int64(split_stream, MZ_STREAM_PROP_DISK_SIZE, disk_size);
err = mz_stream_open(split_stream, path, mode);
if (err != MZ_OK)
{
printf("Error opening file %s\n", path);
}
else
{
handle = mz_zip_open(split_stream, mode);
if (handle == NULL)
{
printf("Error opening zip %s\n", path);
err = MZ_FORMAT_ERROR;
}
if (do_list)
{
err = minizip_list(handle);
}
else if (do_extract)
{
// Create target directory if it doesn't exist
if (destination != NULL)
mz_make_dir(destination);
if (argc > path_arg + 1)
filename_to_extract = argv[path_arg + 1];
if (filename_to_extract == NULL)
err = minizip_extract_all(handle, destination, password, &options);
else
err = minizip_extract_onefile(handle, filename_to_extract, destination, password, &options);
}
else
{
printf("Creating %s\n", path);
// Go through command line args looking for files to add to zip
for (i = path_arg + 1; (i < argc) && (err == MZ_OK); i += 1)
err = minizip_add(handle, argv[i], password, &options, 1);
mz_zip_set_version_madeby(handle, MZ_VERSION_MADEBY);
}
err_close = mz_zip_close(handle);
if (err_close != MZ_OK)
{
printf("Error in closing %s (%d)\n", path, err_close);
err = err_close;
}
mz_stream_close(split_stream);
}
mz_stream_split_delete(&split_stream);
mz_stream_buffered_delete(&buf_stream);
mz_stream_os_delete(&file_stream);
return err;
}
#endif