mirror of
https://github.com/QuasarApp/zip.git
synced 2025-04-27 12:54:32 +00:00
4.7 KiB
4.7 KiB
A portable (OSX/Linux/Windows), simple zip library written in C
This is done by hacking awesome miniz library and layering functions on top of the miniz v1.15 API.
The Idea

It was the reason, why I decided to write zip module on top of the miniz. It required a little bit hacking and wrapping some functions, but I kept simplicity. So, you can grab these 3 files and compile them into your project. I hope that interface is also extremely simple, so you will not have any problems to understand it.
Example (compress)
#include <stdio.h>
#include <string.h>
#include <zip.h>
int main() {
/*
Create a new zip archive with default compression level (6)
*/
struct zip_t *zip = zip_open("foo.zip", ZIP_DEFAULT_COMPRESSION_LEVEL, 'w');
// we should check if zip is NULL and if any other function returned < 0
{
zip_entry_open(zip, "foo-1.txt");
{
char *buf = "Some data here...";
zip_entry_write(zip, buf, strlen(buf));
}
zip_entry_close(zip);
zip_entry_open(zip, "foo-2.txt");
{
// merge 3 files into one entry and compress them on-the-fly.
zip_entry_fwrite(zip, "foo-2.1.txt");
zip_entry_fwrite(zip, "foo-2.2.txt");
zip_entry_fwrite(zip, "foo-2.3.txt");
}
zip_entry_close(zip);
}
// always remember to close and release resources
zip_close(zip);
/*
Append to existing zip archive
*/
zip = zip_open("foo.zip", ZIP_DEFAULT_COMPRESSION_LEVEL, 'a');
// we should check if zip is NULL
{
zip_entry_open(zip, "foo-3.txt");
{
char *buf = "Append some data here...";
zip_entry_write(zip, buf, strlen(buf));
}
zip_entry_close(zip);
}
// always remember to close and release resources
zip_close(zip);
return 0;
}
Example (decompress)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zip.h>
// callback function
int on_extract_entry(const char *filename, void *arg) {
static int i = 0;
int n = *(int *)arg;
printf("Extracted: %s (%d of %d)\n", filename, ++i, n);
return 0;
}
int main() {
/*
Extract the zip archive into /tmp folder
*/
int arg = 2;
zip_extract("foo.zip", "/tmp", on_extract_entry, &arg);
/*
...or open the zip archive with only read access
*/
void *buf = NULL;
size_t bufsize;
struct zip_t *zip = zip_open("foo.zip", 0, 'r');
// we should check if zip is NULL and if any other function returned < 0
{
zip_entry_open(zip, "foo-1.txt");
{
// extract into memory
zip_entry_read(zip, &buf, &bufsize);
printf("Read(foo-1.txt): %zu bytes: %.*s\n", bufsize, (int)bufsize,
buf);
}
zip_entry_close(zip);
zip_entry_open(zip, "foo-2.txt");
{
// extract into a file
zip_entry_fread(zip, "foo-2.txt");
}
zip_entry_close(zip);
}
// always remember to close and release resources
zip_close(zip);
// do something with buffer... and remember to free memory
free(buf);
return 0;
}