static struct zip_source* S_create_source_zip(lua_State* L, struct zip* ar) { struct zip** other_ar = check_archive(L, 4); int file_idx = luaL_checkint(L, 5); int flags = lua_gettop(L) < 6 ? 0 : luaL_checkint(L, 6); int start = lua_gettop(L) < 7 ? 0 : luaL_checkint(L, 7); int len = lua_gettop(L) < 8 ? -1 : luaL_checkint(L, 8); struct zip_source* src = NULL; if ( ! *other_ar ) return; lua_getfenv(L, 1); lua_pushvalue(L, 4); lua_rawget(L, -2); if ( ! lua_isnil(L, -1) ) { lua_pushliteral(L, "Circular reference of zip sources is not allowed"); lua_error(L); } lua_pop(L, 1); /* We store a strong reference to prevent the "other" archive * from getting prematurely gc'ed, we also have the other archive * store a weak reference so the __gc metamethod will be called * if the "other" archive is closed before we are closed. */ S_archive_add_ref(L, 0, 1, 4); S_archive_add_ref(L, 1, 4, 1); src = zip_source_zip(ar, *other_ar, file_idx-1, flags, start, len); if ( NULL != src ) return src; lua_pushstring(L, zip_strerror(ar)); lua_error(L); }
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; }
static int merge_zip(struct zip *za, const char *tname, const char *sname, struct zip **zsp) { struct zip *zs; struct zip_source *source; int i, idx, err; char errstr[1024]; const char *fname; if ((zs=zip_open(sname, 0, &err)) == NULL) { zip_error_to_str(errstr, sizeof(errstr), err, errno); fprintf(stderr, "%s: cannot open zip archive `%s': %s\n", prg, sname, errstr); return -1; } for (i=0; i<zip_get_num_files(zs); i++) { fname = zip_get_name(zs, i, 0); if ((idx=zip_name_locate(za, fname, name_flags)) != -1) { switch (confirm_replace(za, tname, idx, zs, sname, i)) { case 0: break; case 1: if ((source=zip_source_zip(za, zs, i, 0, 0, 0)) == NULL || zip_replace(za, idx, source) < 0) { zip_source_free(source); fprintf(stderr, "%s: cannot replace `%s' in `%s': %s\n", prg, fname, tname, zip_strerror(za)); return -1; } break; case -1: zip_close(zs); return -1; default: fprintf(stderr, "%s: internal error: " "unexpected return code from confirm (%d)\n", prg, err); zip_close(zs); return -1; } } else { if ((source=zip_source_zip(za, zs, i, 0, 0, 0)) == NULL || zip_add(za, fname, source) < 0) { zip_source_free(source); fprintf(stderr, "%s: cannot add `%s' to `%s': %s\n", prg, fname, tname, zip_strerror(za)); zip_close(zs); return -1; } } } *zsp = zs; return 0; }
ZIP_EXTERN int zip_close(struct zip *za) { int survivors; int i, j, error; char *temp; FILE *out; mode_t mask; struct zip_cdir *cd; struct zip_dirent de; struct filelist *filelist; int reopen_on_error; int new_torrentzip; reopen_on_error = 0; if (za == NULL) return -1; if (!_zip_changed(za, &survivors)) { _zip_free(za); return 0; } /* don't create zip files with no entries */ if (survivors == 0) { if (za->zn && za->zp) { if (remove(za->zn) != 0) { _zip_error_set(&za->error, ZIP_ER_REMOVE, errno); return -1; } } _zip_free(za); return 0; } if ((filelist=(struct filelist *)malloc(sizeof(filelist[0])*survivors)) == NULL) return -1; if ((cd=_zip_cdir_new(survivors, &za->error)) == NULL) { free(filelist); return -1; } for (i=0; i<survivors; i++) _zip_dirent_init(&cd->entry[i]); /* archive comment is special for torrentzip */ if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) { cd->comment = (char*)_zip_memdup((void*)(TORRENT_SIG "XXXXXXXX"), TORRENT_SIG_LEN + TORRENT_CRC_LEN, &za->error); if (cd->comment == NULL) { _zip_cdir_free(cd); free(filelist); return -1; } cd->comment_len = TORRENT_SIG_LEN + TORRENT_CRC_LEN; } else if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, ZIP_FL_UNCHANGED) == 0) { if (_zip_cdir_set_comment(cd, za) == -1) { _zip_cdir_free(cd); free(filelist); return -1; } } if ((temp=_zip_create_temp_output(za, &out)) == NULL) { _zip_cdir_free(cd); free(filelist); return -1; } /* create list of files with index into original archive */ for (i=j=0; i<za->nentry; i++) { if (za->entry[i].state == ZIP_ST_DELETED) continue; filelist[j].idx = i; filelist[j].name = zip_get_name(za, i, 0); j++; } survivors = j; if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) qsort(filelist, survivors, sizeof(filelist[0]), _zip_torrentzip_cmp); new_torrentzip = (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 1 && zip_get_archive_flag(za, ZIP_AFL_TORRENT, ZIP_FL_UNCHANGED) == 0); error = 0; for (j=0; j<survivors; j++) { i = filelist[j].idx; /* create new local directory entry */ if (ZIP_ENTRY_DATA_CHANGED(za->entry+i) || new_torrentzip) { _zip_dirent_init(&de); if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0)) _zip_dirent_torrent_normalize(&de); /* use it as central directory entry */ memcpy(cd->entry+j, &de, sizeof(cd->entry[j])); /* set/update file name */ if (za->entry[i].ch_filename == NULL) { if (za->entry[i].state == ZIP_ST_ADDED) { de.filename = strdup("-"); de.filename_len = 1; cd->entry[j].filename = "-"; cd->entry[j].filename_len = 1; } else { de.filename = strdup(za->cdir->entry[i].filename); de.filename_len = strlen(de.filename); cd->entry[j].filename = za->cdir->entry[i].filename; cd->entry[j].filename_len = de.filename_len; } } } else { /* copy existing directory entries */ if (fseeko(za->zp, za->cdir->entry[i].offset, SEEK_SET) != 0) { _zip_error_set(&za->error, ZIP_ER_SEEK, errno); error = 1; break; } if (_zip_dirent_read(&de, za->zp, NULL, NULL, 1, &za->error) != 0) { error = 1; break; } memcpy(cd->entry+j, za->cdir->entry+i, sizeof(cd->entry[j])); if (de.bitflags & ZIP_GPBF_DATA_DESCRIPTOR) { de.crc = za->cdir->entry[i].crc; de.comp_size = za->cdir->entry[i].comp_size; de.uncomp_size = za->cdir->entry[i].uncomp_size; de.bitflags &= ~ZIP_GPBF_DATA_DESCRIPTOR; cd->entry[j].bitflags &= ~ZIP_GPBF_DATA_DESCRIPTOR; } } if (za->entry[i].ch_filename) { free(de.filename); if ((de.filename=strdup(za->entry[i].ch_filename)) == NULL) { error = 1; break; } de.filename_len = strlen(de.filename); cd->entry[j].filename = za->entry[i].ch_filename; cd->entry[j].filename_len = de.filename_len; } if (zip_get_archive_flag(za, ZIP_AFL_TORRENT, 0) == 0 && za->entry[i].ch_comment_len != -1) { /* as the rest of cd entries, its malloc/free is done by za */ cd->entry[j].comment = za->entry[i].ch_comment; cd->entry[j].comment_len = za->entry[i].ch_comment_len; } cd->entry[j].offset = (unsigned int)ftello(out); if (ZIP_ENTRY_DATA_CHANGED(za->entry+i) || new_torrentzip) { struct zip_source *zs; zs = NULL; if (!ZIP_ENTRY_DATA_CHANGED(za->entry+i)) { if ((zs=zip_source_zip(za, za, i, ZIP_FL_RECOMPRESS, 0, -1)) == NULL) { error = 1; break; } } if (add_data(za, zs ? zs : za->entry[i].source, &de, out) < 0) { error = 1; break; } cd->entry[j].last_mod = de.last_mod; cd->entry[j].comp_method = de.comp_method; cd->entry[j].comp_size = de.comp_size; cd->entry[j].uncomp_size = de.uncomp_size; cd->entry[j].crc = de.crc; } else { if (_zip_dirent_write(&de, out, 1, &za->error) < 0) { error = 1; break; } /* we just read the local dirent, file is at correct position */ if (copy_data(za->zp, cd->entry[j].comp_size, out, &za->error) < 0) { error = 1; break; } } _zip_dirent_finalize(&de); } free(filelist); if (!error) { if (write_cdir(za, cd, out) < 0) error = 1; } /* pointers in cd entries are owned by za */ cd->nentry = 0; _zip_cdir_free(cd); if (error) { _zip_dirent_finalize(&de); fclose(out); remove(temp); free(temp); return -1; } if (fclose(out) != 0) { _zip_error_set(&za->error, ZIP_ER_CLOSE, errno); remove(temp); free(temp); return -1; } if (za->zp) { fclose(za->zp); za->zp = NULL; reopen_on_error = 1; } if (_zip_rename(temp, za->zn) != 0) { _zip_error_set(&za->error, ZIP_ER_RENAME, errno); remove(temp); free(temp); if (reopen_on_error) { /* ignore errors, since we're already in an error case */ za->zp = fopen(za->zn, "rb"); } return -1; } #ifndef _MSC_VER mask = umask(0); umask(mask); chmod(za->zn, 0666&~mask); #endif _zip_free(za); free(temp); return 0; }
int main(int argc, char *argv[]) { const char *archive; struct zip *za, *z_in; struct zip_source *zs; char buf[100]; int c, arg, err, flags, idx; flags = 0; prg = argv[0]; if (argc < 2) { fprintf(stderr, usage, prg); return 1; } while ((c=getopt(argc, argv, "cent")) != -1) { switch (c) { case 'c': flags |= ZIP_CHECKCONS; break; case 'e': flags |= ZIP_EXCL; break; case 'n': flags |= ZIP_CREATE; break; case 't': flags |= ZIP_TRUNCATE; break; default: fprintf(stderr, usage, argv[0]); return 1; } } arg = optind; archive = argv[arg++]; if (flags == 0) flags = ZIP_CREATE; if ((za=zip_open(archive, flags, &err)) == NULL) { zip_error_to_str(buf, sizeof(buf), err, errno); fprintf(stderr, "can't open zip archive `%s': %s\n", archive, buf); return 1; } err = 0; while (arg < argc) { if (strcmp(argv[arg], "add") == 0 && arg+2 < argc) { /* add */ if ((zs=zip_source_buffer(za, argv[arg+2], strlen(argv[arg+2]), 0)) == NULL) { fprintf(stderr, "can't create zip_source from buffer: %s\n", zip_strerror(za)); err = 1; break; } if (zip_add(za, argv[arg+1], zs) == -1) { zip_source_free(zs); fprintf(stderr, "can't add file `%s': %s\n", argv[arg+1], zip_strerror(za)); err = 1; break; } arg += 3; } else if (strcmp(argv[arg], "add_dir") == 0 && arg+1 < argc) { /* add directory */ if (zip_add_dir(za, argv[arg+1]) < 0) { fprintf(stderr, "can't add directory `%s': %s\n", argv[arg+1], zip_strerror(za)); err = 1; break; } arg += 2; } else if (strcmp(argv[arg], "add_file") == 0 && arg+4 < argc) { /* add */ if ((zs=zip_source_file(za, argv[arg+2], atoi(argv[arg+3]), atoi(argv[arg+4]))) == NULL) { fprintf(stderr, "can't create zip_source from file: %s\n", zip_strerror(za)); err = 1; break; } if (zip_add(za, argv[arg+1], zs) == -1) { zip_source_free(zs); fprintf(stderr, "can't add file `%s': %s\n", argv[arg+1], zip_strerror(za)); err = 1; break; } arg += 5; } else if (strcmp(argv[arg], "add_from_zip") == 0 && arg+5 < argc) { /* add from another zip file */ idx = atoi(argv[arg+3]); if ((z_in=zip_open(argv[arg+2], ZIP_CHECKCONS, &err)) == NULL) { zip_error_to_str(buf, sizeof(buf), err, errno); fprintf(stderr, "can't open source zip archive `%s': %s\n", argv[arg+2], buf); err = 1; break; } if ((zs=zip_source_zip(za, z_in, idx, 0, atoi(argv[arg+4]), atoi(argv[arg+5]))) == NULL) { fprintf(stderr, "error creating file source from `%s' index '%d': %s\n", argv[arg+2], idx, zip_strerror(za)); zip_close(z_in); err = 1; break; } if (zip_add(za, argv[arg+1], zs) == -1) { fprintf(stderr, "can't add file `%s': %s\n", argv[arg+1], zip_strerror(za)); zip_source_free(zs); zip_close(z_in); err = 1; break; } arg += 6; } else if (strcmp(argv[arg], "count_extra") == 0 && arg+2 < argc) { zip_int16_t count; zip_flags_t ceflags = 0; idx = atoi(argv[arg+1]); ceflags = get_flags(argv[arg+2]); if ((count=zip_file_extra_fields_count(za, idx, ceflags)) < 0) { fprintf(stderr, "can't get extra field count for file at index `%d': %s\n", idx, zip_strerror(za)); err = 1; break; } else { printf("Extra field count: %d\n", count); } arg += 3; } else if (strcmp(argv[arg], "count_extra_by_id") == 0 && arg+3 < argc) { zip_int16_t count, eid; zip_flags_t ceflags = 0; idx = atoi(argv[arg+1]); eid = atoi(argv[arg+2]); ceflags = get_flags(argv[arg+3]); if ((count=zip_file_extra_fields_count_by_id(za, idx, eid, ceflags)) < 0) { fprintf(stderr, "can't get extra field count for file at index `%d' and for id `%d': %s\n", idx, eid, zip_strerror(za)); err = 1; break; } else { printf("Extra field count: %d\n", count); } arg += 4; } else if (strcmp(argv[arg], "delete") == 0 && arg+1 < argc) { /* delete */ idx = atoi(argv[arg+1]); if (zip_delete(za, idx) < 0) { fprintf(stderr, "can't delete file at index `%d': %s\n", idx, zip_strerror(za)); err = 1; break; } arg += 2; } else if (strcmp(argv[arg], "delete_extra") == 0 && arg+1 < argc) { zip_flags_t geflags; zip_uint16_t eid; idx = atoi(argv[arg+1]); eid = atoi(argv[arg+2]); geflags = get_flags(argv[arg+3]); if ((zip_file_extra_field_delete(za, idx, eid, geflags)) < 0) { fprintf(stderr, "can't delete extra field data for file at index `%d', extra field id `%d': %s\n", idx, eid, zip_strerror(za)); err = 1; break; } arg += 4; } else if (strcmp(argv[arg], "delete_extra_by_id") == 0 && arg+1 < argc) { zip_flags_t geflags; zip_uint16_t eid, eidx; idx = atoi(argv[arg+1]); eid = atoi(argv[arg+2]); eidx = atoi(argv[arg+3]); geflags = get_flags(argv[arg+4]); if ((zip_file_extra_field_delete_by_id(za, idx, eid, eidx, geflags)) < 0) { fprintf(stderr, "can't delete extra field data for file at index `%d', extra field id `%d', extra field idx `%d': %s\n", idx, eid, eidx, zip_strerror(za)); err = 1; break; } arg += 5; } else if (strcmp(argv[arg], "get_archive_comment") == 0) { const char *comment; int len; /* get archive comment */ if ((comment=zip_get_archive_comment(za, &len, 0)) == NULL) printf("No archive comment\n"); else printf("Archive comment: %.*s\n", len, comment); arg += 1; } else if (strcmp(argv[arg], "get_extra") == 0 && arg+3 < argc) { zip_flags_t geflags; zip_uint16_t id, eidx, eflen; const zip_uint8_t *efdata; /* get extra field data */ idx = atoi(argv[arg+1]); eidx = atoi(argv[arg+2]); geflags = get_flags(argv[arg+3]); if ((efdata=zip_file_extra_field_get(za, idx, eidx, &id, &eflen, geflags)) == NULL) { fprintf(stderr, "can't get extra field data for file at index %d, extra field %d, flags %u: %s\n", idx, eidx, geflags, zip_strerror(za)); err = 1; } else { printf("Extra field 0x%04x: len %d", id, eflen); if (eflen > 0) { printf(", data "); hexdump(efdata, eflen); } printf("\n"); } arg += 4; } else if (strcmp(argv[arg], "get_extra_by_id") == 0 && arg+4 < argc) { zip_flags_t geflags; zip_uint16_t eid, eidx, eflen; const zip_uint8_t *efdata; idx = atoi(argv[arg+1]); eid = atoi(argv[arg+2]); eidx = atoi(argv[arg+3]); geflags = get_flags(argv[arg+4]); if ((efdata=zip_file_extra_field_get_by_id(za, idx, eid, eidx, &eflen, geflags)) == NULL) { fprintf(stderr, "can't get extra field data for file at index %d, extra field id %d, ef index %d, flags %u: %s\n", idx, eid, eidx, geflags, zip_strerror(za)); err = 1; } else { printf("Extra field 0x%04x: len %d", eid, eflen); if (eflen > 0) { printf(", data "); hexdump(efdata, eflen); } printf("\n"); } arg += 5; } else if (strcmp(argv[arg], "get_file_comment") == 0 && arg+1 < argc) { const char *comment; int len; /* get file comment */ idx = atoi(argv[arg+1]); if ((comment=zip_get_file_comment(za, idx, &len, 0)) == NULL) { fprintf(stderr, "can't get comment for `%s': %s\n", zip_get_name(za, idx, 0), zip_strerror(za)); err = 1; break; } else if (len == 0) printf("No comment for `%s'\n", zip_get_name(za, idx, 0)); else printf("File comment for `%s': %.*s\n", zip_get_name(za, idx, 0), len, comment); arg += 2; } else if (strcmp(argv[arg], "rename") == 0 && arg+2 < argc) { /* rename */ idx = atoi(argv[arg+1]); if (zip_rename(za, idx, argv[arg+2]) < 0) { fprintf(stderr, "can't rename file at index `%d' to `%s': %s\n", idx, argv[arg+2], zip_strerror(za)); err = 1; break; } arg += 3; } else if (strcmp(argv[arg], "set_extra") == 0 && arg+5 < argc) { zip_flags_t geflags; zip_uint16_t eid, eidx; const zip_uint8_t *efdata; idx = atoi(argv[arg+1]); eid = atoi(argv[arg+2]); eidx = atoi(argv[arg+3]); geflags = get_flags(argv[arg+4]); efdata = (zip_uint8_t *)argv[arg+5]; if ((zip_file_extra_field_set(za, idx, eid, eidx, efdata, (zip_uint16_t)strlen((const char *)efdata), geflags)) < 0) { fprintf(stderr, "can't set extra field data for file at index `%d', extra field id `%d', index `%d': %s\n", idx, eid, eidx, zip_strerror(za)); err = 1; break; } arg += 6; } else if (strcmp(argv[arg], "set_file_comment") == 0 && arg+2 < argc) { /* set file comment */ idx = atoi(argv[arg+1]); if (zip_file_set_comment(za, idx, argv[arg+2], (zip_uint16_t)strlen(argv[arg+2]), 0) < 0) { fprintf(stderr, "can't set file comment at index `%d' to `%s': %s\n", idx, argv[arg+2], zip_strerror(za)); err = 1; break; } arg += 3; } else if (strcmp(argv[arg], "set_file_compression") == 0 && arg+3 < argc) { /* set file compression */ zip_int32_t method; zip_uint32_t flags; idx = atoi(argv[arg+1]); method = get_compression_method(argv[arg+2]); flags = atoi(argv[arg+3]); if (zip_set_file_compression(za, idx, method, flags) < 0) { fprintf(stderr, "can't set file compression method at index `%d' to `%s', flags `%d': %s\n", idx, argv[arg+2], flags, zip_strerror(za)); err = 1; break; } arg += 4; } else { fprintf(stderr, "unrecognized command `%s', or not enough arguments\n", argv[arg]); err = 1; break; } } if (zip_close(za) == -1) { fprintf(stderr, "can't close zip archive `%s': %s\n", archive, zip_strerror(za)); return 1; } return err; }