00001 /* 00002 ----------------------------------------------------------------------------- 00003 This source file is part of OGRE 00004 (Object-oriented Graphics Rendering Engine) 00005 For the latest info, see http://www.ogre3d.org/ 00006 00007 Copyright © 2000-2002 The OGRE Team 00008 Also see acknowledgements in Readme.html 00009 00010 This program is free software; you can redistribute it and/or modify it under 00011 the terms of the GNU Lesser General Public License as published by the Free Software 00012 Foundation; either version 2 of the License, or (at your option) any later 00013 version. 00014 00015 This program is distributed in the hope that it will be useful, but WITHOUT 00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public License along with 00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple 00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to 00022 http://www.gnu.org/copyleft/lesser.txt. 00023 ----------------------------------------------------------------------------- 00024 */ 00025 #include "OgreStableHeaders.h" 00026 #include "OgreZip.h" 00027 #include "unzip.h" 00028 00029 #include "OgreArchiveManager.h" 00030 #include "OgreLogManager.h" 00031 #include "OgreException.h" 00032 #include "OgreZipArchiveFactory.h" 00033 #include "OgreStringVector.h" 00034 #include "OgreRoot.h" 00035 00036 namespace Ogre { 00037 00038 //----------------------------------------------------------------------- 00039 bool Zip::fileOpen( const String& strFile, FILE** ppFile ) const 00040 { 00041 unz_file_info tagUFI; 00042 FILE *pFile; 00043 00044 if( unzLocateFile( mArchive, strFile.c_str(), 2 ) == UNZ_OK ) { 00045 //*ppFile = tmpfile(); 00046 pFile = *ppFile; 00047 00048 unzGetCurrentFileInfo( mArchive, &tagUFI, NULL, 0, NULL, 0, NULL, 0 ); 00049 unsigned char* pBuffer = new unsigned char[tagUFI.uncompressed_size]; 00050 unzOpenCurrentFile( mArchive ); 00051 unzReadCurrentFile( mArchive, (void*)pBuffer, tagUFI.uncompressed_size ); 00052 unzCloseCurrentFile( mArchive ); 00053 fwrite( (void*) pBuffer, 1, tagUFI.uncompressed_size, pFile ); 00054 delete[] pBuffer; 00055 00056 fseek( pFile, 0, SEEK_SET ); 00057 return true; 00058 } 00059 00060 return false; 00061 } 00062 00063 //----------------------------------------------------------------------- 00064 bool Zip::fileRead( const String& strFile, DataChunk** ppChunk ) const 00065 { 00066 DataChunk* pChunk = *ppChunk; 00067 unz_file_info tagUFI; 00068 00069 if( unzLocateFile( mArchive, strFile.c_str(), 2 ) == UNZ_OK ) { 00070 unzGetCurrentFileInfo( mArchive, &tagUFI, NULL, 0, NULL, 0, NULL, 0 ); 00071 pChunk->allocate(tagUFI.uncompressed_size); 00072 unzOpenCurrentFile( mArchive ); 00073 unzReadCurrentFile( mArchive, (void*)pChunk->getPtr(), tagUFI.uncompressed_size ); 00074 unzCloseCurrentFile( mArchive ); 00075 00076 return true; 00077 } 00078 00079 return false; 00080 } 00081 00082 //----------------------------------------------------------------------- 00083 bool Zip::fileSave( ::FILE* pFile, const String& strPath, bool bOverwrite /* = false */ ) 00084 { 00085 return false; 00086 } 00087 00088 //----------------------------------------------------------------------- 00089 bool Zip::fileWrite( const DataChunk& refChunk, const String& strPath, bool bOverwrite /* = false */ ) 00090 { 00091 return false; 00092 } 00093 00094 //----------------------------------------------------------------------- 00095 bool Zip::fileTest( const String& strFile ) const 00096 { 00097 if( unzLocateFile( mArchive, strFile.c_str(), 2 ) == UNZ_OK ) 00098 return true; 00099 return false; 00100 } 00101 00102 //----------------------------------------------------------------------- 00103 bool Zip::fileCopy( const String& strSrc, const String& strDest, bool bOverwrite ) 00104 { 00105 return false; 00106 } 00107 00108 //----------------------------------------------------------------------- 00109 bool Zip::fileMove( const String& strSrc, const String& strDest, bool bOverwrite ) 00110 { 00111 return false; 00112 } 00113 00114 //----------------------------------------------------------------------- 00115 bool Zip::fileDele( const String& strFile ) 00116 { 00117 return false; 00118 } 00119 00120 //----------------------------------------------------------------------- 00121 bool Zip::fileInfo( const String& strFile, FileInfo** ppInfo ) const 00122 { 00123 return true; 00124 } 00125 00126 //----------------------------------------------------------------------- 00127 std::vector<String> Zip::dirGetFiles( const String& strDir ) const 00128 { 00129 return const_cast<Zip *>(this)->getAllNamesLike( strDir, "", false ); 00130 } 00131 00132 //----------------------------------------------------------------------- 00133 std::vector<String> Zip::dirGetSubs( const String& strDir ) const 00134 { 00135 return std::vector<String>(); 00136 } 00137 00138 //----------------------------------------------------------------------- 00139 bool Zip::dirDele( const String& strDir, bool bRecursive ) 00140 { 00141 return false; 00142 }; 00143 00144 //----------------------------------------------------------------------- 00145 bool Zip::dirMove( const String& strSrc, const String& strDest, bool bOverwrite ) 00146 { 00147 return false; 00148 }; 00149 00150 //----------------------------------------------------------------------- 00151 bool Zip::dirInfo( const String& strDir, FileInfo** ppInfo ) const 00152 { 00153 return false; 00154 }; 00155 00156 //----------------------------------------------------------------------- 00157 bool Zip::dirCopy( const String& strSrc, const String& strDest, bool bOverwrite ) 00158 { 00159 return false; 00160 }; 00161 00162 //----------------------------------------------------------------------- 00163 bool Zip::dirTest( const String& strDir ) const 00164 { 00165 return false; 00166 }; 00167 00168 //----------------------------------------------------------------------- 00169 StringVector Zip::getAllNamesLike( const String& strStartPath, const String& strPattern, bool bRecursive /* = true */ ) 00170 { 00171 StringVector retVec; 00172 unz_file_info info; 00173 String filename; 00174 String szPattern; 00175 char tmpFilename[260]; 00176 char extraField[260]; 00177 char comment[260]; 00178 00179 szPattern = strPattern; 00180 StringUtil::toLowerCase(szPattern); 00181 00182 int iRes = unzGoToFirstFile(mArchive); 00183 while( iRes == UNZ_OK ) 00184 { 00185 00186 unzGetCurrentFileInfo( mArchive, 00187 &info, 00188 tmpFilename, 259, 00189 extraField, 259, 00190 comment, 259 ); 00191 00192 filename = tmpFilename; 00193 00194 if( info.uncompressed_size > 0 ) 00195 { 00196 StringUtil::toLowerCase(filename); 00197 00198 if( static_cast<int>(filename.find(szPattern)) >= 0 ) 00199 { 00200 if (strStartPath.length() > 0 && strStartPath != "./") 00201 { 00202 if (static_cast<int>(filename.find(strStartPath)) >= 0) 00203 retVec.push_back( filename ); 00204 } 00205 else 00206 retVec.push_back( filename ); 00207 } 00208 } 00209 00210 iRes = unzGoToNextFile( mArchive ); 00211 } 00212 00213 return retVec; 00214 }; 00215 00216 //----------------------------------------------------------------------- 00217 void Zip::load() { 00218 struct stat tagStat; 00219 stat( mName.c_str(), &tagStat ); 00220 00221 if( ( tagStat.st_mode & S_IFDIR ) ) 00222 Except( Exception::ERR_FILE_NOT_FOUND, "Zip archive " + mName + " not found.", 00223 "Zip::load" ); 00224 else 00225 mArchive = unzOpen( mName.c_str() ); 00226 00227 LogManager::getSingleton().logMessage( "Zip Archive codec for " + mName + " created."); 00228 mIsLoaded = true; 00229 }; 00230 00231 //----------------------------------------------------------------------- 00232 void Zip::unload() { 00233 if( mArchive ) 00234 unzClose( mArchive ); 00235 00236 LogManager::getSingleton().logMessage( "Zip Archive Codec for " + mName + " unloaded." ); 00237 00238 delete this; 00239 }; 00240 00241 //----------------------------------------------------------------------- 00242 Zip::Zip() {} 00243 00244 //----------------------------------------------------------------------- 00245 Zip::Zip( const String& name ) 00246 { 00247 mName = name; 00248 } 00249 00250 //----------------------------------------------------------------------- 00251 Zip::~Zip() {} 00252 00253 }
Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:52 2004