mirror of
https://github.com/QuasarApp/zip.git
synced 2025-05-16 21:39:33 +00:00
Add more stats functions (#37)
This commit is contained in:
parent
06d079b6ab
commit
18129e7418
@ -146,6 +146,8 @@ for (i = 0; i < n; ++i) {
|
|||||||
{
|
{
|
||||||
const char *name = zip_entry_name(zip);
|
const char *name = zip_entry_name(zip);
|
||||||
int isdir = zip_entry_isdir(zip);
|
int isdir = zip_entry_isdir(zip);
|
||||||
|
unsigned long long size = zip_entry_size(zip);
|
||||||
|
unsigned int crc32 = zip_entry_crc32(zip);
|
||||||
}
|
}
|
||||||
zip_entry_close(zip);
|
zip_entry_close(zip);
|
||||||
}
|
}
|
||||||
|
21
src/zip.c
21
src/zip.c
@ -192,6 +192,7 @@ int zip_entry_open(struct zip_t *zip, const char *entryname) {
|
|||||||
size_t entrylen = 0;
|
size_t entrylen = 0;
|
||||||
mz_zip_archive *pzip = NULL;
|
mz_zip_archive *pzip = NULL;
|
||||||
mz_uint num_alignment_padding_bytes, level;
|
mz_uint num_alignment_padding_bytes, level;
|
||||||
|
mz_zip_archive_file_stat stats;
|
||||||
|
|
||||||
if (!zip || !entryname) {
|
if (!zip || !entryname) {
|
||||||
return -1;
|
return -1;
|
||||||
@ -225,6 +226,18 @@ int zip_entry_open(struct zip_t *zip, const char *entryname) {
|
|||||||
if (zip->entry.index < 0) {
|
if (zip->entry.index < 0) {
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!mz_zip_reader_file_stat(pzip, zip->entry.index, &stats)) {
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
zip->entry.comp_size = stats.m_comp_size;
|
||||||
|
zip->entry.uncomp_size = stats.m_uncomp_size;
|
||||||
|
zip->entry.uncomp_crc32 = stats.m_crc32;
|
||||||
|
zip->entry.offset = stats.m_central_dir_ofs;
|
||||||
|
zip->entry.header_offset = stats.m_local_header_ofs;
|
||||||
|
zip->entry.method = stats.m_method;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -463,6 +476,14 @@ int zip_entry_isdir(struct zip_t *zip) {
|
|||||||
return (int)mz_zip_reader_is_file_a_directory(&zip->archive, (mz_uint)zip->entry.index);
|
return (int)mz_zip_reader_is_file_a_directory(&zip->archive, (mz_uint)zip->entry.index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned long long zip_entry_size(struct zip_t *zip) {
|
||||||
|
return zip->entry.uncomp_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int zip_entry_crc32(struct zip_t *zip) {
|
||||||
|
return zip->entry.uncomp_crc32;
|
||||||
|
}
|
||||||
|
|
||||||
int zip_entry_write(struct zip_t *zip, const void *buf, size_t bufsize) {
|
int zip_entry_write(struct zip_t *zip, const void *buf, size_t bufsize) {
|
||||||
mz_uint level;
|
mz_uint level;
|
||||||
mz_zip_archive *pzip = NULL;
|
mz_zip_archive *pzip = NULL;
|
||||||
|
22
src/zip.h
22
src/zip.h
@ -132,6 +132,28 @@ extern int zip_entry_index(struct zip_t *zip);
|
|||||||
*/
|
*/
|
||||||
extern int zip_entry_isdir(struct zip_t *zip);
|
extern int zip_entry_isdir(struct zip_t *zip);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Returns an uncompressed size of the current zip entry.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
zip: zip archive handler.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The uncompressed size in bytes.
|
||||||
|
*/
|
||||||
|
extern unsigned long long zip_entry_size(struct zip_t *zip);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Returns CRC-32 checksum of the current zip entry.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
zip: zip archive handler.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The CRC-32 checksum.
|
||||||
|
*/
|
||||||
|
extern unsigned int zip_entry_crc32(struct zip_t *zip);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Compresses an input buffer for the current zip entry.
|
Compresses an input buffer for the current zip entry.
|
||||||
|
|
||||||
|
23
test/test.c
23
test/test.c
@ -7,7 +7,9 @@
|
|||||||
|
|
||||||
#define ZIPNAME "test.zip"
|
#define ZIPNAME "test.zip"
|
||||||
#define TESTDATA1 "Some test data 1...\0"
|
#define TESTDATA1 "Some test data 1...\0"
|
||||||
|
#define CRC32DATA1 2220805626
|
||||||
#define TESTDATA2 "Some test data 2...\0"
|
#define TESTDATA2 "Some test data 2...\0"
|
||||||
|
#define CRC32DATA2 2532008468
|
||||||
|
|
||||||
static int total_entries = 0;
|
static int total_entries = 0;
|
||||||
|
|
||||||
@ -19,6 +21,8 @@ static void test_write(void) {
|
|||||||
assert(0 == zip_entry_write(zip, TESTDATA1, strlen(TESTDATA1)));
|
assert(0 == zip_entry_write(zip, TESTDATA1, strlen(TESTDATA1)));
|
||||||
assert(0 == strcmp(zip_entry_name(zip), "test/test-1.txt"));
|
assert(0 == strcmp(zip_entry_name(zip), "test/test-1.txt"));
|
||||||
assert(total_entries == zip_entry_index(zip));
|
assert(total_entries == zip_entry_index(zip));
|
||||||
|
assert(strlen(TESTDATA1) == zip_entry_size(zip));
|
||||||
|
assert(CRC32DATA1 == zip_entry_crc32(zip));
|
||||||
++total_entries;
|
++total_entries;
|
||||||
assert(0 == zip_entry_close(zip));
|
assert(0 == zip_entry_close(zip));
|
||||||
|
|
||||||
@ -33,17 +37,26 @@ static void test_append(void) {
|
|||||||
assert(0 == strcmp(zip_entry_name(zip), "test/test-2.txt"));
|
assert(0 == strcmp(zip_entry_name(zip), "test/test-2.txt"));
|
||||||
assert(total_entries == zip_entry_index(zip));
|
assert(total_entries == zip_entry_index(zip));
|
||||||
assert(0 == zip_entry_write(zip, TESTDATA2, strlen(TESTDATA2)));
|
assert(0 == zip_entry_write(zip, TESTDATA2, strlen(TESTDATA2)));
|
||||||
|
assert(strlen(TESTDATA2) == zip_entry_size(zip));
|
||||||
|
assert(CRC32DATA2 == zip_entry_crc32(zip));
|
||||||
|
|
||||||
++total_entries;
|
++total_entries;
|
||||||
assert(0 == zip_entry_close(zip));
|
assert(0 == zip_entry_close(zip));
|
||||||
|
|
||||||
assert(0 == zip_entry_open(zip, "test\\empty/"));
|
assert(0 == zip_entry_open(zip, "test\\empty/"));
|
||||||
assert(0 == strcmp(zip_entry_name(zip), "test/empty/"));
|
assert(0 == strcmp(zip_entry_name(zip), "test/empty/"));
|
||||||
|
assert(0 == zip_entry_size(zip));
|
||||||
|
assert(0 == zip_entry_crc32(zip));
|
||||||
|
|
||||||
assert(total_entries == zip_entry_index(zip));
|
assert(total_entries == zip_entry_index(zip));
|
||||||
++total_entries;
|
++total_entries;
|
||||||
assert(0 == zip_entry_close(zip));
|
assert(0 == zip_entry_close(zip));
|
||||||
|
|
||||||
assert(0 == zip_entry_open(zip, "empty/"));
|
assert(0 == zip_entry_open(zip, "empty/"));
|
||||||
assert(0 == strcmp(zip_entry_name(zip), "empty/"));
|
assert(0 == strcmp(zip_entry_name(zip), "empty/"));
|
||||||
|
assert(0 == zip_entry_size(zip));
|
||||||
|
assert(0 == zip_entry_crc32(zip));
|
||||||
|
|
||||||
assert(total_entries == zip_entry_index(zip));
|
assert(total_entries == zip_entry_index(zip));
|
||||||
++total_entries;
|
++total_entries;
|
||||||
assert(0 == zip_entry_close(zip));
|
assert(0 == zip_entry_close(zip));
|
||||||
@ -58,6 +71,8 @@ static void test_read(void) {
|
|||||||
assert(zip != NULL);
|
assert(zip != NULL);
|
||||||
|
|
||||||
assert(0 == zip_entry_open(zip, "test\\test-1.txt"));
|
assert(0 == zip_entry_open(zip, "test\\test-1.txt"));
|
||||||
|
assert(strlen(TESTDATA1) == zip_entry_size(zip));
|
||||||
|
assert(CRC32DATA1 == zip_entry_crc32(zip));
|
||||||
assert(0 == zip_entry_read(zip, (void **)&buf, &bufsize));
|
assert(0 == zip_entry_read(zip, (void **)&buf, &bufsize));
|
||||||
assert(bufsize == strlen(TESTDATA1));
|
assert(bufsize == strlen(TESTDATA1));
|
||||||
assert(0 == strncmp(buf, TESTDATA1, bufsize));
|
assert(0 == strncmp(buf, TESTDATA1, bufsize));
|
||||||
@ -67,6 +82,8 @@ static void test_read(void) {
|
|||||||
bufsize = 0;
|
bufsize = 0;
|
||||||
|
|
||||||
assert(0 == zip_entry_open(zip, "test/test-2.txt"));
|
assert(0 == zip_entry_open(zip, "test/test-2.txt"));
|
||||||
|
assert(strlen(TESTDATA2) == zip_entry_size(zip));
|
||||||
|
assert(CRC32DATA2 == zip_entry_crc32(zip));
|
||||||
assert(0 == zip_entry_read(zip, (void **)&buf, &bufsize));
|
assert(0 == zip_entry_read(zip, (void **)&buf, &bufsize));
|
||||||
assert(bufsize == strlen(TESTDATA2));
|
assert(bufsize == strlen(TESTDATA2));
|
||||||
assert(0 == strncmp(buf, TESTDATA2, bufsize));
|
assert(0 == strncmp(buf, TESTDATA2, bufsize));
|
||||||
@ -75,6 +92,12 @@ static void test_read(void) {
|
|||||||
buf = NULL;
|
buf = NULL;
|
||||||
bufsize = 0;
|
bufsize = 0;
|
||||||
|
|
||||||
|
assert(0 == zip_entry_open(zip, "test\\empty/"));
|
||||||
|
assert(0 == strcmp(zip_entry_name(zip), "test/empty/"));
|
||||||
|
assert(0 == zip_entry_size(zip));
|
||||||
|
assert(0 == zip_entry_crc32(zip));
|
||||||
|
assert(0 == zip_entry_close(zip));
|
||||||
|
|
||||||
zip_close(zip);
|
zip_close(zip);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user