summaryrefslogtreecommitdiff
path: root/zwrapper.c
diff options
context:
space:
mode:
Diffstat (limited to 'zwrapper.c')
-rw-r--r--zwrapper.c128
1 files changed, 128 insertions, 0 deletions
diff --git a/zwrapper.c b/zwrapper.c
new file mode 100644
index 0000000..6d07f2b
--- /dev/null
+++ b/zwrapper.c
@@ -0,0 +1,128 @@
+#include <stdio.h>
+#include <string.h>
+
+#ifdef WIN32
+#include <windows.h>
+#else
+#include <sys/stat.h>
+#include <sys/types.h>
+#endif
+
+#include <zip.h>
+
+#define BUF_SIZE 128
+
+#ifndef MAX_PATH
+#define MAX_PATH 2048
+#endif
+
+const char pathsep =
+#ifdef WIN32
+ '\\';
+#else
+ '/';
+#endif
+
+
+int makedir(const char *dir)
+{
+#ifdef WIN32
+ return mkdir(dir);
+#else
+ return mkdir(dir, 0755);
+#endif
+}
+
+void gen_full_filename(const char *basedir, const char *file, char *dest, size_t destsize)
+{
+int i;
+#ifdef WIN32
+char *fix;
+#endif
+
+ if(basedir == NULL)
+ strncpy(dest, file, destsize);
+ else if(file == NULL)
+ strncpy(dest, basedir, destsize);
+ else {
+ if(basedir[strlen(basedir)-1] == '/' || basedir[strlen(basedir)-1] == '\\')
+ snprintf(dest, destsize, "%s%s", basedir, file);
+ else
+ snprintf(dest, destsize, "%s%c%s", basedir, pathsep, file);
+ }
+
+#ifdef WIN32
+ fix = strchr(dest, '/');
+ while(fix != NULL) {
+ *fix = '\\';
+ fix = strchr(dest, '/');
+ }
+#endif
+
+}
+
+int unzip_file(const char *filename, const char *directory, const char *index_of_files_file)
+{
+struct zip *za;
+struct zip_file *zf;
+struct zip_stat sb;
+
+char buf[BUF_SIZE];
+int err, i;
+size_t this_read, total_read;
+
+char fullfile[MAX_PATH];
+FILE *fp;
+FILE *fp_index;
+
+ za = zip_open(filename, 0, &err);
+ if (za == NULL) {
+ return -1;
+ }
+
+ if(index_of_files_file != NULL)
+ fp_index = fopen(index_of_files_file, "w");
+ else
+ fp_index = NULL;
+
+ for(i=0; i < zip_get_num_entries(za,0); i++) {
+ if (zip_stat_index(za, i, 0, &sb) == 0) {
+ gen_full_filename(directory, sb.name, fullfile, MAX_PATH);
+
+ if(fullfile[strlen(fullfile)-1] == pathsep) {
+
+ makedir(fullfile);
+
+ } else {
+ zf = zip_fopen_index(za, i, 0);
+ fp = fopen(fullfile, "w");
+ if(zf != NULL && fp != NULL) {
+
+ total_read = 0;
+ while(total_read < sb.size) {
+ this_read = zip_fread(zf, buf, BUF_SIZE);
+ fwrite(buf, sizeof(char), this_read, fp);
+ total_read += this_read;
+ }
+
+ if(fp_index != NULL)
+ fprintf(fp_index, "%s\n", fullfile);
+ }
+
+ if(zf != NULL)
+ zip_fclose(zf);
+
+ if(fp != NULL)
+ fclose(fp);
+ }
+
+ }
+ }
+
+ zip_close(za);
+
+ if(fp_index != NULL)
+ fclose(fp_index);
+
+ return 1;
+}