CSEngine
Loading...
Searching...
No Matches
Zip

Classes

struct  zip_t
 

Macros

#define ZIP_DEFAULT_COMPRESSION_LEVEL   6
 
#define ZIP_ENOINIT   -1
 
#define ZIP_EINVENTNAME   -2
 
#define ZIP_ENOENT   -3
 
#define ZIP_EINVMODE   -4
 
#define ZIP_EINVLVL   -5
 
#define ZIP_ENOSUP64   -6
 
#define ZIP_EMEMSET   -7
 
#define ZIP_EWRTENT   -8
 
#define ZIP_ETDEFLINIT   -9
 
#define ZIP_EINVIDX   -10
 
#define ZIP_ENOHDR   -11
 
#define ZIP_ETDEFLBUF   -12
 
#define ZIP_ECRTHDR   -13
 
#define ZIP_EWRTHDR   -14
 
#define ZIP_EWRTDIR   -15
 
#define ZIP_EOPNFILE   -16
 
#define ZIP_EINVENTTYPE   -17
 
#define ZIP_EMEMNOALLOC   -18
 
#define ZIP_ENOFILE   -19
 
#define ZIP_ENOPERM   -20
 
#define ZIP_EOOMEM   -21
 
#define ZIP_EINVZIPNAME   -22
 
#define ZIP_EMKDIR   -23
 
#define ZIP_ESYMLINK   -24
 
#define ZIP_ECLSZIP   -25
 
#define ZIP_ECAPSIZE   -26
 
#define ZIP_EFSEEK   -27
 
#define ZIP_EFREAD   -28
 
#define ZIP_EFWRITE   -29
 

Functions

const char * zip_strerror (int errnum)
 
struct zip_tzip_open (const char *zipname, int level, char mode)
 
void zip_close (struct zip_t *zip)
 
int zip_is64 (struct zip_t *zip)
 
int zip_entry_open (struct zip_t *zip, const char *entryname)
 
int zip_entry_openbyindex (struct zip_t *zip, int index)
 
int zip_entry_close (struct zip_t *zip)
 
const char * zip_entry_name (struct zip_t *zip)
 
int zip_entry_index (struct zip_t *zip)
 
int zip_entry_isdir (struct zip_t *zip)
 
unsigned long long zip_entry_size (struct zip_t *zip)
 
unsigned int zip_entry_crc32 (struct zip_t *zip)
 
int zip_entry_write (struct zip_t *zip, const void *buf, size_t bufsize)
 
int zip_entry_fwrite (struct zip_t *zip, const char *filename)
 
ssize_t zip_entry_read (struct zip_t *zip, void **buf, size_t *bufsize)
 
ssize_t zip_entry_noallocread (struct zip_t *zip, void *buf, size_t bufsize)
 
int zip_entry_fread (struct zip_t *zip, const char *filename)
 
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)
 
int zip_entries_total (struct zip_t *zip)
 
int zip_entries_delete (struct zip_t *zip, char *const entries[], size_t len)
 
int zip_stream_extract (const char *stream, size_t size, const char *dir, int(*on_extract)(const char *filename, void *arg), void *arg)
 
struct zip_tzip_stream_open (const char *stream, size_t size, int level, char mode)
 
ssize_t zip_stream_copy (struct zip_t *zip, void **buf, ssize_t *bufsize)
 
void zip_stream_close (struct zip_t *zip)
 
int zip_create (const char *zipname, const char *filenames[], size_t len)
 
int zip_extract (const char *zipname, const char *dir, int(*on_extract_entry)(const char *filename, void *arg), void *arg)
 

Detailed Description

Macro Definition Documentation

◆ ZIP_DEFAULT_COMPRESSION_LEVEL

#define ZIP_DEFAULT_COMPRESSION_LEVEL   6

Default zip compression level.

Definition at line 50 of file zip.h.

◆ ZIP_ECAPSIZE

#define ZIP_ECAPSIZE   -26

Definition at line 80 of file zip.h.

◆ ZIP_ECLSZIP

#define ZIP_ECLSZIP   -25

Definition at line 79 of file zip.h.

◆ ZIP_ECRTHDR

#define ZIP_ECRTHDR   -13

Definition at line 67 of file zip.h.

◆ ZIP_EFREAD

#define ZIP_EFREAD   -28

Definition at line 82 of file zip.h.

◆ ZIP_EFSEEK

#define ZIP_EFSEEK   -27

Definition at line 81 of file zip.h.

◆ ZIP_EFWRITE

#define ZIP_EFWRITE   -29

Definition at line 83 of file zip.h.

◆ ZIP_EINVENTNAME

#define ZIP_EINVENTNAME   -2

Definition at line 56 of file zip.h.

◆ ZIP_EINVENTTYPE

#define ZIP_EINVENTTYPE   -17

Definition at line 71 of file zip.h.

◆ ZIP_EINVIDX

#define ZIP_EINVIDX   -10

Definition at line 64 of file zip.h.

◆ ZIP_EINVLVL

#define ZIP_EINVLVL   -5

Definition at line 59 of file zip.h.

◆ ZIP_EINVMODE

#define ZIP_EINVMODE   -4

Definition at line 58 of file zip.h.

◆ ZIP_EINVZIPNAME

#define ZIP_EINVZIPNAME   -22

Definition at line 76 of file zip.h.

◆ ZIP_EMEMNOALLOC

#define ZIP_EMEMNOALLOC   -18

Definition at line 72 of file zip.h.

◆ ZIP_EMEMSET

#define ZIP_EMEMSET   -7

Definition at line 61 of file zip.h.

◆ ZIP_EMKDIR

#define ZIP_EMKDIR   -23

Definition at line 77 of file zip.h.

◆ ZIP_ENOENT

#define ZIP_ENOENT   -3

Definition at line 57 of file zip.h.

◆ ZIP_ENOFILE

#define ZIP_ENOFILE   -19

Definition at line 73 of file zip.h.

◆ ZIP_ENOHDR

#define ZIP_ENOHDR   -11

Definition at line 65 of file zip.h.

◆ ZIP_ENOINIT

#define ZIP_ENOINIT   -1

Error codes

Definition at line 55 of file zip.h.

◆ ZIP_ENOPERM

#define ZIP_ENOPERM   -20

Definition at line 74 of file zip.h.

◆ ZIP_ENOSUP64

#define ZIP_ENOSUP64   -6

Definition at line 60 of file zip.h.

◆ ZIP_EOOMEM

#define ZIP_EOOMEM   -21

Definition at line 75 of file zip.h.

◆ ZIP_EOPNFILE

#define ZIP_EOPNFILE   -16

Definition at line 70 of file zip.h.

◆ ZIP_ESYMLINK

#define ZIP_ESYMLINK   -24

Definition at line 78 of file zip.h.

◆ ZIP_ETDEFLBUF

#define ZIP_ETDEFLBUF   -12

Definition at line 66 of file zip.h.

◆ ZIP_ETDEFLINIT

#define ZIP_ETDEFLINIT   -9

Definition at line 63 of file zip.h.

◆ ZIP_EWRTDIR

#define ZIP_EWRTDIR   -15

Definition at line 69 of file zip.h.

◆ ZIP_EWRTENT

#define ZIP_EWRTENT   -8

Definition at line 62 of file zip.h.

◆ ZIP_EWRTHDR

#define ZIP_EWRTHDR   -14

Definition at line 68 of file zip.h.

Function Documentation

◆ zip_close()

void zip_close ( struct zip_t zip)
extern

Closes the zip archive, releases resources - always finalize.

Parameters
zipzip archive handler.

Definition at line 836 of file zip.cpp.

836 {
837 if (zip) {
838 // Always finalize, even if adding failed for some reason, so we have a
839 // valid central directory.
840 mz_zip_writer_finalize_archive(&(zip->archive));
841 zip_archive_truncate(&(zip->archive));
842 mz_zip_writer_end(&(zip->archive));
843 mz_zip_reader_end(&(zip->archive));
844
845 CLEANUP(zip);
846 }
847}

◆ zip_create()

int zip_create ( const char *  zipname,
const char *  filenames[],
size_t  len 
)
extern

Creates a new archive and puts files into a single zip archive.

Parameters
zipnamezip archive file.
filenamesinput files.
lennumber of input files.
Returns
the return code - 0 on success, negative number (< 0) on error.

Definition at line 1541 of file zip.cpp.

1541 {
1542 int err = 0;
1543 size_t i;
1544 mz_zip_archive zip_archive;
1545 struct MZ_FILE_STAT_STRUCT file_stat;
1546 mz_uint32 ext_attributes = 0;
1547
1548 if (!zipname || strlen(zipname) < 1) {
1549 // zip_t archive name is empty or NULL
1550 return ZIP_EINVZIPNAME;
1551 }
1552
1553 // Create a new archive.
1554 if (!memset(&(zip_archive), 0, sizeof(zip_archive))) {
1555 // Cannot memset zip archive
1556 return ZIP_EMEMSET;
1557 }
1558
1559 if (!mz_zip_writer_init_file(&zip_archive, zipname, 0)) {
1560 // Cannot initialize zip_archive writer
1561 return ZIP_ENOINIT;
1562 }
1563
1564 if (!memset((void *)&file_stat, 0, sizeof(struct MZ_FILE_STAT_STRUCT))) {
1565 return ZIP_EMEMSET;
1566 }
1567
1568 for (i = 0; i < len; ++i) {
1569 const char *name = filenames[i];
1570 if (!name) {
1571 err = ZIP_EINVENTNAME;
1572 break;
1573 }
1574
1575 if (MZ_FILE_STAT(name, &file_stat) != 0) {
1576 // problem getting information - check errno
1577 err = ZIP_ENOFILE;
1578 break;
1579 }
1580
1581 if ((file_stat.st_mode & 0200) == 0) {
1582 // MS-DOS read-only attribute
1583 ext_attributes |= 0x01;
1584 }
1585 ext_attributes |= (mz_uint32)((file_stat.st_mode & 0xFFFF) << 16);
1586
1587 if (!mz_zip_writer_add_file(&zip_archive, zip_basename(name), name, "", 0,
1589 ext_attributes)) {
1590 // Cannot add file to zip_archive
1591 err = ZIP_ENOFILE;
1592 break;
1593 }
1594 }
1595
1596 mz_zip_writer_finalize_archive(&zip_archive);
1597 mz_zip_writer_end(&zip_archive);
1598 return err;
1599}
#define ZIP_DEFAULT_COMPRESSION_LEVEL
Definition zip.h:50
#define ZIP_ENOINIT
Definition zip.h:55

References ZIP_DEFAULT_COMPRESSION_LEVEL, and ZIP_ENOINIT.

◆ zip_entries_delete()

int zip_entries_delete ( struct zip_t zip,
char *const  entries[],
size_t  len 
)
extern

Deletes zip archive entries.

Parameters
zipzip archive handler.
entriesarray of zip archive entries to be deleted.
lenthe number of entries to be deleted.
Returns
the number of deleted entries, or negative number (< 0) on error.

Definition at line 1426 of file zip.cpp.

1427 {
1428 int n = 0;
1429 int err = 0;
1430 struct zip_entry_mark_t *entry_mark = NULL;
1431
1432 if (zip == NULL || (entries == NULL && len != 0)) {
1433 return ZIP_ENOINIT;
1434 }
1435
1436 if (entries == NULL && len == 0) {
1437 return 0;
1438 }
1439
1440 n = zip_entries_total(zip);
1441
1442 entry_mark =
1443 (struct zip_entry_mark_t *)calloc(n, sizeof(struct zip_entry_mark_t));
1444 if (!entry_mark) {
1445 return ZIP_EOOMEM;
1446 }
1447
1448 zip->archive.m_zip_mode = MZ_ZIP_MODE_READING;
1449
1450 err = zip_entry_set(zip, entry_mark, n, entries, len);
1451 if (err < 0) {
1452 CLEANUP(entry_mark);
1453 return err;
1454 }
1455
1456 err = zip_entries_delete_mark(zip, entry_mark, n);
1457 CLEANUP(entry_mark);
1458 return err;
1459}
int zip_entries_total(struct zip_t *zip)
Definition zip.cpp:1417
Definition zip.cpp:100

References ZIP_ENOINIT, and zip_entries_total().

◆ zip_entries_total()

int zip_entries_total ( struct zip_t zip)
extern

Returns the number of all entries (files and directories) in the zip archive.

Parameters
zipzip archive handler.
Returns
the return code - the number of entries on success, negative number (< 0) on error.

Definition at line 1417 of file zip.cpp.

1417 {
1418 if (!zip) {
1419 // zip_t handler is not initialized
1420 return ZIP_ENOINIT;
1421 }
1422
1423 return (int)zip->archive.m_total_files;
1424}

References ZIP_ENOINIT.

Referenced by zip_entries_delete().

◆ zip_entry_close()

int zip_entry_close ( struct zip_t zip)
extern

Closes a zip entry, flushes buffer and releases resources.

Parameters
zipzip archive handler.
Returns
the return code - 0 on success, negative number (< 0) on error.

Definition at line 1087 of file zip.cpp.

1087 {
1088 mz_zip_archive *pzip = NULL;
1089 mz_uint level;
1090 tdefl_status done;
1091 mz_uint16 entrylen;
1092 mz_uint16 dos_time = 0, dos_date = 0;
1093 int err = 0;
1094
1095 if (!zip) {
1096 // zip_t handler is not initialized
1097 err = ZIP_ENOINIT;
1098 goto cleanup;
1099 }
1100
1101 pzip = &(zip->archive);
1102 if (pzip->m_zip_mode == MZ_ZIP_MODE_READING) {
1103 goto cleanup;
1104 }
1105
1106 level = zip->level & 0xF;
1107 if (level) {
1108 done = tdefl_compress_buffer(&(zip->entry.comp), "", 0, TDEFL_FINISH);
1109 if (done != TDEFL_STATUS_DONE && done != TDEFL_STATUS_OKAY) {
1110 // Cannot flush compressed buffer
1111 err = ZIP_ETDEFLBUF;
1112 goto cleanup;
1113 }
1114 zip->entry.comp_size = zip->entry.state.m_comp_size;
1115 zip->entry.offset = zip->entry.state.m_cur_archive_file_ofs;
1116 zip->entry.method = MZ_DEFLATED;
1117 }
1118
1119 entrylen = (mz_uint16)strlen(zip->entry.name);
1120 if ((zip->entry.comp_size > 0xFFFFFFFF) || (zip->entry.offset > 0xFFFFFFFF)) {
1121 // No zip64 support, yet
1122 err = ZIP_ENOSUP64;
1123 goto cleanup;
1124 }
1125
1126#ifndef MINIZ_NO_TIME
1127 mz_zip_time_t_to_dos_time(zip->entry.m_time, &dos_time, &dos_date);
1128#endif
1129
1130 if (!mz_zip_writer_create_local_dir_header(
1131 pzip, zip->entry.header, entrylen, 0, zip->entry.uncomp_size,
1132 zip->entry.comp_size, zip->entry.uncomp_crc32, zip->entry.method, 0,
1133 dos_time, dos_date)) {
1134 // Cannot create zip entry header
1135 err = ZIP_ECRTHDR;
1136 goto cleanup;
1137 }
1138
1139 if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.header_offset,
1140 zip->entry.header,
1141 sizeof(zip->entry.header)) != sizeof(zip->entry.header)) {
1142 // Cannot write zip entry header
1143 err = ZIP_EWRTHDR;
1144 goto cleanup;
1145 }
1146
1147 if (!mz_zip_writer_add_to_central_dir(
1148 pzip, zip->entry.name, entrylen, NULL, 0, "", 0,
1149 zip->entry.uncomp_size, zip->entry.comp_size, zip->entry.uncomp_crc32,
1150 zip->entry.method, 0, dos_time, dos_date, zip->entry.header_offset,
1151 zip->entry.external_attr)) {
1152 // Cannot write to zip central dir
1153 err = ZIP_EWRTDIR;
1154 goto cleanup;
1155 }
1156
1157 pzip->m_total_files++;
1158 pzip->m_archive_size = zip->entry.offset;
1159
1160cleanup:
1161 if (zip) {
1162 zip->entry.m_time = 0;
1163 CLEANUP(zip->entry.name);
1164 }
1165 return err;
1166}

References ZIP_ENOINIT.

◆ zip_entry_crc32()

unsigned int zip_entry_crc32 ( struct zip_t zip)
extern

Returns CRC-32 checksum of the current zip entry.

Parameters
zipzip archive handler.
Returns
the CRC-32 checksum.

Definition at line 1205 of file zip.cpp.

1205 {
1206 return zip ? zip->entry.uncomp_crc32 : 0;
1207}

◆ zip_entry_extract()

int zip_entry_extract ( struct zip_t zip,
size_t(*)(void *arg, unsigned long long offset, const void *data, size_t size)  on_extract,
void *  arg 
)
extern

Extracts the current zip entry using a callback function (on_extract).

Parameters
zipzip archive handler.
on_extractcallback function.
argopaque pointer (optional argument, which you can pass to the on_extract callback)
Returns
the return code - 0 on success, negative number (< 0) on error.

◆ zip_entry_fread()

int zip_entry_fread ( struct zip_t zip,
const char *  filename 
)
extern

Extracts the current zip entry into output file.

Parameters
zipzip archive handler.
filenameoutput file.
Returns
the return code - 0 on success, negative number (< 0) on error.

Definition at line 1346 of file zip.cpp.

1346 {
1347 mz_zip_archive *pzip = NULL;
1348 mz_uint idx;
1349 mz_uint32 xattr = 0;
1351
1352 if (!zip) {
1353 // zip_t handler is not initialized
1354 return ZIP_ENOINIT;
1355 }
1356
1357 memset((void *)&info, 0, sizeof(mz_zip_archive_file_stat));
1358 pzip = &(zip->archive);
1359 if (pzip->m_zip_mode != MZ_ZIP_MODE_READING || zip->entry.index < 0) {
1360 // the entry is not found or we do not have read access
1361 return ZIP_ENOENT;
1362 }
1363
1364 idx = (mz_uint)zip->entry.index;
1365 if (mz_zip_reader_is_file_a_directory(pzip, idx)) {
1366 // the entry is a directory
1367 return ZIP_EINVENTTYPE;
1368 }
1369
1370 if (!mz_zip_reader_extract_to_file(pzip, idx, filename, 0)) {
1371 return ZIP_ENOFILE;
1372 }
1373
1374#if defined(_MSC_VER)
1375 (void)xattr; // unused
1376#else
1377 if (!mz_zip_reader_file_stat(pzip, idx, &info)) {
1378 // Cannot get information about zip archive;
1379 return ZIP_ENOFILE;
1380 }
1381
1382 xattr = (info.m_external_attr >> 16) & 0xFFFF;
1383 if (xattr > 0) {
1384 if (chmod(filename, (mode_t)xattr) < 0) {
1385 return ZIP_ENOPERM;
1386 }
1387 }
1388#endif
1389
1390 return 0;
1391}

References ZIP_ENOINIT.

◆ zip_entry_fwrite()

int zip_entry_fwrite ( struct zip_t zip,
const char *  filename 
)
extern

Compresses a file for the current zip entry.

Parameters
zipzip archive handler.
filenameinput file.
Returns
the return code - 0 on success, negative number (< 0) on error.

Definition at line 1247 of file zip.cpp.

1247 {
1248 int err = 0;
1249 size_t n = 0;
1250 FILE *stream = NULL;
1251 mz_uint8 buf[MZ_ZIP_MAX_IO_BUF_SIZE];
1252 struct MZ_FILE_STAT_STRUCT file_stat;
1253
1254 if (!zip) {
1255 // zip_t handler is not initialized
1256 return ZIP_ENOINIT;
1257 }
1258
1259 memset(buf, 0, MZ_ZIP_MAX_IO_BUF_SIZE);
1260 memset((void *)&file_stat, 0, sizeof(struct MZ_FILE_STAT_STRUCT));
1261 if (MZ_FILE_STAT(filename, &file_stat) != 0) {
1262 // problem getting information - check errno
1263 return ZIP_ENOENT;
1264 }
1265
1266 if ((file_stat.st_mode & 0200) == 0) {
1267 // MS-DOS read-only attribute
1268 zip->entry.external_attr |= 0x01;
1269 }
1270 zip->entry.external_attr |= (mz_uint32)((file_stat.st_mode & 0xFFFF) << 16);
1271 zip->entry.m_time = file_stat.st_mtime;
1272
1273#if defined(_MSC_VER)
1274 if (fopen_s(&stream, filename, "rb"))
1275#else
1276 if (!(stream = fopen(filename, "rb")))
1277#endif
1278 {
1279 // Cannot open filename
1280 return ZIP_EOPNFILE;
1281 }
1282
1283 while ((n = fread(buf, sizeof(mz_uint8), MZ_ZIP_MAX_IO_BUF_SIZE, stream)) >
1284 0) {
1285 if (zip_entry_write(zip, buf, n) < 0) {
1286 err = ZIP_EWRTENT;
1287 break;
1288 }
1289 }
1290 fclose(stream);
1291
1292 return err;
1293}
int zip_entry_write(struct zip_t *zip, const void *buf, size_t bufsize)
Definition zip.cpp:1209

References ZIP_ENOINIT, and zip_entry_write().

◆ zip_entry_index()

int zip_entry_index ( struct zip_t zip)
extern

Returns an index of the current zip entry.

Parameters
zipzip archive handler.
Returns
the index on success, negative number (< 0) on error.

Definition at line 1177 of file zip.cpp.

1177 {
1178 if (!zip) {
1179 // zip_t handler is not initialized
1180 return ZIP_ENOINIT;
1181 }
1182
1183 return zip->entry.index;
1184}

References ZIP_ENOINIT.

◆ zip_entry_isdir()

int zip_entry_isdir ( struct zip_t zip)
extern

Determines if the current zip entry is a directory entry.

Parameters
zipzip archive handler.
Returns
the return code - 1 (true), 0 (false), negative number (< 0) on error.

Definition at line 1186 of file zip.cpp.

1186 {
1187 if (!zip) {
1188 // zip_t handler is not initialized
1189 return ZIP_ENOINIT;
1190 }
1191
1192 if (zip->entry.index < 0) {
1193 // zip entry is not opened
1194 return ZIP_EINVIDX;
1195 }
1196
1197 return (int)mz_zip_reader_is_file_a_directory(&zip->archive,
1198 (mz_uint)zip->entry.index);
1199}

References ZIP_ENOINIT.

◆ zip_entry_name()

const char * zip_entry_name ( struct zip_t zip)
extern

Returns a local name of the current zip entry.

The main difference between user's entry name and local entry name is optional relative path. Following .ZIP File Format Specification - the path stored MUST not contain a drive or device letter, or a leading slash. All slashes MUST be forward slashes '/' as opposed to backwards slashes '\' for compatibility with Amiga and UNIX file systems etc.

Parameters
zipzip archive handler.
Returns
the pointer to the current zip entry name, or NULL on error.

Definition at line 1168 of file zip.cpp.

1168 {
1169 if (!zip) {
1170 // zip_t handler is not initialized
1171 return NULL;
1172 }
1173
1174 return zip->entry.name;
1175}

◆ zip_entry_noallocread()

ssize_t zip_entry_noallocread ( struct zip_t zip,
void *  buf,
size_t  bufsize 
)
extern

Extracts the current zip entry into a memory buffer using no memory allocation.

Parameters
zipzip archive handler.
bufpreallocated output buffer.
bufsizeoutput buffer size (in bytes).
Note
ensure supplied output buffer is large enough. zip_entry_size function (returns uncompressed size for the current entry) can be handy to estimate how big buffer is needed. For large entries, please take a look at zip_entry_extract function.
Returns
the return code - the number of bytes actually read on success. Otherwise a -1 on error (e.g. bufsize is not large enough).

Definition at line 1324 of file zip.cpp.

1324 {
1325 mz_zip_archive *pzip = NULL;
1326
1327 if (!zip) {
1328 // zip_t handler is not initialized
1329 return ZIP_ENOINIT;
1330 }
1331
1332 pzip = &(zip->archive);
1333 if (pzip->m_zip_mode != MZ_ZIP_MODE_READING || zip->entry.index < 0) {
1334 // the entry is not found or we do not have read access
1335 return ZIP_ENOENT;
1336 }
1337
1338 if (!mz_zip_reader_extract_to_mem_no_alloc(pzip, (mz_uint)zip->entry.index,
1339 buf, bufsize, 0, NULL, 0)) {
1340 return ZIP_EMEMNOALLOC;
1341 }
1342
1343 return (ssize_t)zip->entry.uncomp_size;
1344}

References ZIP_ENOINIT.

◆ zip_entry_open()

int zip_entry_open ( struct zip_t zip,
const char *  entryname 
)
extern

Opens an entry by name in the zip archive.

For zip archive opened in 'w' or 'a' mode the function will append a new entry. In readonly mode the function tries to locate the entry in global dictionary.

Parameters
zipzip archive handler.
entrynamean entry name in local dictionary.
Returns
the return code - 0 on success, negative number (< 0) on error.

Definition at line 858 of file zip.cpp.

858 {
859 size_t entrylen = 0;
860 mz_zip_archive *pzip = NULL;
861 mz_uint num_alignment_padding_bytes, level;
863 int err = 0;
864
865 if (!zip) {
866 return ZIP_ENOINIT;
867 }
868
869 if (!entryname) {
870 return ZIP_EINVENTNAME;
871 }
872
873 entrylen = strlen(entryname);
874 if (entrylen == 0) {
875 return ZIP_EINVENTNAME;
876 }
877
878 /*
879 .ZIP File Format Specification Version: 6.3.3
880
881 4.4.17.1 The name of the file, with optional relative path.
882 The path stored MUST not contain a drive or
883 device letter, or a leading slash. All slashes
884 MUST be forward slashes '/' as opposed to
885 backwards slashes '\' for compatibility with Amiga
886 and UNIX file systems etc. If input came from standard
887 input, there is no file name field.
888 */
889 if (zip->entry.name) {
890 CLEANUP(zip->entry.name);
891 }
892 zip->entry.name = zip_strrpl(entryname, entrylen, '\\', '/');
893 if (!zip->entry.name) {
894 // Cannot parse zip entry name
895 return ZIP_EINVENTNAME;
896 }
897
898 pzip = &(zip->archive);
899 if (pzip->m_zip_mode == MZ_ZIP_MODE_READING) {
900 zip->entry.index =
901 mz_zip_reader_locate_file(pzip, zip->entry.name, NULL, 0);
902 if (zip->entry.index < 0) {
903 err = ZIP_ENOENT;
904 goto cleanup;
905 }
906
907 if (!mz_zip_reader_file_stat(pzip, (mz_uint)zip->entry.index, &stats)) {
908 err = ZIP_ENOENT;
909 goto cleanup;
910 }
911
912 zip->entry.comp_size = stats.m_comp_size;
913 zip->entry.uncomp_size = stats.m_uncomp_size;
914 zip->entry.uncomp_crc32 = stats.m_crc32;
915 zip->entry.offset = stats.m_central_dir_ofs;
916 zip->entry.header_offset = stats.m_local_header_ofs;
917 zip->entry.method = stats.m_method;
918 zip->entry.external_attr = stats.m_external_attr;
919#ifndef MINIZ_NO_TIME
920 zip->entry.m_time = stats.m_time;
921#endif
922
923 return 0;
924 }
925
926 zip->entry.index = (int)zip->archive.m_total_files;
927 zip->entry.comp_size = 0;
928 zip->entry.uncomp_size = 0;
929 zip->entry.uncomp_crc32 = MZ_CRC32_INIT;
930 zip->entry.offset = zip->archive.m_archive_size;
931 zip->entry.header_offset = zip->archive.m_archive_size;
932 memset(zip->entry.header, 0, MZ_ZIP_LOCAL_DIR_HEADER_SIZE * sizeof(mz_uint8));
933 zip->entry.method = 0;
934
935 // UNIX or APPLE
936#if MZ_PLATFORM == 3 || MZ_PLATFORM == 19
937 // regular file with rw-r--r-- persmissions
938 zip->entry.external_attr = (mz_uint32)(0100644) << 16;
939#else
940 zip->entry.external_attr = 0;
941#endif
942
943 num_alignment_padding_bytes =
944 mz_zip_writer_compute_padding_needed_for_file_alignment(pzip);
945
946 if (!pzip->m_pState || (pzip->m_zip_mode != MZ_ZIP_MODE_WRITING)) {
947 // Invalid zip mode
948 err = ZIP_EINVMODE;
949 goto cleanup;
950 }
951 if (zip->level & MZ_ZIP_FLAG_COMPRESSED_DATA) {
952 // Invalid zip compression level
953 err = ZIP_EINVLVL;
954 goto cleanup;
955 }
956 // no zip64 support yet
957 if ((pzip->m_total_files == 0xFFFF) ||
958 ((pzip->m_archive_size + num_alignment_padding_bytes +
959 MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE +
960 entrylen) > 0xFFFFFFFF)) {
961 // No zip64 support yet
962 err = ZIP_ENOSUP64;
963 goto cleanup;
964 }
965 if (!mz_zip_writer_write_zeros(pzip, zip->entry.offset,
966 num_alignment_padding_bytes +
967 sizeof(zip->entry.header))) {
968 // Cannot memset zip entry header
969 err = ZIP_EMEMSET;
970 goto cleanup;
971 }
972
973 zip->entry.header_offset += num_alignment_padding_bytes;
974 if (pzip->m_file_offset_alignment) {
975 MZ_ASSERT(
976 (zip->entry.header_offset & (pzip->m_file_offset_alignment - 1)) == 0);
977 }
978 zip->entry.offset += num_alignment_padding_bytes + sizeof(zip->entry.header);
979
980 if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, zip->entry.name,
981 entrylen) != entrylen) {
982 // Cannot write data to zip entry
983 err = ZIP_EWRTENT;
984 goto cleanup;
985 }
986
987 zip->entry.offset += entrylen;
988 level = zip->level & 0xF;
989 if (level) {
990 zip->entry.state.m_pZip = pzip;
991 zip->entry.state.m_cur_archive_file_ofs = zip->entry.offset;
992 zip->entry.state.m_comp_size = 0;
993
994 if (tdefl_init(&(zip->entry.comp), mz_zip_writer_add_put_buf_callback,
995 &(zip->entry.state),
996 (int)tdefl_create_comp_flags_from_zip_params(
997 (int)level, -15, MZ_DEFAULT_STRATEGY)) !=
998 TDEFL_STATUS_OKAY) {
999 // Cannot initialize the zip compressor
1000 err = ZIP_ETDEFLINIT;
1001 goto cleanup;
1002 }
1003 }
1004
1005 zip->entry.m_time = time(NULL);
1006
1007 return 0;
1008
1009cleanup:
1010 CLEANUP(zip->entry.name);
1011 return err;
1012}

References ZIP_ENOINIT.

◆ zip_entry_openbyindex()

int zip_entry_openbyindex ( struct zip_t zip,
int  index 
)
extern

Opens a new entry by index in the zip archive.

This function is only valid if zip archive was opened in 'r' (readonly) mode.

Parameters
zipzip archive handler.
indexindex in local dictionary.
Returns
the return code - 0 on success, negative number (< 0) on error.

Definition at line 1014 of file zip.cpp.

1014 {
1015 mz_zip_archive *pZip = NULL;
1017 mz_uint namelen;
1018 const mz_uint8 *pHeader;
1019 const char *pFilename;
1020
1021 if (!zip) {
1022 // zip_t handler is not initialized
1023 return ZIP_ENOINIT;
1024 }
1025
1026 pZip = &(zip->archive);
1027 if (pZip->m_zip_mode != MZ_ZIP_MODE_READING) {
1028 // open by index requires readonly mode
1029 return ZIP_EINVMODE;
1030 }
1031
1032 if (index < 0 || (mz_uint)index >= pZip->m_total_files) {
1033 // index out of range
1034 return ZIP_EINVIDX;
1035 }
1036
1037 if (!(pHeader = &MZ_ZIP_ARRAY_ELEMENT(
1038 &pZip->m_pState->m_central_dir, mz_uint8,
1039 MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets,
1040 mz_uint32, index)))) {
1041 // cannot find header in central directory
1042 return ZIP_ENOHDR;
1043 }
1044
1045 namelen = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_FILENAME_LEN_OFS);
1046 pFilename = (const char *)pHeader + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
1047
1048 /*
1049 .ZIP File Format Specification Version: 6.3.3
1050
1051 4.4.17.1 The name of the file, with optional relative path.
1052 The path stored MUST not contain a drive or
1053 device letter, or a leading slash. All slashes
1054 MUST be forward slashes '/' as opposed to
1055 backwards slashes '\' for compatibility with Amiga
1056 and UNIX file systems etc. If input came from standard
1057 input, there is no file name field.
1058 */
1059 if (zip->entry.name) {
1060 CLEANUP(zip->entry.name);
1061 }
1062 zip->entry.name = zip_strrpl(pFilename, namelen, '\\', '/');
1063 if (!zip->entry.name) {
1064 // local entry name is NULL
1065 return ZIP_EINVENTNAME;
1066 }
1067
1068 if (!mz_zip_reader_file_stat(pZip, (mz_uint)index, &stats)) {
1069 return ZIP_ENOENT;
1070 }
1071
1072 zip->entry.index = index;
1073 zip->entry.comp_size = stats.m_comp_size;
1074 zip->entry.uncomp_size = stats.m_uncomp_size;
1075 zip->entry.uncomp_crc32 = stats.m_crc32;
1076 zip->entry.offset = stats.m_central_dir_ofs;
1077 zip->entry.header_offset = stats.m_local_header_ofs;
1078 zip->entry.method = stats.m_method;
1079 zip->entry.external_attr = stats.m_external_attr;
1080#ifndef MINIZ_NO_TIME
1081 zip->entry.m_time = stats.m_time;
1082#endif
1083
1084 return 0;
1085}

References ZIP_ENOINIT.

◆ zip_entry_read()

ssize_t zip_entry_read ( struct zip_t zip,
void **  buf,
size_t *  bufsize 
)
extern

Extracts the current zip entry into output buffer.

The function allocates sufficient memory for a output buffer.

Parameters
zipzip archive handler.
bufoutput buffer.
bufsizeoutput buffer size (in bytes).
Note
remember to release memory allocated for a output buffer. for large entries, please take a look at zip_entry_extract function.
Returns
the return code - the number of bytes actually read on success. Otherwise a -1 on error.

Definition at line 1295 of file zip.cpp.

1295 {
1296 mz_zip_archive *pzip = NULL;
1297 mz_uint idx;
1298 size_t size = 0;
1299
1300 if (!zip) {
1301 // zip_t handler is not initialized
1302 return ZIP_ENOINIT;
1303 }
1304
1305 pzip = &(zip->archive);
1306 if (pzip->m_zip_mode != MZ_ZIP_MODE_READING || zip->entry.index < 0) {
1307 // the entry is not found or we do not have read access
1308 return ZIP_ENOENT;
1309 }
1310
1311 idx = (mz_uint)zip->entry.index;
1312 if (mz_zip_reader_is_file_a_directory(pzip, idx)) {
1313 // the entry is a directory
1314 return ZIP_EINVENTTYPE;
1315 }
1316
1317 *buf = mz_zip_reader_extract_to_heap(pzip, idx, &size, 0);
1318 if (*buf && bufsize) {
1319 *bufsize = size;
1320 }
1321 return size;
1322}

References ZIP_ENOINIT.

◆ zip_entry_size()

unsigned long long zip_entry_size ( struct zip_t zip)
extern

Returns an uncompressed size of the current zip entry.

Parameters
zipzip archive handler.
Returns
the uncompressed size in bytes.

Definition at line 1201 of file zip.cpp.

1201 {
1202 return zip ? zip->entry.uncomp_size : 0;
1203}

◆ zip_entry_write()

int zip_entry_write ( struct zip_t zip,
const void *  buf,
size_t  bufsize 
)
extern

Compresses an input buffer for the current zip entry.

Parameters
zipzip archive handler.
bufinput buffer.
bufsizeinput buffer size (in bytes).
Returns
the return code - 0 on success, negative number (< 0) on error.

Definition at line 1209 of file zip.cpp.

1209 {
1210 mz_uint level;
1211 mz_zip_archive *pzip = NULL;
1212 tdefl_status status;
1213
1214 if (!zip) {
1215 // zip_t handler is not initialized
1216 return ZIP_ENOINIT;
1217 }
1218
1219 pzip = &(zip->archive);
1220 if (buf && bufsize > 0) {
1221 zip->entry.uncomp_size += bufsize;
1222 zip->entry.uncomp_crc32 = (mz_uint32)mz_crc32(
1223 zip->entry.uncomp_crc32, (const mz_uint8 *)buf, bufsize);
1224
1225 level = zip->level & 0xF;
1226 if (!level) {
1227 if ((pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, buf,
1228 bufsize) != bufsize)) {
1229 // Cannot write buffer
1230 return ZIP_EWRTENT;
1231 }
1232 zip->entry.offset += bufsize;
1233 zip->entry.comp_size += bufsize;
1234 } else {
1235 status = tdefl_compress_buffer(&(zip->entry.comp), buf, bufsize,
1236 TDEFL_NO_FLUSH);
1237 if (status != TDEFL_STATUS_DONE && status != TDEFL_STATUS_OKAY) {
1238 // Cannot compress buffer
1239 return ZIP_ETDEFLBUF;
1240 }
1241 }
1242 }
1243
1244 return 0;
1245}

References ZIP_ENOINIT.

Referenced by zip_entry_fwrite().

◆ zip_extract()

int zip_extract ( const char *  zipname,
const char *  dir,
int(*)(const char *filename, void *arg)  on_extract_entry,
void *  arg 
)
extern

Extracts a zip archive file into directory.

If on_extract_entry is not NULL, the callback will be called after successfully extracted each zip entry. Returning a negative value from the callback will cause abort and return an error. The last argument (void *arg) is optional, which you can use to pass data to the on_extract_entry callback.

Parameters
zipnamezip archive file.
diroutput directory.
on_extract_entryon extract callback.
argopaque pointer.
Returns
the return code - 0 on success, negative number (< 0) on error.

Definition at line 1601 of file zip.cpp.

1602 {
1603 mz_zip_archive zip_archive;
1604
1605 if (!zipname || !dir) {
1606 // Cannot parse zip archive name
1607 return ZIP_EINVZIPNAME;
1608 }
1609
1610 if (!memset(&zip_archive, 0, sizeof(mz_zip_archive))) {
1611 // Cannot memset zip archive
1612 return ZIP_EMEMSET;
1613 }
1614
1615 // Now try to open the archive.
1616 if (!mz_zip_reader_init_file(&zip_archive, zipname, 0)) {
1617 // Cannot initialize zip_archive reader
1618 return ZIP_ENOINIT;
1619 }
1620
1621 return zip_archive_extract(&zip_archive, dir, on_extract, arg);
1622}

References ZIP_ENOINIT.

◆ zip_is64()

int zip_is64 ( struct zip_t zip)
extern

Determines if the archive has a zip64 end of central directory headers.

Parameters
zipzip archive handler.
Returns
the return code - 1 (true), 0 (false), negative number (< 0) on error.

Definition at line 849 of file zip.cpp.

849 {
850 if (!zip || !zip->archive.m_pState) {
851 // zip_t handler or zip state is not initialized
852 return ZIP_ENOINIT;
853 }
854
855 return (int)zip->archive.m_pState->m_zip64;
856}

References ZIP_ENOINIT.

◆ zip_open()

struct zip_t * zip_open ( const char *  zipname,
int  level,
char  mode 
)
extern

Opens zip archive with compression level using the given mode.

Parameters
zipnamezip archive file name.
levelcompression level (0-9 are the standard zlib-style levels).
modefile access mode.
  • 'r': opens a file for reading/extracting (the file must exists).
  • 'w': creates an empty file for writing.
  • 'a': appends to an existing archive.
Returns
the zip archive handler or NULL on error

Definition at line 779 of file zip.cpp.

779 {
780 struct zip_t *zip = NULL;
781
782 if (!zipname || strlen(zipname) < 1) {
783 // zip_t archive name is empty or NULL
784 goto cleanup;
785 }
786
787 if (level < 0)
788 level = MZ_DEFAULT_LEVEL;
789 if ((level & 0xF) > MZ_UBER_COMPRESSION) {
790 // Wrong compression level
791 goto cleanup;
792 }
793
794 zip = (struct zip_t *)calloc((size_t)1, sizeof(struct zip_t));
795 if (!zip)
796 goto cleanup;
797
798 zip->level = (mz_uint)level;
799 switch (mode) {
800 case 'w':
801 // Create a new archive.
802 if (!mz_zip_writer_init_file(&(zip->archive), zipname, 0)) {
803 // Cannot initialize zip_archive writer
804 goto cleanup;
805 }
806 break;
807
808 case 'r':
809 case 'a':
810 case 'd':
811 if (!mz_zip_reader_init_file(
812 &(zip->archive), zipname,
813 zip->level | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY)) {
814 // An archive file does not exist or cannot initialize
815 // zip_archive reader
816 goto cleanup;
817 }
818 if ((mode == 'a' || mode == 'd') &&
819 !mz_zip_writer_init_from_reader(&(zip->archive), zipname)) {
820 mz_zip_reader_end(&(zip->archive));
821 goto cleanup;
822 }
823 break;
824
825 default:
826 goto cleanup;
827 }
828
829 return zip;
830
831cleanup:
832 CLEANUP(zip);
833 return NULL;
834}
Definition zip.cpp:88

◆ zip_stream_close()

void zip_stream_close ( struct zip_t zip)
extern

Close zip archive releases resources.

Parameters
zipzip archive handler.
Returns

Definition at line 1533 of file zip.cpp.

1533 {
1534 if (zip) {
1535 mz_zip_writer_end(&(zip->archive));
1536 mz_zip_reader_end(&(zip->archive));
1537 CLEANUP(zip);
1538 }
1539}

◆ zip_stream_copy()

ssize_t zip_stream_copy ( struct zip_t zip,
void **  buf,
ssize_t *  bufsize 
)
extern

Copy zip archive stream output buffer.

Parameters
zipzip archive handler.
bufoutput buffer. User should free buf.
bufsizeoutput buffer size (in bytes).
Returns
copy size

Definition at line 1517 of file zip.cpp.

1517 {
1518 if (!zip) {
1519 return ZIP_ENOINIT;
1520 }
1521
1522 zip_archive_finalize(&(zip->archive));
1523
1524 if (bufsize != NULL) {
1525 *bufsize = zip->archive.m_archive_size;
1526 }
1527 *buf = calloc(sizeof(unsigned char), zip->archive.m_archive_size);
1528 memcpy(*buf, zip->archive.m_pState->m_pMem, zip->archive.m_archive_size);
1529
1530 return zip->archive.m_archive_size;
1531}

References ZIP_ENOINIT.

◆ zip_stream_extract()

int zip_stream_extract ( const char *  stream,
size_t  size,
const char *  dir,
int(*)(const char *filename, void *arg)  on_extract,
void *  arg 
)
extern

Extracts a zip archive stream into directory.

If on_extract is not NULL, the callback will be called after successfully extracted each zip entry. Returning a negative value from the callback will cause abort and return an error. The last argument (void *arg) is optional, which you can use to pass data to the on_extract callback.

Parameters
streamzip archive stream.
sizestream size.
diroutput directory.
on_extracton extract callback.
argopaque pointer.
Returns
the return code - 0 on success, negative number (< 0) on error.

Definition at line 1461 of file zip.cpp.

1463 {
1464 mz_zip_archive zip_archive;
1465 if (!stream || !dir) {
1466 // Cannot parse zip archive stream
1467 return ZIP_ENOINIT;
1468 }
1469 if (!memset(&zip_archive, 0, sizeof(mz_zip_archive))) {
1470 // Cannot memset zip archive
1471 return ZIP_EMEMSET;
1472 }
1473 if (!mz_zip_reader_init_mem(&zip_archive, stream, size, 0)) {
1474 // Cannot initialize zip_archive reader
1475 return ZIP_ENOINIT;
1476 }
1477
1478 return zip_archive_extract(&zip_archive, dir, on_extract, arg);
1479}

References ZIP_ENOINIT.

◆ zip_stream_open()

struct zip_t * zip_stream_open ( const char *  stream,
size_t  size,
int  level,
char  mode 
)
extern

Opens zip archive stream into memory.

Parameters
streamzip archive stream.
sizestream size.
Returns
the zip archive handler or NULL on error

Definition at line 1481 of file zip.cpp.

1482 {
1483 struct zip_t *zip = (struct zip_t *)calloc((size_t)1, sizeof(struct zip_t));
1484 if (!zip) {
1485 return NULL;
1486 }
1487
1488 if (level < 0) {
1489 level = MZ_DEFAULT_LEVEL;
1490 }
1491 if ((level & 0xF) > MZ_UBER_COMPRESSION) {
1492 // Wrong compression level
1493 goto cleanup;
1494 }
1495 zip->level = (mz_uint)level;
1496
1497 if ((stream != NULL) && (size > 0) && (mode == 'r')) {
1498 if (!mz_zip_reader_init_mem(&(zip->archive), stream, size, 0)) {
1499 goto cleanup;
1500 }
1501 } else if ((stream == NULL) && (size == 0) && (mode == 'w')) {
1502 // Create a new archive.
1503 if (!mz_zip_writer_init_heap(&(zip->archive), 0, 1024)) {
1504 // Cannot initialize zip_archive writer
1505 goto cleanup;
1506 }
1507 } else {
1508 goto cleanup;
1509 }
1510 return zip;
1511
1512cleanup:
1513 CLEANUP(zip);
1514 return NULL;
1515}

◆ zip_strerror()

const char * zip_strerror ( int  errnum)
extern

Looks up the error message string coresponding to an error number.

Parameters
errnumerror number
Returns
error message string coresponding to errnum or NULL if error is not found.

Definition at line 140 of file zip.cpp.

140 {
141 errnum = -errnum;
142 if (errnum <= 0 || errnum >= 30) {
143 return NULL;
144 }
145
146 return zip_errlist[errnum];
147}