Debian Codegraph @

doc/mz_zip_rw.md

raw symbols SCIP
1 ## MZ_ZIP_RW <!-- omit in toc -->
2
3 The _mz_zip_reader_ and _mz_zip_writer_ objects allows you to easily extract or create zip files.
4
5 - [Reader Callbacks](#reader-callbacks)
6 - [mz\_zip\_reader\_overwrite\_cb](#mz_zip_reader_overwrite_cb)
7 - [mz\_zip\_reader\_password\_cb](#mz_zip_reader_password_cb)
8 - [mz\_zip\_reader\_progress\_cb](#mz_zip_reader_progress_cb)
9 - [mz\_zip\_reader\_entry\_cb](#mz_zip_reader_entry_cb)
10 - [Reader Open/Close](#reader-openclose)
11 - [mz\_zip\_reader\_is\_open](#mz_zip_reader_is_open)
12 - [mz\_zip\_reader\_open](#mz_zip_reader_open)
13 - [mz\_zip\_reader\_open\_file](#mz_zip_reader_open_file)
14 - [mz\_zip\_reader\_open\_file\_in\_memory](#mz_zip_reader_open_file_in_memory)
15 - [mz\_zip\_reader\_open\_buffer](#mz_zip_reader_open_buffer)
16 - [mz\_zip\_reader\_close](#mz_zip_reader_close)
17 - [Reader Entry Enumeration](#reader-entry-enumeration)
18 - [mz\_zip\_reader\_goto\_first\_entry](#mz_zip_reader_goto_first_entry)
19 - [mz\_zip\_reader\_goto\_next\_entry](#mz_zip_reader_goto_next_entry)
20 - [mz\_zip\_reader\_locate\_entry](#mz_zip_reader_locate_entry)
21 - [Reader Entry](#reader-entry)
22 - [mz\_zip\_reader\_entry\_open](#mz_zip_reader_entry_open)
23 - [mz\_zip\_reader\_entry\_close](#mz_zip_reader_entry_close)
24 - [mz\_zip\_reader\_entry\_read](#mz_zip_reader_entry_read)
25 - [mz\_zip\_reader\_entry\_get\_hash](#mz_zip_reader_entry_get_hash)
26 - [mz\_zip\_reader\_entry\_get\_first\_hash](#mz_zip_reader_entry_get_first_hash)
27 - [mz\_zip\_reader\_entry\_get\_info](#mz_zip_reader_entry_get_info)
28 - [mz\_zip\_reader\_entry\_is\_dir](#mz_zip_reader_entry_is_dir)
29 - [mz\_zip\_reader\_entry\_save](#mz_zip_reader_entry_save)
30 - [mz\_zip\_reader\_entry\_save\_process](#mz_zip_reader_entry_save_process)
31 - [mz\_zip\_reader\_entry\_save\_file](#mz_zip_reader_entry_save_file)
32 - [mz\_zip\_reader\_entry\_save\_buffer](#mz_zip_reader_entry_save_buffer)
33 - [mz\_zip\_reader\_entry\_save\_buffer\_length](#mz_zip_reader_entry_save_buffer_length)
34 - [Reader Bulk Extract](#reader-bulk-extract)
35 - [mz\_zip\_reader\_save\_all](#mz_zip_reader_save_all)
36 - [Reader Object](#reader-object)
37 - [mz\_zip\_reader\_set\_pattern](#mz_zip_reader_set_pattern)
38 - [mz\_zip\_reader\_set\_password](#mz_zip_reader_set_password)
39 - [mz\_zip\_reader\_set\_raw](#mz_zip_reader_set_raw)
40 - [mz\_zip\_reader\_get\_raw](#mz_zip_reader_get_raw)
41 - [mz\_zip\_reader\_get\_zip\_cd](#mz_zip_reader_get_zip_cd)
42 - [mz\_zip\_reader\_get\_comment](#mz_zip_reader_get_comment)
43 - [mz\_zip\_reader\_set\_recover](#mz_zip_reader_set_recover)
44 - [mz\_zip\_reader\_set\_encoding](#mz_zip_reader_set_encoding)
45 - [mz\_zip\_reader\_set\_overwrite\_cb](#mz_zip_reader_set_overwrite_cb)
46 - [mz\_zip\_reader\_set\_password\_cb](#mz_zip_reader_set_password_cb)
47 - [mz\_zip\_reader\_set\_progress\_cb](#mz_zip_reader_set_progress_cb)
48 - [mz\_zip\_reader\_set\_progress\_interval](#mz_zip_reader_set_progress_interval)
49 - [mz\_zip\_reader\_set\_entry\_cb](#mz_zip_reader_set_entry_cb)
50 - [mz\_zip\_reader\_get\_zip\_handle](#mz_zip_reader_get_zip_handle)
51 - [mz\_zip\_reader\_create](#mz_zip_reader_create)
52 - [mz\_zip\_reader\_delete](#mz_zip_reader_delete)
53 - [Writer Callbacks](#writer-callbacks)
54 - [mz\_zip\_writer\_overwrite\_cb](#mz_zip_writer_overwrite_cb)
55 - [mz\_zip\_writer\_password\_cb](#mz_zip_writer_password_cb)
56 - [mz\_zip\_writer\_progress\_cb](#mz_zip_writer_progress_cb)
57 - [mz\_zip\_writer\_entry\_cb](#mz_zip_writer_entry_cb)
58 - [Writer Open/Close](#writer-openclose)
59 - [mz\_zip\_writer\_is\_open](#mz_zip_writer_is_open)
60 - [mz\_zip\_writer\_open](#mz_zip_writer_open)
61 - [mz\_zip\_writer\_open\_file](#mz_zip_writer_open_file)
62 - [mz\_zip\_writer\_open\_file\_in\_memory](#mz_zip_writer_open_file_in_memory)
63 - [mz\_zip\_writer\_close](#mz_zip_writer_close)
64 - [Writer Entry](#writer-entry)
65 - [mz\_zip\_writer\_entry\_open](#mz_zip_writer_entry_open)
66 - [mz\_zip\_writer\_entry\_close](#mz_zip_writer_entry_close)
67 - [mz\_zip\_writer\_entry\_write](#mz_zip_writer_entry_write)
68 - [Writer Add/Compress](#writer-addcompress)
69 - [mz\_zip\_writer\_add](#mz_zip_writer_add)
70 - [mz\_zip\_writer\_add\_process](#mz_zip_writer_add_process)
71 - [mz\_zip\_writer\_add\_info](#mz_zip_writer_add_info)
72 - [mz\_zip\_writer\_add\_buffer](#mz_zip_writer_add_buffer)
73 - [mz\_zip\_writer\_add\_file](#mz_zip_writer_add_file)
74 - [mz\_zip\_writer\_add\_path](#mz_zip_writer_add_path)
75 - [mz\_zip\_writer\_copy\_from\_reader](#mz_zip_writer_copy_from_reader)
76 - [Writer Object](#writer-object)
77 - [mz\_zip\_writer\_set\_password](#mz_zip_writer_set_password)
78 - [mz\_zip\_writer\_set\_comment](#mz_zip_writer_set_comment)
79 - [mz\_zip\_writer\_set\_raw](#mz_zip_writer_set_raw)
80 - [mz\_zip\_writer\_get\_raw](#mz_zip_writer_get_raw)
81 - [mz\_zip\_writer\_set\_aes](#mz_zip_writer_set_aes)
82 - [mz\_zip\_writer\_set\_compress\_method](#mz_zip_writer_set_compress_method)
83 - [mz\_zip\_writer\_set\_compress\_level](#mz_zip_writer_set_compress_level)
84 - [mz\_zip\_writer\_set\_zip\_cd](#mz_zip_writer_set_zip_cd)
85 - [mz\_zip\_writer\_set\_overwrite\_cb](#mz_zip_writer_set_overwrite_cb)
86 - [mz\_zip\_writer\_set\_password\_cb](#mz_zip_writer_set_password_cb)
87 - [mz\_zip\_writer\_set\_progress\_cb](#mz_zip_writer_set_progress_cb)
88 - [mz\_zip\_writer\_set\_progress\_interval](#mz_zip_writer_set_progress_interval)
89 - [mz\_zip\_writer\_set\_entry\_cb](#mz_zip_writer_set_entry_cb)
90 - [mz\_zip\_writer\_get\_zip\_handle](#mz_zip_writer_get_zip_handle)
91 - [mz\_zip\_writer\_create](#mz_zip_writer_create)
92 - [mz\_zip\_writer\_delete](#mz_zip_writer_delete)
93
94 ## Reader Callbacks
95
96 ### mz_zip_reader_overwrite_cb
97
98 Callback that called before an existing file is about to be overwritten. It can be set by calling _mz_zip_reader_set_overwrite_cb_.
99
100 **Arguments**
101 |Type|Name|Description|
102 |-|-|-|
103 |void *|handle|_mz_zip_reader_ instance|
104 |void *|userdata|Pointer that is passed to _mz_zip_reader_set_overwrite_cb_|
105 |mz_zip_file *|file_info|Zip entry|
106 |const char *|path|Target path on disk|
107
108 **Return**
109 |Type|Description|
110 |-|-|
111 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK to overwrite, MZ_EXIST_ERROR to skip.|
112
113 **Example**
114
115 See _minizip_extract_overwrite_cb_ callback in minizip.c.
116
117 ### mz_zip_reader_password_cb
118
119 Callback that is called before a password is required to extract a password protected zip entry. It can be set by calling _mz_zip_reader_set_password_cb_.
120
121 **Arguments**
122 |Type|Name|Description|
123 |-|-|-|
124 |void *|handle|_mz_zip_reader_ instance|
125 |void *|userdata|Pointer that is passed to _mz_zip_reader_set_password_cb_|
126 |mz_zip_file *|file_info|Zip entry|
127 |char *|password|Password character array buffer|
128 |int32|max_password|Maximum password size|
129
130 **Return**
131 |Type|Description|
132 |-|-|
133 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
134
135 **Example**
136 ```
137 static int32_t example_password_cb(void *handle, void *userdata, mz_zip_file *file_info, char *password, int32_t max_password) {
138 strncpy(password, "my password", max_password);
139 return MZ_OK;
140 }
141 mz_zip_reader_set_password_cb(zip_reader, 0, example_password_cb);
142 ```
143
144 ### mz_zip_reader_progress_cb
145
146 Callback that is called to report extraction progress. This can be set by calling _mz_zip_reader_set_progress_cb_.
147
148 Progress calculation depends on whether or not raw data is being extracted. If raw data, then use `position / file_info->compressed_size` otherwise use `position / file_info->uncompressed_size`.
149
150 **Arguments**
151 |Type|Name|Description|
152 |-|-|-|
153 |void *|handle|_mz_zip_reader_ instance|
154 |void *|userdata|Pointer that is passed to _mz_zip_reader_progress_cb_|
155 |mz_zip_file *|file_info|Zip entry|
156 |int64_t|position|File position.|
157
158 **Return**
159 |Type|Description|
160 |-|-|
161 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
162
163 **Example**
164
165 See _minizip_extract_progress_cb_ in minizip.c.
166
167 ### mz_zip_reader_entry_cb
168
169 Callback that is called when a new zip entry is starting extraction. It can be set by calling _mz_zip_reader_entry_cb_.
170
171 **Arguments**
172 |Type|Name|Description|
173 |-|-|-|
174 |void *|handle|_mz_zip_reader_ instance|
175 |void *|userdata|Pointer that is passed to _mz_zip_reader_entry_cb_|
176 |const char *|path|Target path on disk|
177
178 **Return**
179 |Type|Description|
180 |-|-|
181 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
182
183 **Example**
184
185 See _minizip_extract_entry_cb_ in minizip.c.
186
187 ## Reader Open/Close
188
189 ### mz_zip_reader_is_open
190
191 Checks to see if the zip file is open.
192
193 **Arguments**
194 |Type|Name|Description|
195 |-|-|-|
196 |void *|handle|_mz_zip_reader_ instance|
197
198 **Return**
199 |Type|Description|
200 |-|-|
201 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if open.|
202
203 **Example**
204 ```
205 if (mz_zip_reader_is_open(zip_reader) == MZ_OK)
206 printf("Zip file is open in reader\n");
207 ```
208
209 ### mz_zip_reader_open
210
211 Opens zip file from stream.
212
213 **Arguments**
214 |Type|Name|Description|
215 |-|-|-|
216 |void *|handle|_mz_zip_reader_ instance|
217 |void *|stream|_mz_stream_ instance|
218
219 **Return**
220 |Type|Description|
221 |-|-|
222 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if opened.|
223
224 **Example**
225 ```
226 const char *path = "c:\\my.zip";
227
228 void *zip_reader = mz_zip_reader_create();
229 void *file_stream = mz_stream_os_create();
230
231 err = mz_stream_os_open(file_stream, path, MZ_OPEN_MODE_READ);
232 if (err == MZ_OK) {
233 err = mz_zip_reader_open(zip_reader, file_stream);
234 if (err == MZ_OK) {
235 printf("Zip reader was opened %s\n", path);
236 mz_zip_reader_close(zip_reader);
237 }
238 }
239
240 mz_stream_os_delete(&file_stream);
241 mz_zip_reader_delete(&zip_reader);
242 ```
243
244 ### mz_zip_reader_open_file
245
246 Opens zip file from a file path.
247
248 **Arguments**
249 |Type|Name|Description|
250 |-|-|-|
251 |void *|handle|_mz_zip_reader_ instance|
252 |const char *|path|Path to zip file|
253
254 **Return**
255 |Type|Description|
256 |-|-|
257 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if opened.|
258
259 **Example**
260 ```
261 const char *path = "c:\\my.zip";
262 void *zip_reader = mz_zip_reader_create();
263 if (mz_zip_reader_open_file(zip_reader, path) == MZ_OK) {
264 printf("Zip reader was opened %s\n", path);
265 mz_zip_reader_close(zip_reader);
266 }
267 mz_zip_reader_delete(&zip_reader);
268 ```
269
270 ### mz_zip_reader_open_file_in_memory
271
272 Opens zip file from a file path into memory for faster access.
273
274 **Arguments**
275 |Type|Name|Description|
276 |-|-|-|
277 |void *|handle|_mz_zip_reader_ instance|
278 |const char *|path|Path to zip file|
279
280 **Return**
281 |Type|Description|
282 |-|-|
283 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if opened.|
284
285 **Example**
286 ```
287 const char *path = "c:\\my.zip";
288 void *zip_reader = mz_zip_reader_create();
289 if (mz_zip_reader_open_file_in_memory(zip_reader, path) == MZ_OK) {
290 printf("Zip reader was opened in memory %s\n", path);
291 mz_zip_reader_close(zip_reader);
292 }
293 mz_zip_reader_delete(&zip_reader);
294 ```
295
296 ### mz_zip_reader_open_buffer
297
298 Opens zip file from memory buffer.
299
300 **Arguments**
301 |Type|Name|Description|
302 |-|-|-|
303 |void *|handle|_mz_zip_reader_ instance|
304 |uint8_t *|buf|Buffer containing zip|
305 |int32_t|len|Length of buffer|
306 |int32_t|copy|Copy buffer internally if 1|
307
308 **Return**
309 |Type|Description|
310 |-|-|
311 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if opened.|
312
313 **Example**
314 ```
315 uint8 *buffer = NULL;
316 int32 buffer_length = 0;
317 // TODO: Load zip file into memory buffer
318 void *zip_reader = mz_zip_reader_create();
319 if (mz_zip_reader_open_buffer(zip_reader, buffer, buffer_length) == MZ_OK) {
320 printf("Zip reader was opened from buffer\n");
321 mz_zip_reader_close(zip_reader);
322 }
323 mz_zip_reader_delete(&zip_reader);
324 ```
325
326 ### mz_zip_reader_close
327
328 Closes the zip file.
329
330 **Arguments**
331 |Type|Name|Description|
332 |-|-|-|
333 |void *|handle|_mz_zip_reader_ instance|
334
335 **Return**
336 |Type|Description|
337 |-|-|
338 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
339
340 **Example**
341 ```
342 if (mz_zip_reader_close(zip_reader) == MZ_OK)
343 printf("Zip reader closed\n");
344 ```
345
346 ## Reader Entry Enumeration
347
348 ### mz_zip_reader_goto_first_entry
349
350 Goto the first entry in the zip file. If a pattern has been specified by calling _mz_zip_reader_set_pattern_, then it goes to the first entry matching the pattern.
351
352 **Arguments**
353 |Type|Name|Description|
354 |-|-|-|
355 |void *|handle|_mz_zip_reader_ instance|
356
357 **Return**
358 |Type|Description|
359 |-|-|
360 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful, MZ_END_OF_LIST if no more entries.|
361
362 **Example**
363 ```
364 mz_zip_file *file_info = NULL;
365 if ((mz_zip_reader_goto_first_entry(zip_reader) == MZ_OK) &&
366 (mz_zip_reader_entry_get_info(zip_reader, &file_info) == MZ_OK)) {
367 printf("Zip first entry %s\n", file_info->filename);
368 }
369 ```
370
371 ### mz_zip_reader_goto_next_entry
372
373 Goto the next entry in the zip file. If a pattern has been specified by calling _mz_zip_reader_set_pattern_, then it goes to the next entry matching the pattern.
374
375 **Arguments**
376 |Type|Name|Description|
377 |-|-|-|
378 |void *|handle|_mz_zip_reader_ instance|
379
380 **Return**
381 |Type|Description|
382 |-|-|
383 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful, MZ_END_OF_LIST if no more entries.|
384
385 **Example**
386 ```
387 if (mz_zip_reader_goto_first_entry(zip_reader) == MZ_OK) {
388 do {
389 mz_zip_file *file_info = NULL;
390 if (mz_zip_reader_entry_get_info(zip_reader, &file_info) != MZ_OK) {
391 printf("Unable to get zip entry info\n");
392 break;
393 }
394 printf("Zip entry %s\n", file_info->filename);
395 } while (mz_zip_reader_goto_next_entry(zip_reader) == MZ_OK);
396 }
397 ```
398
399 ### mz_zip_reader_locate_entry
400
401 Locates an entry by filename.
402
403 **Arguments**
404 |Type|Name|Description|
405 |-|-|-|
406 |void *|handle|_mz_zip_reader_ instance|
407 |const char *|filename|Filename to find|
408 |uint8_t|ignore_case|Ignore case during search if 1.|
409
410 **Return**
411 |Type|Description|
412 |-|-|
413 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful, MZ_END_OF_LIST if not found.|
414
415 **Example**
416 ```
417 const char *search_filename = "test1.txt";
418 if (mz_zip_reader_locate_entry(zip_reader, search_filename, 1) == MZ_OK)
419 printf("Found %s\n", search_filename);
420 else
421 printf("Could not find %s\n", search_filename);
422 ```
423
424 ## Reader Entry
425
426 ### mz_zip_reader_entry_open
427
428 Opens an entry for reading.
429
430 **Arguments**
431 |Type|Name|Description|
432 |-|-|-|
433 |void *|handle|_mz_zip_reader_ instance|
434
435 **Return**
436 |Type|Description|
437 |-|-|
438 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
439
440 **Example**
441 ```
442 if (mz_zip_reader_goto_first_entry(zip_reader) == MZ_OK) {
443 if (mz_zip_reader_entry_open(zip_reader) == MZ_OK) {
444 char buf[120];
445 int32_t bytes_read = 0;
446 bytes_read = mz_zip_reader_entry_read(zip_reader, buf, sizeof(buf));
447 if (bytes_read > 0) {
448 printf("Bytes read from entry %d\n", bytes_read);
449 }
450 mz_zip_reader_entry_close(zip_reader);
451 }
452 }
453 ```
454
455 ### mz_zip_reader_entry_close
456
457 Closes an entry that has been opened for reading or writing.
458
459 **Arguments**
460 |Type|Name|Description|
461 |-|-|-|
462 |void *|handle|_mz_zip_reader_ instance|
463
464 **Return**
465 |Type|Description|
466 |-|-|
467 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
468
469 **Example**
470 ```
471 if (mz_zip_reader_entry_open(zip_reader) == MZ_OK) {
472 if (mz_zip_reader_entry_close(zip_reader) == MZ_OK) {
473 printf("Entry closed successfully\n");
474 }
475 }
476 ```
477
478 ### mz_zip_reader_entry_read
479
480 Reads an entry after being opened.
481
482 **Arguments**
483 |Type|Name|Description|
484 |-|-|-|
485 |void *|handle|_mz_zip_reader_ instance|
486 |void *|buf|Buffer to read into|
487 |int32_t|len|Maximum length of buffer to read into|
488
489 **Return**
490 |Type|Description|
491 |-|-|
492 |int32_t|If < 0 then [MZ_ERROR](mz_error.md) code, otherwise number of bytes read. When there are no more bytes left to read then 0 is returned.|
493
494 **Example**
495 ```
496 if (mz_zip_reader_goto_first_entry(zip_reader) == MZ_OK) {
497 if (mz_zip_reader_entry_open(zip_reader) == MZ_OK) {
498 char buf[4096];
499 int32_t bytes_read = 0;
500 int32_t err = MZ_OK;
501 do {
502 bytes_read = mz_zip_reader_entry_read(zip_reader, buf, sizeof(buf));
503 if (bytes_read < 0) {
504 err = bytes_read;
505 break;
506 }
507 printf("Bytes read from entry %d\n", bytes_read);
508 } while (bytes_read > 0);
509 mz_zip_reader_entry_close(zip_reader);
510 }
511 }
512 ```
513
514 ### mz_zip_reader_entry_get_hash
515
516 Gets a hash algorithm from the entry's extra field.
517
518 **Arguments**
519 |Type|Name|Description|
520 |-|-|-|
521 |void *|handle|_mz_zip_reader_ instance|
522 |uint16_t|algorithm|[MZ_HASH](mz_hash.md) algorithm identifier|
523 |uint8_t *|digest|Digest buffer|
524 |int32_t|digest_size|Maximum digest buffer size|
525
526 **Return**
527 |Type|Description|
528 |-|-|
529 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if hash found.|
530
531 **Example**
532 ```
533 if (mz_zip_reader_goto_first_entry(zip_reader) == MZ_OK) {
534 uint8_t sha1_digest[MZ_HASH_SHA1_SIZE];
535 if (mz_zip_reader_entry_get_hash(zip_reader, MZ_HASH_SHA1, sha1_digest, sizeof(sha1_digest)) == MZ_OK) {
536 printf("Found sha1 digest for entry\n");
537 }
538 }
539 ```
540
541 ### mz_zip_reader_entry_get_first_hash
542
543 Gets the most secure hash algorithm from the entry's extra field.
544
545 **Arguments**
546 |Type|Name|Description|
547 |-|-|-|
548 |void *|handle|_mz_zip_reader_ instance|
549 |uint16_t *|algorithm|Pointer to store [MZ_HASH](mz_hash.md) algorithm identifier|
550 |uint16_t *|digest_size|Pointer to store digest size|
551
552 **Return**
553 |Type|Description|
554 |-|-|
555 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if hash found.|
556
557 **Example**
558 ```
559 uint16_t algorithm = 0;
560 uint16_t digest_size = 0;
561 if (mz_zip_reader_entry_get_first_hash(zip_reader, &algorithm, &digest_size) == MZ_OK) {
562 printf("Found hash: algo %d size %d\n", algorithm, digest_size);
563 }
564 ```
565
566 ### mz_zip_reader_entry_get_info
567
568 Gets the current entry file info.
569
570 **Arguments**
571 |Type|Name|Description|
572 |-|-|-|
573 |void *|handle|_mz_zip_reader_ instance|
574 |mz_zip_file **|file_info|Pointer to [mz_zip_file](mz_zip_file.md) structure|
575
576 **Return**
577 |Type|Description|
578 |-|-|
579 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
580
581 **Example**
582 ```
583 mz_zip_file *file_info = NULL;
584 if (mz_zip_reader_goto_first_entry(zip_reader) == MZ_OK) {
585 if (mz_zip_reader_entry_get_info(zip_reader, &file_info) == MZ_OK) {
586 printf("First entry: %s\n", file_info->filename);
587 }
588 }
589 ```
590
591 ### mz_zip_reader_entry_is_dir
592
593 Gets the current entry is a directory.
594
595 **Arguments**
596 |Type|Name|Description|
597 |-|-|-|
598 |void *|handle|_mz_zip_reader_ instance|
599
600 **Return**
601 |Type|Description|
602 |-|-|
603 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
604
605 **Example**
606 ```
607 if (mz_zip_reader_goto_first_entry(zip_reader) == MZ_OK) {
608 if (mz_zip_reader_entry_is_dir(zip_reader) == MZ_OK) {
609 printf("Entry is a directory\n");
610 }
611 }
612 ```
613
614 ### mz_zip_reader_entry_save
615
616 Save the current entry to a stream. Each time the function needs to write to the stream it will call the _mz_stream_write_cb_ callback with the _stream_ pointer. This is a blocking call that will not return until the entire entry is written to the stream or until an error has occured.
617
618 **Arguments**
619 |Type|Name|Description|
620 |-|-|-|
621 |void *|handle|_mz_zip_reader_ instance|
622 |void *|stream|_mz_stream_ instance|
623 |mz_stream_write_cb|write_cb|Stream write callback|
624
625 **Return**
626 |Type|Description|
627 |-|-|
628 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
629
630 **Example**
631 ```
632 // TODO: Open file stream
633
634 const char *path = "c:\\my.zip";
635 const char *entry_path = "c:\\entry.dat";
636
637 void *zip_reader = mz_zip_reader_create();
638
639 err = mz_zip_reader_open_file(zip_reader, path);
640 if (err == MZ_OK) {
641 printf("Zip reader was opened %s\n", path);
642 err = mz_zip_reader_goto_first_entry(zip_reader);
643 if (err == MZ_OK) {
644 void *entry_stream = mz_stream_os_create();
645 err = mz_stream_os_open(entry_stream, entry_path, MZ_OPEN_MODE_WRITE);
646 if (err == MZ_OK) {
647 err = mz_zip_reader_entry_save(zip_reader, file_stream, mz_stream_os_write);
648 mz_stream_os_close(entry_stream);
649 }
650 mz_stream_os_delete(&entry_stream);
651 }
652 mz_zip_reader_close(zip_reader);
653 }
654
655 mz_zip_reader_delete(&zip_reader);
656 ```
657
658 ### mz_zip_reader_entry_save_process
659
660 Saves a portion of the current entry to a stream. Each time the function is called it will read from the zip file once and then write the output to the _mz_stream_write_cb_ callback with _stream_ pointer. This is intended to be used when writing zip file in a process loop.
661
662 **Arguments**
663 |Type|Name|Description|
664 |-|-|-|
665 |void *|handle|_mz_zip_reader_ instance|
666 |void *|stream|_mz_stream_ instance|
667 |mz_stream_write_cb|write_cb|Stream write callback|
668
669 **Return**
670 |Type|Description|
671 |-|-|
672 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if more to process, MZ_END_OF_STREAM if no more data to process.|
673
674 **Example**
675 ```
676 int32_t err = MZ_OK;
677 // TODO: Open zip reader and entry stream.
678 while (1) {
679 err = mz_zip_reader_entry_save_process(zip_reader, entry_stream, mz_stream_os_write);
680 if (err != MZ_OK) {
681 printf("There was an error writing to stream (%d)\n", err);
682 break;
683 }
684 if (err == MZ_END_OF_STREAM) {
685 printf("Finished writing to stream\n");
686 break;
687 }
688 }
689 ```
690
691 ### mz_zip_reader_entry_save_file
692
693 Save the current entry to a file.
694
695 **Arguments**
696 |Type|Name|Description|
697 |-|-|-|
698 |void *|handle|_mz_zip_reader_ instance|
699 |const char *|path|Path to save entry on disk|
700
701 **Return**
702 |Type|Description|
703 |-|-|
704 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
705
706 **Example**
707 ```
708 if (mz_zip_reader_goto_first_entry(zip_reader) == MZ_OK) {
709 if (mz_zip_reader_entry_save_file(zip_reader, "entry1.bin") == MZ_OK) {
710 printf("First entry saved to disk successfully\n");
711 }
712 }
713 ```
714
715 ### mz_zip_reader_entry_save_buffer
716
717 Save the current entry to a memory buffer. To get the size required use _mz_zip_reader_entry_save_buffer_length_.
718
719 **Arguments**
720 |Type|Name|Description|
721 |-|-|-|
722 |void *|handle|_mz_zip_reader_ instance|
723 |void *|buf|Buffer to decompress to|
724 |int32_t|len|Maximum size of buffer|
725
726 **Return**
727 |Type|Description|
728 |-|-|
729 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful, or MZ_BUF_ERROR if _buf_ is too small.|
730
731 **Example**
732 ```
733 int32_t buf_size = (int32_t)mz_zip_reader_entry_save_buffer_length(zip_reader);
734 char *buf = (char *)malloc(buf_size);
735 int32_t err = mz_zip_reader_entry_save_buffer(zip_reader, buf, buf_size);
736 if (err == MZ_OK) {
737 // TODO: Do something with buffer
738 }
739 free(buf);
740 ```
741
742 ### mz_zip_reader_entry_save_buffer_length
743
744 Gets the length of the buffer required to save.
745
746 **Arguments**
747 |Type|Name|Description|
748 |-|-|-|
749 |void *|handle|_mz_zip_reader_ instance|
750
751 **Return**
752 |Type|Description|
753 |-|-|
754 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
755
756 **Example**
757 ```
758 int32_t buf_size = (int32_t)mz_zip_reader_entry_save_buffer_length(zip_reader);
759 char *buf = (char *)malloc(buf_size);
760 int32_t err = mz_zip_reader_entry_save_buffer(zip_reader, buf, buf_size);
761 if (err == MZ_OK) {
762 // TODO: Do something with buffer
763 }
764 free(buf);
765 ```
766
767 ## Reader Bulk Extract
768
769 ### mz_zip_reader_save_all
770
771 Save all files into a directory.
772
773 **Arguments**
774 |Type|Name|Description|
775 |-|-|-|
776 |void *|handle|_mz_zip_reader_ instance|
777 |const char *|destination_dir|Directory to extract all files to|
778
779 **Return**
780 |Type|Description|
781 |-|-|
782 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
783
784 **Example**
785 ```
786 const char *destination_dir = "c:\\temp\\";
787 if (mz_zip_reader_save_all(zip_reader, destination_dir) == MZ_OK) {
788 printf("All files successfully saved to %s\n", destination_dir);
789 }
790 ```
791
792 ## Reader Object
793
794 ### mz_zip_reader_set_pattern
795
796 Sets the match pattern for entries in the zip file, if null all entries are matched. This match pattern is used when calling _mz_zip_reader_goto_first_entry_ and _mz_zip_reader_goto_next_entry_.
797
798 **Arguments**
799 |Type|Name|Description|
800 |-|-|-|
801 |void *|handle|_mz_zip_reader_ instance|
802 |const char *|pattern|Search pattern or NULL if not used|
803 |uint8_t|ignore_case|Ignore case when matching if 1|
804
805 **Return**
806 |Type|Description|
807 |-|-|
808 |void|No return|
809
810 **Example**
811 ```
812 int32_t matches = 0;
813 const char *pattern = "*.txt";
814 mz_zip_reader_set_pattern(zip_reader, pattern, 1);
815 if (mz_zip_reader_goto_first_entry(zip_reader) == MZ_OK) {
816 do {
817 matches += 1;
818 } while (mz_zip_reader_goto_next_entry(zip_reader) == MZ_OK);
819 }
820 printf("Found %d zip entries matching pattern %s\n", matches, pattern);
821 ```
822
823 ### mz_zip_reader_set_password
824
825 Sets the password required for extracting entire zip file. If not specified, then _mz_zip_reader_password_cb_ will be called for password protected zip entries.
826
827 **Arguments**
828 |Type|Name|Description|
829 |-|-|-|
830 |void *|handle|_mz_zip_reader_ instance|
831 |const char *|password|Password to use for entire zip file|
832
833 **Return**
834 |Type|Description|
835 |-|-|
836 |void|No return|
837
838 **Example**
839 ```
840 mz_zip_reader_set_password(zip_handle, "mypassword");
841 ```
842
843 ### mz_zip_reader_set_raw
844
845 Sets whether or not it should save the entry raw.
846
847 **Arguments**
848 |Type|Name|Description|
849 |-|-|-|
850 |void *|handle|_mz_zip_reader_ instance|
851 |uint8_t|raw|Save entry as raw data if 1|
852
853 **Return**
854 |Type|Description|
855 |-|-|
856 |void|No return|
857
858 **Example**
859 ```
860 mz_zip_reader_set_raw(zip_reader, 1);
861 ```
862
863 ### mz_zip_reader_get_raw
864
865 Gets whether or not it should save the entry raw.
866
867 **Arguments**
868 |Type|Name|Description|
869 |-|-|-|
870 |void *|handle|_mz_zip_reader_ instance|
871 |uint8_t *|raw|Pointer to store if saving as raw data|
872
873 **Return**
874 |Type|Description|
875 |-|-|
876 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
877
878 **Example**
879 ```
880 uint8_t raw = 0;
881 mz_zip_reader_get_raw(zip_reader, &raw);
882 printf("Entry will be saved as %s data\n", (raw) ? "raw gzip" : "decompressed");
883 ```
884
885 ### mz_zip_reader_get_zip_cd
886
887 Gets whether or not the archive has a zipped central directory.
888
889 **Arguments**
890 |Type|Name|Description|
891 |-|-|-|
892 |void *|handle|_mz_zip_reader_ instance|
893 |uint8_t *|zip_cd|Pointer to store if central directory is zipped|
894
895 **Return**
896 |Type|Description|
897 |-|-|
898 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
899
900 **Example**
901 ```
902 uint8_t zip_cd = 0;
903 mz_zip_reader_get_zip_cd(zip_reader, &zip_cd);
904 printf("Central directory %s zipped\n", (zip_cd) ? "is" : "is not");
905 ```
906
907 ### mz_zip_reader_get_comment
908
909 Gets the comment for the central directory.
910
911 **Arguments**
912 |Type|Name|Description|
913 |-|-|-|
914 |void *|handle|_mz_zip_reader_ instance|
915 |const char **|comment|Pointer to store global comment pointer|
916
917 **Return**
918 |Type|Description|
919 |-|-|
920 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
921
922 **Example**
923 ```
924 const char *global_comment = NULL;
925 if (mz_zip_reader_get_comment(zip_reader, &global_comment) == MZ_OK) {
926 printf("Zip comment: %s\n", global_comment);
927 }
928 ```
929
930 ### mz_zip_reader_set_recover
931
932 Sets the ability to recover the central dir by reading local file headers.
933
934 **Arguments**
935 |Type|Name|Description|
936 |-|-|-|
937 |void *|handle|_mz_zip_reader_ instance|
938 |uint8_t|recover|Set to 1 if recover method is supported, 0 otherwise.|
939
940 **Return**
941 |Type|Description|
942 |-|-|
943 |void|No return|
944
945 **Example**
946 ```
947 mz_zip_reader_set_recover(zip_reader, 1);
948 ```
949
950 ### mz_zip_reader_set_encoding
951
952 Sets whether or not it should support a special character encoding in zip file names.
953
954 **Arguments**
955 |Type|Name|Description|
956 |-|-|-|
957 |void *|handle|_mz_zip_reader_ instance|
958 |int32_t|encoding|[MZ_ENCODING](mz_encoding.md) identifier|
959
960 **Return**
961 |Type|Description|
962 |-|-|
963 |void|No return|
964
965 **Example**
966 ```
967 mz_zip_reader_set_encoding(zip_reader, MZ_ENCODING_CODEPAGE_437);
968 ```
969
970 ### mz_zip_reader_set_overwrite_cb
971
972 Sets the callback for what to do when a file is about to be overwritten.
973
974 **Arguments**
975 |Type|Name|Description|
976 |-|-|-|
977 |void *|handle|_mz_zip_reader_ instance|
978 |void *|userdata|User supplied data|
979 |mz_zip_reader_overwrite_cb|cb|_mz_zip_reader_overwrite_cb_ function pointer|
980
981 **Return**
982 |Type|Description|
983 |-|-|
984 |void|No return|
985
986 **Example**
987
988 See example for _mz_zip_reader_overwrite_cb_.
989
990 ### mz_zip_reader_set_password_cb
991
992 Sets the callback for what to do when a password is required and hasn't been set.
993
994 **Arguments**
995 |Type|Name|Description|
996 |-|-|-|
997 |void *|handle|_mz_zip_reader_ instance|
998 |void *|userdata|User supplied data|
999 |mz_zip_reader_password_cb|cb|_mz_zip_reader_password_cb_ function pointer|
1000
1001 **Return**
1002 |Type|Description|
1003 |-|-|
1004 |void|No return|
1005
1006 **Example**
1007
1008 See example for _mz_zip_reader_password_cb_.
1009
1010 ### mz_zip_reader_set_progress_cb
1011
1012 Sets the callback that gets called to update extraction progress. This callback is called on an interval specified by _mz_zip_reader_set_progress_interval_.
1013
1014 **Arguments**
1015 |Type|Name|Description|
1016 |-|-|-|
1017 |void *|handle|_mz_zip_reader_ instance|
1018 |void *|userdata|User supplied data|
1019 |mz_zip_reader_progress_cb|cb|_mz_zip_reader_progress_cb_ function pointer|
1020
1021 **Return**
1022 |Type|Description|
1023 |-|-|
1024 |void|No return|
1025
1026 **Example**
1027
1028 See example for _mz_zip_reader_progress_cb_.
1029
1030 ### mz_zip_reader_set_progress_interval
1031
1032 Let at least milliseconds pass between calls to progress callback.
1033
1034 **Arguments**
1035 |Type|Name|Description|
1036 |-|-|-|
1037 |void *|handle|_mz_zip_reader_ instance|
1038 |uint32_t|milliseconds|Number of milliseconds to wait before calling _mz_zip_reader_progress_cb_ during extraction|
1039
1040 **Return**
1041 |Type|Description|
1042 |-|-|
1043 |void|No return|
1044
1045 **Example**
1046 ```
1047 mz_zip_reader_set_progress_interval(zip_reader, 1000); // Wait 1 sec
1048 ```
1049
1050 ### mz_zip_reader_set_entry_cb
1051
1052 Sets callback for when a new zip file entry is encountered during extraction.
1053
1054 **Arguments**
1055 |Type|Name|Description|
1056 |-|-|-|
1057 |void *|handle|_mz_zip_reader_ instance|
1058 |void *|userdata|User supplied data|
1059 |mz_zip_reader_entry_cb|cb|_mz_zip_reader_entry_cb_ function pointer|
1060
1061 **Return**
1062 |Type|Description|
1063 |-|-|
1064 |void|No return|
1065
1066 **Example**
1067
1068 See example for _mz_zip_reader_entry_cb_.
1069
1070 ### mz_zip_reader_get_zip_handle
1071
1072 Gets the underlying zip instance handle.
1073
1074 **Arguments**
1075 |Type|Name|Description|
1076 |-|-|-|
1077 |void *|handle|_mz_zip_reader_ instance|
1078 |void **|zip_handle|Pointer to store _mz_zip_ instance|
1079
1080 **Return**
1081 |Type|Description|
1082 |-|-|
1083 |void|No return|
1084
1085 **Example**
1086 ```
1087 void *zip_handle = NULL;
1088 mz_zip_reader_get_zip_handle(zip_reader, &zip_handle);
1089 mz_zip_goto_first_entry(zip_handle);
1090 ```
1091
1092 ### mz_zip_reader_create
1093
1094 Creates a _mz_zip_reader_ instance and returns its pointer.
1095
1096 **Return**
1097 |Type|Description|
1098 |-|-|
1099 |void *|Pointer to the _mz_zip_reader_ instance|
1100
1101 **Example**
1102 ```
1103 void *zip_reader = mz_zip_reader_create();
1104 ```
1105
1106 ### mz_zip_reader_delete
1107
1108 Deletes a _mz_zip_reader_ instance and resets its pointer to zero.
1109
1110 **Arguments**
1111 |Type|Name|Description|
1112 |-|-|-|
1113 |void **|handle|Pointer to the _mz_zip_reader_ instance|
1114
1115 **Return**
1116 |Type|Description|
1117 |-|-|
1118 |void|No return|
1119
1120 **Example**
1121 ```
1122 void *zip_reader = mz_zip_reader_create();
1123 mz_zip_reader_delete(&zip_reader);
1124 ```
1125
1126 ## Writer Callbacks
1127
1128 ### mz_zip_writer_overwrite_cb
1129
1130 Callback that is called when it is about to overwrite an existing zip file. This callback can be set by calling _mz_zip_writer_set_overwrite_cb_.
1131
1132 **Arguments**
1133 |Type|Name|Description|
1134 |-|-|-|
1135 |void **|handle|Pointer to the _mz_zip_writer_ instance|
1136 |void *|userdata|User data pointer|
1137 |const char *|path|Zip file path that will be overwritten|
1138
1139 **Return**
1140 |Type|Description|
1141 |-|-|
1142 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if it should overwrite|
1143
1144 **Example**
1145 ```
1146 int32_t writer_overwrite_cb(void *handle, void *userdata, const char *path) {
1147 printf("Zip file is going to be overwritten %s\n", path);
1148 // if not ok return MZ_INTERNAL_ERROR;
1149 return MZ_OK;
1150 }
1151 mz_zip_writer_set_overwrite_cb(zip_writer, NULL, writer_overwrite_cb);
1152 ```
1153
1154 Also see _minizip_add_overwrite_cb_ for advanced example.
1155
1156 ### mz_zip_writer_password_cb
1157
1158 Callback that is called when it needs a password. Any entries that are added with the _MZ_ZIP_FLAG_ENCRYPTED_ flag will need a password. This callback can be set by calling _mz_zip_writer_set_password_cb_.
1159
1160 **Arguments**
1161 |Type|Name|Description|
1162 |-|-|-|
1163 |void **|handle|Pointer to the _mz_zip_writer_ instance|
1164 |void *|userdata|User data pointer|
1165 |mz_zip_file *|file_info|Entry that needs password when adding|
1166 |char *|password|Password character buffer|
1167 |int32_t|max_password|Maximum password buffer size|
1168
1169 **Return**
1170 |Type|Description|
1171 |-|-|
1172 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
1173
1174 **Example**
1175 ```
1176 int32_t writer_password_cb(void *handle, void *userdata, mz_zip_file *file_info, char *password, int32_t max_password) {
1177 printf("Supplying password for %s\n", file_info->filename);
1178 strncpy(password, "mypassword", max_password - 1);
1179 password[max_password - 1] = 0;
1180 return MZ_OK;
1181 }
1182 mz_zip_writer_set_password_cb(zip_writer, NULL, writer_password_cb);
1183 ```
1184
1185 ### mz_zip_writer_progress_cb
1186
1187 Callback that is called during compression to report progress. It is called on an interval specified by _mz_zip_writer_set_progress_interval_. This callback can be set by calling _mz_zip_writer_set_progress_cb_.
1188
1189 **Arguments**
1190 |Type|Name|Description|
1191 |-|-|-|
1192 |void **|handle|Pointer to the _mz_zip_writer_ instance|
1193 |void *|userdata|User data pointer|
1194 |mz_zip_file *|file_info|Entry that is being compressed|
1195 |int64_t|position|File write position. To calculate progress when writing raw use `position / file_info->compressed_size`. To calculate progress when writing data to be compressed use `position / file_info->uncompressed_size`.|
1196
1197 **Return**
1198 |Type|Description|
1199 |-|-|
1200 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
1201
1202 **Example**
1203
1204 See example in _minizip_extract_progress_cb_.
1205
1206 ### mz_zip_writer_entry_cb
1207
1208 Callback that is called for each entry that is compressed. This callback can be set by calling _mz_zip_writer_set_entry_cb.
1209
1210 **Arguments**
1211 |Type|Name|Description|
1212 |-|-|-|
1213 |void **|handle|Pointer to the _mz_zip_writer_ instance|
1214 |void *|userdata|User data pointer|
1215 |mz_zip_file *|file_info|Entry that is being compressed|
1216
1217 **Return**
1218 |Type|Description|
1219 |-|-|
1220 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
1221
1222 **Example**
1223
1224 See example in _minizip_extract_entry_cb.
1225
1226 ## Writer Open/Close
1227
1228 ### mz_zip_writer_is_open
1229
1230 Checks to see if the zip file is open.
1231
1232 **Arguments**
1233 |Type|Name|Description|
1234 |-|-|-|
1235 |void *|handle|_mz_zip_writer_ instance|
1236
1237 **Return**
1238 |Type|Description|
1239 |-|-|
1240 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if open.|
1241
1242 **Example**
1243 ```
1244 if (mz_zip_writer_is_open(zip_writer) == MZ_OK)
1245 printf("Zip file is open in writer\n");
1246 ```
1247
1248 ### mz_zip_writer_open
1249
1250 Opens zip file from stream.
1251
1252 **Arguments**
1253 |Type|Name|Description|
1254 |-|-|-|
1255 |void *|handle|_mz_zip_writer_ instance|
1256 |void *|stream|_mz_stream_ instance|
1257 |uint8_t|append|Opens in append mode if 1|
1258
1259 **Return**
1260 |Type|Description|
1261 |-|-|
1262 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if opened.|
1263
1264 **Example**
1265 ```
1266 const char *path = "c:\\my.zip";
1267
1268 void *zip_writer = mz_zip_writer_create();
1269 void *file_stream = mz_stream_os_create();
1270
1271 err = mz_stream_os_open(file_stream, path, MZ_OPEN_MODE_WRITE | MZ_OPEN_MODE_CREATE);
1272 if (err == MZ_OK) {
1273 err = mz_zip_writer_open(zip_writer, file_stream, 0);
1274 if (err == MZ_OK) {
1275 printf("Zip writer was opened %s\n", path);
1276 mz_zip_writer_close(zip_writer);
1277 }
1278 }
1279
1280 mz_stream_os_delete(&file_stream);
1281 mz_zip_writer_delete(&zip_writer);
1282 ```
1283
1284 ### mz_zip_writer_open_file
1285
1286 Opens zip file from a file path.
1287
1288 **Arguments**
1289 |Type|Name|Description|
1290 |-|-|-|
1291 |void *|handle|_mz_zip_reader_ instance|
1292 |const char *|path|Path to zip file|
1293 |int64_t|disk_size|Disk size in bytes if using disk spanning, otherwise 0|
1294 |uint8_t|append|Opens in append mode if 1|
1295
1296 **Return**
1297 |Type|Description|
1298 |-|-|
1299 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if opened.|
1300
1301 **Example**
1302 ```
1303 const char *path = "c:\\my.zip";
1304 void *zip_writer = mz_zip_writer_create();
1305 if (mz_zip_writer_open_file(zip_writer, path, 0, 0) == MZ_OK) {
1306 printf("Zip writer was opened %s\n", path);
1307 mz_zip_writer_close(zip_writer);
1308 }
1309 mz_zip_writer_delete(&zip_writer);
1310 ```
1311
1312 ### mz_zip_writer_open_file_in_memory
1313
1314 Opens zip file from a file path into memory for faster access.
1315
1316 **Arguments**
1317 |Type|Name|Description|
1318 |-|-|-|
1319 |void *|handle|_mz_zip_writer_ instance|
1320 |const char *|path|Path to zip file|
1321
1322 **Return**
1323 |Type|Description|
1324 |-|-|
1325 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if opened.|
1326
1327 **Example**
1328 ```
1329 const char *path = "c:\\my.zip";
1330 void *zip_writer = mz_zip_writer_create();
1331 if (mz_zip_writer_open_file_in_memory(zip_writer, path) == MZ_OK) {
1332 printf("Zip writer was opened in memory %s\n", path);
1333 mz_zip_writer_close(zip_writer);
1334 }
1335 mz_zip_writer_delete(&zip_writer);
1336 ```
1337
1338 ### mz_zip_writer_close
1339
1340 Closes the zip file.
1341
1342 **Arguments**
1343 |Type|Name|Description|
1344 |-|-|-|
1345 |void *|handle|_mz_zip_writer_ instance|
1346
1347 **Return**
1348 |Type|Description|
1349 |-|-|
1350 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
1351
1352 **Example**
1353 ```
1354 if (mz_zip_writer_close(zip_writer) == MZ_OK)
1355 printf("Zip writer closed\n");
1356 ```
1357
1358 ## Writer Entry
1359
1360 ### mz_zip_writer_entry_open
1361
1362 Opens an entry in the zip file for writing.
1363
1364 **Arguments**
1365 |Type|Name|Description|
1366 |-|-|-|
1367 |void *|handle|_mz_zip_writer_ instance|
1368 |mz_zip_file *|file_info|Zip entry info for entry that is being written|
1369
1370 **Return**
1371 |Type|Description|
1372 |-|-|
1373 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
1374
1375 **Example**
1376 ```
1377 mz_zip_file file_info = { 0 };
1378
1379 file_info.filename = "newfile.txt";
1380 file_info.modified_date = time(NULL);
1381 file_info.version_madeby = MZ_VERSION_MADEBY;
1382 file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
1383 file_info.flag = MZ_ZIP_FLAG_UTF8;
1384
1385 if (mz_zip_writer_entry_open(zip_writer, &file_info) == MZ_OK) {
1386 printf("Started writing new entry %s\n", file_info.filename);
1387 }
1388 ```
1389
1390 ### mz_zip_writer_entry_close
1391
1392 Closes entry in zip file.
1393
1394 **Arguments**
1395 |Type|Name|Description|
1396 |-|-|-|
1397 |void *|handle|_mz_zip_writer_ instance|
1398
1399 **Return**
1400 |Type|Description|
1401 |-|-|
1402 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
1403
1404 **Example**
1405 ```
1406 if (mz_zip_writer_close(zip_writer) == MZ_OK) {
1407 printf("Stopped writing new entry\n");
1408 }
1409 ```
1410
1411 ### mz_zip_writer_entry_write
1412
1413 Writes data into entry for zip.
1414
1415 **Arguments**
1416 |Type|Name|Description|
1417 |-|-|-|
1418 |void *|handle|_mz_zip_writer_ instance|
1419 |void *|buf|Buffer of data to write|
1420 int32_t|len|Number of bytes to write|
1421
1422 **Return**
1423 |Type|Description|
1424 |-|-|
1425 |int32_t|Bytes written or [MZ_ERROR](mz_error.md) code if less than 0.|
1426
1427
1428 **Example**
1429 ```
1430 mz_zip_file file_info = { 0 };
1431
1432 file_info.filename = "newfile.txt";
1433 file_info.modified_date = time(NULL);
1434 file_info.version_madeby = MZ_VERSION_MADEBY;
1435 file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
1436 file_info.flag = MZ_ZIP_FLAG_UTF8;
1437
1438 if (mz_zip_writer_entry_open(zip_writer, &file_info) == MZ_OK) {
1439 printf("Started writing new entry %s\n", file_info.filename);
1440 int32_t bytes_written = mz_zip_writer_entry_write(zip_writer, "test", 4);
1441 if (bytes_written == 4) {
1442 printf("Successfully wrote test\n");
1443 }
1444 mz_zip_writer_entry_close(zip_writer);
1445 }
1446 ```
1447
1448 ## Writer Add/Compress
1449
1450 ### mz_zip_writer_add
1451
1452 Writes all data to the currently open entry in the zip.
1453
1454 **Arguments**
1455 |Type|Name|Description|
1456 |-|-|-|
1457 |void *|handle|_mz_zip_writer_ instance|
1458 |void *|stream|_mz_stream_ instance|
1459 |mz_stream_read_cb|read_cb|Callback to read from when adding new entry|
1460
1461 **Return**
1462 |Type|Description|
1463 |-|-|
1464 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
1465
1466 **Example**
1467 ```
1468 void *mem_stream = mz_stream_mem_create();
1469 mz_stream_mem_set_grow(mem_stream, 1);
1470 mz_stream_mem_write(mem_stream, "test", 4);
1471 mz_stream_mem_seek(mem_stream, 0, MZ_SEEK_SET);
1472
1473 mz_zip_file file_info = { 0 };
1474
1475 file_info.filename = "newfile.txt";
1476 file_info.modified_date = time(NULL);
1477 file_info.version_madeby = MZ_VERSION_MADEBY;
1478 file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
1479 file_info.flag = MZ_ZIP_FLAG_UTF8;
1480
1481 if (mz_zip_writer_entry_open(zip_writer, &file_info) == MZ_OK) {
1482 if (mz_zip_writer_add(zip_writer, mem_stream, mz_stream_mem_read) == MZ_OK) {
1483 printf("Added new entry from stream\n");
1484 }
1485 mz_zip_writer_entry_close(zip_writer);
1486 }
1487
1488 mz_stream_mem_delete(&mem_stream);
1489 ```
1490
1491 ### mz_zip_writer_add_process
1492
1493 Writes a portion of data to the currently open entry in the zip. This function is intended to be used in process loops where you don't want to compress the entire file in one function.
1494
1495 **Arguments**
1496 |Type|Name|Description|
1497 |-|-|-|
1498 |void *|handle|_mz_zip_writer_ instance|
1499 |void *|stream|_mz_stream_ instance|
1500 |mz_stream_read_cb|read_cb|Callback to read from when adding new entry|
1501
1502 **Return**
1503 |Type|Description|
1504 |-|-|
1505 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful or MZ_END_OF_STREAM if done.|
1506
1507 **Example**
1508
1509 See source code for _mz_zip_writer_add_.
1510
1511 ### mz_zip_writer_add_info
1512
1513 Adds an entry to the zip based on the info.
1514
1515 **Arguments**
1516 |Type|Name|Description|
1517 |-|-|-|
1518 |void *|handle|_mz_zip_writer_ instance|
1519 |void *|stream|_mz_stream_ instance|
1520 |mz_stream_read_cb|read_cb|Callback to read from when adding new entry|
1521 |mz_file_info *|file_info|Zip entry information for adding new entry|
1522
1523 **Return**
1524 |Type|Description|
1525 |-|-|
1526 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
1527
1528 **Example**
1529 ```
1530 void *mem_stream = mz_stream_mem_create();
1531 mz_stream_mem_set_grow(mem_stream, 1);
1532 mz_stream_mem_write(mem_stream, "test", 4);
1533 mz_stream_mem_seek(mem_stream, 0, MZ_SEEK_SET);
1534
1535 mz_zip_file file_info = { 0 };
1536
1537 file_info.filename = "newfile.txt";
1538 file_info.modified_date = time(NULL);
1539 file_info.version_madeby = MZ_VERSION_MADEBY;
1540 file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
1541 file_info.flag = MZ_ZIP_FLAG_UTF8;
1542
1543 if (mz_zip_writer_add_info(zip_writer, mem_stream, mz_stream_mem_read, &file_info) == MZ_OK) {
1544 printf("Added new entry from stream\n");
1545 }
1546
1547 mz_stream_mem_delete(&mem_stream);
1548 ```
1549
1550 ### mz_zip_writer_add_buffer
1551
1552 Adds an entry to the zip with a memory buffer.
1553
1554 **Arguments**
1555 |Type|Name|Description|
1556 |-|-|-|
1557 |void *|handle|_mz_zip_writer_ instance|
1558 |void *|buf|Buffer to read when compressing|
1559 |int32_t|len|Length of buffer to read|
1560 |mz_file_info *|file_info|Zip entry information for adding new entry|
1561
1562 **Return**
1563 |Type|Description|
1564 |-|-|
1565 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
1566
1567 **Example**
1568 ```
1569 mz_zip_file file_info = { 0 };
1570
1571 file_info.filename = "newfile.txt";
1572 file_info.modified_date = time(NULL);
1573 file_info.version_madeby = MZ_VERSION_MADEBY;
1574 file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
1575 file_info.flag = MZ_ZIP_FLAG_UTF8;
1576
1577 char *contents = "test";
1578
1579 if (mz_zip_writer_add_buffer(zip_writer, contents, strlen(contents), &file_info) == MZ_OK) {
1580 printf("Added new entry from buffer\n");
1581 }
1582 ```
1583
1584 ### mz_zip_writer_add_file
1585
1586 Adds an entry to the zip from a file.
1587
1588 **Arguments**
1589 |Type|Name|Description|
1590 |-|-|-|
1591 |void *|handle|_mz_zip_writer_ instance|
1592 |const char *|path|Path to file on disk to add|
1593 |const char *|filename_in_zip|Filename in zip to write|
1594
1595 **Return**
1596 |Type|Description|
1597 |-|-|
1598 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
1599
1600 **Example**
1601 ```
1602 const char *path = "c:\\mydatafiles\\tx101.txt";
1603 const char *filename_in_zip = "101.txt;
1604 if (mz_zip_writer_add_file(zip_writer, path, filename_in_zip) == MZ_OK) {
1605 printf("Entry added to zip %s as %s\n", path, filename_in_zip);
1606 }
1607 ```
1608
1609 ### mz_zip_writer_add_path
1610
1611 Enumerates a directory or pattern and adds entries to the zip.
1612
1613 **Arguments**
1614 |Type|Name|Description|
1615 |-|-|-|
1616 |void *|handle|_mz_zip_writer_ instance|
1617 |const char *|path|Path of directory to add (can include search pattern)|
1618 |const char *|root_path|Root directory to start adding from. Entries will be named in the relative to this root path|
1619 |uint8_t|include_path|Include the full path if 1|
1620 |uint8_t|recursive|Process directory recursively if 1|
1621
1622 **Return**
1623 |Type|Description|
1624 |-|-|
1625 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
1626
1627 **Example**
1628 ```
1629 if (mz_zip_writer_add_path(zip_writer, "c:\\dir1\\dir2\\", "c:\\dir1\", 0, 1) == MZ_OK) {
1630 printf("Added entries from c:\\dir1\\dir2\\ recursively\n");
1631 }
1632 ```
1633
1634 ### mz_zip_writer_copy_from_reader
1635
1636 Adds an entry from a zip reader instance. This copies the current entry from the zip reader instance.
1637
1638 **Arguments**
1639 |Type|Name|Description|
1640 |-|-|-|
1641 |void *|handle|_mz_zip_writer_ instance|
1642 |void *|reader|_mz_zip_reader_ instance|
1643
1644 **Return**
1645 |Type|Description|
1646 |-|-|
1647 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
1648
1649 **Example**
1650
1651 See source code for _minizip_erase_ where it erases a zip entry by copying all entries from the source zip file to the target zip file.
1652
1653 ## Writer Object
1654
1655 ### mz_zip_writer_set_password
1656
1657 Password to use for encrypting files in the zip.
1658
1659 **Arguments**
1660 |Type|Name|Description|
1661 |-|-|-|
1662 |void *|handle|_mz_zip_writer_ instance|
1663 |const char *|password|Password string|
1664
1665 **Return**
1666 |Type|Description|
1667 |-|-|
1668 |void|No return|
1669
1670 **Example**
1671 ```
1672 mz_zip_writer_set_password(zip_writer, "myzippass");
1673 ```
1674
1675 ### mz_zip_writer_set_comment
1676
1677 Comment to use for the archive.
1678
1679 **Arguments**
1680 |Type|Name|Description|
1681 |-|-|-|
1682 |void *|handle|_mz_zip_writer_ instance|
1683 |const char *|comment|Global zip file comment string|
1684
1685 **Return**
1686 |Type|Description|
1687 |-|-|
1688 |void|No return|
1689
1690 **Example**
1691 ```
1692 mz_zip_writer_set_comment(zip_writer, "This is my zip file -- hands off!");
1693 ```
1694
1695 ### mz_zip_writer_set_raw
1696
1697 Sets whether or not we should write the entry raw.
1698
1699 **Arguments**
1700 |Type|Name|Description|
1701 |-|-|-|
1702 |void *|handle|_mz_zip_writer_ instance|
1703 |uint8_t|raw|Write data in zip in raw mode (don't compress) if 1|
1704
1705 **Return**
1706 |Type|Description|
1707 |-|-|
1708 |void|No return|
1709
1710 **Example**
1711 ```
1712 mz_zip_writer_set_raw(zip_writer, 1);
1713 ```
1714
1715 ### mz_zip_writer_get_raw
1716
1717 Gets whether or not we should write the entry raw.
1718
1719 **Arguments**
1720 |Type|Name|Description|
1721 |-|-|-|
1722 |void *|handle|_mz_zip_writer_ instance|
1723 |uint8_t *|raw|Pointer to store if using raw mode|
1724
1725 **Return**
1726 |Type|Description|
1727 |-|-|
1728 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
1729
1730 **Example**
1731 ```
1732 uint8_t raw = 0;
1733 mz_zip_writer_get_raw(zip_writer, &raw);
1734 printf("Writing zip entries in mode: %s\n", (raw) ? "raw" : "normal");
1735 ```
1736
1737 ### mz_zip_writer_set_aes
1738
1739 Use aes encryption when adding files in zip.
1740
1741 **Arguments**
1742 |Type|Name|Description|
1743 |-|-|-|
1744 |void *|handle|_mz_zip_writer_ instance|
1745 |uint8_t|aes|Encrypt files with AES 256-bit if 1|
1746
1747 **Return**
1748 |Type|Description|
1749 |-|-|
1750 |void|No return|
1751
1752 **Example**
1753 ```
1754 mz_zip_writer_set_aes(zip_writer, 1);
1755 ```
1756
1757 ### mz_zip_writer_set_compress_method
1758
1759 Sets the compression method when adding files in zip.
1760
1761 **Arguments**
1762 |Type|Name|Description|
1763 |-|-|-|
1764 |void *|handle|_mz_zip_writer_ instance|
1765 |uint16_t|compress_method|[MZ_COMPRESS_METHOD](mz_compress_method.md) compression method when adding entries|
1766
1767 **Return**
1768 |Type|Description|
1769 |-|-|
1770 |void|No return|
1771
1772 **Example**
1773 ```
1774 mz_zip_writer_set_compress_method(zip_writer, MZ_COMPRESS_METHOD_STORE);
1775 ```
1776
1777 ### mz_zip_writer_set_compress_level
1778
1779 Sets the compression level when adding files in zip.
1780
1781 **Arguments**
1782 |Type|Name|Description|
1783 |-|-|-|
1784 |void *|handle|_mz_zip_writer_ instance|
1785 |int16_t|compress_level|[MZ_COMPRESS_LEVEL](mz_compress_level.md) compression level when adding entries|
1786
1787 **Return**
1788 |Type|Description|
1789 |-|-|
1790 |void|No return|
1791
1792 **Example**
1793 ```
1794 mz_zip_writer_set_compress_level(zip_writer, MZ_COMPRESS_LEVEL_BEST);
1795 ```
1796
1797 ### mz_zip_writer_set_zip_cd
1798
1799 Sets whether or not the central directory should be zipped.
1800
1801 **Arguments**
1802 |Type|Name|Description|
1803 |-|-|-|
1804 |void *|handle|_mz_zip_writer_ instance|
1805 |uint8_t|zip_cd|Zip the central directory if 1|
1806
1807 **Return**
1808 |Type|Description|
1809 |-|-|
1810 |void|No return|
1811
1812 **Example**
1813 ```
1814 mz_zip_writer_set_zip_cd(zip_writer, 1);
1815 ```
1816
1817 ### mz_zip_writer_set_overwrite_cb
1818
1819 Sets the callback for what to do when a zip file is about to be overwritten.
1820
1821 **Arguments**
1822 |Type|Name|Description|
1823 |-|-|-|
1824 |void *|handle|_mz_zip_writer_ instance|
1825 |void *|userdata|User supplied data|
1826 |mz_zip_writer_overwrite_cb|cb|_mz_zip_writer_overwrite_cb_ function pointer|
1827
1828 **Return**
1829 |Type|Description|
1830 |-|-|
1831 |void|No return|
1832
1833 **Example**
1834
1835 See example for _mz_zip_writer_overwrite_cb_.
1836
1837 ### mz_zip_writer_set_password_cb
1838
1839 Sets the callback for what to do when a password for an entry.
1840
1841 **Arguments**
1842 |Type|Name|Description|
1843 |-|-|-|
1844 |void *|handle|_mz_zip_writer_ instance|
1845 |void *|userdata|User supplied data|
1846 |mz_zip_writer_password_cb|cb|_mz_zip_writer_password_cb_ function pointer|
1847
1848 **Return**
1849 |Type|Description|
1850 |-|-|
1851 |void|No return|
1852
1853 **Example**
1854
1855 See example for _mz_zip_writer_password_cb_.
1856
1857 ### mz_zip_writer_set_progress_cb
1858
1859 Sets the callback that gets called to update compression progress. This callback is called on an interval specified by _mz_zip_writer_set_progress_interval_.
1860
1861 **Arguments**
1862 |Type|Name|Description|
1863 |-|-|-|
1864 |void *|handle|_mz_zip_writer_ instance|
1865 |void *|userdata|User supplied data|
1866 |mz_zip_writer_progress_cb|cb|_mz_zip_writer_progress_cb_ function pointer|
1867
1868 **Return**
1869 |Type|Description|
1870 |-|-|
1871 |void|No return|
1872
1873 **Example**
1874
1875 See example for _mz_zip_writer_progress_cb_.
1876
1877 ### mz_zip_writer_set_progress_interval
1878
1879 Let at least milliseconds pass between calls to progress callback.
1880
1881 **Arguments**
1882 |Type|Name|Description|
1883 |-|-|-|
1884 |void *|handle|_mz_zip_writer_ instance|
1885 |uint32_t|milliseconds|Number of milliseconds to wait before calling _mz_zip_writer_progress_cb_ during extraction|
1886
1887 **Return**
1888 |Type|Description|
1889 |-|-|
1890 |void|No return|
1891
1892 **Example**
1893 ```
1894 mz_zip_writer_set_progress_interval(zip_writer, 1000); // Wait 1 sec
1895 ```
1896
1897 ### mz_zip_writer_set_entry_cb
1898
1899 Sets callback for when a new zip file entry is encountered during compression.
1900
1901 **Arguments**
1902 |Type|Name|Description|
1903 |-|-|-|
1904 |void *|handle|_mz_zip_writer_ instance|
1905 |void *|userdata|User supplied data|
1906 |mz_zip_writer_entry_cb|cb|_mz_zip_writer_entry_cb_ function pointer|
1907
1908 **Return**
1909 |Type|Description|
1910 |-|-|
1911 |void|No return|
1912
1913 **Example**
1914
1915 See example for _mz_zip_writer_entry_cb_.
1916
1917 ### mz_zip_writer_get_zip_handle
1918
1919 Gets the underlying zip instance handle.
1920
1921 **Arguments**
1922 |Type|Name|Description|
1923 |-|-|-|
1924 |void *|handle|_mz_zip_writer_ instance|
1925 |void **|zip_handle|Pointer to store _mz_zip_ instance|
1926
1927 **Return**
1928 |Type|Description|
1929 |-|-|
1930 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
1931
1932 **Example**
1933 ```
1934 void *zip_handle = NULL;
1935 mz_zip_writer_get_zip_handle(zip_writer, &zip_handle);
1936 ```
1937
1938 ### mz_zip_writer_create
1939
1940 Creates a _mz_zip_writer_ instance and returns its pointer.
1941
1942 **Return**
1943 |Type|Description|
1944 |-|-|
1945 |void *|Pointer to the _mz_zip_writer_ instance|
1946
1947 **Example**
1948 ```
1949 void *zip_writer = mz_zip_writer_create();
1950 ```
1951
1952 ### mz_zip_writer_delete
1953
1954 Deletes a _mz_zip_writer_ instance and resets its pointer to zero.
1955
1956 **Arguments**
1957 |Type|Name|Description|
1958 |-|-|-|
1959 |void **|handle|Pointer to the _mz_zip_writer_ instance|
1960
1961 **Return**
1962 |Type|Description|
1963 |-|-|
1964 |void|No return|
1965
1966 **Example**
1967 ```
1968 void *zip_writer = mz_zip_writer_create();
1969 mz_zip_writer_delete(&zip_writer);
1970 ```