packvfs

annotate 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
rev   line source
nuclear@3 1 #include <stdio.h>
nuclear@3 2 #include "minizip/unzip.h"
nuclear@3 3
nuclear@3 4 int procfile(const char *zip_fname, const char *fname);
nuclear@3 5
nuclear@3 6 int main(int argc, char **argv)
nuclear@3 7 {
nuclear@3 8 int i, listonly = 0;
nuclear@3 9
nuclear@3 10 for(i=1; i<argc; i++) {
nuclear@3 11 if(argv[i][0] == '-') {
nuclear@3 12 switch(argv[i][1]) {
nuclear@3 13 case 'l':
nuclear@3 14 listonly = 1;
nuclear@3 15 break;
nuclear@3 16
nuclear@3 17 default:
nuclear@3 18 fprintf(stderr, "unexpected option: %s\n", argv[i]);
nuclear@3 19 return 1;
nuclear@3 20 }
nuclear@3 21 } else {
nuclear@3 22 if(!listonly) {
nuclear@3 23 procfile(argv[i], argv[i + 1]);
nuclear@3 24 i++;
nuclear@3 25 } else {
nuclear@3 26 procfile(argv[i], 0);
nuclear@3 27 listonly = 0;
nuclear@3 28 }
nuclear@3 29 }
nuclear@3 30 }
nuclear@3 31
nuclear@3 32 return 0;
nuclear@3 33 }
nuclear@3 34
nuclear@3 35
nuclear@3 36 int procfile(const char *zip_fname, const char *fname)
nuclear@3 37 {
nuclear@3 38 char buf[512];
nuclear@3 39 unzFile zip;
nuclear@3 40 unz_global_info ginf;
nuclear@3 41 int res = -1;
nuclear@3 42
nuclear@3 43 if(!(zip = unzOpen(zip_fname))) {
nuclear@3 44 fprintf(stderr, "failed to open zip file: %s\n", zip_fname);
nuclear@3 45 return -1;
nuclear@3 46 }
nuclear@3 47
nuclear@3 48 unzGetGlobalInfo(zip, &ginf);
nuclear@3 49
nuclear@3 50 if(fname) {
nuclear@3 51 int sz;
nuclear@3 52
nuclear@3 53 if(unzLocateFile(zip, fname, 1) != UNZ_OK) {
nuclear@3 54 fprintf(stderr, "failed to locate \"%s\" in zip file: %s\n", fname, zip_fname);
nuclear@3 55 goto err;
nuclear@3 56 }
nuclear@3 57
nuclear@3 58 if(unzOpenCurrentFile(zip) != UNZ_OK) {
nuclear@3 59 fprintf(stderr, "failed to open \"%s\" in zip file: %s\n", fname, zip_fname);
nuclear@3 60 goto err;
nuclear@3 61 }
nuclear@3 62
nuclear@3 63 while((sz = unzReadCurrentFile(zip, buf, sizeof buf)) > 0) {
nuclear@3 64 fwrite(buf, 1, sz, stdout);
nuclear@3 65 }
nuclear@3 66 fflush(stdout);
nuclear@3 67
nuclear@3 68 unzCloseCurrentFile(zip);
nuclear@3 69
nuclear@3 70 } else {
nuclear@3 71 /* just list the contents */
nuclear@3 72 if(unzGoToFirstFile(zip) != UNZ_OK) {
nuclear@3 73 fprintf(stderr, "failed to start content listing\n");
nuclear@3 74 goto err;
nuclear@3 75 }
nuclear@3 76
nuclear@3 77 do {
nuclear@3 78 unz_file_info file_info;
nuclear@3 79 if(unzGetCurrentFileInfo(zip, &file_info, buf, sizeof buf, 0, 0, 0, 0) != UNZ_OK) {
nuclear@3 80 fprintf(stderr, "failed to retrieve file information\n");
nuclear@3 81 goto err;
nuclear@3 82 }
nuclear@3 83
nuclear@3 84 printf("%s - %lu bytes (%lu compressed)\n", buf, file_info.uncompressed_size,
nuclear@3 85 file_info.compressed_size);
nuclear@3 86 } while(unzGoToNextFile(zip) == UNZ_OK);
nuclear@3 87 }
nuclear@3 88
nuclear@3 89 res = 0; /* success */
nuclear@3 90 err:
nuclear@3 91 unzClose(zip);
nuclear@3 92 return res;
nuclear@3 93 }