//----------------------------------------------------------------------- void ZipArchive::load() { if (!mZzipDir) { zzip_error_t zzipError; mZzipDir = zzip_dir_open(mName.c_str(), &zzipError); checkZzipError(zzipError, "opening archive"); // Cache names ZZIP_DIRENT zzipEntry; while (zzip_dir_read(mZzipDir, &zzipEntry)) { FileInfo info; info.archive = this; // Get basename / path StringUtil::splitFilename(zzipEntry.d_name, info.basename, info.path); // ignore folder entries if (info.basename.empty()) continue; info.filename = zzipEntry.d_name; // Get sizes info.compressedSize = static_cast<size_t>(zzipEntry.d_csize); info.uncompressedSize = static_cast<size_t>(zzipEntry.st_size); mFileList.push_back(info); } } }
size_t getEntry(const char *name, char *contentFile, char **ptr) { size_t size=0; ZZIP_DIR *dir; ZZIP_DIRENT entry; ZZIP_FILE *file; char *buf; dir = zzip_dir_open(name, 0); if (!dir) { fprintf(stderr, "failed to open %s\n", name); return 0; } zzip_dir_read(dir, &entry); file = zzip_file_open(dir, contentFile, 0); (void)zzip_seek(file, 0, SEEK_END); size = zzip_tell(file); (void)zzip_seek(file, 0, SEEK_SET); buf = (char *)malloc(size+1); (void)zzip_read(file, buf, size); buf[size] = '\0'; *ptr = buf; zzip_file_close(file); zzip_dir_close(dir); return size; }
void ZipResourceManager::populate() { zzip_error_t zzipError; ZZIP_DIR* zzipDir = zzip_dir_open(resourcePath.c_str(), &zzipError); checkZzipError(zzipError); ZZIP_DIRENT zzipEntry; while (zzip_dir_read(zzipDir, &zzipEntry)) { int compressedSize = zzipEntry.d_csize; int uncompressedSize = zzipEntry.st_size; std::string name = std::string(zzipEntry.d_name); files[name] = SDL_RWFromZZIP((resourcePath + "/" + name).c_str(), "rb"); } zzip_dir_close(zzipDir); }
void ArchiveZip::enumerate(Array<Path>& paths, bool dir) { if (!isValid || !handle) return; ZZIP_DIR* zip = (ZZIP_DIR*) handle; ZZIP_DIRENT entry; zzip_rewinddir(zip); while( zzip_dir_read(zip, &entry) != 0 ) { Path name = entry.d_name; bool isDir = !name.empty() && name[name.size()-1] == '/'; if( (dir && isDir) || (!dir && !isDir) ) paths.pushBack(name); } }
bool PluginLoader::ParseDirectory(FileSpecifier& dir) { std::vector<dir_entry> de; if (!dir.ReadDirectory(de)) { return false; } for (std::vector<dir_entry>::const_iterator it = de.begin(); it != de.end(); ++it) { FileSpecifier file = dir + it->name; if (it->name == "Plugin.xml") { ParsePlugin(file); } else if (it->is_directory && it->name[0] != '.') { ParseDirectory(file); } #ifdef HAVE_ZZIP else if (algo::ends_with(it->name, ".zip") || algo::ends_with(it->name, ".ZIP")) { // search it for a Plugin.xml file ZZIP_DIR* zzipdir = zzip_dir_open(file.GetPath(), 0); if (zzipdir) { ZZIP_DIRENT dirent; while (zzip_dir_read(zzipdir, &dirent)) { if (algo::ends_with(dirent.d_name, "Plugin.xml")) { std::string archive = file.GetPath(); FileSpecifier file_name = FileSpecifier(archive.substr(0, archive.find_last_of('.'))) + dirent.d_name; ParsePlugin(file_name); } } zzip_dir_close(zzipdir); } } #endif } return true; }
/** 加载ZIP文件包 */ bool FZipFilePack::Load(void) { if( !m_pZipDir ) { zzip_error_t zipError; m_pZipDir = zzip_dir_open( m_sName.c_str(),&zipError ); if( !m_pZipDir ) return false; if( zipError != ZZIP_NO_ERROR ) { AString errInfo; GetZipErrorDesc( zipError,errInfo ); // 抛出异常 FLOG_WARNING( "FZipFilePack::Load(), An exception is found when loading zip archive(" + errInfo + ")!" ); return false; } ZZIP_DIRENT dirEntry; while( zzip_dir_read(m_pZipDir,&dirEntry) ) { VFileInfo info; info.pFilePack = this; AStringUtil::SplitPath( dirEntry.d_name,info.sBaseName,info.sPath ); info.nCompressedSize = static_cast<size_t>(dirEntry.d_csize); info.nUncompressedSize = static_cast<size_t>(dirEntry.st_size); info.sFileName = dirEntry.d_name; // 如果是目录 if( info.sBaseName == "" ) { info.sFileName = info.sFileName.substr( 0,info.sFileName.length()-1 ); AStringUtil::SplitPath( info.sFileName,info.sBaseName,info.sPath ); info.nCompressedSize = (size_t)-1; } m_FileInfos.push_back( info ); } } return true; }
//----------------------------------------------------------------------- void ZipArchive::load() { OGRE_LOCK_AUTO_MUTEX; if (!mZzipDir) { zzip_error_t zzipError; mZzipDir = zzip_dir_open_ext_io(mName.c_str(), &zzipError, 0, mPluginIo); checkZzipError(zzipError, "opening archive"); // Cache names ZZIP_DIRENT zzipEntry; while (zzip_dir_read(mZzipDir, &zzipEntry)) { FileInfo info; info.archive = this; // Get basename / path StringUtil::splitFilename(zzipEntry.d_name, info.basename, info.path); info.filename = zzipEntry.d_name; // Get sizes info.compressedSize = static_cast<size_t>(zzipEntry.d_csize); info.uncompressedSize = static_cast<size_t>(zzipEntry.st_size); // folder entries if (info.basename.empty()) { info.filename = info.filename.substr (0, info.filename.length () - 1); StringUtil::splitFilename(info.filename, info.basename, info.path); // Set compressed size to -1 for folders; anyway nobody will check // the compressed size of a folder, and if he does, its useless anyway info.compressedSize = size_t (-1); } #if !OGRE_RESOURCEMANAGER_STRICT else { info.filename = info.basename; } #endif mFileList.push_back(info); } } }
GList *get_filelist_from_zip(const char *archname, GtkFileFilter * filter) { GList *filelist = NULL; gchar *filename = NULL; ZZIP_DIR *dir = zzip_dir_open(archname, 0); if (dir) { ZZIP_DIRENT dirent; while (zzip_dir_read(dir, &dirent) != 0) { filename = g_strdup(dirent.d_name); if ((filter != NULL && gtk_filename_filter(filename, filter)) || filter == NULL) filelist = g_list_append(filelist, filename); } zzip_dir_close(dir); } return filelist; }
size_t getEntry(const char *name, char *contentFile, char **ptr) { size_t size=0; ZZIP_DIR *dir; ZZIP_DIRENT entry; ZZIP_FILE *file; zzip_error_t error; char *buf; o_log(DEBUGM, "opening %s", name); dir = zzip_dir_open(name, &error); if (!dir) { o_log(ERROR, "failed to open %s", name); return 0; } while(zzip_dir_read(dir, &entry)) { if( 0 == strcmp(entry.d_name, contentFile) ) { file = zzip_file_open(dir, contentFile, O_RDONLY); (void)zzip_seek(file, 0, SEEK_END); size = zzip_tell(file); (void)zzip_seek(file, 0, SEEK_SET); buf = (char *)malloc(size+1); (void)zzip_read(file, buf, size); buf[size] = '\0'; *ptr = buf; zzip_file_close(file); } } zzip_dir_close(dir); return size; }
static int zipread_showlist(request_rec *r, const char *filter) { const char *filename = r->filename; const char *pathinfo = r->path_info; unsigned int len_filter = filter ? strlen(filter) : 0; ZZIP_DIRENT dirent; int pi_start; char *ppdir; apr_array_header_t * arr = apr_array_make(r->pool, 0, sizeof(char *)); // open the zip file ZZIP_DIR *dir = zzip_dir_open(filename, 0); if (!dir) return HTTP_NOT_FOUND; r->content_type = "text/html"; zipread_showheader(r, r->uri); if (filter && *filter == '/') { filter++; len_filter--; } // figure out the parent directory ppdir = apr_pstrdup(r->pool, ""); if (pathinfo && strlen(pathinfo) >= 2) { int i; for (i = strlen(pathinfo)-2 ; i >= 1 ; i--) { if (pathinfo[i] == '/') { ppdir = apr_pstrndup(r->pool, pathinfo, i); break; } } } else { // the parent dir is outside of the zip file. ppdir = "/.."; } // find the start of pathinfo in r->uri if (pathinfo) pi_start = ap_find_path_info(r->uri, r->path_info); else pi_start = strlen(r->uri); ap_rprintf(r,"<img src=\"/icons/back.gif\" alt=\"[BCK]\" /><a href=\"%s%s/\">%s</a>\n", apr_pstrndup(r->pool, r->uri, pi_start), ppdir, "Parent Directory"); while ( zzip_dir_read(dir, &dirent) ) { if (!filter || strncmp(dirent.d_name, filter, len_filter) == 0) { char **n = apr_array_push(arr); *n = apr_pstrdup(r->pool, dirent.d_name); } } { char **list = (char **)arr->elts; char *old = ""; int i; // Sort the list of files we contructed qsort((void *)list, arr->nelts, sizeof(char *), zipread_cmp); // Show the list of files: get first path part and remove the duplicates if (1 && filter) { for (i = 0; i < arr->nelts; i++) { int dir_found = 0; char *p1; // cut off anything after the first / if ((p1 = strchr(list[i] + len_filter, '/'))) { dir_found = 1; *(p1+1) = '\0'; } if (strcmp(list[i], old) != 0) { if (list[i][strlen(list[i])-1] == '/') { dir_found = 1; // skip the base filter directory entry if (strcmp(list[i], filter) == 0) continue; } zipread_showentry(r, list[i], list[i] + len_filter, dir_found, pi_start); old = apr_pstrdup(r->pool, list[i]); } } } else { // just list all paths unfiltered for (i = 0; i < arr->nelts; i++) { int dir_found = 0; if (list[i][strlen(list[i])-1] == '/') { dir_found = 1; } zipread_showentry(r, list[i], list[i], dir_found, pi_start); } } } ap_rputs("<hr /></pre>mod_zipread on Apache 2.0 (built on "__DATE__ " " __TIME__ ")\n</body></html>", r); zzip_dir_close (dir); return OK; }
bool ZipAssetBundle::DeserializeFromDiskSource() { if (!assetAPI_->Cache()) { LogError("ZipAssetBundle::DeserializeFromDiskSource: Cannot process archive, AssetAPI cache is null."); return false; } else if (DiskSource().Empty()) { LogError("ZipAssetBundle::DeserializeFromDiskSource: Cannot process archive, no disk source for " + Name()); return false; } /* We want to detect if the extracted files are already up to date to save time. If the last modified date for the sub asset is the same as the parent zip file, we don't extract it. If the zip is re-downloaded from source everything will get unpacked even if only one file would have changed inside it. We could do uncompressed size comparisons but that is not a absolute guarantee that the file has not changed. We'll be on the safe side to unpack the whole zip file. Zip files are meant for deploying the scene and should be touched rather rarely. Note that local:// refs are unpacked to cache but the zips disk source is not in the cache. Meaning that local:// zip files will always be extracted fully even if the disk source was not changed, we don't have a mechanism to get the last modified date properly except from the asset cache. For local scenes this should be fine as there is no real need to zip the scene up as you already have the disk sources right there in the storage. The last modified query will fail if the file is open with zziplib, do it first. */ uint zipLastModified = assetAPI_->Cache()->LastModified(Name()); const String diskSourceInternal = Urho3D::GetInternalPath(DiskSource()); zzip_error_t error = ZZIP_NO_ERROR; archive_ = zzip_dir_open(diskSourceInternal.CString(), &error); if (CheckAndLogZzipError(error) || CheckAndLogArchiveError(archive_) || !archive_) { archive_ = 0; return false; } int uncompressing = 0; ZZIP_DIRENT archiveEntry; while(zzip_dir_read(archive_, &archiveEntry)) { String relativePath = Urho3D::GetInternalPath(archiveEntry.d_name); if (!relativePath.EndsWith("/")) { String subAssetRef = GetFullAssetReference(relativePath); ZipArchiveFile file; file.relativePath = relativePath; file.cachePath = Urho3D::GetInternalPath(assetAPI_->Cache()->DiskSourceByRef(subAssetRef)); file.lastModified = assetAPI_->Cache()->LastModified(subAssetRef); file.compressedSize = archiveEntry.d_csize; file.uncompressedSize = archiveEntry.st_size; /* Mark this file for extraction. If both cache files have valid dates and they differ extract. If they have the same date stamp skip extraction. Note that file.lastModified will be non-valid for non cached files so we will cover also missing files. */ file.doExtract = (zipLastModified > 0 && file.lastModified > 0) ? (zipLastModified != file.lastModified) : true; if (file.doExtract) uncompressing++; files_.Push(file); fileCount_++; } } // Close the zzip directory ptr Close(); // If the zip file was empty we don't want IsLoaded to fail on the files_ check. // The bundle loaded fine but there was no content, log a warning. if (files_.Empty()) { LogWarning("ZipAssetBundle: Bundle loaded but does not contain any files " + Name()); files_.Push(ZipArchiveFile()); Loaded.Emit(this); return true; } // Don't spin the worker if all sub assets are up to date in cache. if (uncompressing > 0) { // Now that the file info has been read, continue in a worker thread. LogDebug("ZipAssetBundle: File information read for " + Name() + ". File count: " + String(files_.Size()) + ". Starting worker thread to uncompress " + String(uncompressing) + " files."); // ZipWorker is a QRunnable we can pass to QThreadPool, it will handle scheduling it and deletes it when done. worker_ = new ZipWorker(this, zipLastModified, diskSourceInternal, files_); if (!worker_->Run()) { LogError("ZipAssetBundle: Failed to start worker thread for " + Name()); files_.Clear(); return false; } assetAPI_->GetFramework()->Frame()->Updated.Connect(this, &ZipAssetBundle::CheckDone); } else Loaded.Emit(this); return true; }
void process_zip(char *pPath) { #ifdef HAVE_LIBZZIP ZZIP_DIR *dir; ZZIP_FILE *fp; ZZIP_DIRENT dirent; char buf[BUF_SIZE]; int nRead; long depth = 0; int ret = 0; dir = zzip_dir_open(pPath, 0); if (!dir) { return; } while (zzip_dir_read(dir, &dirent)) { fp = zzip_file_open(dir, dirent.d_name, 0); if (fp) { // pull the data and scan while ((nRead = zzip_file_read(fp, buf, BUF_SIZE)) > 0) { depth += nRead; if (is_match(buf,nRead)) { ret = 1; if (!LogTotalMatches) { send_match(hit,pPath); break; } } bzero(buf, sizeof(buf)); if ((ScanDepth != 0) && (depth >= (ScanDepth * 1024))) { break; } } zzip_file_close(fp); } } if ((LogTotalMatches && TotalMatches) || ret) { send_match(hit, pPath); } zzip_dir_close(dir); #else #ifdef HAVE_LIBZIP struct zip *za; int err, ret = 0, nRead; char errstr[1024], buf[BUF_SIZE]; long depth; if ((za = zip_open(pPath, 0, &err)) == NULL) { return; } while ((nRead = zip_fread(za, &buf, BUF_SIZE)) > 0) { depth += nRead; if (is_match(buf,nRead)) { ret = 1; if (!LogTotalMatches) { send_match(hit,pPath); zip_close(za); return; } } bzero(buf, sizeof(buf)); if ((ScanDepth != 0) && (depth >= (ScanDepth * 1024))) { break; } } if ((LogTotalMatches && TotalMatches) || ret) { send_match(hit, pPath); } zip_close(za); #endif /* HAVE_LIBZIP */ return; #endif /* HAVE_ZZIP */ }
void RasterWeatherStore::ScanAll(const GeoPoint &location, OperationEnvironment &operation) { /* not holding the lock here, because this method is only called during startup, when the other threads aren't running yet */ operation.SetText(_("Scanning weather forecast")); ZZIP_DIR *dir = OpenArchive(); if (dir == nullptr) return; operation.SetProgressRange(MAX_WEATHER_TIMES); maps.clear(); std::set<tstring> names; for (const auto &i : WeatherDescriptors) { if (i.name == nullptr) { /* special case: 0 = terrain */ assert(maps.empty()); auto &m = maps.append(); m.name.clear(); m.label = i.label; m.help = i.help; } else { if (maps.full()) break; MapItem item(i.name); item.label = i.label; item.help = i.help; if (ScanMapItem(dir, item)) maps.push_back(item); names.insert(i.name); } } ZZIP_DIRENT e; while (zzip_dir_read(dir, &e)) { if (maps.full()) break; const char *filename = e.d_name; if (!StringEndsWith(filename, ".jp2")) continue; MapItem item(_T("")); const char *dot = strchr(filename, '.'); if (dot == nullptr || dot == filename || size_t(dot - filename) >= item.name.capacity()) continue; item.name.SetASCII(filename, dot); item.label = nullptr; item.help = nullptr; if (!names.insert(item.name.c_str()).second) continue; if (ScanMapItem(dir, item)) maps.push_back(item); } // TODO: scan the rest zzip_dir_close(dir); }
bool CZipArchive::loadArchive(){ static mchar tempBuff[128]; if(!m_zipDir){ zzip_error_t err; //m_file=gFileSystem.openFile(getName()); //if(!m_file || !m_file->getFD())return 0; //m_zipDir = zzip_dir_fdopen(m_file->getFD()->_file,&err); core::stringc fileName; core::string_to_char(getName(),fileName); m_zipDir = zzip_dir_open(fileName.c_str(),&err); if(checkForError(err,mT(" loadArchive()")))return false; ZZIP_DIRENT zipEntry; sFileEntry entry; core::string tstr; core::string zipName; core::stringc tmpc; while(zzip_dir_read(m_zipDir,&zipEntry)){ zipName=zipEntry.d_name; zipName.replaceChar('\\','/'); int x=zipName.findlast('/'); if(x==zipName.length()){ // entry.dirPath=zipName; // entry.filePath=zipName; entry.dirPath=mT(""); entry.filePath=mT(""); zipName[zipName.length()]='\0'; x=zipName.findlast('/'); tstr=zipName.substr(x,zipName.length()-x+1); entry.fileName=tstr; entry.isfolder=true; entry.fileData=0; }else{ //entry.filePath=zipName; entry.dirPath=mT(""); entry.filePath=mT(""); tstr=zipName.substr(x+1,zipName.length()); entry.fileName=tstr; core::string_to_char(zipName,tmpc); zzip_file*zipFile=zzip_file_open(m_zipDir,tmpc.c_str(),ZZIP_ONLYZIP | ZZIP_CASELESS); if(!zipFile){ int zerr = zzip_error(m_zipDir); checkForError(zerr,mT("open zip file")); entry.fileData=0; }else{ ZZIP_STAT zstate; zzip_dir_stat(m_zipDir,tmpc.c_str(),&zstate,ZZIP_CASEINSENSITIVE); entry.fileData=new CZipFile(entry.fileName.c_str(),zipFile,zstate.st_size); } int s=zipName.find('/'); tstr=zipName.substr(s+1,x-s); entry.dirPath=tstr; entry.filePath=tstr+entry.fileName; entry.isfolder=false; } entry.compressSize=zipEntry.d_csize; entry.uncompressSize=zipEntry.st_size; m_files.push_back(entry); } } return 1; }
static int unzzip_cat (int argc, char ** argv, int extract) { int done = 0; int argn; ZZIP_DIR* disk; zzip_error_t error; if (argc == 1) { printf (__FILE__" version "ZZIP_PACKAGE" "ZZIP_VERSION"\n"); return EXIT_OK; /* better provide an archive argument */ } disk = zzip_dir_open (argv[1], &error); if (! disk) { fprintf(stderr, "%s: %s\n", argv[1], zzip_strerror(error)); return exitcode(error); } if (argc == 2) { /* list all */ ZZIP_DIRENT entry; while(zzip_dir_read(disk, &entry)) { char* name = entry.d_name; FILE* out = stdout; if (extract) out = create_fopen(name, "wb", 1); if (! out) { DBG3("fopen' %s : %s", name, strerror(errno)); if (errno != EISDIR) done = EXIT_ERRORS; continue; } unzzip_cat_file (disk, name, out); if (extract) fclose(out); } } else { /* list only the matching entries - in order of zip directory */ ZZIP_DIRENT entry; while(zzip_dir_read(disk, &entry)) { char* name = entry.d_name; for (argn=1; argn < argc; argn++) { if (! _zzip_fnmatch (argv[argn], name, _zzip_FNM_NOESCAPE|_zzip_FNM_PATHNAME|_zzip_FNM_PERIOD)) { FILE* out = stdout; if (extract) out = create_fopen(name, "wb", 1); if (! out) { DBG3("fopen. %s : %s", name, strerror(errno)); if (errno != EISDIR) done = EXIT_ERRORS; continue; } unzzip_cat_file (disk, name, out); if (extract) fclose(out); break; /* match loop */ } } } } zzip_dir_close(disk); return done; }