summaryrefslogtreecommitdiffstats
path: root/source/unzip/miniunz.c
blob: 83c61a9acfa0cf1279b17e7fa9b78543da875e03 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
   miniunz.c
   Version 1.01e, February 12th, 2005

   Copyright (C) 1998-2005 Gilles Vollant
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <utime.h>
#include <stdbool.h>
#include "unzip.h"

#define CASESENSITIVITY (0)
#define WRITEBUFFERSIZE (8192)
#define MAXFILENAME (256)

int zip_size = 0;
long extract_part_size = 0;
long zip_progress = 0;
int unzip_file_counter = 0;
int unzip_file_count = 0;
char no_unzip_list[10][300];
int no_unzip_count = 0;

static int mymkdir(const char* dirname)
{
	return mkdir(dirname,0775);
}

int makedir(char *newdir)
{
	char *buffer;
	char *p;
	int len = strlen(newdir);

	if (len <= 0){return 0;}

	buffer = malloc(len+1);
	strcpy(buffer,newdir);

	if (buffer[len-1] == '/'){buffer[len-1] = '\0';}
	if (mymkdir(buffer) == 0)
		{
		free(buffer);
		return 1;
		}

	p = buffer+1;

	while(1)
		{
		char hold;

		while(*p && *p != '\\' && *p != '/'){++p;}
		hold = *p;
		*p = 0;
		if ((mymkdir(buffer) == -1) && (errno == ENOENT))
			{
			printf("Couldn't create directory %s\n",buffer);
			free(buffer);
			return 0;
			}
		if (hold == 0){break;}
		*p++ = hold;
		}

free(buffer);
return 1;
}

static int do_extract_currentfile(unzFile uf,const int* popt_extract_without_path,int* popt_overwrite,const char* password)
{
	char filename_inzip[256];
	char *filename_withoutpath;
	char *p;
	int err = UNZ_OK;
	FILE *fout = NULL;
	void* buf;
	uInt size_buf;

	unz_file_info file_info;
	err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);

	if (err != UNZ_OK)
		{
		printf("Error %d with zipfile in unzGetCurrentFileInfo\n",err);
		return err;
		}

	size_buf = WRITEBUFFERSIZE;
	buf = malloc(size_buf);
	if (buf==NULL)
		{
		printf("Error allocating memory\n");
		return UNZ_INTERNALERROR;
		}

	p = filename_withoutpath = filename_inzip;
	while ((*p) != '\0')
		{
		if (((*p)=='/') || ((*p)=='\\')){filename_withoutpath = p+1;}
		++p;
		}

	if ((*filename_withoutpath)=='\0')
		{
		if ((*popt_extract_without_path)==0)
			{
			printf("Creating directory: %s\n",filename_inzip);
			mymkdir(filename_inzip);
			}
		}
	else
		{
		char* write_filename;
		int skip = 0;

		if ((*popt_extract_without_path)==0){write_filename = filename_inzip;}
	        else {write_filename = filename_withoutpath;}

		bool ok_to_unzip = true;

		int d;
		for (d = 0; d < no_unzip_count; ++d)
			{
			//printf("SECOND = %s\n", no_unzip_list[d]);
			if (strcmp(no_unzip_list[d], write_filename) == 0)
				{
				FILE *f = fopen(write_filename, "rb");
				if (f != NULL)
					{
				ok_to_unzip = false;
					}
				}
			}

		if (ok_to_unzip)
			{
			err = unzOpenCurrentFilePassword(uf,password);
			if (err!=UNZ_OK)
				{
				printf("Error %d with zipfile in unzOpenCurrentFilePassword\n",err);
				}

			if (((*popt_overwrite)==0) && (err==UNZ_OK))
				{
				char rep=0;
				FILE* ftestexist;
				ftestexist = fopen(write_filename,"rb");
				if (ftestexist!=NULL)
					{
					fclose(ftestexist);
					}

				if (rep == 'A'){*popt_overwrite=1;}//default overwrite all
				}

			if ((skip == 0) && (err==UNZ_OK))
				{
				fout=fopen(write_filename,"wb");

				/* some zipfiles don't contain directory alone before file */
				if ((fout==NULL) && ((*popt_extract_without_path)==0) && (filename_withoutpath!=(char*)filename_inzip))
					{
					char c = *(filename_withoutpath-1);
					*(filename_withoutpath-1)='\0';
					makedir(write_filename);
					*(filename_withoutpath-1)=c;
					fout=fopen(write_filename,"wb");
					}

				if (fout==NULL)
					{
					printf("Error opening %s\n", write_filename);
					}
				}

			if (fout!=NULL)
				{
				//printf(" extracting: %s\n",write_filename);

				int temp_size = 0;
				do
					{
					err = unzReadCurrentFile(uf,buf,size_buf);
					if (err<0)
						{
						printf("Error %d with zipfile in unzReadCurrentFile\n",err);
						break;
						}

					if (err)
						{
						if (fwrite(buf,err,1,fout)!=1)
							{
							printf("error in writing extracted file\n");
							err=UNZ_ERRNO;
							break;
							}
						}

					zip_progress += size_buf;
					temp_size += size_buf;
					//updating_current_size += size_buf;
					}
				while (err);

				if (temp_size > file_info.uncompressed_size)
					{
					zip_progress -= temp_size;
					zip_progress += file_info.uncompressed_size;
					//updating_current_size -= temp_size;
					//updating_current_size += file_info.uncompressed_size;
					}

				if (fout){fclose(fout);}
				}

			if (err==UNZ_OK)
				{
				err = unzCloseCurrentFile (uf);
				if (err!=UNZ_OK)
					{
					printf("error %d with zipfile in unzCloseCurrentFile\n",err);
					}
				}
			else {unzCloseCurrentFile(uf);} /* don't lose the error */
		}
	}

	free(buf);
	return err;
}

int zipInfo(unzFile uf)
{
	uLong i;
	unz_global_info gi;
	int err;
	uLong totalSize = 0;
	unz_file_info file_info;
	char filename_inzip[256];

	err = unzGetGlobalInfo (uf,&gi);

	for (i=0;i<gi.number_entry;++i)
		{
		err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);

		if (err != UNZ_OK){return err;}

		totalSize += file_info.uncompressed_size;

		if ((i+1)<gi.number_entry)
			{
			err = unzGoToNextFile(uf);
			if (err != UNZ_OK){return err;}
			}
		}

	err = unzGoToFirstFile(uf);
	if (err != UNZ_OK){return err;}

	return totalSize;
}


int extractZip(unzFile uf,int opt_extract_without_path,int opt_overwrite,const char* password)
{
	zip_progress = 0;
	zip_size = zipInfo(uf);
	extract_part_size = (int) (zip_size / 100);
	//printf("Zipped up total size: %i\n",zip_size);

	uLong i;
	unz_global_info gi;
	int err;

	err = unzGetGlobalInfo (uf,&gi);
	if (err!=UNZ_OK){printf("error %d with zipfile in unzGetGlobalInfo \n",err);}

	unzip_file_count = gi.number_entry;

	for (i=0;i<gi.number_entry;++i)
		{
		unzip_file_counter = (i+1);

		//printf("%li / %li done, zip progress = %li / %i\n", (i+1), gi.number_entry, zip_progress, zip_size);
		printf(".");
		if (do_extract_currentfile(uf,&opt_extract_without_path,&opt_overwrite,password) != UNZ_OK){break;}

		if ((i+1)<gi.number_entry)
			{
			err = unzGoToNextFile(uf);
			if (err != UNZ_OK)
				{
				printf("Error %d with zipfile in unzGoToNextFile\n",err);
				break;
				}
			}
		}
    return 0;
}

int extractZipOnefile(unzFile uf,const char* filename,int opt_extract_without_path,int opt_overwrite,const char* password)
{
	if (unzLocateFile(uf,filename,CASESENSITIVITY)!=UNZ_OK)
		{
		printf("file %s not found in the zipfile\n",filename);
		return 2;
		}

		if (do_extract_currentfile(uf,&opt_extract_without_path,&opt_overwrite,password) == UNZ_OK){return 0;}
		else {return 1;}
}