mz_zip.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* mz_zip.h -- Zip manipulation
  2. part of the minizip-ng project
  3. Copyright (C) 2010-2021 Nathan Moinvaziri
  4. https://github.com/zlib-ng/minizip-ng
  5. Copyright (C) 2009-2010 Mathias Svensson
  6. Modifications for Zip64 support
  7. http://result42.com
  8. Copyright (C) 1998-2010 Gilles Vollant
  9. https://www.winimage.com/zLibDll/minizip.html
  10. This program is distributed under the terms of the same license as zlib.
  11. See the accompanying LICENSE file for the full text of the license.
  12. */
  13. #ifndef MZ_ZIP_H
  14. #define MZ_ZIP_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /***************************************************************************/
  19. typedef struct mz_zip_file_s {
  20. uint16_t version_madeby; /* version made by */
  21. uint16_t version_needed; /* version needed to extract */
  22. uint16_t flag; /* general purpose bit flag */
  23. uint16_t compression_method; /* compression method */
  24. time_t modified_date; /* last modified date in unix time */
  25. time_t accessed_date; /* last accessed date in unix time */
  26. time_t creation_date; /* creation date in unix time */
  27. uint32_t crc; /* crc-32 */
  28. int64_t compressed_size; /* compressed size */
  29. int64_t uncompressed_size; /* uncompressed size */
  30. uint16_t filename_size; /* filename length */
  31. uint16_t extrafield_size; /* extra field length */
  32. uint16_t comment_size; /* file comment length */
  33. uint32_t disk_number; /* disk number start */
  34. int64_t disk_offset; /* relative offset of local header */
  35. uint16_t internal_fa; /* internal file attributes */
  36. uint32_t external_fa; /* external file attributes */
  37. const char *filename; /* filename utf8 null-terminated string */
  38. const uint8_t *extrafield; /* extrafield data */
  39. const char *comment; /* comment utf8 null-terminated string */
  40. const char *linkname; /* sym-link filename utf8 null-terminated string */
  41. uint16_t zip64; /* zip64 extension mode */
  42. uint16_t aes_version; /* winzip aes extension if not 0 */
  43. uint8_t aes_encryption_mode; /* winzip aes encryption mode */
  44. uint16_t pk_verify; /* pkware encryption verifier */
  45. } mz_zip_file, mz_zip_entry;
  46. /***************************************************************************/
  47. typedef int32_t (*mz_zip_locate_entry_cb)(void *handle, void *userdata, mz_zip_file *file_info);
  48. /***************************************************************************/
  49. void * mz_zip_create(void **handle);
  50. /* Create zip instance for opening */
  51. void mz_zip_delete(void **handle);
  52. /* Delete zip object */
  53. int32_t mz_zip_open(void *handle, void *stream, int32_t mode);
  54. /* Create a zip file, no delete file in zip functionality */
  55. int32_t mz_zip_close(void *handle);
  56. /* Close the zip file */
  57. int32_t mz_zip_get_comment(void *handle, const char **comment);
  58. /* Get a pointer to the global comment */
  59. int32_t mz_zip_set_comment(void *handle, const char *comment);
  60. /* Sets the global comment used for writing zip file */
  61. int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby);
  62. /* Get the version made by */
  63. int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby);
  64. /* Sets the version made by used for writing zip file */
  65. int32_t mz_zip_set_recover(void *handle, uint8_t recover);
  66. /* Sets the ability to recover the central dir by reading local file headers */
  67. int32_t mz_zip_set_data_descriptor(void *handle, uint8_t data_descriptor);
  68. /* Sets the use of data descriptor flag when writing zip entries */
  69. int32_t mz_zip_get_stream(void *handle, void **stream);
  70. /* Get a pointer to the stream used to open */
  71. int32_t mz_zip_set_cd_stream(void *handle, int64_t cd_start_pos, void *cd_stream);
  72. /* Sets the stream to use for reading the central dir */
  73. int32_t mz_zip_get_cd_mem_stream(void *handle, void **cd_mem_stream);
  74. /* Get a pointer to the stream used to store the central dir in memory */
  75. int32_t mz_zip_set_number_entry(void *handle, uint64_t number_entry);
  76. /* Sets the total number of entries */
  77. int32_t mz_zip_get_number_entry(void *handle, uint64_t *number_entry);
  78. /* Get the total number of entries */
  79. int32_t mz_zip_set_disk_number_with_cd(void *handle, uint32_t disk_number_with_cd);
  80. /* Sets the disk number containing the central directory record */
  81. int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd);
  82. /* Get the disk number containing the central directory record */
  83. /***************************************************************************/
  84. int32_t mz_zip_entry_is_open(void *handle);
  85. /* Check to see if entry is open for read/write */
  86. int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password);
  87. /* Open for reading the current file in the zip file */
  88. int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len);
  89. /* Read bytes from the current file in the zip file */
  90. int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compressed_size,
  91. int64_t *uncompressed_size);
  92. /* Close the current file for reading and get data descriptor values */
  93. int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info,
  94. int16_t compress_level, uint8_t raw, const char *password);
  95. /* Open for writing the current file in the zip file */
  96. int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len);
  97. /* Write bytes from the current file in the zip file */
  98. int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compressed_size,
  99. int64_t uncompressed_size);
  100. /* Close the current file for writing and set data descriptor values */
  101. int32_t mz_zip_entry_seek_local_header(void *handle);
  102. /* Seeks to the local header for the entry */
  103. int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t crc32);
  104. /* Close the current file in the zip file where raw is compressed data */
  105. int32_t mz_zip_entry_close(void *handle);
  106. /* Close the current file in the zip file */
  107. /***************************************************************************/
  108. int32_t mz_zip_entry_is_dir(void *handle);
  109. /* Checks to see if the entry is a directory */
  110. int32_t mz_zip_entry_is_symlink(void *handle);
  111. /* Checks to see if the entry is a symbolic link */
  112. int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info);
  113. /* Get info about the current file, only valid while current entry is open */
  114. int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info);
  115. /* Get local info about the current file, only valid while current entry is being read */
  116. int32_t mz_zip_entry_set_extrafield(void *handle, const uint8_t *extrafield, uint16_t extrafield_size);
  117. /* Sets or updates the extra field for the entry to be used before writing cd */
  118. int64_t mz_zip_get_entry(void *handle);
  119. /* Return offset of the current entry in the zip file */
  120. int32_t mz_zip_goto_entry(void *handle, int64_t cd_pos);
  121. /* Go to specified entry in the zip file */
  122. int32_t mz_zip_goto_first_entry(void *handle);
  123. /* Go to the first entry in the zip file */
  124. int32_t mz_zip_goto_next_entry(void *handle);
  125. /* Go to the next entry in the zip file or MZ_END_OF_LIST if reaching the end */
  126. int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case);
  127. /* Locate the file with the specified name in the zip file or MZ_END_LIST if not found */
  128. int32_t mz_zip_locate_first_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb);
  129. /* Locate the first matching entry based on a match callback */
  130. int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb);
  131. /* Locate the next matching entry based on a match callback */
  132. /***************************************************************************/
  133. int32_t mz_zip_attrib_is_dir(uint32_t attrib, int32_t version_madeby);
  134. /* Checks to see if the attribute is a directory based on platform */
  135. int32_t mz_zip_attrib_is_symlink(uint32_t attrib, int32_t version_madeby);
  136. /* Checks to see if the attribute is a symbolic link based on platform */
  137. int32_t mz_zip_attrib_convert(uint8_t src_sys, uint32_t src_attrib, uint8_t target_sys,
  138. uint32_t *target_attrib);
  139. /* Converts file attributes from one host system to another */
  140. int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attrib);
  141. /* Converts posix file attributes to win32 file attributes */
  142. int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attrib);
  143. /* Converts win32 file attributes to posix file attributes */
  144. /***************************************************************************/
  145. int32_t mz_zip_extrafield_find(void *stream, uint16_t type, int32_t max_seek, uint16_t *length);
  146. /* Seeks to extra field by its type and returns its length */
  147. int32_t mz_zip_extrafield_contains(const uint8_t *extrafield, int32_t extrafield_size,
  148. uint16_t type, uint16_t *length);
  149. /* Gets whether an extrafield exists and its size */
  150. int32_t mz_zip_extrafield_read(void *stream, uint16_t *type, uint16_t *length);
  151. /* Reads an extrafield header from a stream */
  152. int32_t mz_zip_extrafield_write(void *stream, uint16_t type, uint16_t length);
  153. /* Writes an extrafield header to a stream */
  154. /***************************************************************************/
  155. int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm);
  156. /* Convert dos date/time format to struct tm */
  157. time_t mz_zip_dosdate_to_time_t(uint64_t dos_date);
  158. /* Convert dos date/time format to time_t */
  159. int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm);
  160. /* Convert time_t to time struct */
  161. uint32_t mz_zip_time_t_to_dos_date(time_t unix_time);
  162. /* Convert time_t to dos date/time format */
  163. uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm);
  164. /* Convert struct tm to dos date/time format */
  165. int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time);
  166. /* Convert ntfs time to unix time */
  167. int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time);
  168. /* Convert unix time to ntfs time */
  169. /***************************************************************************/
  170. int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case);
  171. /* Compare two paths without regard to slashes */
  172. /***************************************************************************/
  173. const
  174. char* mz_zip_get_compression_method_string(int32_t compression_method);
  175. /* Gets a string representing the compression method */
  176. /***************************************************************************/
  177. #ifdef __cplusplus
  178. }
  179. #endif
  180. #endif /* _ZIP_H */