bool ZipFile::unpack() { close(); // delete the buffer const vfspos sz = size(); // will reopen the file if(sz < 0) return false; _buf = new char[size_t(sz) + 1]; if(!_buf) return false; if(!mz_zip_reader_extract_to_mem(MZ, _fileIdx, _buf, (size_t)sz, 0)) { delete [] _buf; _buf = NULL; return false; // this should not happen } _bufSize = sz; // In case of text data, make sure the buffer is always terminated with '\0'. // Won't hurt for binary data, so just do it in all cases. _buf[sz] = 0; if(_mode.find("b") == std::string::npos) // text mode? { _bufSize = (vfspos)strnNLcpy(_buf, _buf); } return true; }
bool CZipArchive::ExtractFile(std::size_t index) //---------------------------------------------- { mz_zip_archive *zip = static_cast<mz_zip_archive*>(zipFile); if(index >= contents.size()) { return false; } mz_uint bestFile = index; mz_zip_archive_file_stat stat; MemsetZero(stat); mz_zip_reader_file_stat(zip, bestFile, &stat); if(stat.m_uncomp_size >= std::numeric_limits<std::size_t>::max()) { return false; } try { data.resize(static_cast<std::size_t>(stat.m_uncomp_size)); } catch(...) { return false; } if(!mz_zip_reader_extract_to_mem(zip, bestFile, &data[0], static_cast<std::size_t>(stat.m_uncomp_size), 0)) { return false; } comment = mpt::ToWide(mpt::CharsetCP437, std::string(stat.m_comment, stat.m_comment + stat.m_comment_size)); return true; }
U64 ZipIO::_readCompressed(void *Data, U64 DataSize){ if (!_kisopen || !_kready || _kfindex < 0 || _kmode == OpenMode::WRITE) return 0; if (mz_zip_reader_extract_to_mem(&_kzarchive, _kfindex, Data, (size_t)DataSize, 0)) return DataSize; KD_PRINT("reading archive error"); return 0; }
bool ZipArchive::decompressEntry(const Entry* entry, char* output) { // Check that the given pointer refers to an entry in our list. if (entry == nullptr || entry < entryList.data() || entry >= entryList.data() + entryList.size()) { return false; } // Get the index of the entry (this arithmetic is only legal in an array). size_t index = entry - entryList.data(); size_t size = entry->uncompressedSize; return mz_zip_reader_extract_to_mem(&minizData, index, output, size, 0); }
const void *VFSFileZip::getBuf(allocator_func alloc /* = NULL */, delete_func del /* = NULL */) { assert(!alloc == !del); // either both or none may be defined. Checked extra early to prevent possible errors later. VFS_GUARD_OPT(this); // _fixedStr gets deleted on mode change, so doing this check here is fine if(_fixedStr) return _fixedStr; if(!_buf) { size_t sz = (size_t)size(); _buf = allocHelperExtra(alloc, sz, 4); if(!_buf) return NULL; _delfunc = del; if(!zip_reader_reopen_vfsfile(_zip, 0)) return false; // can happen if the underlying zip file was deleted if(!mz_zip_reader_extract_to_mem(_zip, _zipstat.m_file_index, _buf, sz, 0)) return false; // this should not happen if(_mode.find("b") == std::string::npos) // text mode? { _fixedStr = allocHelperExtra(alloc, sz, 4); strnNLcpy(_fixedStr, (const char*)_buf); // FIXME: is this really correct? VFSFile::dropBuf(true); return _fixedStr; } } return _buf; }
RefCountedPtr<FileData> FileSourceZip::ReadFile(const std::string &path) { if (!m_archive) return RefCountedPtr<FileData>(); mz_zip_archive *zip = reinterpret_cast<mz_zip_archive*>(m_archive); const Directory *dir; std::string filename; if (!FindDirectoryAndFile(path, dir, filename)) return RefCountedPtr<FileData>(); std::map<std::string,FileStat>::const_iterator i = dir->files.find(filename); if (i == dir->files.end()) return RefCountedPtr<FileData>(); const FileStat &st = (*i).second; char *data = reinterpret_cast<char*>(std::malloc(st.size)); if (!mz_zip_reader_extract_to_mem(zip, st.index, data, st.size, 0)) { printf("FileSourceZip::ReadFile: couldn't extract '%s'\n", path.c_str()); return RefCountedPtr<FileData>(); } return RefCountedPtr<FileData>(new FileDataMalloc(st.info, st.size, data)); }
bool fe_zip_open_to_buff( const char *archive, const char *filename, FE_ZIP_ALLOC_CALLBACK callback, void **buff, size_t *buff_size ) { ASSERT( buff != NULL ); mz_zip_archive zip; memset( &zip, 0, sizeof( zip ) ); if ( !mz_zip_reader_init_file( &zip, archive, 0 ) ) { std::cerr << "Error initializing zip. zip: " << archive << std::endl; return false; } int index = mz_zip_reader_locate_file( &zip, filename, NULL, 0 ); if ( index < 0 ) { std::cerr << "Error locating file. zip: " << archive << ", file: " << filename << std::endl; mz_zip_reader_end( &zip ); return false; } mz_zip_archive_file_stat file_stat; if ( !mz_zip_reader_file_stat(&zip, index, &file_stat) ) { std::cerr << "Error reading filestats. zip: " << archive << ", file: " << filename << std::endl; mz_zip_reader_end( &zip ); return false; } void *tb = callback( file_stat.m_uncomp_size ); if ( tb == NULL ) { std::cerr << "Error allocating zip buffer. zip: " << archive << ", file: " << filename << std::endl; mz_zip_reader_end( &zip ); return false; } if ( !mz_zip_reader_extract_to_mem( &zip, index, tb, file_stat.m_uncomp_size, 0 ) ) { std::cerr << "Error extracting to buffer. zip: " << archive << ", file: " << filename << std::endl; mz_zip_reader_end( &zip ); delete (char *)tb; return false; } mz_zip_reader_end( &zip ); *buff = tb; if ( buff_size ) *buff_size = file_stat.m_uncomp_size; return true; }