summaryrefslogtreecommitdiff
path: root/zwrapper.c
blob: b4d5f894b29df6d5f10a4433df8d9097fcb3677f (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#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

}

void ensure_directory(const char *base, const char *filename)
{
char *onedir;
char *fulldir;
char *tmp;

    if(filename == NULL || base == NULL) return;
    if(strlen(filename) == 0) return;

    onedir = (char *)malloc((strlen(filename)+1)*sizeof(char));
    fulldir = (char *)malloc((strlen(filename)+strlen(base)+2)*sizeof(char));
    
    strcpy(onedir, filename);
    tmp = onedir;
    
    tmp = strchr(onedir, pathsep);    
#ifdef WIN32
    if(tmp == NULL)
        tmp = strchr(onedir, '/');
#endif
    
    while(tmp != NULL) {
    
        *tmp = '\0';
        
        gen_full_filename(base, onedir, fulldir, (strlen(filename)+strlen(base)+2));
        makedir(fulldir);
    
        *tmp = pathsep;
        tmp++;
#ifdef WIN32
        if(strchr(tmp, pathsep)  == NULL)
            tmp = strchr(tmp, '/');
        else
            tmp = strchr(tmp, pathsep);
#else
        tmp = strchr(tmp, pathsep); 
#endif
    }
    
    free(onedir);
    free(fulldir);
}

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 {
            
                ensure_directory(directory, sb.name);
            
                zf = zip_fopen_index(za, i, 0);
                fp = fopen(fullfile, "wb");
                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;
}