/
unzip.c
154 lines (148 loc) · 3.44 KB
/
unzip.c
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
#include <stdio.h>
#include <stdlib.h>
#include "dezip.h"
#include "zlib/unzip.h"
#include "zlib/zlib.h"
#define SUFFIX ".gz"
int main(int argc, const char* argv[])
{
char *name;
FILE *fin;
char filename[MAX_PATH];
if(argc < 2)
{
printf("Unzip: args is to few\n");
return -1;
}
strcpy(filename, argv[1]);
strcat(filename, SUFFIX);
printf("Compressing '%s' ...\n", argv[1]);
gzcompress(argv[1], filename);
printf("Extracting '%s' ...\n", filename);
name = decompress(filename); // extract the zip file
if(name) printf("[%s] is decompressed from [%s]\n", name, argv[1]);
return 0;
}
void gzcompress(const char *file, const char * filename)
{
FILE * fin;
void * buf;
size_t size, temp;
gzFile gzfile;
fin = fopen(file, "rb");
fseek(fin, 0, SEEK_END);
size = ftell(fin);
printf("Input file size %d\n", size);
buf = malloc(size);
rewind(fin);
fread(buf, 1, size, fin);
gzfile = gzopen(filename, "wb");
temp = gzwrite(gzfile, buf, size);
printf("Write size %d\n", temp);
size = gzflush(gzfile, Z_FINISH);
if(size != Z_OK)
printf("Doomed!\n");
free(buf);
gzclose(gzfile);
}
char* decompress(const char *zfin)
{
FILE *fin, *fout;
unsigned char * buf;
unsigned long size, nReadBytes;
unsigned char signature[4];
static unsigned char szCurrentFile[MAX_PATH];
assert(zfin != NULL);
/* open and detect file type */
fin = fopen(zfin, "rb");
if(!fin)
{
printf("Unzip: file [%s] open error\n", zfin);
return NULL;
}
fread(signature, 1, 4, fin);
if( signature[3] == 0x04 && signature[2] == 0x03 &&
signature[1] == 0x4b && signature[0] == 0x50 )
{
int ret;
unzFile unZipDir;
unz_file_info unZipFileInfo;
printf("Unzip: [%s] is a zip file\n", zfin);
unZipDir = unzOpen(zfin);
if(unZipDir == NULL)
{
printf("Unzip: can't uncompress %s\n", zfin);
return NULL;
}
ret = unzGoToFirstFile(unZipDir);
while(ret == UNZ_OK)
{
ret = unzGetCurrentFileInfo(unZipDir, &unZipFileInfo,
szCurrentFile, MAX_PATH, NULL, 0, NULL, 0);
if(ret)
{
printf("Unzip: get file info error\n");
return NULL;
}
else if(szCurrentFile[strlen(szCurrentFile)-1] == '/')
{
printf("[./%s]\n", szCurrentFile);
mkdir(szCurrentFile);
}
else
{
size = unZipFileInfo.uncompressed_size;
printf(" ./%s -> %d\n", szCurrentFile, size);
buf = malloc(size);
assert(buf != NULL);
if (UNZ_OK == unzOpenCurrentFile(unZipDir))
{
nReadBytes = unzReadCurrentFile(unZipDir, buf, size);
unzCloseCurrentFile(unZipDir);
fout = fopen(szCurrentFile, "wb");
assert(fout != NULL);
fwrite(buf, size, 1, fout);
fclose(fout);
}
free(buf);
}
ret = unzGoToNextFile(unZipDir);
}
unzClose(unZipDir);
}
else if(signature[1] == 0x8b && signature[0] == 0x1f)
{
int len;
char * ptr;
gzFile gzfile;
printf("Unzip: [%s] is a gzip file\n", zfin);
gzfile = gzopen(zfin, "rb");
if(gzfile == NULL)
{
printf("Unzip: can't uncompress %s\n", zfin);
return NULL;
}
buf = malloc(MAX_BUFFER);
assert(buf != NULL);
size = gzread(gzfile, buf, MAX_BUFFER);
len = strlen(zfin);
ptr = strrchr(zfin, '.');
if(ptr)
len = ptr - zfin;
else
len -= 1;
strncpy(szCurrentFile, zfin, len);
printf("%s -> %d\n", szCurrentFile, size);
fout = fopen(szCurrentFile, "wb");
assert(fout != NULL);
fwrite(buf, 1, size, fout);
fclose(fout);
gzclose(gzfile);
free(buf);
}
else
{
printf("Unzip: file [%s] is not a zip or gzip file\n", zfin);
}
return szCurrentFile;
}