-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest.c
More file actions
132 lines (111 loc) · 2.59 KB
/
test.c
File metadata and controls
132 lines (111 loc) · 2.59 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zip.h"
#include "unzip.h"
int main(int argc, char** argv)
{
int ret;
/// zip file
zipFile zf;
zf = zipOpen64("test.zip", 0);
if (zf == NULL) {
fprintf(stderr, "zipOpen64 error\n");
return 1;
}
zip_fileinfo zi;
zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour = \
zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
zi.dosDate = 0;
zi.internal_fa = 0;
zi.external_fa = 0;
ret = zipOpenNewFileInZip(zf, "dir/1.txt", &zi, NULL,0,NULL,0,NULL, Z_DEFLATED, 1);
if (ret != ZIP_OK) {
fprintf(stderr, "zipOpenNewFileInZip error\n");
return 1;
}
ret = zipWriteInFileInZip(zf, "12345", 6);
if (ret != ZIP_OK) {
fprintf(stderr, "zipWriteInFileInZip error\n");
return 1;
}
ret = zipCloseFileInZip(zf);
if (ret != ZIP_OK) {
fprintf(stderr, "zipCloseFileInZip error\n");
return 1;
}
ret = zipOpenNewFileInZip(zf, "2.txt", &zi, NULL,0,NULL,0,NULL, Z_DEFLATED, 1);
if (ret != ZIP_OK) {
fprintf(stderr, "zipOpenNewFileInZip error\n");
return 1;
}
ret = zipWriteInFileInZip(zf, "测试", 6);
if (ret != ZIP_OK) {
fprintf(stderr, "zipWriteInFileInZip error\n");
return 1;
}
ret = zipCloseFileInZip(zf);
if (ret != ZIP_OK) {
fprintf(stderr, "zipCloseFileInZip error\n");
return 1;
}
ret = zipClose(zf, NULL);
if (ret != ZIP_OK) {
fprintf(stderr, "zipClose error\n");
return 1;
}
/// unzip file
unzFile uf;
unz_global_info64 gi;
unz_file_info64 file_info;
uf = unzOpen64("test.zip");
if (uf == NULL) {
fprintf(stderr, "unzOpen64 error\n");
return 1;
}
ret = unzGetGlobalInfo64(uf, &gi);
if (ret != UNZ_OK) {
fprintf(stderr, "unzGetGlobalInfo64 error\n");
return 1;
}
char filename[64];
char buf[1024];
int len;
unzGoToFirstFile(uf);
while(1) {
ret = unzGetCurrentFileInfo64(uf, &file_info, filename, sizeof(filename), NULL, 0, NULL, 0);
if (ret != UNZ_OK) {
fprintf(stderr, "unzGetCurrentFileInfo64 error\n");
return 1;
}
printf("filename = %s\n", filename);
ret = unzOpenCurrentFile(uf);
if (ret != UNZ_OK) {
fprintf(stderr, "unzOpenCurrentFile error\n");
return 1;
}
do {
memset(buf, 0, 1024);
len = unzReadCurrentFile(uf, buf, 1023);
if (len < 0) {
fprintf(stderr, "unzReadCurrentFile error\n");
return 1;
}
if (len > 0) {
printf("%s", buf);
}
} while(len > 0);
printf("\n");
unzCloseCurrentFile(uf);
ret = unzGoToNextFile(uf);
if (ret == UNZ_END_OF_LIST_OF_FILE) {
break;
}
if (ret != UNZ_OK) {
fprintf(stderr, "unzGoToNextFile error\n");
return 1;
}
}
unzClose(uf);
return 0;
}