packvfs

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/zipcat/src/main.c	Mon Nov 04 06:46:17 2013 +0200
     1.3 @@ -0,0 +1,93 @@
     1.4 +#include <stdio.h>
     1.5 +#include "minizip/unzip.h"
     1.6 +
     1.7 +int procfile(const char *zip_fname, const char *fname);
     1.8 +
     1.9 +int main(int argc, char **argv)
    1.10 +{
    1.11 +	int i, listonly = 0;
    1.12 +
    1.13 +	for(i=1; i<argc; i++) {
    1.14 +		if(argv[i][0] == '-') {
    1.15 +			switch(argv[i][1]) {
    1.16 +			case 'l':
    1.17 +				listonly = 1;
    1.18 +				break;
    1.19 +
    1.20 +			default:
    1.21 +				fprintf(stderr, "unexpected option: %s\n", argv[i]);
    1.22 +				return 1;
    1.23 +			}
    1.24 +		} else {
    1.25 +			if(!listonly) {
    1.26 +				procfile(argv[i], argv[i + 1]);
    1.27 +				i++;
    1.28 +			} else {
    1.29 +				procfile(argv[i], 0);
    1.30 +				listonly = 0;
    1.31 +			}
    1.32 +		}
    1.33 +	}
    1.34 +
    1.35 +	return 0;
    1.36 +}
    1.37 +
    1.38 +
    1.39 +int procfile(const char *zip_fname, const char *fname)
    1.40 +{
    1.41 +	char buf[512];
    1.42 +	unzFile zip;
    1.43 +	unz_global_info ginf;
    1.44 +	int res = -1;
    1.45 +
    1.46 +	if(!(zip = unzOpen(zip_fname))) {
    1.47 +		fprintf(stderr, "failed to open zip file: %s\n", zip_fname);
    1.48 +		return -1;
    1.49 +	}
    1.50 +
    1.51 +	unzGetGlobalInfo(zip, &ginf);
    1.52 +
    1.53 +	if(fname) {
    1.54 +		int sz;
    1.55 +
    1.56 +		if(unzLocateFile(zip, fname, 1) != UNZ_OK) {
    1.57 +			fprintf(stderr, "failed to locate \"%s\" in zip file: %s\n", fname, zip_fname);
    1.58 +			goto err;
    1.59 +		}
    1.60 +
    1.61 +		if(unzOpenCurrentFile(zip) != UNZ_OK) {
    1.62 +			fprintf(stderr, "failed to open \"%s\" in zip file: %s\n", fname, zip_fname);
    1.63 +			goto err;
    1.64 +		}
    1.65 +
    1.66 +		while((sz = unzReadCurrentFile(zip, buf, sizeof buf)) > 0) {
    1.67 +			fwrite(buf, 1, sz, stdout);
    1.68 +		}
    1.69 +		fflush(stdout);
    1.70 +
    1.71 +		unzCloseCurrentFile(zip);
    1.72 +
    1.73 +	} else {
    1.74 +		/* just list the contents */
    1.75 +		if(unzGoToFirstFile(zip) != UNZ_OK) {
    1.76 +			fprintf(stderr, "failed to start content listing\n");
    1.77 +			goto err;
    1.78 +		}
    1.79 +
    1.80 +		do {
    1.81 +			unz_file_info file_info;
    1.82 +			if(unzGetCurrentFileInfo(zip, &file_info, buf, sizeof buf, 0, 0, 0, 0) != UNZ_OK) {
    1.83 +				fprintf(stderr, "failed to retrieve file information\n");
    1.84 +				goto err;
    1.85 +			}
    1.86 +
    1.87 +			printf("%s - %lu bytes (%lu compressed)\n", buf, file_info.uncompressed_size,
    1.88 +					file_info.compressed_size);
    1.89 +		} while(unzGoToNextFile(zip) == UNZ_OK);
    1.90 +	}
    1.91 +
    1.92 +	res = 0;	/* success */
    1.93 +err:
    1.94 +	unzClose(zip);
    1.95 +	return res;
    1.96 +}