Home | History | Annotate | Download | only in libzipfile
      1 #include <zipfile/zipfile.h>
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 
      6 void dump_zipfile(FILE* to, zipfile_t file);
      7 
      8 int
      9 main(int argc, char** argv)
     10 {
     11     FILE* f;
     12     size_t size, unsize;
     13     void* buf;
     14     void* scratch;
     15     zipfile_t zip;
     16     zipentry_t entry;
     17     int err;
     18     enum { HUH, LIST, UNZIP } what = HUH;
     19 
     20     if (strcmp(argv[2], "-l") == 0 && argc == 3) {
     21         what = LIST;
     22     }
     23     else if (strcmp(argv[2], "-u") == 0 && argc == 5) {
     24         what = UNZIP;
     25     }
     26     else {
     27         fprintf(stderr, "usage: test_zipfile ZIPFILE -l\n"
     28                         "          lists the files in the zipfile\n"
     29                         "       test_zipfile ZIPFILE -u FILENAME SAVETO\n"
     30                         "          saves FILENAME from the zip file into SAVETO\n");
     31         return 1;
     32     }
     33 
     34     f = fopen(argv[1], "r");
     35     if (f == NULL) {
     36         fprintf(stderr, "couldn't open %s\n", argv[1]);
     37         return 1;
     38     }
     39 
     40     fseek(f, 0, SEEK_END);
     41     size = ftell(f);
     42     rewind(f);
     43 
     44     buf = malloc(size);
     45     fread(buf, 1, size, f);
     46 
     47     zip = init_zipfile(buf, size);
     48     if (zip == NULL) {
     49         fprintf(stderr, "inti_zipfile failed\n");
     50         return 1;
     51     }
     52 
     53     fclose(f);
     54 
     55 
     56     switch (what)
     57     {
     58         case LIST:
     59             dump_zipfile(stdout, zip);
     60             break;
     61         case UNZIP:
     62             entry = lookup_zipentry(zip, argv[3]);
     63             if (entry == NULL) {
     64                 fprintf(stderr, "zip file '%s' does not contain file '%s'\n",
     65                                 argv[1], argv[1]);
     66                 return 1;
     67             }
     68             f = fopen(argv[4], "w");
     69             if (f == NULL) {
     70                 fprintf(stderr, "can't open file for writing '%s'\n", argv[4]);
     71                 return 1;
     72             }
     73             unsize = get_zipentry_size(entry);
     74             size = unsize * 1.001;
     75             scratch = malloc(size);
     76             printf("scratch=%p\n", scratch);
     77             err = decompress_zipentry(entry, scratch, size);
     78             if (err != 0) {
     79                 fprintf(stderr, "error decompressing file\n");
     80                 return 1;
     81             }
     82             fwrite(scratch, unsize, 1, f);
     83             free(scratch);
     84             fclose(f);
     85             break;
     86     }
     87 
     88     free(buf);
     89 
     90     return 0;
     91 }
     92 
     93