//----------------------------------------------------------------------- DataStreamPtr ZipArchive::open(const String& filename) const { // Format not used here (always binary) ZZIP_FILE* zzipFile = zzip_file_open(mZzipDir, filename.c_str(), ZZIP_ONLYZIP | ZZIP_CASELESS); if (!zzipFile) { int zerr = zzip_error(mZzipDir); String zzDesc = getZzipErrorDescription((zzip_error_t)zerr); LogManager::getSingleton().logMessage( mName + " - Unable to open file " + filename + ", error was '" + zzDesc + "'"); // return null pointer return DataStreamPtr(); } // Get uncompressed size too ZZIP_STAT zstat; zzip_dir_stat(mZzipDir, filename.c_str(), &zstat, ZZIP_CASEINSENSITIVE); // Construct & return stream return DataStreamPtr(new ZipDataStream(filename, zzipFile, static_cast<size_t>(zstat.st_size))); }
//----------------------------------------------------------------------- DataStreamPtr ZipArchive::open(const String& filename, bool readOnly) const { // zziplib is not threadsafe OGRE_LOCK_AUTO_MUTEX; String lookUpFileName = filename; #if OGRE_RESOURCEMANAGER_STRICT const int flags = 0; #else const int flags = ZZIP_CASELESS; #endif // Format not used here (always binary) ZZIP_FILE* zzipFile = zzip_file_open(mZzipDir, lookUpFileName.c_str(), ZZIP_ONLYZIP | flags); #if !OGRE_RESOURCEMANAGER_STRICT if (!zzipFile) // Try if we find the file { String basename, path; StringUtil::splitFilename(lookUpFileName, basename, path); const FileInfoListPtr fileNfo = findFileInfo(basename, true); if (fileNfo->size() == 1) // If there are more files with the same do not open anyone { Ogre::FileInfo info = fileNfo->at(0); lookUpFileName = info.path + info.basename; zzipFile = zzip_file_open(mZzipDir, lookUpFileName.c_str(), ZZIP_ONLYZIP | flags); // When an error happens here we will catch it below } } #endif if (!zzipFile) { int zerr = zzip_error(mZzipDir); String zzDesc = getZzipErrorDescription((zzip_error_t)zerr); OGRE_EXCEPT(Exception::ERR_FILE_NOT_FOUND, mName+ " Cannot open file: " + lookUpFileName + " - "+zzDesc, "ZipArchive::open"); } // Get uncompressed size too ZZIP_STAT zstat; zzip_dir_stat(mZzipDir, lookUpFileName.c_str(), &zstat, flags); // Construct & return stream return DataStreamPtr(OGRE_NEW ZipDataStream(lookUpFileName, zzipFile, static_cast<size_t>(zstat.st_size))); }
//----------------------------------------------------------------------- DataStreamPtr ZipArchive::open(const String& filename, bool readOnly) { // zziplib is not threadsafe OGRE_LOCK_AUTO_MUTEX; String lookUpFileName = filename; // Format not used here (always binary) ZZIP_FILE* zzipFile = zzip_file_open(mZzipDir, lookUpFileName.c_str(), ZZIP_ONLYZIP | ZZIP_CASELESS); if (!zzipFile) // Try if we find the file { const Ogre::FileInfoListPtr fileNfo = findFileInfo(lookUpFileName, true); if (fileNfo->size() == 1) // If there are more files with the same do not open anyone { Ogre::FileInfo info = fileNfo->at(0); lookUpFileName = info.path + info.basename; zzipFile = zzip_file_open(mZzipDir, lookUpFileName.c_str(), ZZIP_ONLYZIP | ZZIP_CASELESS); // When an error happens here we will catch it below } } if (!zzipFile) { int zerr = zzip_error(mZzipDir); String zzDesc = getZzipErrorDescription((zzip_error_t)zerr); LogManager::getSingleton().logMessage( mName + " - Unable to open file " + lookUpFileName + ", error was '" + zzDesc + "'", LML_CRITICAL); // return null pointer return DataStreamPtr(); } // Get uncompressed size too ZZIP_STAT zstat; zzip_dir_stat(mZzipDir, lookUpFileName.c_str(), &zstat, ZZIP_CASEINSENSITIVE); // Construct & return stream return DataStreamPtr(OGRE_NEW ZipDataStream(lookUpFileName, zzipFile, static_cast<size_t>(zstat.st_size))); }
/** 打开一个ZIP文件 @Param 文件的名字 */ FVFileStream* FZipFilePack::Open( const AString& filename ) { ZZIP_FILE* pFile = zzip_file_open( m_pZipDir,filename.c_str(), ZZIP_ONLYZIP|ZZIP_CASELESS ); if( !pFile ) { AString errDesc; int zipError = zzip_error( m_pZipDir ); if( zipError != ZZIP_NO_ERROR ) { GetZipErrorDesc( zipError,errDesc ); FLOG_WARNINGF( "FZipFilePack::Open(), Open file (%s) from ZIP package(%s) failed!",filename.c_str(),m_sName.c_str() ); } return NULL; } ZZIP_STAT zipStat; zzip_dir_stat( m_pZipDir,filename.c_str(),&zipStat,ZZIP_CASEINSENSITIVE ); // 创建一个ZIP文件流 return new FZipFileStream( filename,pFile,zipStat.st_size ); }
static int zipread_showfile(request_rec *r, const char *fname) { char *zipfile, *name; ZZIP_DIR *dir; unsigned int itnum; if (!r->path_info) return HTTP_NOT_FOUND; zipfile = r->filename; if (!fname || !*fname) { name = apr_pstrdup(r->pool, r->path_info); } else { name = apr_pstrcat(r->pool, r->path_info, fname, NULL); } r->content_type = zipread_getcontenttype(r, name); if (*name == '/') name++; // ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "mod_zipread showfile: %s - %s - %s", zipfile, fname, name); for(itnum = 0; itnum < 5; itnum++) { dir = zzip_dir_open(zipfile, 0); if (dir) { ZZIP_STAT st; // fetch stat info of filename, before opening it if (zzip_dir_stat(dir, name, &st, 0) != 0) { // check if a directory entry is available for that name. name = apr_pstrcat(r->pool, name, "/", NULL); if (zzip_dir_stat(dir, name, &st, 0) != 0) { zzip_dir_close(dir); ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "mod_zipread showfile stat failed: %d - %s", zzip_error(dir), zzip_strerror(zzip_error(dir))); return HTTP_NOT_FOUND; } // found a directory entry, do an external redirect to get the // links in the directory listing right. name = apr_pstrcat(r->pool, r->uri, "/", NULL); apr_table_setn(r->headers_out, "Location", name); // ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "mod_zipread showfile directory entry."); return HTTP_MOVED_PERMANENTLY; } ap_set_content_length(r, st.st_size); // cannot check last-modified date of the file itself here, because // zziplib doesnt extract it. instead we use the zip file's date r->mtime = r->finfo.mtime; ap_set_last_modified(r); if (!r->header_only) { ZZIP_FILE *fp = zzip_file_open(dir, name, 0); if (fp) { int len; char buf[32769]; while ((len = zzip_file_read (fp, buf, 32768))) { ap_rwrite(buf, len, r); } zzip_file_close(fp); zzip_dir_close(dir); return OK; } else { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "mod_zipread showfile file open failed: %d - %s.", zzip_error(dir), zzip_strerror(zzip_error(dir))); if (zzip_dir_stat(dir, name, &st, 0) != 0) { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "mod_zipread showfile after stat failed: %d - %s", zzip_error(dir), zzip_strerror(zzip_error(dir))); break; } zzip_dir_close(dir); continue; } } zzip_dir_close(dir); return OK; } else { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "mod_zipread showfile zip file not open."); return HTTP_NOT_FOUND; } } zzip_dir_close (dir); return HTTP_NOT_FOUND; }
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; }