Home | History | Annotate | Download | only in libzipfile
      1 #include <zipfile/zipfile.h>
      2 #include <string.h>
      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 HUH:
     59             break;
     60         case LIST:
     61             dump_zipfile(stdout, zip);
     62             break;
     63         case UNZIP:
     64             entry = lookup_zipentry(zip, argv[3]);
     65             if (entry == NULL) {
     66                 fprintf(stderr, "zip file '%s' does not contain file '%s'\n",
     67                                 argv[1], argv[1]);
     68                 return 1;
     69             }
     70             f = fopen(argv[4], "w");
     71             if (f == NULL) {
     72                 fprintf(stderr, "can't open file for writing '%s'\n", argv[4]);
     73                 return 1;
     74             }
     75             unsize = get_zipentry_size(entry);
     76             size = unsize * 1.001;
     77             scratch = malloc(size);
     78             printf("scratch=%p\n", scratch);
     79             err = decompress_zipentry(entry, scratch, size);
     80             if (err != 0) {
     81                 fprintf(stderr, "error decompressing file\n");
     82                 return 1;
     83             }
     84             fwrite(scratch, unsize, 1, f);
     85             free(scratch);
     86             fclose(f);
     87             break;
     88     }
     89 
     90     free(buf);
     91 
     92     return 0;
     93 }
     94 
     95