void *unzip_file(zipfile_t zip, const char *name, unsigned *sz) { void *data; zipentry_t entry; unsigned datasz; entry = lookup_zipentry(zip, name); if (entry == NULL) { fprintf(stderr, "archive does not contain '%s'\n", name); return 0; } *sz = get_zipentry_size(entry); datasz = *sz * 1.001; data = malloc(datasz); if(data == 0) { fprintf(stderr, "failed to allocate %d bytes\n", *sz); return 0; } if (decompress_zipentry(entry, data, datasz)) { fprintf(stderr, "failed to unzip '%s' from archive\n", name); free(data); return 0; } return data; }
void *unzip_file(zipfile_t zip, const char *name, unsigned *sz) { void *data; zipentry_t entry; unsigned datasz; entry = lookup_zipentry(zip, name); if (entry == NULL) { //fprintf(stderr, "archive does not contain '%s'\n", name); return 0; } *sz = get_zipentry_size(entry); datasz = *sz * 1.001; data = malloc(datasz); if(data == 0) { /* fall back to decompress entry to file @name */ char path[PATH_MAX] = ""; int fd; void *addr; snprintf(path, sizeof(path), "/tmp/%s", name); if (decompress_zipentry(entry, path, 0)) { fprintf(stderr, "failed to decompress '%s' from archive\n", name); return 0; } /* parse_config will write it in memory, so open it with "rw" */ fd = open(path, O_RDWR); if (fd == -1) { fprintf(stderr, "failed to open file: '%s': %m\n", path); return 0; } unlink(path); *sz = lseek(fd, 0, SEEK_END); addr = mmap(NULL, *sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (addr == MAP_FAILED) { fprintf(stderr, "mmap failed: %m\n"); close(fd); return 0; } return addr; } if (decompress_zipentry(entry, data, datasz)) { fprintf(stderr, "failed to unzip '%s' from archive\n", name); free(data); return 0; } return data; }
int main(int argc, char** argv) { FILE* f; size_t size, unsize; void* buf; void* scratch; zipfile_t zip; zipentry_t entry; int err; enum { HUH, LIST, UNZIP } what = HUH; if (strcmp(argv[2], "-l") == 0 && argc == 3) { what = LIST; } else if (strcmp(argv[2], "-u") == 0 && argc == 5) { what = UNZIP; } else { fprintf(stderr, "usage: test_zipfile ZIPFILE -l\n" " lists the files in the zipfile\n" " test_zipfile ZIPFILE -u FILENAME SAVETO\n" " saves FILENAME from the zip file into SAVETO\n"); return 1; } f = fopen(argv[1], "r"); if (f == NULL) { fprintf(stderr, "couldn't open %s\n", argv[1]); return 1; } fseek(f, 0, SEEK_END); size = ftell(f); rewind(f); buf = malloc(size); fread(buf, 1, size, f); zip = init_zipfile(buf, size); if (zip == NULL) { fprintf(stderr, "inti_zipfile failed\n"); return 1; } fclose(f); switch (what) { case HUH: break; case LIST: dump_zipfile(stdout, zip); break; case UNZIP: entry = lookup_zipentry(zip, argv[3]); if (entry == NULL) { fprintf(stderr, "zip file '%s' does not contain file '%s'\n", argv[1], argv[1]); return 1; } f = fopen(argv[4], "w"); if (f == NULL) { fprintf(stderr, "can't open file for writing '%s'\n", argv[4]); return 1; } unsize = get_zipentry_size(entry); size = unsize * 1.001; scratch = malloc(size); printf("scratch=%p\n", scratch); err = decompress_zipentry(entry, scratch, size); if (err != 0) { fprintf(stderr, "error decompressing file\n"); return 1; } fwrite(scratch, unsize, 1, f); free(scratch); fclose(f); break; } free(buf); return 0; }