summaryrefslogtreecommitdiff
path: root/zwrapper.c
blob: 6d07f2bf96e47b20d59ce6348b7307b4b49cace0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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;
}