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
executable file
·401 lines (334 loc) · 10.9 KB
/
Copy pathminizip.c
File metadata and controls
executable file
·401 lines (334 loc) · 10.9 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
/* minizip.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_zip.h"
/***************************************************************************/
void minizip_banner()
{
printf("Minizip %s - https://github.com/nmoinvaz/minizip\n", MZ_VERSION);
printf("---------------------------------------------------\n");
}
void minizip_help()
{
printf("Usage : minizip [-o] [-a] [-0 to -9] [-b|-m] [-s] [-j] [-p password] [-k 512] file.zip [files_to_add]\n\n");
printf(" -o Overwrite existing file.zip\n");
printf(" -a Append to existing file.zip\n");
printf(" -0 Store only\n");
printf(" -1 Compress faster\n");
printf(" -9 Compress better\n");
#ifdef HAVE_BZIP2
printf(" -b BZIP2 compression\n");
#endif
#ifdef HAVE_LZMA
printf(" -m LZMA compression\n");
#endif
#ifdef HAVE_AES
printf(" -s AES encryption\n");
#endif
printf(" -p Encryption password\n");
printf(" -k Disk size in KB\n");
printf(" -j Exclude path and store only the file name\n\n");
}
/***************************************************************************/
typedef struct minizip_opt_s {
uint8_t exclude_path;
} minizip_opt;
/***************************************************************************/
int32_t minizip_add_file(void *handle, const char *path, minizip_opt *options, mz_zip_compress *compress_info, mz_zip_crypt *crypt_info)
{
mz_zip_file file_info = { 0 };
int32_t read = 0;
int32_t written = 0;
int16_t err = MZ_OK;
int16_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.filename = filenameinzip;
if (mz_file_get_size(path) >= UINT32_MAX)
file_info.zip64 = 1;
mz_os_get_file_date(path, &file_info.dos_date);
// Add to zip
err = mz_zip_entry_open(handle, &file_info, compress_info, crypt_info);
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_STREAM_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);
return err;
}
int32_t minizip_add(void *handle, const char *path, minizip_opt *options, mz_zip_compress *compress_info, mz_zip_crypt *crypt_info, uint8_t recursive)
{
DIR *dir = NULL;
struct dirent *entry = NULL;
int16_t err = 0;
char full_path[320];
if (mz_os_is_dir(path) != MZ_OK)
return minizip_add_file(handle, path, options, compress_info, crypt_info);
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, options, compress_info, crypt_info, recursive);
if (err != MZ_OK)
return err;
}
mz_os_close_dir(dir);
return MZ_OK;
}
#ifndef NOMAIN
int main(int argc, char *argv[])
{
void *handle = NULL;
void *file_stream = NULL;
void *split_stream = NULL;
void *open_stream = NULL;
char *path = NULL;
minizip_opt options;
mz_zip_compress compress_info;
mz_zip_crypt crypt_info;
int64_t disk_size = 0;
int32_t path_arg = 0;
uint8_t opt_append = 0;
uint8_t opt_open_existing = 0;
uint8_t opt_exclude_path = 0;
int16_t mode = 0;
int16_t err_close = 0;
int16_t err = 0;
int16_t i = 0;
minizip_banner();
if (argc == 1)
{
minizip_help();
return 0;
}
memset(&options, 0, sizeof(options));
memset(&compress_info, 0, sizeof(compress_info));
memset(&crypt_info, 0, sizeof(crypt_info));
compress_info.method = MZ_COMPRESS_METHOD_DEFLATE;
compress_info.level = MZ_COMPRESS_LEVEL_DEFAULT;
// 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 == 'o') || (c == 'O'))
opt_append = 1;
if ((c == 'a') || (c == 'A'))
opt_open_existing = 1;
if ((c >= '0') && (c <= '9'))
{
compress_info.level = (c - '0');
if (compress_info.level == 0)
compress_info.method = MZ_COMPRESS_METHOD_RAW;
}
if ((c == 'j') || (c == 'J'))
options.exclude_path = 1;
#ifdef HAVE_BZIP2
if ((c == 'b') || (c == 'B'))
compress_info.method = MZ_COMPRESS_METHOD_BZIP2;
#endif
#ifdef HAVE_LZMA
if ((c == 'm') || (c == 'M'))
compress_info.method = MZ_COMPRESS_METHOD_LZMA;
#endif
#ifdef HAVE_AES
if ((c == 's') || (c == 'S'))
crypt_info.aes = 1;
#endif
if (((c == 'k') || (c == 'k')) && (i + 1 < argc))
{
disk_size = atoi(argv[i + 1]) * 1024;
i += 1;
}
if (((c == 'p') || (c == 'P')) && (i + 1 < argc))
{
crypt_info.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];
if (opt_open_existing)
{
// If the file doesn't exist, we don't append file
if (mz_file_exists(path) != MZ_OK)
opt_append = 0;
}
else if (opt_append == 0)
{
// If ask the user what to do because append and overwrite args not set
if (mz_file_exists(path) != MZ_OK)
{
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')
{
opt_open_existing = 1;
}
else if (rep == 'N')
{
minizip_help();
return 0;
}
}
}
mz_stream_os_create(&file_stream);
mode = MZ_STREAM_MODE_READWRITE;
if (opt_append)
mode |= MZ_STREAM_MODE_APPEND;
else
mode |= MZ_STREAM_MODE_CREATE;
if (disk_size > 0)
{
mz_stream_split_create(&split_stream);
mz_stream_set_base(split_stream, file_stream);
mz_stream_split_set_prop_int64(split_stream, MZ_STREAM_PROP_DISK_SIZE, disk_size);
open_stream = split_stream;
}
else
{
open_stream = file_stream;
}
err = mz_stream_open(open_stream, path, mode);
if (err != MZ_OK)
{
printf("Error opening file %s\n", path);
}
else
{
handle = mz_zip_open(opt_open_existing, open_stream);
if (handle == NULL)
{
printf("Error opening zip %s\n", path);
err = MZ_FORMAT_ERROR;
}
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], &options, &compress_info, &crypt_info, 1);
err_close = mz_zip_close(handle, NULL, MZ_VERSION_MADEBY);
if (err_close != MZ_OK)
printf("Error in closing %s (%d)\n", path, err_close);
}
mz_stream_os_close(file_stream);
}
if (split_stream != NULL)
mz_stream_split_delete(&split_stream);
mz_stream_os_delete(&file_stream);
return err;
}
#endif