forked from zlib-ng/minizip-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminiunz.c
More file actions
474 lines (396 loc) · 13 KB
/
Copy pathminiunz.c
File metadata and controls
474 lines (396 loc) · 13 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
/* miniunz.c
Version 2.0.1, October 16th, 2017
part of the MiniZip project
Copyright (C) 2012-2017 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 <time.h>
#include <errno.h>
#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_split.h"
#include "mz_unzip.h"
/***************************************************************************/
void miniunz_banner()
{
printf("Miniunz %s - https://github.com/nmoinvaz/minizip\n", MZ_VERSION);
printf("---------------------------------------------------\n");
}
void miniunz_help()
{
printf("Usage : miniunz [-e] [-x] [-v] [-l] [-o] [-p password] file.zip [file_to_extr.] [-d extractdir]\n\n" \
" -e Extract without path (junk paths)\n" \
" -x Extract with path\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");
}
/***************************************************************************/
typedef struct miniunz_opt_s {
uint8_t extract_without_path;
uint8_t overwrite;
} miniunz_opt;
/***************************************************************************/
int32_t miniunz_list(void *handle)
{
mz_unzip_file *file_info = NULL;
uint32_t ratio = 0;
int16_t level = 0;
int16_t err = MZ_OK;
struct tm tmu_date = { 0 };
const char *string_method = NULL;
char crypt = ' ';
err = mz_unzip_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");
do
{
err = mz_unzip_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_dosdate_to_tm(file_info->dos_date, &tmu_date);
printf(" %7llu %6s%c %7llu %3u%% %2.2u-%2.2u-%2.2u %2.2u:%2.2u %8.8x %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_unzip_goto_next_entry(handle);
}
while (err == MZ_OK);
if (err != MZ_END_OF_LIST && err != MZ_OK)
{
printf("Error %d going to next entry in zip file\n", err);
return err;
}
return MZ_OK;
}
int32_t miniunz_extract_currentfile(void *handle, const char *destination, const char *password, miniunz_opt *options)
{
mz_unzip_file *file_info = NULL;
uint8_t buf[INT16_MAX];
int32_t read = 0;
int32_t written = 0;
int16_t err = MZ_OK;
int16_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_unzip_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->extract_without_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->extract_without_path == 0)
{
printf("Creating directory: %s\n", out_path);
mz_make_dir(out_path);
}
return err;
}
err = mz_unzip_entry_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_file_exists(out_path)))
{
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_STREAM_MODE_CREATE) != MZ_OK) &&
(options->extract_without_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_STREAM_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_unzip_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->dos_date);
}
else
{
printf("Error opening %s\n", out_path);
}
mz_stream_os_delete(&stream);
err_close = mz_unzip_entry_close(handle);
if (err_close != MZ_OK)
printf("Error %d closing entry in zip file\n", err_close);
return err;
}
int32_t miniunz_extract_all(void *handle, const char *destination, const char *password, miniunz_opt *options)
{
int16_t err = MZ_OK;
err = mz_unzip_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 1;
}
while (err == MZ_OK)
{
err = miniunz_extract_currentfile(handle, destination, password, options);
if (err != MZ_OK)
break;
err = mz_unzip_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 1;
}
}
return 0;
}
int32_t miniunz_extract_onefile(void *handle, const char *filename, const char *destination, const char *password, miniunz_opt *options)
{
if (mz_unzip_locate_entry(handle, filename, NULL) != MZ_OK)
{
printf("File %s not found in the zip file\n", filename);
return 2;
}
if (miniunz_extract_currentfile(handle, destination, password, options) == MZ_OK)
return 0;
return 1;
}
#ifndef NOMAIN
int main(int argc, const char *argv[])
{
void *handle = NULL;
void *stream = NULL;
void *split_stream = NULL;
void *open_stream = NULL;
miniunz_opt options;
int16_t i = 0;
uint8_t do_list = 0;
uint8_t do_extract = 1;
const char *path = NULL;
const char *password = NULL;
const char *destination = NULL;
const char *filename_to_extract = NULL;
int err = 0;
miniunz_banner();
if (argc == 1)
{
miniunz_help();
return 0;
}
memset(&options, 0, sizeof(options));
// Parse command line options
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'))
do_list = 1;
if ((c == 'v') || (c == 'V'))
do_list = 1;
if ((c == 'x') || (c == 'X'))
do_extract = 1;
if ((c == 'e') || (c == 'E'))
do_extract = options.extract_without_path = 1;
if ((c == 'o') || (c == 'O'))
options.overwrite = 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 == NULL)
path = argv[i];
else if ((filename_to_extract == NULL) && (destination == NULL))
filename_to_extract = argv[i];
}
if (path == NULL)
{
printf("Error missing path\n");
return 1;
}
mz_stream_os_create(&stream);
mz_stream_split_create(&split_stream);
mz_stream_set_base(split_stream, stream);
err = mz_stream_open(split_stream, path, MZ_STREAM_MODE_READ);
if (err != MZ_OK)
{
printf("Error opening file %s\n", path);
}
else
{
// Open zip file
handle = mz_unzip_open(split_stream);
if (handle == NULL)
{
printf("Error opening zip %s\n", path);
err = 1;
}
else
{
printf("%s opened\n", path);
// Process command line options
if (do_list)
{
err = miniunz_list(handle);
}
else if (do_extract)
{
// Create target directory if it doesn't exist
if (destination != NULL)
mz_make_dir(destination);
if (filename_to_extract == NULL)
err = miniunz_extract_all(handle, destination, password, &options);
else
err = miniunz_extract_onefile(handle, filename_to_extract, destination, password, &options);
}
mz_unzip_close(handle);
}
mz_stream_os_close(stream);
}
mz_stream_split_delete(&split_stream);
mz_stream_os_delete(&stream);
return err;
}
#endif