ZIP_EXTERN zip_source_t * zip_source_buffer(zip_t *za, const void *data, zip_uint64_t len, int freep) { if (za == NULL) return NULL; return zip_source_buffer_create(data, len, freep, &za->error); }
COpcPackageReader::COpcPackageReader(_In_ PImportStream pImportStream) { if (pImportStream.get() == nullptr) throw CNMRException(NMR_ERROR_INVALIDPARAM); m_ZIPError.str = nullptr; m_ZIPError.sys_err = 0; m_ZIPError.zip_err = 0; m_ZIParchive = nullptr; m_ZIPsource = nullptr; try { // determine stream size nfUint64 nStreamSize = pImportStream->retrieveSize(); pImportStream->seekPosition(0, true); if (nStreamSize == 0) throw CNMRException(NMR_ERROR_COULDNOTGETSTREAMPOSITION); // read ZIP into memory m_Buffer.resize ((size_t) nStreamSize); pImportStream->readBuffer(&m_Buffer[0], nStreamSize, true); // create ZIP objects zip_error_init(&m_ZIPError); m_ZIPsource = zip_source_buffer_create(&m_Buffer[0], (size_t) nStreamSize, 0, &m_ZIPError); if (m_ZIPsource == nullptr) throw CNMRException(NMR_ERROR_COULDNOTREADZIPFILE); m_ZIParchive = zip_open_from_source(m_ZIPsource, ZIP_RDONLY | ZIP_CHECKCONS, &m_ZIPError); if (m_ZIParchive == nullptr) throw CNMRException(NMR_ERROR_COULDNOTREADZIPFILE); // Get ZIP Content nfInt64 nEntryCount = zip_get_num_entries(m_ZIParchive, ZIP_FL_UNCHANGED); if (nEntryCount < 0) throw CNMRException(NMR_ERROR_COULDNOTREADZIPFILE); // List Entries nfInt64 nIndex; for (nIndex = 0; nIndex < nEntryCount; nIndex++) { const char * pszName = zip_get_name(m_ZIParchive, (nfUint64) nIndex, ZIP_FL_ENC_GUESS); std::string sUTF8Name(pszName); std::wstring sUTF16Name = fnUTF8toUTF16(sUTF8Name); m_ZIPEntries.insert(std::make_pair(sUTF16Name, nIndex)); } readContentTypes(); readRootRelationships(); } catch (...) { releaseZIP(); throw; } }
zip_source_t *loadFromFile() { zip_error_t error; static unsigned char data[1000]; FILE *x = fopen("x.zip", "r"); size_t n = fread(data, 1, 1000, x); fprintf(stderr, "fread: n=%d\n", n); zip_source_t *zsrc = zip_source_buffer_create(data, n, 0, &error); return zsrc; }
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; } }
int main() { zip_source_t *zsrc = loadFromFile(); if (!zsrc) return 1; fprintf(stderr, "zip_open_from_source...\n"); zip_error_t error; zip_error_init(&error); zip_t *archive = zip_open_from_source(zsrc, 0, &error); if (!archive) return 2; zip_error_fini(&error); show(archive, "after zip_open_from_source"); const char *name = "y.txt"; zip_flags_t flags = 0; zip_source_t *otherZsrc = zip_source_buffer_create("hello", 5, 0, &error); fprintf(stderr, "zip_file_add...\n"); zip_int64_t i = zip_file_add(archive, name, otherZsrc, flags); fprintf(stderr, "zip_file_add(): i=%lld\n", i); // show(archive, "after zip_file_add"); zip_source_keep(zsrc); int ret; ret = zip_close(archive); // print to stdout zip_stat_t zst; ret = zip_source_stat(zsrc, &zst); fprintf(stderr, "zip_source_stat: ret=%d, zst.size=%d\n", ret, zst.size); ret = zip_source_open(zsrc); fprintf(stderr, "zip_source_open: ret=%d\n", ret); unsigned char data[10000]; zip_uint64_t n = zip_source_read(zsrc, data, sizeof(data)); fprintf(stderr, "zip_source_read: n=%d\n", n); write(1, data, n); // print to stdout }