Canorus  0.0
zip.h
Go to the documentation of this file.
1 /*
2  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
3  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
4  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
5  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
6  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
7  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
8  * OTHER DEALINGS IN THE SOFTWARE.
9  */
10 
11 #pragma once
12 #ifndef ZIP_H
13 #define ZIP_H
14 
15 #include <string.h>
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #ifndef MAX_PATH
22 #define MAX_PATH 32767 /* # chars in a path name including NULL */
23 #endif
24 
25 #define ZIP_DEFAULT_COMPRESSION_LEVEL 6
26 
27 /*
28  This data structure is used throughout the library to represent zip archive
29  - forward declaration.
30 */
31 struct zip_t;
32 
33 /*
34  Opens zip archive with compression level using the given mode.
35 
36  Args:
37  zipname: zip archive file name.
38  level: compression level (0-9 are the standard zlib-style levels).
39  mode: file access mode.
40  'r': opens a file for reading/extracting (the file must exists).
41  'w': creates an empty file for writing.
42  'a': appends to an existing archive.
43 
44  Returns:
45  The zip archive handler or NULL on error
46 */
47 extern struct zip_t *zip_open(const char *zipname, int level, char mode);
48 
49 /*
50  Closes the zip archive, releases resources - always finalize.
51 
52  Args:
53  zip: zip archive handler.
54 */
55 extern void zip_close(struct zip_t *zip);
56 
57 /*
58  Opens an entry by name in the zip archive.
59  For zip archive opened in 'w' or 'a' mode the function will append
60  a new entry. In readonly mode the function tries to locate the entry
61  in global dictionary.
62 
63  Args:
64  zip: zip archive handler.
65  entryname: an entry name in local dictionary.
66 
67  Returns:
68  The return code - 0 on success, negative number (< 0) on error.
69 */
70 extern int zip_entry_open(struct zip_t *zip, const char *entryname);
71 
72 /*
73  Opens a new entry by index in the zip archive.
74  This function is only valid if zip archive was opened in 'r' (readonly) mode.
75 
76  Args:
77  zip: zip archive handler.
78  index: index in local dictionary.
79 
80  Returns:
81  The return code - 0 on success, negative number (< 0) on error.
82 */
83 extern int zip_entry_openbyindex(struct zip_t *zip, int index);
84 
85 /*
86  Closes a zip entry, flushes buffer and releases resources.
87 
88  Args:
89  zip: zip archive handler.
90 
91  Returns:
92  The return code - 0 on success, negative number (< 0) on error.
93 */
94 extern int zip_entry_close(struct zip_t *zip);
95 
96 /*
97  Returns a local name of the current zip entry.
98  The main difference between user's entry name and local entry name
99  is optional relative path.
100  Following .ZIP File Format Specification - the path stored MUST not contain
101  a drive or device letter, or a leading slash.
102  All slashes MUST be forward slashes '/' as opposed to backwards slashes '\'
103  for compatibility with Amiga and UNIX file systems etc.
104 
105  Args:
106  zip: zip archive handler.
107 
108  Returns:
109  The pointer to the current zip entry name, or NULL on error.
110 */
111 extern const char *zip_entry_name(struct zip_t *zip);
112 
113 /*
114  Returns an index of the current zip entry.
115 
116  Args:
117  zip: zip archive handler.
118 
119  Returns:
120  The index on success, negative number (< 0) on error.
121 */
122 extern int zip_entry_index(struct zip_t *zip);
123 
124 /*
125  Determines if the current zip entry is a directory entry.
126 
127  Args:
128  zip: zip archive handler.
129 
130  Returns:
131  The return code - 1 (true), 0 (false), negative number (< 0) on error.
132 */
133 extern int zip_entry_isdir(struct zip_t *zip);
134 
135 /*
136  Returns an uncompressed size of the current zip entry.
137 
138  Args:
139  zip: zip archive handler.
140 
141  Returns:
142  The uncompressed size in bytes.
143 */
144 extern unsigned long long zip_entry_size(struct zip_t *zip);
145 
146 /*
147  Returns CRC-32 checksum of the current zip entry.
148 
149  Args:
150  zip: zip archive handler.
151 
152  Returns:
153  The CRC-32 checksum.
154 */
155 extern unsigned int zip_entry_crc32(struct zip_t *zip);
156 
157 /*
158  Compresses an input buffer for the current zip entry.
159 
160  Args:
161  zip: zip archive handler.
162  buf: input buffer.
163  bufsize: input buffer size (in bytes).
164 
165  Returns:
166  The return code - 0 on success, negative number (< 0) on error.
167 */
168 extern int zip_entry_write(struct zip_t *zip, const void *buf, size_t bufsize);
169 
170 /*
171  Compresses a file for the current zip entry.
172 
173  Args:
174  zip: zip archive handler.
175  filename: input file.
176 
177  Returns:
178  The return code - 0 on success, negative number (< 0) on error.
179 */
180 extern int zip_entry_fwrite(struct zip_t *zip, const char *filename);
181 
182 /*
183  Extracts the current zip entry into output buffer.
184  The function allocates sufficient memory for a output buffer.
185 
186  Args:
187  zip: zip archive handler.
188  buf: output buffer.
189  bufsize: output buffer size (in bytes).
190 
191  Note:
192  - remember to release memory allocated for a output buffer.
193  - for large entries, please take a look at zip_entry_extract function.
194 
195  Returns:
196  The return code - 0 on success, negative number (< 0) on error.
197 */
198 extern int zip_entry_read(struct zip_t *zip, void **buf, size_t *bufsize);
199 
200 /*
201  Extracts the current zip entry into a memory buffer using no memory allocation.
202 
203  Args:
204  zip: zip archive handler.
205  buf: preallocated output buffer.
206  bufsize: output buffer size (in bytes).
207 
208  Note:
209  - ensure supplied output buffer is large enough.
210  - zip_entry_size function (returns uncompressed size for the current entry)
211  can be handy to estimate how big buffer is needed.
212  - for large entries, please take a look at zip_entry_extract function.
213 
214  Returns:
215  The return code - 0 on success, negative number (< 0) on error (e.g. bufsize
216  is not large enough).
217 */
218 extern int zip_entry_noallocread(struct zip_t *zip, void *buf, size_t bufsize);
219 
220 /*
221  Extracts the current zip entry into output file.
222 
223  Args:
224  zip: zip archive handler.
225  filename: output file.
226 
227  Returns:
228  The return code - 0 on success, negative number (< 0) on error.
229 */
230 extern int zip_entry_fread(struct zip_t *zip, const char *filename);
231 
232 /*
233  Extracts the current zip entry using a callback function (on_extract).
234 
235  Args:
236  zip: zip archive handler.
237  on_extract: callback function.
238  arg: opaque pointer (optional argument,
239  which you can pass to the on_extract callback)
240 
241  Returns:
242  The return code - 0 on success, negative number (< 0) on error.
243  */
244 extern int zip_entry_extract(struct zip_t *zip,
245  size_t (*on_extract)(void *arg,
246  unsigned long long offset,
247  const void *data,
248  size_t size),
249  void *arg);
250 
251 /*
252  Returns the number of all entries (files and directories) in the zip archive.
253 
254  Args:
255  zip: zip archive handler.
256 
257  Returns:
258  The return code - the number of entries on success,
259  negative number (< 0) on error.
260 */
261 extern int zip_total_entries(struct zip_t *zip);
262 
263 /*
264  Creates a new archive and puts files into a single zip archive.
265 
266  Args:
267  zipname: zip archive file.
268  filenames: input files.
269  len: number of input files.
270 
271  Returns:
272  The return code - 0 on success, negative number (< 0) on error.
273 */
274 extern int zip_create(const char *zipname, const char *filenames[], size_t len);
275 
276 /*
277  Extracts a zip archive file into directory.
278 
279  If on_extract_entry is not NULL, the callback will be called after
280  successfully extracted each zip entry.
281  Returning a negative value from the callback will cause abort and return an
282  error. The last argument (void *arg) is optional, which you can use to pass
283  data to the on_extract_entry callback.
284 
285  Args:
286  zipname: zip archive file.
287  dir: output directory.
288  on_extract_entry: on extract callback.
289  arg: opaque pointer.
290 
291  Returns:
292  The return code - 0 on success, negative number (< 0) on error.
293 */
294 extern int zip_extract(const char *zipname, const char *dir,
295  int (*on_extract_entry)(const char *filename, void *arg),
296  void *arg);
297 
298 #ifdef __cplusplus
299 }
300 #endif
301 
302 #endif
zip_open
struct zip_t * zip_open(const char *zipname, int level, char mode)
zip_entry_read
int zip_entry_read(struct zip_t *zip, void **buf, size_t *bufsize)
zip_entry_noallocread
int zip_entry_noallocread(struct zip_t *zip, void *buf, size_t bufsize)
zip_entry_name
const char * zip_entry_name(struct zip_t *zip)
zip_entry_size
unsigned long long zip_entry_size(struct zip_t *zip)
zip_total_entries
int zip_total_entries(struct zip_t *zip)
zip_entry_openbyindex
int zip_entry_openbyindex(struct zip_t *zip, int index)
zip_close
void zip_close(struct zip_t *zip)
zip_entry_close
int zip_entry_close(struct zip_t *zip)
zip_entry_fwrite
int zip_entry_fwrite(struct zip_t *zip, const char *filename)
zip_entry_crc32
unsigned int zip_entry_crc32(struct zip_t *zip)
zip_entry_write
int zip_entry_write(struct zip_t *zip, const void *buf, size_t bufsize)
zip_entry_extract
int zip_entry_extract(struct zip_t *zip, size_t(*on_extract)(void *arg, unsigned long long offset, const void *data, size_t size), void *arg)
zip_create
int zip_create(const char *zipname, const char *filenames[], size_t len)
zip_entry_isdir
int zip_entry_isdir(struct zip_t *zip)
zip_entry_open
int zip_entry_open(struct zip_t *zip, const char *entryname)
zip_entry_index
int zip_entry_index(struct zip_t *zip)
zip_extract
int zip_extract(const char *zipname, const char *dir, int(*on_extract_entry)(const char *filename, void *arg), void *arg)
zip_entry_fread
int zip_entry_fread(struct zip_t *zip, const char *filename)