static int add_from_zip(int argc, char *argv[]) { zip_uint64_t idx, start; zip_int64_t len; int err; zip_source_t *zs; /* add from another zip file */ idx = strtoull(argv[2], NULL, 10); start = strtoull(argv[3], NULL, 10); len = strtoll(argv[4], NULL, 10); if ((z_in[z_in_count]=zip_open(argv[1], ZIP_CHECKCONS, &err)) == NULL) { zip_error_t error; zip_error_init_with_code(&error, err); fprintf(stderr, "can't open zip archive '%s': %s\n", argv[1], zip_error_strerror(&error)); zip_error_fini(&error); return -1; } if ((zs=zip_source_zip(za, z_in[z_in_count], idx, 0, start, len)) == NULL) { fprintf(stderr, "error creating file source from '%s' index '%" PRIu64 "': %s\n", argv[1], idx, zip_strerror(za)); zip_close(z_in[z_in_count]); return -1; } if (zip_add(za, argv[0], zs) == -1) { fprintf(stderr, "can't add file '%s': %s\n", argv[0], zip_strerror(za)); zip_source_free(zs); zip_close(z_in[z_in_count]); return -1; } z_in_count++; return 0; }
/* {{{ php_zip_ops_read */ static size_t php_zip_ops_read(php_stream *stream, char *buf, size_t count) { ssize_t n = 0; STREAM_DATA_FROM_STREAM(); if (self->za && self->zf) { n = zip_fread(self->zf, buf, count); if (n < 0) { #if LIBZIP_VERSION_MAJOR < 1 int ze, se; zip_file_error_get(self->zf, &ze, &se); stream->eof = 1; php_error_docref(NULL, E_WARNING, "Zip stream error: %s", zip_file_strerror(self->zf)); #else zip_error_t *err; err = zip_file_get_error(self->zf); stream->eof = 1; php_error_docref(NULL, E_WARNING, "Zip stream error: %s", zip_error_strerror(err)); zip_error_fini(err); #endif return 0; } /* cast count to signed value to avoid possibly negative n * being cast to unsigned value */ if (n == 0 || n < (ssize_t)count) { stream->eof = 1; } else { self->cursor += n; } } return (n < 1 ? 0 : (size_t)n); }
VFSZipFileTLS* vfs_zipfile_get_tls(VFSNode *node, bool create) { VFSZipFileData *zdata = node->data1; VFSZipFileTLS *tls = SDL_TLSGet(zdata->tls_id); if(tls || !create) { return tls; } tls = calloc(1, sizeof(VFSZipFileTLS)); SDL_TLSSet(zdata->tls_id, tls, (void(*)(void*))vfs_zipfile_free_tls); zip_source_t *src = zip_source_function_create(vfs_zipfile_srcfunc, node, &tls->error); zip_t *zip = tls->zip = zip_open_from_source(src, ZIP_RDONLY, &tls->error); // FIXME: Taisei currently doesn't handle zip files without explicit directory entries correctly (file listing will not work) if(!zip) { char *r = vfs_node_repr(zdata->source, true); vfs_set_error("Failed to open zip archive '%s': %s", r, zip_error_strerror(&tls->error)); free(r); vfs_zipfile_free_tls(tls); SDL_TLSSet(zdata->tls_id, 0, NULL); zip_source_free(src); return NULL; } return tls; }
int main(int argc, char *argv[]) { const char *archive; const char *file; const char *name; zip_t *za; zip_source_t *zs; int err; FILE *fp; prg = argv[0]; if (argc != 3) { fprintf(stderr, "usage: %s archive file\n", prg); return 1; } archive = argv[1]; file = argv[2]; if ((za=zip_open(archive, ZIP_CREATE, &err)) == NULL) { zip_error_t error; zip_error_init_with_code(&error, err); fprintf(stderr, "%s: can't open zip archive '%s': %s\n", prg, archive, zip_error_strerror(&error)); zip_error_fini(&error); return 1; } if ((fp=fopen(file, "r")) == NULL) { fprintf(stderr, "%s: can't open input file '%s': %s\n", prg, file, strerror(errno)); return 1; } if ((zs=zip_source_filep(za, fp, 0, -1)) == NULL) { fprintf(stderr, "%s: error creating file source for '%s': %s\n", prg, file, zip_strerror(za)); return 1; } if ((name=strrchr(file, '/')) == NULL) name = file; if (zip_add(za, name, zs) == -1) { zip_source_free(zs); fprintf(stderr, "%s: can't add file '%s': %s\n", prg, file, zip_strerror(za)); return 1; } if (zip_close(za) == -1) { fprintf(stderr, "%s: can't close zip archive '%s': %s\n", prg, archive, zip_strerror(za)); return 1; } return 0; }
std::string GetZipError(int err) { zip_error_t error; zip_error_init_with_code(&error, err); std::string errS(zip_error_strerror(&error)); zip_error_fini(&error); return errS; }
PCK_Catalog *PCK_OpenZipFile(const char *zipName){ int err=0; zip_error_t error; zip_error_init(&error); struct zip* z = zip_open(zipName,ZIP_RDONLY,&err); if(err != ZIP_ER_OK ){ zip_error_init_with_code(&error, err); SDL_SetError("PCK_OpenZipFile failed:%d %s ",err, zip_error_strerror(&error)); zip_error_fini(&error); return NULL; } PCK_Catalog* ptr=PCK_AllocCatalog(); ptr->openRW = zip_OpenRW; ptr->close = zip_Close; ptr->context = (SDL_RWops*)z; }
tt_zipsrc_t *tt_zipsrc_blob_create(IN void *p, IN tt_u32_t len, IN tt_bool_t free) { zip_source_t *zs; zip_error_t ze; // TT_ASSERT(p != NULL); zs = zip_source_buffer_create(p, len, free, &ze); if (zs != NULL) { return zs; } else { TT_ERROR("fail to create zipsrc blob: %s", zip_error_strerror(&ze)); return NULL; } }
static int cat(int argc, char *argv[]) { /* output file contents to stdout */ zip_uint64_t idx; zip_int64_t n; zip_file_t *zf; char buf[8192]; int err; idx = strtoull(argv[0], NULL, 10); #ifdef _WIN32 /* Need to set stdout to binary mode for Windows */ setmode(fileno(stdout), _O_BINARY); #endif if ((zf=zip_fopen_index(za, idx, 0)) == NULL) { fprintf(stderr, "can't open file at index '%" PRIu64 "': %s\n", idx, zip_strerror(za)); return -1; } while ((n=zip_fread(zf, buf, sizeof(buf))) > 0) { if (fwrite(buf, (size_t)n, 1, stdout) != 1) { zip_fclose(zf); fprintf(stderr, "can't write file contents to stdout: %s\n", strerror(errno)); return -1; } } if (n == -1) { zip_fclose(zf); fprintf(stderr, "can't read file at index '%" PRIu64 "': %s\n", idx, zip_file_strerror(zf)); return -1; } if ((err = zip_fclose(zf)) != 0) { zip_error_t error; zip_error_init_with_code(&error, err); fprintf(stderr, "can't close file at index '%" PRIu64 "': %s\n", idx, zip_error_strerror(&error)); return -1; } return 0; }
SDL_RWops *SDL_RWFromZip(struct zip *z,const char *fname) { zip_file_t* zf; struct zip_stat st; Sint64 idx = zip_name_locate(z, fname, 0); if ( idx < 0){ SDL_SetError("Can't find file %s",fname); return NULL; } //printf("Found file %s with idx %ld\n",fname,idx); zf=zip_fopen_index(z,idx,ZIP_FL_UNCHANGED); if(zf == NULL ){ zip_error_t *error = zip_get_error(z); SDL_SetError("PCK_RWFromZip failed for idx=%ld:%s", idx,zip_error_strerror(error)); zip_error_fini(error); return NULL; } zip_stat_init(&st); if (zip_stat_index(z, idx, 0, &st) == 0) { } SDL_RWops *c = SDL_AllocRW(); if (c == NULL) return NULL; c->seek = sdl_zip_seekfunc; c->size = sdl_zip_sizefunc; c->read = sdl_zip_readfunc; c->write = sdl_zip_writefunc; c->close = sdl_zip_closefunc; c->type = SDL_RW_TAG_CURRENT; ZipInfo* zi = SDL_malloc(sizeof(ZipInfo)); zi->size = st.size; zi->zip = z; c->hidden.unknown.data1 = zf; c->hidden.unknown.data2 = zi; return c; }
void print_zip_err(char *prefix, zip_t *zip) { zip_error_t *err = zip_get_error(zip); printf("%s: %s\n", prefix, zip_error_strerror(err)); zip_error_fini(err); }
struct input_file *read_file_list (int argc, char **argv, int *nf_r, char *errstr) { struct input_file *list = (struct input_file *) NULL; FILE *fp = (FILE *) NULL; char line[16384], *av, *p, *s; int a, f, nf = 0; #ifndef _WIN32 glob_t gbuf; int rv, op; /* Init */ gbuf.gl_pathv = (char **) NULL; #endif int is_zip; #ifdef ZIPSUPPORT zip_t *z = (zip_t *) NULL; zip_error_t ze; struct zip_stat sb; int zerrno; int ent, nent; const char *name; int namelen; zip_uint8_t opsys; zip_uint32_t extattr; #endif /* Loop over arguments */ for(a = 0; a < argc; a++) { av = argv[a]; if(*av == '@') { /* @list form */ av++; /* skip past the @ */ is_zip = is_zip_file(av, errstr); if(is_zip < 0) goto error; if(is_zip) { #ifdef ZIPSUPPORT z = zip_open(av, 0, &zerrno); if(!z) { zip_error_init_with_code(&ze, zerrno); report_err(errstr, "zip_open: %s: %s", av, zip_error_strerror(&ze)); zip_error_fini(&ze); goto error; } nent = zip_get_num_entries(z, 0); if(nent < 0) { report_err(errstr, "zip_get_num_entries: %s", zip_strerror(z)); goto error; } for(ent = 0; ent < nent; ent++) { if(zip_stat_index(z, ent, 0, &sb)) { report_err(errstr, "zip_stat_index(%d): %s\n", ent, zip_strerror(z)); goto error; } if(zip_file_get_external_attributes(z, ent, 0, &opsys, &extattr)) { report_err(errstr, "zip_get_external_attributes(%d): %s\n", ent, zip_strerror(z)); goto error; } name = sb.name; namelen = strlen(name); if((opsys != ZIP_OPSYS_AMIGA && extattr & 0x10) || (namelen > 0 && name[namelen-1] == '/')) /* Is directory */ continue; /* Otherwise, add to list */ list = (struct input_file *) realloc(list, (nf+1) * sizeof(struct input_file)); if(!list) { report_syserr(errstr, "realloc"); goto error; } s = strdup(name); if(!s) { report_syserr(errstr, "strdup"); goto error; } list[nf].filename = s; list[nf].ient = ent; list[nf].iarg = a; list[nf].arg = av; nf++; } if(zip_close(z)) { report_err(errstr, "zip_close: %s", zip_strerror(z)); goto error; } z = (zip_t *) NULL; #else report_err(errstr, "not compiled with zip support"); goto error; #endif } else { fp = fopen(av, "r"); if(!fp) { report_syserr(errstr, "open: %s", av); goto error; } while(fgets(line, sizeof(line), fp)) { p = sstrip(line); if(*p) { list = (struct input_file *) realloc(list, (nf+1) * sizeof(struct input_file)); if(!list) { report_syserr(errstr, "realloc"); goto error; } s = strdup(p); if(!s) { report_syserr(errstr, "strdup"); goto error; } list[nf].filename = s; list[nf].ient = -1; list[nf].iarg = a; list[nf].arg = av; nf++; } } if(ferror(fp)) { report_syserr(errstr, "read: %s", av); goto error; } fclose(fp); fp = (FILE *) NULL; } } else { /* glob or single filename */ #ifndef _WIN32 rv = glob(av, 0, NULL, &gbuf); if(rv == 0) { /* succeeded */ /* Allocate block */ list = (struct input_file *) realloc(list, (nf+gbuf.gl_pathc) * sizeof(struct input_file)); if(!list) { report_syserr(errstr, "realloc"); goto error; } /* Zero out the new pointers */ memset(list+nf, 0, gbuf.gl_pathc * sizeof(struct input_file)); /* Record so we know to free them */ op = nf; nf += gbuf.gl_pathc; for(f = 0; f < gbuf.gl_pathc; f++) { s = strdup(gbuf.gl_pathv[f]); if(!s) { report_syserr(errstr, "strdup"); goto error; } list[op].filename = s; list[op].ient = -1; list[op].iarg = a; list[op].arg = av; op++; } globfree(&gbuf); gbuf.gl_pathv = (char **) NULL; } else if(rv == GLOB_NOMATCH) { /* no match */ #endif /* _WIN32 */ /* Assume it's a single entry. */ list = (struct input_file *) realloc(list, (nf+1) * sizeof(struct input_file)); if(!list) { report_syserr(errstr, "realloc"); goto error; } s = strdup(av); if(!s) { report_syserr(errstr, "strdup"); goto error; } list[nf].filename = s; list[nf].ient = -1; list[nf].iarg = a; list[nf].arg = av; nf++; #ifndef _WIN32 } else { report_err(errstr, "glob error: %s", av); goto error; } #endif } } *nf_r = nf; return(list); error: if(fp) fclose(fp); #ifndef _WIN32 if(gbuf.gl_pathv) globfree(&gbuf); #endif #ifdef ZIPSUPPORT if(z) { zip_close(z); z = (zip_t *) NULL; } #endif if(list) { for(f = 0; f < nf; f++) if(list[f].filename) free((void *) list[f].filename); free((void *) list); } return((struct input_file *) NULL); }