packvfs
view test/zipcat/src/main.c @ 3:ef6c1472607f
jesus fucking christ that was easy... written a test prog "zipcat" to try out
zlib's contrib library "minizip", to list and read files out of zip archives
directly...
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 04 Nov 2013 06:46:17 +0200 |
parents | |
children |
line source
1 #include <stdio.h>
2 #include "minizip/unzip.h"
4 int procfile(const char *zip_fname, const char *fname);
6 int main(int argc, char **argv)
7 {
8 int i, listonly = 0;
10 for(i=1; i<argc; i++) {
11 if(argv[i][0] == '-') {
12 switch(argv[i][1]) {
13 case 'l':
14 listonly = 1;
15 break;
17 default:
18 fprintf(stderr, "unexpected option: %s\n", argv[i]);
19 return 1;
20 }
21 } else {
22 if(!listonly) {
23 procfile(argv[i], argv[i + 1]);
24 i++;
25 } else {
26 procfile(argv[i], 0);
27 listonly = 0;
28 }
29 }
30 }
32 return 0;
33 }
36 int procfile(const char *zip_fname, const char *fname)
37 {
38 char buf[512];
39 unzFile zip;
40 unz_global_info ginf;
41 int res = -1;
43 if(!(zip = unzOpen(zip_fname))) {
44 fprintf(stderr, "failed to open zip file: %s\n", zip_fname);
45 return -1;
46 }
48 unzGetGlobalInfo(zip, &ginf);
50 if(fname) {
51 int sz;
53 if(unzLocateFile(zip, fname, 1) != UNZ_OK) {
54 fprintf(stderr, "failed to locate \"%s\" in zip file: %s\n", fname, zip_fname);
55 goto err;
56 }
58 if(unzOpenCurrentFile(zip) != UNZ_OK) {
59 fprintf(stderr, "failed to open \"%s\" in zip file: %s\n", fname, zip_fname);
60 goto err;
61 }
63 while((sz = unzReadCurrentFile(zip, buf, sizeof buf)) > 0) {
64 fwrite(buf, 1, sz, stdout);
65 }
66 fflush(stdout);
68 unzCloseCurrentFile(zip);
70 } else {
71 /* just list the contents */
72 if(unzGoToFirstFile(zip) != UNZ_OK) {
73 fprintf(stderr, "failed to start content listing\n");
74 goto err;
75 }
77 do {
78 unz_file_info file_info;
79 if(unzGetCurrentFileInfo(zip, &file_info, buf, sizeof buf, 0, 0, 0, 0) != UNZ_OK) {
80 fprintf(stderr, "failed to retrieve file information\n");
81 goto err;
82 }
84 printf("%s - %lu bytes (%lu compressed)\n", buf, file_info.uncompressed_size,
85 file_info.compressed_size);
86 } while(unzGoToNextFile(zip) == UNZ_OK);
87 }
89 res = 0; /* success */
90 err:
91 unzClose(zip);
92 return res;
93 }