cv::Mat ZipByteReader::Read(size_t seqId, const std::string& path, bool grayscale) { // Find index of the file in .zip file. auto r = m_seqIdToIndex.find(seqId); if (r == m_seqIdToIndex.end()) RuntimeError("Could not find file %s in the zip file, sequence id = %lu", path.c_str(), (long)seqId); zip_uint64_t index = std::get<0>((*r).second); zip_uint64_t size = std::get<1>((*r).second); auto contents = m_workspace.pop_or_create([size]() { return vector<unsigned char>(size); }); if (contents.size() < size) contents.resize(size); auto zipFile = m_zips.pop_or_create([this]() { return OpenZip(); }); { std::unique_ptr<zip_file_t, void(*)(zip_file_t*)> file( zip_fopen_index(zipFile.get(), index, 0), [](zip_file_t* f) { assert(f != nullptr); int err = zip_fclose(f); assert(ZIP_ER_OK == err); #ifdef NDEBUG UNUSED(err); #endif }); assert(nullptr != file); if (nullptr == file) { RuntimeError("Could not open file %s in the zip file, sequence id = %lu, zip library error: %s", path.c_str(), (long)seqId, GetZipError(zip_error_code_zip(zip_get_error(zipFile.get()))).c_str()); } assert(contents.size() >= size); zip_uint64_t bytesRead = zip_fread(file.get(), contents.data(), size); assert(bytesRead == size); if (bytesRead != size) { RuntimeError("Bytes read %lu != expected %lu while reading file %s", (long)bytesRead, (long)size, path.c_str()); } } m_zips.push(std::move(zipFile)); cv::Mat img; if (grayscale) img = cv::imdecode(cv::Mat(1, (int)size, CV_8UC1, contents.data()), cv::IMREAD_GRAYSCALE); else img = cv::imdecode(cv::Mat(1, (int)size, CV_8UC1, contents.data()), cv::IMREAD_COLOR); assert(nullptr != img.data); m_workspace.push(std::move(contents)); return img; }
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); }