zip/src/zip.c

727 lines
20 KiB
C
Raw Normal View History

2015-03-23 15:08:14 -07:00
/*
2017-02-19 00:31:45 +01:00
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
2015-03-23 15:08:14 -07:00
#include "zip.h"
2017-02-14 20:05:54 +01:00
#include "miniz.h"
#include <errno.h>
#include <sys/stat.h>
2017-02-14 23:15:19 +01:00
#include <time.h>
2015-03-23 15:08:14 -07:00
2017-02-14 23:15:19 +01:00
#if defined _WIN32 || defined __WIN32__
/* Win32, DOS */
#include <direct.h>
#define MKDIR(DIRNAME) _mkdir(DIRNAME)
#define STRCLONE(STR) ((STR) ? _strdup(STR) : NULL)
2017-02-14 20:05:54 +01:00
#define HAS_DEVICE(P) \
((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) && \
(P)[1] == ':')
#define FILESYSTEM_PREFIX_LEN(P) (HAS_DEVICE(P) ? 2 : 0)
2015-03-24 02:33:15 -07:00
#define ISSLASH(C) ((C) == '/' || (C) == '\\')
2017-02-14 23:15:19 +01:00
#else
#define MKDIR(DIRNAME) mkdir(DIRNAME, 0755)
#define STRCLONE(STR) ((STR) ? strdup(STR) : NULL)
2015-03-23 15:08:14 -07:00
#endif
#ifndef FILESYSTEM_PREFIX_LEN
2015-03-24 02:33:15 -07:00
#define FILESYSTEM_PREFIX_LEN(P) 0
2015-03-23 15:08:14 -07:00
#endif
#ifndef ISSLASH
2015-03-24 02:33:15 -07:00
#define ISSLASH(C) ((C) == '/')
2015-03-23 15:08:14 -07:00
#endif
2017-02-19 00:31:45 +01:00
#define CLEANUP(ptr) \
2017-02-14 20:05:54 +01:00
do { \
if (ptr) { \
free((void *)ptr); \
ptr = NULL; \
} \
} while (0)
2015-03-24 02:33:15 -07:00
2015-08-19 02:51:05 -07:00
static char *basename(const char *name) {
char const *p;
2017-02-14 20:05:54 +01:00
char const *base = name += FILESYSTEM_PREFIX_LEN(name);
2015-03-23 15:08:14 -07:00
int all_slashes = 1;
for (p = name; *p; p++) {
2015-08-19 02:51:05 -07:00
if (ISSLASH(*p))
2015-03-23 15:08:14 -07:00
base = p + 1;
else
all_slashes = 0;
}
/* If NAME is all slashes, arrange to return `/'. */
2017-02-14 20:05:54 +01:00
if (*base == '\0' && ISSLASH(*name) && all_slashes) --base;
2015-03-23 15:08:14 -07:00
return (char *)base;
}
2015-08-19 02:51:05 -07:00
static int mkpath(const char *path) {
char const *p;
2017-02-14 20:05:54 +01:00
char npath[MAX_PATH + 1] = {0};
2015-08-19 02:51:05 -07:00
int len = 0;
for (p = path; *p && len < MAX_PATH; p++) {
if (ISSLASH(*p) && len > 0) {
2017-02-14 23:15:19 +01:00
if (MKDIR(npath) == -1)
2017-02-14 20:05:54 +01:00
if (errno != EEXIST) return -1;
2015-08-19 02:51:05 -07:00
}
npath[len++] = *p;
}
return 0;
}
static char *strrpl(const char *str, size_t n, char oldchar, char newchar) {
2018-01-08 11:33:02 +01:00
char *rpl = (char *)calloc((1 + n), sizeof(char));
char *begin = rpl;
char c;
2018-01-08 11:33:02 +01:00
size_t i;
for(i = 0; (i < n) && (c = *str++); ++i) {
2017-11-07 20:20:18 +01:00
if (c == oldchar) {
c = newchar;
}
*rpl++ = c;
}
return begin;
}
2015-03-23 15:08:14 -07:00
struct zip_entry_t {
2017-02-19 00:31:45 +01:00
int index;
2017-02-14 20:05:54 +01:00
const char *name;
mz_uint64 uncomp_size;
mz_uint64 comp_size;
mz_uint32 uncomp_crc32;
mz_uint64 offset;
mz_uint8 header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
mz_uint64 header_offset;
mz_uint16 method;
2015-03-23 15:08:14 -07:00
mz_zip_writer_add_state state;
2017-02-14 20:05:54 +01:00
tdefl_compressor comp;
2015-03-23 15:08:14 -07:00
};
struct zip_t {
2017-02-14 20:05:54 +01:00
mz_zip_archive archive;
mz_uint level;
struct zip_entry_t entry;
2015-03-23 15:08:14 -07:00
};
2017-02-19 00:31:45 +01:00
struct zip_t *zip_open(const char *zipname, int level, char mode) {
2017-02-14 20:05:54 +01:00
struct zip_t *zip = NULL;
2015-03-24 14:44:50 -07:00
2015-03-23 15:08:14 -07:00
if (!zipname || strlen(zipname) < 1) {
// zip_t archive name is empty or NULL
2017-02-19 00:31:45 +01:00
goto cleanup;
2015-03-23 15:08:14 -07:00
}
if (level < 0) level = MZ_DEFAULT_LEVEL;
if ((level & 0xF) > MZ_UBER_COMPRESSION) {
// Wrong compression level
2017-02-19 00:31:45 +01:00
goto cleanup;
2015-03-23 15:08:14 -07:00
}
2015-03-24 14:44:50 -07:00
zip = (struct zip_t *)calloc((size_t)1, sizeof(struct zip_t));
2017-02-19 00:31:45 +01:00
if (!zip) goto cleanup;
zip->level = level;
switch (mode) {
case 'w':
// Create a new archive.
if (!mz_zip_writer_init_file(&(zip->archive), zipname, 0)) {
// Cannot initialize zip_archive writer
goto cleanup;
}
break;
2015-03-24 02:33:15 -07:00
2017-02-19 00:31:45 +01:00
case 'r':
case 'a':
2017-02-14 20:05:54 +01:00
if (!mz_zip_reader_init_file(
&(zip->archive), zipname,
level | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY)) {
2017-02-19 00:31:45 +01:00
// An archive file does not exist or cannot initialize
// zip_archive reader
goto cleanup;
2015-03-24 02:33:15 -07:00
}
2017-02-19 00:31:45 +01:00
if (mode == 'a' &&
!mz_zip_writer_init_from_reader(&(zip->archive), zipname)) {
2015-03-24 02:33:15 -07:00
mz_zip_reader_end(&(zip->archive));
2017-02-19 00:31:45 +01:00
goto cleanup;
2015-03-24 02:33:15 -07:00
}
2017-02-19 00:31:45 +01:00
break;
default:
goto cleanup;
2015-03-23 15:08:14 -07:00
}
return zip;
2017-02-19 00:31:45 +01:00
cleanup:
CLEANUP(zip);
return NULL;
2015-03-23 15:08:14 -07:00
}
2015-03-24 14:44:50 -07:00
void zip_close(struct zip_t *zip) {
2015-03-23 15:08:14 -07:00
if (zip) {
2017-02-14 20:05:54 +01:00
// Always finalize, even if adding failed for some reason, so we have a
// valid central directory.
2015-03-23 15:08:14 -07:00
mz_zip_writer_finalize_archive(&(zip->archive));
2017-02-19 00:31:45 +01:00
2015-03-23 15:08:14 -07:00
mz_zip_writer_end(&(zip->archive));
2017-02-19 00:31:45 +01:00
mz_zip_reader_end(&(zip->archive));
2015-03-23 15:08:14 -07:00
2017-02-19 00:31:45 +01:00
CLEANUP(zip);
2015-03-23 15:08:14 -07:00
}
}
2015-03-24 14:44:50 -07:00
int zip_entry_open(struct zip_t *zip, const char *entryname) {
size_t entrylen = 0;
2017-02-14 20:05:54 +01:00
mz_zip_archive *pzip = NULL;
mz_uint num_alignment_padding_bytes, level;
2015-03-24 14:44:50 -07:00
2017-02-14 20:05:54 +01:00
if (!zip || !entryname) {
2015-03-23 15:08:14 -07:00
return -1;
}
2015-03-24 15:30:04 -07:00
2015-03-24 14:44:50 -07:00
entrylen = strlen(entryname);
2015-03-23 15:08:14 -07:00
if (entrylen < 1) {
return -1;
}
/*
.ZIP File Format Specification Version: 6.3.3
4.4.17.1 The name of the file, with optional relative path.
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. If input came from standard
input, there is no file name field.
*/
zip->entry.name = strrpl(entryname, entrylen, '\\', '/');
2015-03-23 15:08:14 -07:00
if (!zip->entry.name) {
// Cannot parse zip entry name
return -1;
}
pzip = &(zip->archive);
2018-01-08 11:33:02 +01:00
if (pzip->m_zip_mode == MZ_ZIP_MODE_READING) {
zip->entry.index = mz_zip_reader_locate_file(pzip, zip->entry.name, NULL, 0);
if (zip->entry.index < 0) {
goto cleanup;
}
return 0;
}
zip->entry.index = zip->archive.m_total_files;
2015-03-23 15:08:14 -07:00
zip->entry.comp_size = 0;
zip->entry.uncomp_size = 0;
zip->entry.uncomp_crc32 = MZ_CRC32_INIT;
zip->entry.offset = zip->archive.m_archive_size;
zip->entry.header_offset = zip->archive.m_archive_size;
2017-02-14 20:05:54 +01:00
memset(zip->entry.header, 0,
MZ_ZIP_LOCAL_DIR_HEADER_SIZE * sizeof(mz_uint8));
2015-03-23 15:08:14 -07:00
zip->entry.method = 0;
2017-02-14 20:05:54 +01:00
num_alignment_padding_bytes =
mz_zip_writer_compute_padding_needed_for_file_alignment(pzip);
2015-03-23 15:08:14 -07:00
if (!pzip->m_pState || (pzip->m_zip_mode != MZ_ZIP_MODE_WRITING)) {
// Wrong zip mode
goto cleanup;
2015-03-23 15:08:14 -07:00
}
if (zip->level & MZ_ZIP_FLAG_COMPRESSED_DATA) {
// Wrong zip compression level
goto cleanup;
2015-03-23 15:08:14 -07:00
}
// no zip64 support yet
2017-02-14 20:05:54 +01:00
if ((pzip->m_total_files == 0xFFFF) ||
((pzip->m_archive_size + num_alignment_padding_bytes +
MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE +
entrylen) > 0xFFFFFFFF)) {
2015-03-23 15:08:14 -07:00
// No zip64 support yet
goto cleanup;
2015-03-23 15:08:14 -07:00
}
2017-02-14 20:05:54 +01:00
if (!mz_zip_writer_write_zeros(
pzip, zip->entry.offset,
num_alignment_padding_bytes + sizeof(zip->entry.header))) {
2015-03-23 15:08:14 -07:00
// Cannot memset zip entry header
goto cleanup;
2015-03-23 15:08:14 -07:00
}
zip->entry.header_offset += num_alignment_padding_bytes;
2017-02-14 20:05:54 +01:00
if (pzip->m_file_offset_alignment) {
MZ_ASSERT((zip->entry.header_offset &
(pzip->m_file_offset_alignment - 1)) == 0);
}
zip->entry.offset +=
num_alignment_padding_bytes + sizeof(zip->entry.header);
2015-03-23 15:08:14 -07:00
2017-02-14 20:05:54 +01:00
if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, zip->entry.name,
entrylen) != entrylen) {
2015-03-23 15:08:14 -07:00
// Cannot write data to zip entry
goto cleanup;
2015-03-23 15:08:14 -07:00
}
zip->entry.offset += entrylen;
2015-03-24 14:44:50 -07:00
level = zip->level & 0xF;
2015-03-23 15:08:14 -07:00
if (level) {
zip->entry.state.m_pZip = pzip;
zip->entry.state.m_cur_archive_file_ofs = zip->entry.offset;
zip->entry.state.m_comp_size = 0;
2017-02-14 20:05:54 +01:00
if (tdefl_init(&(zip->entry.comp), mz_zip_writer_add_put_buf_callback,
&(zip->entry.state),
tdefl_create_comp_flags_from_zip_params(
level, -15, MZ_DEFAULT_STRATEGY)) !=
TDEFL_STATUS_OKAY) {
2015-03-23 15:08:14 -07:00
// Cannot initialize the zip compressor
goto cleanup;
2015-03-23 15:08:14 -07:00
}
}
return 0;
cleanup:
CLEANUP(zip->entry.name);
return -1;
2015-03-23 15:08:14 -07:00
}
2018-01-08 11:33:02 +01:00
int zip_entry_openbyindex(struct zip_t *zip, int index) {
mz_zip_archive *pZip = NULL;
if (!zip) {
// zip_t handler is not initialized
return -1;
}
pZip = &(zip->archive);
if (pZip->m_zip_mode != MZ_ZIP_MODE_READING) {
// open by index requires readonly mode
return -1;
}
2018-01-11 23:20:41 +01:00
if (index < 0 || (mz_uint)index >= pZip->m_total_files) {
2018-01-08 11:33:02 +01:00
// index out of range
return -1;
}
const mz_uint8 *pHeader = &MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, index));
if (!pHeader) {
// cannot find header in central directory
return -1;
}
mz_uint namelen = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_FILENAME_LEN_OFS);
const char *pFilename = (const char *)pHeader + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
/*
.ZIP File Format Specification Version: 6.3.3
4.4.17.1 The name of the file, with optional relative path.
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. If input came from standard
input, there is no file name field.
*/
zip->entry.name = strrpl(pFilename, namelen, '\\', '/');
if (!zip->entry.name) {
// local entry name is NULL
return -1;
}
zip->entry.index = index;
return 0;
}
2015-03-24 14:44:50 -07:00
int zip_entry_close(struct zip_t *zip) {
2017-02-14 20:05:54 +01:00
mz_zip_archive *pzip = NULL;
mz_uint level;
tdefl_status done;
mz_uint16 entrylen;
2015-03-24 14:44:50 -07:00
time_t t;
struct tm *tm;
mz_uint16 dos_time, dos_date;
2017-02-19 00:31:45 +01:00
int status = -1;
2015-03-24 14:44:50 -07:00
2015-03-23 15:08:14 -07:00
if (!zip) {
// zip_t handler is not initialized
goto cleanup;
2015-03-23 15:08:14 -07:00
}
2018-01-08 11:33:02 +01:00
pzip = &(zip->archive);
if (pzip->m_zip_mode == MZ_ZIP_MODE_READING) {
status = 0;
goto cleanup;
2017-02-19 00:31:45 +01:00
}
2015-03-24 14:44:50 -07:00
level = zip->level & 0xF;
2015-03-23 15:08:14 -07:00
if (level) {
2015-03-24 14:44:50 -07:00
done = tdefl_compress_buffer(&(zip->entry.comp), "", 0, TDEFL_FINISH);
2015-03-23 15:08:14 -07:00
if (done != TDEFL_STATUS_DONE && done != TDEFL_STATUS_OKAY) {
// Cannot flush compressed buffer
2017-02-19 00:31:45 +01:00
goto cleanup;
2015-03-23 15:08:14 -07:00
}
zip->entry.comp_size = zip->entry.state.m_comp_size;
zip->entry.offset = zip->entry.state.m_cur_archive_file_ofs;
zip->entry.method = MZ_DEFLATED;
}
2015-03-24 14:44:50 -07:00
entrylen = (mz_uint16)strlen(zip->entry.name);
t = time(NULL);
tm = localtime(&t);
2017-02-14 20:05:54 +01:00
dos_time = (mz_uint16)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) +
((tm->tm_sec) >> 1));
dos_date = (mz_uint16)(((tm->tm_year + 1900 - 1980) << 9) +
((tm->tm_mon + 1) << 5) + tm->tm_mday);
2015-03-23 15:08:14 -07:00
// no zip64 support yet
2017-02-14 20:05:54 +01:00
if ((zip->entry.comp_size > 0xFFFFFFFF) ||
(zip->entry.offset > 0xFFFFFFFF)) {
2015-03-23 15:08:14 -07:00
// No zip64 support, yet
2017-02-19 00:31:45 +01:00
goto cleanup;
2015-03-23 15:08:14 -07:00
}
2017-02-14 20:05:54 +01:00
if (!mz_zip_writer_create_local_dir_header(
pzip, zip->entry.header, entrylen, 0, zip->entry.uncomp_size,
zip->entry.comp_size, zip->entry.uncomp_crc32, zip->entry.method, 0,
dos_time, dos_date)) {
2015-03-23 15:08:14 -07:00
// Cannot create zip entry header
2017-02-19 00:31:45 +01:00
goto cleanup;
2015-03-23 15:08:14 -07:00
}
2017-02-14 20:05:54 +01:00
if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.header_offset,
zip->entry.header, sizeof(zip->entry.header)) !=
sizeof(zip->entry.header)) {
2015-03-23 15:08:14 -07:00
// Cannot write zip entry header
2017-02-19 00:31:45 +01:00
goto cleanup;
2015-03-23 15:08:14 -07:00
}
2017-02-14 20:05:54 +01:00
if (!mz_zip_writer_add_to_central_dir(
pzip, zip->entry.name, entrylen, NULL, 0, "", 0,
zip->entry.uncomp_size, zip->entry.comp_size,
zip->entry.uncomp_crc32, zip->entry.method, 0, dos_time, dos_date,
zip->entry.header_offset, 0)) {
2015-03-23 15:08:14 -07:00
// Cannot write to zip central dir
2017-02-19 00:31:45 +01:00
goto cleanup;
2015-03-23 15:08:14 -07:00
}
pzip->m_total_files++;
pzip->m_archive_size = zip->entry.offset;
2017-02-19 00:31:45 +01:00
status = 0;
2015-03-23 15:08:14 -07:00
2017-02-19 00:31:45 +01:00
cleanup:
CLEANUP(zip->entry.name);
return status;
2015-03-23 15:08:14 -07:00
}
const char *zip_entry_name(struct zip_t *zip) {
if (!zip) {
// zip_t handler is not initialized
return NULL;
}
return zip->entry.name;
}
int zip_entry_index(struct zip_t *zip) {
if (!zip) {
// zip_t handler is not initialized
return -1;
}
return zip->entry.index;
}
2018-01-08 11:33:02 +01:00
int zip_entry_isdir(struct zip_t *zip) {
if (!zip) {
// zip_t handler is not initialized
return -1;
}
if (zip->entry.index < 0) {
// zip entry is not opened
return -1;
}
return (int)mz_zip_reader_is_file_a_directory(&zip->archive, (mz_uint)zip->entry.index);
}
2015-03-24 14:44:50 -07:00
int zip_entry_write(struct zip_t *zip, const void *buf, size_t bufsize) {
2017-02-14 20:05:54 +01:00
mz_uint level;
mz_zip_archive *pzip = NULL;
tdefl_status status;
2015-03-24 14:44:50 -07:00
2015-03-23 15:08:14 -07:00
if (!zip) {
// zip_t handler is not initialized
return -1;
}
2015-03-24 14:44:50 -07:00
pzip = &(zip->archive);
2015-03-23 15:08:14 -07:00
if (buf && bufsize > 0) {
zip->entry.uncomp_size += bufsize;
2017-02-14 20:05:54 +01:00
zip->entry.uncomp_crc32 = (mz_uint32)mz_crc32(
zip->entry.uncomp_crc32, (const mz_uint8 *)buf, bufsize);
2015-03-23 15:08:14 -07:00
2017-02-14 20:05:54 +01:00
level = zip->level & 0xF;
2015-03-23 15:08:14 -07:00
if (!level) {
2017-02-14 20:05:54 +01:00
if ((pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, buf,
bufsize) != bufsize)) {
2015-03-23 15:08:14 -07:00
// Cannot write buffer
return -1;
}
zip->entry.offset += bufsize;
zip->entry.comp_size += bufsize;
} else {
2017-02-14 20:05:54 +01:00
status = tdefl_compress_buffer(&(zip->entry.comp), buf, bufsize,
TDEFL_NO_FLUSH);
2015-03-23 15:08:14 -07:00
if (status != TDEFL_STATUS_DONE && status != TDEFL_STATUS_OKAY) {
// Cannot compress buffer
return -1;
}
}
}
return 0;
}
2015-03-24 14:44:50 -07:00
int zip_entry_fwrite(struct zip_t *zip, const char *filename) {
2017-02-14 20:05:54 +01:00
int status = 0;
2015-03-24 14:44:50 -07:00
size_t n = 0;
2017-02-14 20:05:54 +01:00
FILE *stream = NULL;
mz_uint8 buf[MZ_ZIP_MAX_IO_BUF_SIZE] = {0};
2015-03-24 14:44:50 -07:00
2015-03-23 15:08:14 -07:00
if (!zip) {
// zip_t handler is not initialized
return -1;
}
2015-03-24 14:44:50 -07:00
stream = fopen(filename, "rb");
2015-03-23 15:08:14 -07:00
if (!stream) {
// Cannot open filename
return -1;
}
2017-02-14 20:05:54 +01:00
while ((n = fread(buf, sizeof(mz_uint8), MZ_ZIP_MAX_IO_BUF_SIZE, stream)) >
0) {
2015-03-23 15:08:14 -07:00
if (zip_entry_write(zip, buf, n) < 0) {
status = -1;
break;
}
}
fclose(stream);
return status;
}
2017-02-19 00:31:45 +01:00
int zip_entry_read(struct zip_t *zip, void **buf, size_t *bufsize) {
mz_zip_archive *pzip = NULL;
mz_uint idx;
if (!zip) {
// zip_t handler is not initialized
return -1;
}
2018-01-08 11:33:02 +01:00
pzip = &(zip->archive);
if (pzip->m_zip_mode != MZ_ZIP_MODE_READING || zip->entry.index < 0) {
2017-02-19 00:31:45 +01:00
// the entry is not found or we do not have read access
return -1;
}
idx = (mz_uint)zip->entry.index;
if (mz_zip_reader_is_file_a_directory(pzip, idx)) {
// the entry is a directory
return -1;
}
*buf = mz_zip_reader_extract_to_heap(pzip, idx, bufsize, 0);
return (*buf) ? 0 : -1;
}
int zip_entry_fread(struct zip_t *zip, const char *filename) {
mz_zip_archive *pzip = NULL;
mz_uint idx;
if (!zip) {
// zip_t handler is not initialized
return -1;
}
2018-01-08 11:33:02 +01:00
pzip = &(zip->archive);
if (pzip->m_zip_mode != MZ_ZIP_MODE_READING || zip->entry.index < 0) {
2017-02-19 00:31:45 +01:00
// the entry is not found or we do not have read access
return -1;
}
idx = (mz_uint)zip->entry.index;
if (mz_zip_reader_is_file_a_directory(pzip, idx)) {
// the entry is a directory
return -1;
}
return (mz_zip_reader_extract_to_file(pzip, idx, filename, 0)) ? 0 : -1;
}
int zip_entry_extract(struct zip_t *zip,
size_t (*on_extract)(void *arg, unsigned long long offset,
const void *buf, size_t bufsize),
void *arg) {
mz_zip_archive *pzip = NULL;
mz_uint idx;
if (!zip) {
// zip_t handler is not initialized
return -1;
}
2018-01-08 11:33:02 +01:00
pzip = &(zip->archive);
if (pzip->m_zip_mode != MZ_ZIP_MODE_READING || zip->entry.index < 0) {
// the entry is not found or we do not have read access
return -1;
}
idx = (mz_uint)zip->entry.index;
2018-01-08 11:33:02 +01:00
return (mz_zip_reader_extract_to_callback(pzip, idx, on_extract, arg, 0)) ? 0 : -1;
}
2018-01-06 02:39:55 +01:00
int zip_total_entries(struct zip_t *zip) {
if (!zip) {
// zip_t handler is not initialized
return -1;
}
return zip->archive.m_total_files;
}
2015-03-24 03:19:14 -07:00
int zip_create(const char *zipname, const char *filenames[], size_t len) {
2017-02-14 20:05:54 +01:00
int status = 0;
size_t i;
mz_zip_archive zip_archive;
2015-03-24 14:44:50 -07:00
2015-03-24 03:19:14 -07:00
if (!zipname || strlen(zipname) < 1) {
// zip_t archive name is empty or NULL
return -1;
}
// Create a new archive.
if (!memset(&(zip_archive), 0, sizeof(zip_archive))) {
// Cannot memset zip archive
return -1;
}
if (!mz_zip_writer_init_file(&zip_archive, zipname, 0)) {
// Cannot initialize zip_archive writer
2015-03-23 15:08:14 -07:00
return -1;
}
2015-03-24 14:44:50 -07:00
for (i = 0; i < len; ++i) {
2015-03-23 15:08:14 -07:00
const char *name = filenames[i];
2015-03-24 03:19:14 -07:00
if (!name) {
status = -1;
break;
}
2015-03-23 15:08:14 -07:00
2017-02-14 20:05:54 +01:00
if (!mz_zip_writer_add_file(&zip_archive, basename(name), name, "", 0,
ZIP_DEFAULT_COMPRESSION_LEVEL)) {
2015-03-23 15:08:14 -07:00
// Cannot add file to zip_archive
2015-03-24 03:19:14 -07:00
status = -1;
break;
2015-03-23 15:08:14 -07:00
}
}
2015-03-24 03:19:14 -07:00
mz_zip_writer_finalize_archive(&zip_archive);
mz_zip_writer_end(&zip_archive);
return status;
2015-03-23 15:08:14 -07:00
}
2017-02-14 20:05:54 +01:00
int zip_extract(const char *zipname, const char *dir,
int (*on_extract)(const char *filename, void *arg), void *arg) {
2017-02-19 00:31:45 +01:00
int status = -1;
2015-03-24 15:30:04 -07:00
mz_uint i, n;
2017-02-14 20:05:54 +01:00
char path[MAX_PATH + 1] = {0};
mz_zip_archive zip_archive;
mz_zip_archive_file_stat info;
2015-03-24 14:44:50 -07:00
size_t dirlen = 0;
2015-03-23 15:08:14 -07:00
if (!memset(&(zip_archive), 0, sizeof(zip_archive))) {
// Cannot memset zip archive
return -1;
}
2015-03-24 14:44:50 -07:00
2015-03-23 15:08:14 -07:00
if (!zipname || !dir) {
// Cannot parse zip archive name
return -1;
}
2017-02-14 20:05:54 +01:00
dirlen = strlen(dir);
2015-03-24 14:44:50 -07:00
if (dirlen + 1 > MAX_PATH) {
return -1;
}
2015-03-23 15:08:14 -07:00
// Now try to open the archive.
if (!mz_zip_reader_init_file(&zip_archive, zipname, 0)) {
// Cannot initialize zip_archive reader
2017-02-19 00:31:45 +01:00
return -1;
2015-03-23 15:08:14 -07:00
}
strcpy(path, dir);
2017-02-14 20:05:54 +01:00
if (!ISSLASH(path[dirlen - 1])) {
2017-02-14 23:15:19 +01:00
#if defined _WIN32 || defined __WIN32__
2015-03-23 15:08:14 -07:00
path[dirlen] = '\\';
#else
path[dirlen] = '/';
#endif
++dirlen;
}
// Get and print information about each file in the archive.
2015-03-24 15:30:04 -07:00
n = mz_zip_reader_get_num_files(&zip_archive);
2015-03-24 14:44:50 -07:00
for (i = 0; i < n; ++i) {
2015-03-23 15:08:14 -07:00
if (!mz_zip_reader_file_stat(&zip_archive, i, &info)) {
// Cannot get information about zip archive;
2017-02-19 00:31:45 +01:00
goto out;
2015-03-23 15:08:14 -07:00
}
strncpy(&path[dirlen], info.m_filename, MAX_PATH - dirlen);
2015-08-19 02:51:05 -07:00
if (mkpath(path) < 0) {
// Cannot make a path
2017-02-19 00:31:45 +01:00
goto out;
2015-08-19 02:51:05 -07:00
}
if (!mz_zip_reader_is_file_a_directory(&zip_archive, i)) {
if (!mz_zip_reader_extract_to_file(&zip_archive, i, path, 0)) {
// Cannot extract zip archive to file
goto out;
}
2015-03-23 15:08:14 -07:00
}
if (on_extract) {
if (on_extract(path, arg) < 0) {
2017-02-19 00:31:45 +01:00
goto out;
2015-03-23 15:08:14 -07:00
}
}
}
2017-02-19 00:31:45 +01:00
status = 0;
2015-03-23 15:08:14 -07:00
2017-02-19 00:31:45 +01:00
out:
2015-03-23 15:08:14 -07:00
// Close the archive, freeing any resources it was using
if (!mz_zip_reader_end(&zip_archive)) {
// Cannot end zip reader
status = -1;
}
return status;
}