Debian Codegraph @

doc/mz_zip.md

raw symbols SCIP
1 # MZ_ZIP <!-- omit in toc -->
2
3 The _mz_zip_ object allows for the reading and writing of the a zip file and its entries.
4
5 - [Archive](#archive)
6 - [mz\_zip\_create](#mz_zip_create)
7 - [mz\_zip\_delete](#mz_zip_delete)
8 - [mz\_zip\_open](#mz_zip_open)
9 - [mz\_zip\_close](#mz_zip_close)
10 - [mz\_zip\_get\_comment](#mz_zip_get_comment)
11 - [mz\_zip\_set\_comment](#mz_zip_set_comment)
12 - [mz\_zip\_get\_version\_madeby](#mz_zip_get_version_madeby)
13 - [mz\_zip\_set\_version\_madeby](#mz_zip_set_version_madeby)
14 - [mz\_zip\_set\_recover](#mz_zip_set_recover)
15 - [mz\_zip\_set\_data\_descriptor](#mz_zip_set_data_descriptor)
16 - [mz\_zip\_get\_stream](#mz_zip_get_stream)
17 - [mz\_zip\_set\_cd\_stream](#mz_zip_set_cd_stream)
18 - [mz\_zip\_get\_cd\_mem\_stream](#mz_zip_get_cd_mem_stream)
19 - [mz\_zip\_set\_number\_entry](#mz_zip_set_number_entry)
20 - [mz\_zip\_get\_number\_entry](#mz_zip_get_number_entry)
21 - [mz\_zip\_set\_disk\_number\_with\_cd](#mz_zip_set_disk_number_with_cd)
22 - [mz\_zip\_get\_disk\_number\_with\_cd](#mz_zip_get_disk_number_with_cd)
23 - [Entry I/O](#entry-io)
24 - [mz\_zip\_entry\_is\_open](#mz_zip_entry_is_open)
25 - [mz\_zip\_entry\_read\_open](#mz_zip_entry_read_open)
26 - [mz\_zip\_entry\_read](#mz_zip_entry_read)
27 - [mz\_zip\_entry\_read\_close](#mz_zip_entry_read_close)
28 - [mz\_zip\_entry\_write\_open](#mz_zip_entry_write_open)
29 - [mz\_zip\_entry\_write](#mz_zip_entry_write)
30 - [mz\_zip\_entry\_write\_close](#mz_zip_entry_write_close)
31 - [mz\_zip\_entry\_seek\_local\_header](#mz_zip_entry_seek_local_header)
32 - [mz\_zip\_entry\_get\_compress\_stream](#mz_zip_entry_get_compress_stream)
33 - [mz\_zip\_entry\_close\_raw](#mz_zip_entry_close_raw)
34 - [mz\_zip\_entry\_close](#mz_zip_entry_close)
35 - [Entry Enumeration](#entry-enumeration)
36 - [mz\_zip\_entry\_is\_dir](#mz_zip_entry_is_dir)
37 - [mz\_zip\_entry\_is\_symlink](#mz_zip_entry_is_symlink)
38 - [mz\_zip\_entry\_get\_info](#mz_zip_entry_get_info)
39 - [mz\_zip\_entry\_get\_local\_info](#mz_zip_entry_get_local_info)
40 - [mz\_zip\_get\_entry](#mz_zip_get_entry)
41 - [mz\_zip\_goto\_entry](#mz_zip_goto_entry)
42 - [mz\_zip\_goto\_first\_entry](#mz_zip_goto_first_entry)
43 - [mz\_zip\_goto\_next\_entry](#mz_zip_goto_next_entry)
44 - [mz\_zip\_locate\_entry](#mz_zip_locate_entry)
45 - [mz\_zip\_locate\_first\_entry](#mz_zip_locate_first_entry)
46 - [mz\_zip\_locate\_next\_entry](#mz_zip_locate_next_entry)
47 - [System Attributes](#system-attributes)
48 - [mz\_zip\_attrib\_is\_dir](#mz_zip_attrib_is_dir)
49 - [mz\_zip\_attrib\_is\_symlink](#mz_zip_attrib_is_symlink)
50 - [mz\_zip\_attrib\_convert](#mz_zip_attrib_convert)
51 - [mz\_zip\_attrib\_posix\_to\_win32](#mz_zip_attrib_posix_to_win32)
52 - [mz\_zip\_attrib\_win32\_to\_posix](#mz_zip_attrib_win32_to_posix)
53 - [Extrafield](#extrafield)
54 - [mz\_zip\_extrafield\_find](#mz_zip_extrafield_find)
55 - [mz\_zip\_extrafield\_contains](#mz_zip_extrafield_contains)
56 - [mz\_zip\_extrafield\_read](#mz_zip_extrafield_read)
57 - [mz\_zip\_extrafield\_write](#mz_zip_extrafield_write)
58 - [Time/Date](#timedate)
59 - [mz\_zip\_dosdate\_to\_tm](#mz_zip_dosdate_to_tm)
60 - [mz\_zip\_dosdate\_to\_time\_t](#mz_zip_dosdate_to_time_t)
61 - [mz\_zip\_time\_t\_to\_tm](#mz_zip_time_t_to_tm)
62 - [mz\_zip\_time\_t\_to\_dos\_date](#mz_zip_time_t_to_dos_date)
63 - [mz\_zip\_tm\_to\_dosdate](#mz_zip_tm_to_dosdate)
64 - [mz\_zip\_ntfs\_to\_unix\_time](#mz_zip_ntfs_to_unix_time)
65 - [mz\_zip\_unix\_to\_ntfs\_time](#mz_zip_unix_to_ntfs_time)
66 - [Path](#path)
67 - [mz\_zip\_path\_compare](#mz_zip_path_compare)
68 - [String](#string)
69 - [mz\_zip\_get\_compression\_method\_string](#mz_zip_get_compression_method_string)
70
71 ## Archive
72
73 ### mz_zip_create
74
75 Creates a _mz_zip_ instance and returns its pointer.
76
77 **Return**
78 |Type|Description|
79 |-|-|
80 |void *|Pointer to the _mz_zip_ instance|
81
82 **Example**
83 ```
84 void *zip_handle = mz_zip_create();
85 ```
86
87 ### mz_zip_delete
88
89 Deletes a _mz_zip_ instance and resets its pointer to zero.
90
91 **Arguments**
92 |Type|Name|Description|
93 |-|-|-|
94 |void **|handle|Pointer to the _mz_zip_ instance|
95
96 **Return**
97 |Type|Description|
98 |-|-|
99 |void|No return|
100
101 **Example**
102 ```
103 void *zip_handle = mz_zip_create();
104 mz_zip_delete(&zip_handle);
105 ```
106
107 ### mz_zip_open
108
109 Opens a zip file given a stream.
110
111 **Arguments**
112 |Type|Name|Description|
113 |-|-|-|
114 |void *|handle|_mz_zip_ instance|
115 |void *|stream|_mz_stream_ instance|
116 |int32_t|mode|Open mode (See [MZ_OPEN_MODE](mz_open_mode.md))|
117
118 **Return**
119 |Type|Description|
120 |-|-|
121 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
122
123 **Example**
124 ```
125 // TODO: Create stream
126
127 void *zip_handle = mz_zip_create();
128 err = mz_zip_open(zip_handle, stream, MZ_OPEN_MODE_READ);
129 if (err != MZ_OK)
130 printf("Error opening zip file for reading\n");
131 else
132 mz_zip_close(zip_handle);
133 mz_zip_delete(&zip_handle);
134
135 // TODO: Delete stream
136 ```
137
138 ### mz_zip_close
139
140 Close a zip file.
141
142 **Arguments**
143 |Type|Name|Description|
144 |-|-|-|
145 |void *|handle|_mz_zip_ instance|
146
147 **Return**
148 |Type|Description|
149 |-|-|
150 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
151
152 **Example**
153 ```
154 // TODO: Create and open mz_zip instance
155 mz_zip_close(zip_handle);
156 mz_zip_delete(&zip_handle);
157 ```
158
159 ### mz_zip_get_comment
160
161 Gets the zip file's global comment string.
162
163 **Arguments**
164 |Type|Name|Description|
165 |-|-|-|
166 |void *|handle|_mz_zip_ instance|
167 |const char **|comment|Pointer to null-terminated comment character array|
168
169 **Return**
170 |Type|Description|
171 |-|-|
172 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
173
174 **Example**
175 ```
176 const char *global_comment = NULL;
177 if (mz_zip_get_comment(zip_handle, &global_comment) == MZ_OK)
178 printf("Zip file global comment: %s\n", global_comment);
179 else
180 printf("Zip file does not contain a global comment\n");
181 ```
182
183 ### mz_zip_set_comment
184
185 Sets the zip file's global comment string when the zip file is opened for writing. According to the zip file specification, each zip file can store a global comment with a maximum length of _UINT16_MAX_ or 65535 characters.
186
187 **Arguments**
188 |Type|Name|Description|
189 |-|-|-|
190 |void *|handle|_mz_zip_ instance|
191 |const char *|comment|Null-terminated comment character array|
192
193 **Return**
194 |Type|Description|
195 |-|-|
196 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
197
198 **Example**
199 ```
200 const char *global_comment = "Hi! This is my zip file.";
201 if (mz_zip_set_comment(zip_handle, global_comment) == MZ_OK)
202 printf("Successfully set the zip file's global comment\n");
203 ```
204
205 ### mz_zip_get_version_madeby
206
207 Gets the zip file's version information.
208
209 **Arguments**
210 |Type|Name|Description|
211 |-|-|-|
212 |void *|handle|_mz_zip_ instance|
213 |uint16_t *|version_madeby|Pointer to version value (See [PKWARE zip app note](zip/appnote.txt) 4.4.2)|
214
215 **Return**
216 |Type|Description|
217 |-|-|
218 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
219
220 **Example**
221 ```
222 uint16_t version_madeby = 0;
223 if (mz_zip_get_version_madeby(zip_handle, &version_madeby) == MZ_OK) {
224 printf("Zip eocd version made by: %04x\n", version_madeby);
225 printf("Zip eocd host system: %d\n", MZ_HOST_SYSTEM(version_madeby));
226 printf("Zip eocd app note version: %d\n", (version_madeby & 0xFF));
227 }
228 ```
229
230 ### mz_zip_set_version_madeby
231
232 Sets the zip file's version information. The application that original wrote the zip file would have set this value to indicate what operating system and version of the zip specification that was used when making it. This version is stored in the end of central directory record; there is another similar _version_madeby_ stored for each zip entry.
233
234 **Arguments**
235 |Type|Name|Description|
236 |-|-|-|
237 |void *|handle|_mz_zip_ instance|
238 |uint16_t|version_madeby|Version value (See [PKWARE zip app note](zip/appnote.txt) 4.4.2)|
239
240 **Return**
241 |Type|Description|
242 |-|-|
243 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
244
245 **Example**
246 ```
247 mz_zip_set_version_madeby(zip_handle, MZ_VERSION_MADEBY);
248 // MZ_VERSION_MADEBY is the macro use to represent the version made by for the host system
249 uint16_t custom_version_madeby = ((MZ_HOST_SYSTEM_WINDOWS_NTFS << 8) | 45)
250 mz_zip_set_version_madeby(zip_handle, custom_version_madeby);
251 ```
252
253 ### mz_zip_set_recover
254
255 Sets the ability to recover/repair the central directory. When the central directory is damaged or incorrectly written, it may be possible to recover it by reading the local file headers. Reading all of the local file headers can be a disk intensive operation and take significant time for large files.
256
257 **Arguments**
258 |Type|Name|Description|
259 |-|-|-|
260 |void *|handle|_mz_zip_ instance|
261 |uint8_t|recover|Set to 1 to enable central directory recover, set to 0 otherwise.|
262
263 **Return**
264 |Type|Description|
265 |-|-|
266 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
267
268 **Example**
269 ```
270 void *zip_handle = mz_zip_create();
271 // Enable central directory recover/repair
272 if (mz_zip_set_recover(zip_handle, 1) == MZ_OK)
273 printf("Central directory recovery enabled if necessary\n");
274 ```
275
276 ### mz_zip_set_data_descriptor
277
278 Sets wehther or not zip file entries will be written with a data descriptor. When data descriptor writing is enabled it will zero out the crc32, compressed size, and uncompressed size in the local header. By default data descriptor writing is enabled and disabling it will cause zip file entry writing to seek backwards to fill in these values after writing the compressed data.
279
280 **Arguments**
281 |Type|Name|Description|
282 |-|-|-|
283 |void *|handle|_mz_zip_ instance|
284 |uint8_t|data_descriptor|Set to 1 to enable data descriptor writing, set to 0 otherwise.|
285
286 **Return**
287 |Type|Description|
288 |-|-|
289 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
290
291 **Example**
292 ```
293 void *zip_handle = mz_zip_create();
294 // Enable data descriptor writing for zip entries
295 if (mz_zip_set_data_descriptor(zip_handle, 0) == MZ_OK)
296 printf("Local file header entries will be written with crc32 and sizes\n");
297 ```
298
299 ### mz_zip_get_stream
300
301 Gets the _mz_stream_ handle used in the call to _mz_zip_open_.
302
303 **Arguments**
304 |Type|Name|Description|
305 |-|-|-|
306 |void *|handle|_mz_zip_ instance|
307 |void **|stream|Pointer to _mz_stream_ instance|
308
309 **Return**
310 |Type|Description|
311 |-|-|
312 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
313
314 **Example**
315 ```
316 void *zip_handle = mz_zip_create();
317 if (mz_zip_open(zip_handle, stream_handle, MZ_OPEN_MODE_READ) == MZ_OK) {
318 void *stream2_handle = NULL;
319 mz_zip_get_stream(zip_handle, &stream2_handle);
320 assert(stream_handle == stream2_handle);
321 }
322 ```
323
324 ### mz_zip_set_cd_stream
325
326 Sets the stream to use for reading the central directory.
327
328 The central directory stream might not be the same as the stream used for reading local file entries. In the case of split disks, they are different and handles to both are open at the same time to keep from opening and closing streams while reading entries.
329
330 This function is used when encrypting the central directory, it is decrypted and extracted into a memory stream, and that memory stream is set as the central directory stream.
331
332 **Arguments**
333 |Type|Name|Description|
334 |-|-|-|
335 |void *|handle|_mz_zip_ instance|
336 |int64_t|cd_start_pos|Start position of central directory in the stream|
337 |void *|cd_stream|_mz_stream_ instance to use for reading central directory. The cd_stream must be valid as long as it is being used by _mz_zip_.|
338
339 **Return**
340 |Type|Description|
341 |-|-|
342 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
343
344 **Example**
345 ```
346 void *cd_mem_stream = mz_stream_mem_create();
347 // TODO: Write central directory to memory stream
348 mz_zip_set_cd_stream(zip_handle, 0, cd_mem_stream);
349 ```
350
351 ### mz_zip_get_cd_mem_stream
352
353 Gets the stream used to store the central directory in memory for writing.
354
355 When writing to a zip file the central directory is written to this memory stream and written after all entries are written. When a zip file is opened for appending, the central directory is stored in memory while the appending new zip entries.
356
357 **Arguments**
358 |Type|Name|Description|
359 |-|-|-|
360 |void *|handle|_mz_zip_ instance|
361 |void *|cd_stream|Pointer to _mz_stream_ instance that is used for writing the central directory.|
362
363 **Return**
364 |Type|Description|
365 |-|-|
366 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful|
367
368 **Example**
369 ```
370 // TODO: Open zip file for writing and write at least one entry
371 void *cd_mem_stream = NULL;
372 if (mz_zip_get_cd_mem_stream(zip_handle, &cd_mem_stream) == MZ_OK) {
373 int64_t org_position = mz_stream_tell(cd_mem_stream);
374 mz_stream_seek(cd_mem_stream, 0, MZ_SEEK_END);
375 int64_t cd_length = mz_stream_tell(cd_mem_stream);
376 mz_stream_seek(cd_mem_stream, org_position, MZ_SEEK_SET);
377 printf("Length of central directory to write: %d\n", cd_length);
378 }
379 ```
380
381 ### mz_zip_set_number_entry
382
383 Sets the total number of entries in the zip file. Useful when loading central directory manually via _mz_zip_set_cd_stream_.
384
385 **Arguments**
386 |Type|Name|Description|
387 |-|-|-|
388 |void *|handle|_mz_zip_ instance|
389 |uint64_t|number_entry|Total number of entries in central directory|
390
391 **Return**
392 |Type|Description|
393 |-|-|
394 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
395
396 **Example**
397 ```
398 // TODO: Load central directory into memory stream
399 mz_zip_set_cd_stream(zip_handle, 0, cd_mem_stream);
400 mz_zip_set_number_entry(zip_handle, 10);
401 ```
402
403 ### mz_zip_get_number_entry
404
405 Gets the total number of entries in the zip file after it has been opened.
406
407 **Arguments**
408 |Type|Name|Description|
409 |-|-|-|
410 |void *|handle|_mz_zip_ instance|
411 |uint64_t|number_entry|Pointer to store total number of entries in central directory|
412
413 **Return**
414 |Type|Description|
415 |-|-|
416 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
417
418 **Example**
419 ```
420 uint64_t number_entry = 0;
421
422 // TODO: Open zip file
423
424 if (mz_zip_get_number_entry(zip_handle, &number_entry) == MZ_OK)
425 printf("Total number of entries in zip file %d\n", number_entry);
426 ```
427
428 ### mz_zip_set_disk_number_with_cd
429
430 Sets the disk number containing the central directory record. Useful for when loading the central directory manually using _mz_zip_set_cd_stream_.
431
432 **Arguments**
433 |Type|Name|Description|
434 |-|-|-|
435 |void *|handle|_mz_zip_ instance|
436 |uint32_t|disk_number_with_cd|Disk number containing the central directory|
437
438 **Return**
439 |Type|Description|
440 |-|-|
441 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
442
443 **Example**
444 ```
445 // TODO: Load central directory into memory stream
446 mz_zip_set_cd_stream(zip_handle, 0, cd_mem_stream);
447 mz_zip_set_number_entry(zip_handle, 10);
448 mz_zip_set_disk_number_with_cd(zip_handle, 0);
449 ```
450 ### mz_zip_get_disk_number_with_cd
451
452 Gets the disk number containing the central directory.
453
454 **Arguments**
455 |Type|Name|Description|
456 |-|-|-|
457 |void *|handle|_mz_zip_ instance|
458 |uint32_t *|disk_number_with_cd|Pointer to store disk number containing the central directory|
459
460 **Return**
461 |Type|Description|
462 |-|-|
463 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
464
465 **Example**
466 ```
467 uint32_t disk_number_with_cd = 0;
468 // TODO: Open zip file
469 if (mz_zip_get_disk_number_with_cd(zip_handle, &disk_number_with_cd) == MZ_OK)
470 printf("Disk number containing cd: %d\n", disk_number_with_cd);
471 ```
472
473 ## Entry I/O
474
475 ### mz_zip_entry_is_open
476
477 Gets whether or not a zip entry is open for reading or writing.
478
479 **Arguments**
480 |Type|Name|Description|
481 |-|-|-|
482 |void *|handle|_mz_zip_ instance|
483
484 **Return**
485 |Type|Description|
486 |-|-|
487 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if open|
488
489 **Example**
490 ```
491 // TODO: Open zip file
492 if (mz_zip_entry_is_open(zip_handle) == MZ_OK)
493 printf("Zip entry is open for reading or writing\n");
494 ```
495
496 ### mz_zip_entry_read_open
497
498 Opens for reading the current entry in the zip file. To navigate to an entry use _mz_zip_goto_first_entry_, _mz_zip_goto_next_entry_, or _mz_zip_locate_entry_.
499
500 Normally, when reading from a zip entry, the data will be automatically decrypted and decompressed. To read the raw zip entry data, set the raw parameter to 1. This is useful if you want access to the raw gzip data (assuming the entry is gzip compressed).
501
502 **Arguments**
503 |Type|Name|Description|
504 |-|-|-|
505 |void *|handle|_mz_zip_ instance|
506 |uint8_t|raw|Open for raw reading if 1.|
507 |const char *|password|Null-terminated password character array, or NULL if no password needed for reading.|
508
509 **Return**
510 |Type|Description|
511 |-|-|
512 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if entry opened for reading.|
513
514 **Example**
515 ```
516 err = mz_zip_goto_first_entry(zip_handle);
517 if (err == MZ_OK)
518 err = mz_zip_entry_read_open(zip_handle, 0, NULL);
519 if (err == MZ_OK)
520 printf("Zip entry open for reading\n");
521 ```
522
523 ### mz_zip_entry_read
524
525 Reads bytes from the current entry in the zip file. The data returned in the buffer will be the decrypted and decompressed bytes unless the entry is opened for raw data reading.
526
527 **Arguments**
528 |Type|Name|Description|
529 |-|-|-|
530 |void *|handle|_mz_zip_ instance|
531 |void *|buf|Read buffer array|
532 |int32_t|len|Maximum bytes to read.|
533
534 **Return**
535 |Type|Description|
536 |-|-|
537 |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.|
538
539 **Example**
540 ```
541 int32_t bytes_read;
542 int32_t err = MZ_OK;
543 char buf[4096];
544 do {
545 bytes_read = mz_zip_entry_read(zip_handle, buf, sizeof(buf));
546 if (bytes_read < 0) {
547 err = bytes_read;
548 }
549 // TODO: Do something with buf bytes
550 } while (err == MZ_OK && bytes_read > 0);
551 ```
552
553 ### mz_zip_entry_read_close
554
555 Closes the current entry in the zip file for reading and returns the data descriptor values if the zip entry has the data descriptor flag set. If the data descriptor values are not necessary, _mz_zip_entry_close_ can be used instead.
556
557 **Arguments**
558 |Type|Name|Description|
559 |-|-|-|
560 |void *|handle|_mz_zip_ instance|
561 |uint32_t *|crc32|Pointer to store crc32 value from data descriptor. Use NULL if not needed.|
562 |int64_t *|compressed_size|Pointer to store compressed size from data descriptor. Use NULL if not needed.|
563 |int64_t *|uncompressed_size|Pointer to store uncompressed size from data descriptor. Use NULL if not needed.|
564
565 **Return**
566 |Type|Description|
567 |-|-|
568 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
569
570 **Example**
571 ```
572 uint32_t crc32 = 0;
573 int64_t compressed_size = 0;
574 int64_t uncompressed_size = 0;
575 int32_t err = mz_zip_entry_read_close(zip_handle, &crc32, &compressed_size, &uncompressed_size);
576 if (err == MZ_OK) {
577 printf("Zip entry crc32: %08x\n", crc32);
578 printf("Zip entry compressed size: %lld\n", compressed_size);
579 printf("Zip entry uncompressed size: %lld\n", uncompressed_size);
580 } else {
581 printf("Unknown error closing zip entry for reading (%d)\n", err);
582 }
583 ```
584
585 ### mz_zip_entry_write_open
586
587 Opens for a new entry in the zip file for writing.
588
589 **Arguments**
590 |Type|Name|Description|
591 |-|-|-|
592 |void *|handle|_mz_zip_ instance|
593 |const mz_zip_file *|file_info|Pointer to [_mz_zip_file_](mz_zip_file.md) structure containing information about entry being added.|
594 |int16_t|compress_level|Compression level 0-9. Higher is better compression. (See [MZ_COMPRESS_LEVEL](mz_compress_level.md))|
595 |uint8_t|raw|Write raw data if 1, otherwise the data will be compressed and encrypted based on supplied parameters.|
596 |const char *|password|Null-terminated password character array. Use NULL if encryption not required.|
597
598 **Return**
599 |Type|Description|
600 |-|-|
601 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if entry opened for writing.|
602
603 **Example**
604
605 See _mz_zip_writer_add_file_ for a full example on how to populate the [mz_zip_file](mz_zip_file.md) structure.
606 ```
607 mz_zip_file file_info;
608
609 memset(&file_info, 0, sizeof(file_info));
610
611 file_info.version_madeby = MZ_VERSION_MADEBY;
612 file_info.compression_method = MZ_COMPRESS_METHOD_DEFLATE;
613 file_info.filename = "myfile.txt";
614 file_info.flag = MZ_ZIP_FLAG_UTF8;
615 ...
616
617 err = mz_zip_entry_write_open(zip_handle, &file_info, 0, NULL);
618 if (err == MZ_OK)
619 printf("Zip entry open for writing\n");
620 ```
621
622 ### mz_zip_entry_write
623
624 Write data to the current entry in the zip file that was opened for writing.
625
626 **Arguments**
627 |Type|Name|Description|
628 |-|-|-|
629 |void *|handle|_mz_zip_ instance|
630 |void *|buf|Write buffer array|
631 |int32_t|len|Maximum bytes to write.|
632
633 **Return**
634 |Type|Description|
635 |-|-|
636 |int32_t|If < 0 then [MZ_ERROR](mz_error.md) code, otherwise number of bytes written.|
637
638 **Example**
639 ```
640 char buf[4096];
641 int32_t bytes_to_write = sizeof(buf);
642 int32_t bytes_written = 0;
643 int32_t total_bytes_written = 0;
644 int32_t err = MZ_OK;
645 // Fill buf with x'es
646 memset(buf, 'x', sizeof(buf));
647 do {
648 bytes_written = mz_zip_entry_write(zip_handle, buf + total_bytes_written,
649 bytes_to_write);
650 if (bytes_written < 0) {
651 err = bytes_written;
652 } else {
653 total_bytes_written += bytes_written;
654 bytes_to_write -= bytes_written;
655 }
656 } while (err == MZ_OK && bytes_to_write > 0);
657 ```
658
659 ### mz_zip_entry_write_close
660
661 Closes the current entry in the zip file for writing and allows setting the data descriptor values if the zip entry has the data descriptor flag set. If the data descriptor values are not necessary, _mz_zip_entry_close_ can be used instead.
662
663 **Arguments**
664 |Type|Name|Description|
665 |-|-|-|
666 |void *|handle|_mz_zip_ instance|
667 |uint32_t|crc32|Crc32 value to store in the data descriptor.|
668 |int64_t|compressed_size|Compressed size to store in the data descriptor.|
669 |int64_t|uncompressed_size|Uncompressed size to store in the data descriptor.|
670
671 **Return**
672 |Type|Description|
673 |-|-|
674 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
675
676 **Example**
677 ```
678 uint32_t crc32 = 0;
679 int64_t compressed_size = 0;
680 int64_t uncompressed size = 0;
681 // TODO: Calculate crc32, compressed size, and uncompressed size
682 int32_t err = mz_zip_entry_write_close(zip_handle, crc32, compressed_size, uncompressed_size);
683 if (err == MZ_OK)
684 printf("Zip file entry closed for writing\n");
685 ```
686
687 ### mz_zip_entry_seek_local_header
688 Seeks to the local header for the entry.
689
690 **Arguments**
691 |Type|Name|Description|
692 |-|-|-|
693 |void *|handle|_mz_zip_ instance|
694
695 **Return**
696 |Type|Description|
697 |-|-|
698 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
699
700 ### mz_zip_entry_get_compress_stream
701 Gets a pointer to the compression stream for the current entry. Can be used to retrieve the compressed bytes read during an operation.
702
703 **Arguments**
704 |Type|Name|Description|
705 |-|-|-|
706 |void *|handle|_mz_zip_ instance|
707 |void **|compress_stream|Pointer to _mz_stream_ instance|
708
709 **Return**
710 |Type|Description|
711 |-|-|
712 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
713
714 **Example**
715 ```
716 int32_t err = mz_zip_goto_first_entry(zip_handle);
717 if (err == MZ_OK)
718 err = mz_zip_entry_seek_local_header(zip_handle);
719 if (err == MZ_OK) {
720 void *stream = NULL;
721 mz_zip_get_stream(zip_handle, &stream);
722 int64_t position = mz_stream_tell(stream);
723 printf("Position of local header of first entry: %lld\n", position);
724 }
725 ```
726
727 ### mz_zip_entry_close_raw
728
729 Closes the current entry in the zip file. To be used to close an entry that has been opened for reading or writing in raw mode.
730
731 **Arguments**
732 |Type|Name|Description|
733 |-|-|-|
734 |void *|handle|_mz_zip_ instance|
735 |int64_t|uncompressed_size|Uncompressed size to store in the data descriptor. Only used when entry was opened for writing.|
736 |uint32_t|crc32|Crc32 value to store in the data descriptor. Only used when entry was opened for writing.|
737
738 Compressed size is already known through calls to _mz_zip_entry_write_.
739
740 **Return**
741 |Type|Description|
742 |-|-|
743 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
744
745 **Example**
746 ```
747 #include <zlib.h>
748 int32_t err = mz_zip_entry_read_write(zip_handle, &file_info, 1, NULL);
749 if (err == MZ_OK) {
750 z_stream zs;
751 int32_t my_string_crc32;
752 int32_t my_string_size;
753 char buf[4096];
754 char *my_string = "mystring";
755
756 my_string_size = strlen(my_string);
757 my_string_crc32 = crc32(0, my_string, my_string_size);
758
759 memset(&zs, 0, sizeof(zs));
760
761 zs.zalloc = Z_NULL;
762 zs.zfree = Z_NULL;
763 zs.opaque = Z_NULL;
764 zs.avail_in = my_string_size;
765 zs.next_in = my_string;
766 zs.avail_out = sizeof(buf);
767 zs.next_out = buf;
768
769 deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | 16, 8, Z_DEFAULT_STRATEGY);
770 err = deflate(&zs, Z_FINISH);
771 deflateEnd(&zs);
772
773 mz_zip_entry_write(zip_handle, buf, zs.total_out);
774 mz_zip_entry_close_raw(zip_handle, my_string_size, my_string_crc32);
775 }
776 ```
777
778 ### mz_zip_entry_close
779
780 Closes the current entry in the zip file.
781
782 **Arguments**
783 |Type|Name|Description|
784 |-|-|-|
785 |void *|handle|_mz_zip_ instance|
786
787 **Return**
788 |Type|Description|
789 |-|-|
790 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
791
792 **Example**
793 ```
794 int32_t err = mz_zip_entry_close(zip_handle);
795 if (err == MZ_OK)
796 printf("Zip entry closed\n");
797 ```
798
799 ## Entry Enumeration
800
801 ### mz_zip_entry_is_dir
802
803 When enumerating zip entries, returns whether or not the current entry is a directory. This function accounts for the various directory attribute values on each OS.
804
805 **Arguments**
806 |Type|Name|Description|
807 |-|-|-|
808 |void *|handle|_mz_zip_ instance|
809
810 **Return**
811 |Type|Description|
812 |-|-|
813 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if entry is a directory.|
814
815 **Example**
816 ```
817 int32_t err = mz_zip_goto_first_entry(zip_handle);
818 if (err == MZ_OK) {
819 if (mz_zip_entry_is_dir(zip_handle) == MZ_OK) {
820 printf("First entry in zip file is a directory\n");
821 }
822 }
823 ```
824
825 ### mz_zip_entry_is_symlink
826
827 When enumerating zip entries, returns whether or not the current entry is a symbolic link. This function accounts for the various symbolic link attribute values on each OS.
828
829 **Arguments**
830 |Type|Name|Description|
831 |-|-|-|
832 |void *|handle|_mz_zip_ instance|
833
834 **Return**
835 |Type|Description|
836 |-|-|
837 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if entry is a symbolic link.|
838
839 **Example**
840 ```
841 int32_t err = mz_zip_goto_first_entry(zip_handle);
842 if (err == MZ_OK) {
843 if (mz_zip_entry_is_symlink(zip_handle) == MZ_OK) {
844 printf("First entry in zip file is a symbolic link\n");
845 }
846 }
847 ```
848
849 ### mz_zip_entry_get_info
850
851 Gets central directory file information about the current entry in the zip file.
852
853 **Arguments**
854 |Type|Name|Description|
855 |-|-|-|
856 |void *|handle|_mz_zip_ instance|
857 |mz_zip_file **|file_info|Pointer to _mz_zip_file_ structure. Pointer is only valid for the while the entry is the current entry.|
858
859 **Return**
860 |Type|Description|
861 |-|-|
862 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
863
864 **Example**
865 ```
866 int32_t err = mz_zip_goto_first_entry(zip_handle);
867 if (err == MZ_OK) {
868 mz_zip_file *file_info = NULL;
869 err = mz_zip_entry_get_info(zip_handle, &file_info);
870 if (err == MZ_OK) {
871 printf("Central directory entry filename: %s\n", file_info->filename);
872 }
873 }
874 ```
875
876 ### mz_zip_entry_get_local_info
877
878 Gets local file header file information about the current entry in the zip file.
879
880 **Arguments**
881 |Type|Name|Description|
882 |-|-|-|
883 |void *|handle|_mz_zip_ instance|
884 |mz_zip_file **|file_info|Pointer to _mz_zip_file_ structure. Pointer is only valid for the while the entry is the current entry.|
885
886 **Return**
887 |Type|Description|
888 |-|-|
889 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
890
891 **Example**
892 ```
893 int32_t err = mz_zip_goto_first_entry(zip_handle);
894 if (err == MZ_OK) {
895 mz_zip_file *local_file_info = NULL;
896 err = mz_zip_entry_get_info(zip_handle, &local_file_info);
897 if (err == MZ_OK) {
898 printf("Local header entry filename: %s\n", local_file_info->filename);
899 }
900 }
901 ```
902
903 ### mz_zip_get_entry
904
905 Returns the offset of the current entry in the zip file.
906
907 **Arguments**
908 |Type|Name|Description|
909 |-|-|-|
910 |void *|handle|_mz_zip_ instance|
911
912 **Return**
913 |Type|Description|
914 |-|-|
915 |int64_t|Byte position in zip file, if < 0, then [MZ_ERROR](mz_error.md).|
916
917 **Example**
918 ```
919 int32_t err = mz_zip_goto_first_entry(zip_handle);
920 if (err == MZ_OK) {
921 int64_t entry_offset = mz_zip_get_entry(zip_handle);
922 if (entry_offset >= 0) {
923 printf("Entry offset %lld\n", entry_offset);
924 }
925 }
926 ```
927
928 ### mz_zip_goto_entry
929
930 Manually set the central directory stream position for and read entry.
931
932 **Arguments**
933 |Type|Name|Description|
934 |-|-|-|
935 |void *|handle|_mz_zip_ instance|
936 |int64_t|cd_pos|Position in the central directory stream|
937
938 **Return**
939 |Type|Description|
940 |-|-|
941 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
942
943 **Example**
944 ```
945 // Read the entry at position 0 in central directory stream
946 mz_zip_goto_entry(zip_handle, 0);
947 ```
948
949 ### mz_zip_goto_first_entry
950
951 Go to the first entry in the zip file.
952
953 **Arguments**
954 |Type|Name|Description|
955 |-|-|-|
956 |void *|handle|_mz_zip_ instance|
957
958 **Return**
959 |Type|Description|
960 |-|-|
961 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful, MZ_END_OF_LIST if no more entries.|
962
963 **Example**
964 ```
965 int32_t err = mz_zip_goto_first_entry(zip_handle);
966 if (err == MZ_OK) {
967 mz_zip_file *file_info = NULL;
968 err = mz_zip_entry_get_info(zip_handle, &file_info);
969 if (err == MZ_OK) {
970 printf("First entry is %s\n", file_info->filename);
971 }
972 }
973 ```
974
975 ### mz_zip_goto_next_entry
976
977 Go to the next entry in the zip file.
978
979 **Arguments**
980 |Type|Name|Description|
981 |-|-|-|
982 |void *|handle|_mz_zip_ instance|
983
984 **Return**
985 |Type|Description|
986 |-|-|
987 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful, MZ_END_OF_LIST if no more entries.|
988
989 **Example**
990 ```
991 int32_t i = 0;
992 int32_t err = mz_zip_goto_first_entry(zip_handle);
993 while (err == MZ_OK) {
994 mz_zip_file *file_info = NULL;
995 err = mz_zip_entry_get_info(zip_handle, &file_info);
996 if (err != MZ_OK) {
997 printf("Failed to get entry %d info\n", i);
998 break;
999 }
1000 printf("Entry %d is %s\n", i, file_info->filename);
1001 err = mz_zip_goto_next_entry(zip_handle);
1002 }
1003 ```
1004
1005 ### mz_zip_locate_entry
1006
1007 Locate the entry with the specified name in the zip file.
1008
1009 **Arguments**
1010 |Type|Name|Description|
1011 |-|-|-|
1012 |void *|handle|_mz_zip_ instance|
1013 |const char *|filename|Filename character array|
1014 |uint8_t|ignore_case|Ignore case during lookup if 1.|
1015
1016 **Return**
1017 |Type|Description|
1018 |-|-|
1019 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful, MZ_END_OF_LIST if not found.|
1020
1021 **Example**
1022 ```
1023 const char *search_path = "test.txt";
1024 int32_t err = mz_zip_locate_entry(zip_handle, search_path, 0);
1025 if (err == MZ_OK) {
1026 printf("%s was found\n", search_path);
1027 else
1028 printf("%s was not found\n", search_path);
1029 ```
1030
1031 ### mz_zip_locate_first_entry
1032
1033 Locate the first matching entry based on a match callback.
1034
1035 **Arguments**
1036 |Type|Name|Description|
1037 |-|-|-|
1038 |void *|handle|_mz_zip_ instance|
1039 |void *|userdata|User pointer|
1040 |mz_zip_locate_entry_cb|cb|Callback to locate filter function|
1041
1042 **Return**
1043 |Type|Description|
1044 |-|-|
1045 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful, MZ_END_OF_LIST if not found.|
1046
1047 **Example**
1048 ```
1049 static int32_t locate_a_entries_cb(void *handle, void *userdata, mz_zip_file *file_info) {
1050 if (file_info->filename[0] == 'a') {
1051 return MZ_OK;
1052 }
1053 return MZ_EXIST_ERROR;
1054 }
1055 int32_t err = mz_zip_locate_first_entry(zip_handle, NULL, locate_a_entries_cb);
1056 if (err == MZ_OK) {
1057 mz_zip_file *file_info = NULL;
1058 err = mz_zip_entry_get_info(zip_handle, &file_info);
1059 if (err == MZ_OK) {
1060 printf("First entry beginning with a is %s\n", file_info->filename);
1061 }
1062 } else {
1063 printf("No entries beginning with a found\n");
1064 }
1065 ```
1066
1067 ### mz_zip_locate_next_entry
1068
1069 Locate the next matching entry based on a match callback.
1070
1071 **Arguments**
1072 |Type|Name|Description|
1073 |-|-|-|
1074 |void *|handle|_mz_zip_ instance|
1075 |void *|userdata|User pointer|
1076 |mz_zip_locate_entry_cb|cb|Callback to locate filter function|
1077
1078 **Return**
1079 |Type|Description|
1080 |-|-|
1081 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful, MZ_END_OF_LIST if not found.|
1082
1083 **Example**
1084 ```
1085 static int32_t locate_a_entries_cb(void *handle, void *userdata, mz_zip_file *file_info) {
1086 if (file_info->filename[0] == 'a') {
1087 return MZ_OK;
1088 }
1089 return MZ_EXIST_ERROR;
1090 }
1091 int32_t err = mz_zip_locate_first_entry(zip_handle, NULL, locate_a_entries_cb);
1092 if (err == MZ_END_OF_LIST) {
1093 printf("No entries beginning with a found\n");
1094 return;
1095 }
1096 while (err == MZ_OK) {
1097 mz_zip_file *file_info = NULL;
1098 err = mz_zip_entry_get_info(zip_handle, &file_info);
1099 if (err != MZ_OK) {
1100 printf("Error getting entry info\n");
1101 break;
1102 }
1103 printf("Entry beginning with a is %s\n", file_info->filename);
1104 err = mz_zip_locate_next_entry(zip_handle, 0, locate_a_entries_cb);
1105 }
1106 ```
1107
1108 ## System Attributes
1109
1110 ### mz_zip_attrib_is_dir
1111
1112 Checks to see if the attribute is a directory based on platform.
1113
1114 **Arguments**
1115 |Type|Name|Description|
1116 |-|-|-|
1117 |uint32_t|attrib|External file attributes|
1118 |int32_t|version_madeby|Version made by value|
1119
1120 **Return**
1121 |Type|Description|
1122 |-|-|
1123 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if is a directory.|
1124
1125 **Example**
1126 ```
1127 mz_zip_file *file_info = NULL;
1128 if (mz_zip_entry_get_info(zip_handle, &file_info) == MZ_OK) {
1129 if (mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK) {
1130 printf("Entry is a directory\n");
1131 } else {
1132 printf("Entry is not a directory\n");
1133 }
1134 }
1135 ```
1136
1137 ### mz_zip_attrib_is_symlink
1138
1139 Checks to see if the attribute is a symbolic link based on platform.
1140
1141 **Arguments**
1142 |Type|Name|Description|
1143 |-|-|-|
1144 |uint32_t|attrib|External file attributes|
1145 |int32_t|version_madeby|Version made by value|
1146
1147 **Return**
1148 |Type|Description|
1149 |-|-|
1150 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if is a directory.|
1151
1152 **Example**
1153 ```
1154 mz_zip_file *file_info = NULL;
1155 if (mz_zip_entry_get_info(zip_handle, &file_info) == MZ_OK) {
1156 if (mz_zip_attrib_is_symlink(file_info->external_fa, file_info->version_madeby) == MZ_OK) {
1157 printf("Entry is a symbolic link\n");
1158 } else {
1159 printf("Entry is not a symbolic link\n");
1160 }
1161 }
1162 ```
1163
1164 ### mz_zip_attrib_convert
1165
1166 Converts file attributes from one host system to another.
1167
1168 **Arguments**
1169 |Type|Name|Description|
1170 |-|-|-|
1171 |uint8_t|src_sys|Source system (See [MZ_HOST_SYSTEM](mz_host_system.md))|
1172 |uint32_t|src_attrib|Source system attribute value|
1173 |uint8_t|target_sys|Target system (See [MZ_HOST_SYSTEM](mz_host_system.md))|
1174 |uint32_t *|target_attrib|Pointer to store target system attribute value|
1175
1176 **Return**
1177 |Type|Description|
1178 |-|-|
1179 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if converted successfully.|
1180
1181 **Example**
1182 ```
1183 uint32_t target_attrib = 0;
1184 // Windows FILE_ATTRIBUTE_DIRECTORY (0x10)
1185 if (mz_zip_attrib_convert(MZ_HOST_SYSTEM_WINDOWS_NTFS, 0x10, MZ_HOST_SYSTEM_UNIX, &target_attrib) == MZ_OK) {
1186 printf("Unix file attributes: %08x\n", target_attrib);
1187 if (S_ISDIR(target_attribute)) {
1188 printf("Unix attribute is a directory\n");
1189 }
1190 }
1191 ```
1192
1193 ### mz_zip_attrib_posix_to_win32
1194
1195 Converts posix file attributes to win32 file attributes.
1196
1197 **Arguments**
1198 |Type|Name|Description|
1199 |-|-|-|
1200 |uint32_t|posix_attrib|Posix attributes value|
1201 |uint32_t *|win32_attrib|Pointer to store windows attributes value|
1202
1203 **Return**
1204 |Type|Description|
1205 |-|-|
1206 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if converted successfully.|
1207
1208 **Example**
1209 ```
1210 uint32_t posix_attrib = 0x00008124;
1211 uint32_t win32_attrib = 0;
1212 if (mz_zip_attrib_posix_to_win32(posix_attrib, &win32_attrib) == MZ_OK) {
1213 printf("Win32 file system attributes: %08x\n", win32_attrib);
1214 // Windows FILE_ATTRIBUTE_READONLY (0x01)
1215 if ((win32_attrib & 0x01) != 0) {
1216 printf("Win32 attribute is readonly\n");
1217 }
1218 }
1219 ```
1220
1221 ### mz_zip_attrib_win32_to_posix
1222
1223 Converts win32 file attributes to posix file attributes.
1224
1225 **Arguments**
1226 |Type|Name|Description|
1227 |-|-|-|
1228 |uint32_t|win32_attrib|Windows attributes value|
1229 |uint32_t *|posix_attrib|Pointer to store posix attributes value|
1230
1231 **Return**
1232 |Type|Description|
1233 |-|-|
1234 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if converted successfully.|
1235
1236 **Example**
1237 ```
1238 uint32_t posix_attrib = 0; // Read only
1239 uint32_t win32_attrib = 0x01; // FILE_ATTRIBUTE_READONLY;
1240 if (mz_zip_attrib_win32_to_posix(win32_attrib), &posix_attrib) == MZ_OK) {
1241 printf("Posix file system attributes: %08x\n", posix_attrib);
1242 if ((posix_attrib & 0000222) == 0) {
1243 printf("Posix attribute is readonly\n");
1244 }
1245 }
1246 ```
1247
1248 ## Extrafield
1249
1250 ### mz_zip_extrafield_find
1251
1252 Seeks using a _mz_stream_ to an extra field by its type and returns its length.
1253
1254 **Arguments**
1255 |Type|Name|Description|
1256 |-|-|-|
1257 |void *|stream|_mz_stream_ instance|
1258 |uint16_t|type|Extra field type indentifier (See [PKWARE zip app note](zip/appnote.iz.txt) section 4.5.2)|
1259 |int32_t|max_seek|Maximum length to search for extrafield|
1260 |uint16_t *|length|Pointer to extra field length|
1261
1262 **Return**
1263 |Type|Description|
1264 |-|-|
1265 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if found, MZ_EXIST_ERROR if not found.|
1266
1267 **Example**
1268 ```
1269 mz_zip_file *file_info = NULL;
1270 uint16_t extrafield_length = 0;
1271
1272 mz_zip_entry_get_info(zip_handle, &file_info);
1273
1274 void *file_extra_stream = mz_stream_mem_create();
1275 mz_stream_mem_set_buffer(file_extra_stream, (void *)file_info->extrafield,
1276 file_info->extrafield_size);
1277
1278 if (mz_zip_extrafield_find(file_extra_stream, MZ_ZIP_EXTENSION_AES, INT32_MAX, &extrafield_length) == MZ_OK)
1279 printf("Found AES extra field, length %d\n", extrafield_length);
1280 else
1281 printf("Unable to find AES extra field in zip entry\n");
1282
1283 mz_stream_mem_delete(&file_extra_stream);
1284 ```
1285
1286 ### mz_zip_extrafield_contains
1287
1288 Searchs a buffer to determine whether an extrafield exists and returns its length if found.
1289
1290 **Arguments**
1291 |Type|Name|Description|
1292 |-|-|-|
1293 |const uint8_t *|extrafield|Extra field buffer array|
1294 |int32_t|extrafield_size|Maximim buffer bytes|
1295 |uint16_t|type|Extrafield type identifier (See [PKWARE zip app note](zip/appnote.iz.txt) section 4.5.2)|
1296 |uint16_t *|length|Pointer to store extrafield length|
1297
1298 **Return**
1299 |Type|Description|
1300 |-|-|
1301 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if found, MZ_EXIST_ERROR if not found.|
1302
1303 **Example**
1304 ```
1305 mz_zip_file *file_info = NULL;
1306 uint16_t extrafield_length = 0;
1307
1308 mz_zip_entry_get_info(zip_handle, &file_info);
1309 if (mz_zip_extrafield_contains(file_info->extrafield, file_info->extrafield_size, MZ_ZIP_EXTENSION_AES, &extrafield_length) == MZ_OK)
1310 printf("Found AES extra field, length %d\n", extrafield_length);
1311 else
1312 printf("Unable to find AES extra field in zip entry\n");
1313
1314 ```
1315
1316 ### mz_zip_extrafield_read
1317
1318 Reads an extrafield header from a stream.
1319
1320 **Arguments**
1321 |Type|Name|Description|
1322 |-|-|-|
1323 |void *|stream|_mz_stream_ instance|
1324 |uint16_t *|type|Pointer to store extrafield type identifier (See [PKWARE zip app note](zip/appnote.iz.txt) section 4.5.2)|
1325 |uint16_t *|length|Pointer to store extrafield length|
1326
1327 **Return**
1328 |Type|Description|
1329 |-|-|
1330 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if extrafield read.|
1331
1332 **Example**
1333 ```
1334 mz_zip_file *file_info = NULL;
1335 uint16_t extrafield_type = 0;
1336 uint16_t extrafield_length = 0;
1337
1338 void *file_extra_stream = mz_stream_mem_create();
1339 mz_stream_mem_set_buffer(file_extra_stream, (void *)file_info->extrafield,
1340 file_info->extrafield_size);
1341
1342 while (mz_zip_extrafield_read(field_extra_stream, &extrafield_type, &extrafield_length) == MZ_OK) {
1343 printf("Extra field type: %04x\n", extrafield_type);
1344 printf("Extra field length: %d\n", extrafield_length);
1345 }
1346
1347 mz_stream_mem_delete(&file_extra_stream);
1348 ```
1349
1350 ### mz_zip_extrafield_write
1351
1352 Writes an extrafield header to a stream.
1353
1354 **Arguments**
1355 |Type|Name|Description|
1356 |-|-|-|
1357 |void *|stream|_mz_stream_ instance|
1358 |uint16_t|type|Extrafield type identifier (See [PKWARE zip app note](zip/appnote.iz.txt) section 4.5.2)|
1359 |uint16_t|length|Extrafield length|
1360
1361 **Return**
1362 |Type|Description|
1363 |-|-|
1364 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
1365
1366 **Example**
1367 ```
1368 #define MY_CUSTOM_FIELD_TYPE (0x8080)
1369 #define MY_CUSTOM_FIELD_LENGTH (sizeof(uint32_t))
1370 #define MY_CUSTOM_FIELD_VALUE (1)
1371
1372 if (mz_zip_extrafield_write(field_extra_stream, MY_CUSTOM_FIELD_TYPE, MY_CUSTOM_FIELD_LENGTH) == MZ_OK)
1373 mz_stream_write_uint32(field_extra_stream, MY_CUSTOM_FIELD_VALUE);
1374 ```
1375
1376 ## Time/Date
1377
1378 ### mz_zip_dosdate_to_tm
1379
1380 Convert dos date/time format to struct tm.
1381
1382 **Arguments**
1383 |Type|Name|Description|
1384 |-|-|-|
1385 |uint64_t|dos_date|Dos date/time timestamp|
1386 |struct tm *|ptm|Pointer to tm structure|
1387
1388 **Return**
1389 |Type|Description|
1390 |-|-|
1391 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
1392
1393 **Example**
1394 ```
1395 struct tm tmu_date = { 0 };
1396 uint32_t dos_date = 0x50454839;
1397 mz_zip_dosdate_to_tm(dos_date, &tmu_date);
1398 printf("Time: %02d/%02d/%d %02d:%02d:%02d\n",
1399 (uint32_t)tmu_date.tm_mon + 1, (uint32_t)tmu_date.tm_mday,
1400 (uint32_t)tmu_date.tm_year % 100,
1401 (uint32_t)tmu_date.tm_hour, (uint32_t)tmu_date.tm_min,
1402 (uint32_t)tmu_date.tm_sec);
1403 ```
1404
1405 ### mz_zip_dosdate_to_time_t
1406
1407 Convert dos date/time format to unix timestamp.
1408
1409 **Arguments**
1410 |Type|Name|Description|
1411 |-|-|-|
1412 |uint64_t|dos_date|Dos date/time timestamp|
1413
1414 **Return**
1415 |Type|Description|
1416 |-|-|
1417 |time_t|Unix timestamp, 0 if not converted.|
1418
1419 **Example**
1420 ```
1421 uint32_t dos_date = 0x50454839;
1422 time_t unix_time = mz_zip_dosdate_to_time_t(unix_time);
1423 // Unix date/time = 1580922110
1424 printf("Unix date/time: %lld\n", unix_time);
1425 ```
1426
1427 ### mz_zip_time_t_to_tm
1428
1429 Convert unix timestamp to time struct.
1430
1431 **Arguments**
1432 |Type|Name|Description|
1433 |-|-|-|
1434 |time_t|unix_time|Unix timestamp|
1435 |struct tm *|ptm|Pointer to tm structure|
1436
1437 **Return**
1438 |Type|Description|
1439 |-|-|
1440 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
1441
1442 **Example**
1443 ```
1444 struct tm tmu_date = { 0 };
1445 time_t unix_time = 1580922110;
1446 mz_zip_time_t_to_tm(unix_time, &tmu_date);
1447 printf("Time: %02d/%02d/%d %02d:%02d:%02d\n",
1448 (uint32_t)tmu_date.tm_mon + 1, (uint32_t)tmu_date.tm_mday,
1449 (uint32_t)tmu_date.tm_year % 100,
1450 (uint32_t)tmu_date.tm_hour, (uint32_t)tmu_date.tm_min,
1451 (uint32_t)tmu_date.tm_sec);
1452 ```
1453
1454 ### mz_zip_time_t_to_dos_date
1455
1456 Convert unix timestamp to dos date/time format.
1457
1458 **Arguments**
1459 |Type|Name|Description|
1460 |-|-|-|
1461 |time_t|unix_time|Unix timestamp|
1462
1463 **Return**
1464 |Type|Description|
1465 |-|-|
1466 |uint32_t|Dos/date timestamp, 0 if not converted.|
1467
1468 **Example**
1469 ```
1470 time_t unix_time = 1580922110;
1471 uint32_t dos_date = mz_zip_time_t_to_dos_date(unix_time);
1472 // Dos date/time = 0x50454839
1473 printf("Dos date/time: %08x\n", dos_date);
1474 ```
1475
1476 ### mz_zip_tm_to_dosdate
1477
1478 Convert struct tm to dos date/time format.
1479
1480 **Arguments**
1481 |Type|Name|Description|
1482 |-|-|-|
1483 |const struct tm *|ptm|Pointer to tm structure|
1484
1485 **Return**
1486 |Type|Description|
1487 |-|-|
1488 |uint32_t|Dos/date timestamp, 0 if not converted.|
1489
1490 **Example**
1491 ```
1492 struct tm tmu_date = { 50, 1, 9, 5, 1, 120, 3, 35, 0 };
1493 uint32_t dos_date = mz_zip_tm_to_dosdate(&tmu_date);
1494 printf("Dos date/time: %08x\n", dos_date);
1495 ```
1496
1497 ### mz_zip_ntfs_to_unix_time
1498
1499 Convert ntfs time to unix time.
1500
1501 **Arguments**
1502 |Type|Name|Description|
1503 |-|-|-|
1504 |uint64_t|ntfs_time|Windows NTFS timestamp|
1505 |time_t *|unix_time|Pointer to store Unix timestamp|
1506
1507 **Return**
1508 |Type|Description|
1509 |-|-|
1510 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
1511
1512 **Example**
1513 ```
1514 time_t unix_time = 0;
1515 uint64_t ntfs_time = 132253957100000000LL;
1516 if (mz_zip_ntfs_to_unix_time(ntfs_time, &unix_time) == MZ_OK) {
1517 // Unix time = 1580922110
1518 printf("NTFS -> Unix: %lld -> %lld\n", ntfs_time, unix_time);
1519 }
1520 ```
1521
1522 ### mz_zip_unix_to_ntfs_time
1523
1524 Convert unix time to ntfs time.
1525
1526 **Arguments**
1527 |Type|Name|Description|
1528 |-|-|-|
1529 |time_t|unix_time|Unix timestamp|
1530 |uint64_t *|ntfs_time|Pointer to store Windows NTFS timestamp|
1531
1532 **Return**
1533 |Type|Description|
1534 |-|-|
1535 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
1536
1537 **Example**
1538 ```
1539 time_t unix_time = 1580922110;
1540 uint64_t ntfs_time = 0;
1541 if (mz_zip_unix_to_ntfs_time(unix_time, &ntfs_time) == MZ_OK) {
1542 // NTFS time = 132253957100000000
1543 printf("Unix -> NTFS: %lld -> %lld\n", unix_time, ntfs_time);
1544 }
1545 ```
1546
1547 ## Path
1548
1549 ### mz_zip_path_compare
1550
1551 Compare two paths without regard to slashes. Some zip files have paths with unix slashes and some zip files have paths containing windows slashes.
1552
1553 **Arguments**
1554 |Type|Name|Description|
1555 |-|-|-|
1556 |const char *|path1|First path to compare|
1557 |const char *|path2|Second path to compare|
1558 |uint8_t|ignore_case|Ignore case if 1.|
1559
1560 **Return**
1561 |Type|Description|
1562 |-|-|
1563 |int32_t|[MZ_ERROR](mz_error.md) code, MZ_OK if successful.|
1564
1565 **Example**
1566 ```
1567 const char *search_path = "folder1/test1.txt";
1568 mz_zip_file *file_info = NULL;
1569 if (mz_zip_entry_get_info(zip_handle, &file_info) == MZ_OK) {
1570 if (mz_zip_path_compare(file_info->filename, search_path, 0) == MZ_OK)
1571 printf("Found %s\n", search_path);
1572 else
1573 printf("Not found %s\n", search_path);
1574 }
1575 ```
1576
1577 ## String
1578
1579 ### mz_zip_get_compression_method_string
1580
1581 Gets a string representing the compression method.
1582
1583 **Arguments**
1584 |Type|Name|Description|
1585 |-|-|-|
1586 |int32_t|compression_method|Compression method index|
1587
1588 **Return**
1589 |Type|Description|
1590 |-|-|
1591 |const char *|String representing compression method or "?" if not found|
1592
1593 **Example**
1594 ```
1595 const char *method = mz_zip_get_compression_method_string(MZ_ZIP_COMPRESS_METHOD_LZMA);
1596 printf("Compression method %s\n", method);
1597 ```