Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreZip.cpp

Go to the documentation of this file.
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 
00028 #include "OgreArchiveManager.h"
00029 #include "OgreLogManager.h"
00030 #include "OgreException.h"
00031 #include "OgreZipArchiveFactory.h"
00032 #include "OgreStringVector.h"
00033 #include "OgreRoot.h"
00034 
00035 namespace Ogre {
00036 
00037     //-----------------------------------------------------------------------
00038     bool Zip::fileOpen( const String& strFile, FILE** ppFile ) const
00039     {
00040         unz_file_info tagUFI;
00041         FILE *pFile;
00042 
00043         if( unzLocateFile( mArchive, strFile.c_str(), 2 ) == UNZ_OK ) {
00044             //*ppFile = tmpfile();
00045             pFile = *ppFile;
00046 
00047             unzGetCurrentFileInfo( mArchive, &tagUFI, NULL, 0, NULL, 0, NULL, 0 );
00048             unsigned char* pBuffer = new unsigned char[tagUFI.uncompressed_size];
00049             unzOpenCurrentFile( mArchive );
00050             unzReadCurrentFile( mArchive, (void*)pBuffer, tagUFI.uncompressed_size );
00051             unzCloseCurrentFile( mArchive );
00052             fwrite( (void*) pBuffer, 1, tagUFI.uncompressed_size, pFile );
00053             delete[] pBuffer;
00054 
00055             fseek( pFile, 0, SEEK_SET );
00056             return true;
00057         }
00058 
00059         return false;
00060     }
00061 
00062     //-----------------------------------------------------------------------
00063     bool Zip::fileRead( const String& strFile, DataChunk** ppChunk ) const
00064     {
00065         DataChunk* pChunk = *ppChunk;
00066         unz_file_info tagUFI;
00067 
00068         if( unzLocateFile( mArchive, strFile.c_str(), 2 ) == UNZ_OK ) {
00069             unzGetCurrentFileInfo( mArchive, &tagUFI, NULL, 0, NULL, 0, NULL, 0 );
00070             pChunk->allocate(tagUFI.uncompressed_size);
00071             unzOpenCurrentFile( mArchive );
00072             unzReadCurrentFile( mArchive, (void*)pChunk->getPtr(), tagUFI.uncompressed_size );
00073             unzCloseCurrentFile( mArchive );
00074 
00075             return true;
00076         }
00077 
00078         return false;
00079     }
00080 
00081     //-----------------------------------------------------------------------
00082     bool Zip::fileSave( ::FILE* pFile, const String& strPath, bool bOverwrite /* = false */ )
00083     {
00084         return false;
00085     }
00086 
00087     //-----------------------------------------------------------------------
00088     bool Zip::fileWrite( const DataChunk& refChunk, const String& strPath, bool bOverwrite /* = false */ )
00089     {
00090         return false;
00091     }
00092 
00093     //-----------------------------------------------------------------------
00094     bool Zip::fileTest( const String& strFile ) const
00095     {
00096         if( unzLocateFile( mArchive, strFile.c_str(), 2 ) == UNZ_OK )
00097             return true;
00098         return false;
00099     }
00100 
00101     //-----------------------------------------------------------------------
00102     bool Zip::fileCopy( const String& strSrc, const String& strDest, bool bOverwrite )
00103     {
00104         return false;
00105     }
00106 
00107     //-----------------------------------------------------------------------
00108     bool Zip::fileMove( const String& strSrc, const String& strDest, bool bOverwrite )
00109     {
00110         return false;
00111     }
00112 
00113     //-----------------------------------------------------------------------
00114     bool Zip::fileDele( const String& strFile )
00115     {
00116         return false;
00117     }
00118 
00119     //-----------------------------------------------------------------------
00120     bool Zip::fileInfo( const String& strFile, FileInfo** ppInfo ) const
00121     {
00122         return true;
00123     }
00124 
00125     //-----------------------------------------------------------------------
00126     std::vector<String> Zip::dirGetFiles( const String& strDir ) const
00127     {
00128         return const_cast<Zip *>(this)->getAllNamesLike( strDir, "", false );
00129     }
00130 
00131     //-----------------------------------------------------------------------
00132     std::vector<String> Zip::dirGetSubs( const String& strDir ) const
00133     {
00134         return std::vector<String>();
00135     }
00136 
00137     //-----------------------------------------------------------------------
00138     bool Zip::dirDele( const String& strDir, bool bRecursive )
00139     {
00140         return false;
00141     };
00142 
00143     //-----------------------------------------------------------------------
00144     bool Zip::dirMove( const String& strSrc, const String& strDest, bool bOverwrite )
00145     {
00146         return false;
00147     };
00148 
00149     //-----------------------------------------------------------------------
00150     bool Zip::dirInfo( const String& strDir, FileInfo** ppInfo ) const
00151     {
00152         return false;
00153     };
00154 
00155     //-----------------------------------------------------------------------
00156     bool Zip::dirCopy( const String& strSrc, const String& strDest, bool bOverwrite )
00157     {
00158         return false;
00159     };
00160 
00161     //-----------------------------------------------------------------------
00162     bool Zip::dirTest( const String& strDir ) const
00163     {
00164         return false;
00165     };
00166 
00167     //-----------------------------------------------------------------------
00168     StringVector Zip::getAllNamesLike( const String& strStartPath, const String& strPattern, bool bRecursive /* = true */ )
00169     {
00170         StringVector retVec;
00171         unz_file_info info;
00172         String filename;
00173         String szPattern;
00174         char tmpFilename[260];
00175         char extraField[260];
00176         char comment[260];
00177 
00178         szPattern = strPattern;
00179         szPattern.toLowerCase();
00180 
00181         int iRes = unzGoToFirstFile(mArchive);
00182         while( iRes == UNZ_OK )
00183         {
00184 
00185             unzGetCurrentFileInfo( mArchive,
00186                          &info,
00187                          tmpFilename, 259,
00188                          extraField, 259,
00189                          comment, 259 );
00190 
00191             filename = tmpFilename;
00192 
00193             if( info.uncompressed_size > 0 )
00194             {
00195                 filename.toLowerCase();
00196             
00197                 if( static_cast<int>(filename.find(szPattern)) >= 0 )
00198                 {
00199                     if (strStartPath.length() > 0 && strStartPath != "./")
00200                     {
00201                         if (static_cast<int>(filename.find(strStartPath)) >= 0)
00202                             retVec.push_back( filename );
00203                     }
00204                     else
00205                         retVec.push_back( filename );
00206                 }
00207             }
00208 
00209             iRes = unzGoToNextFile( mArchive );
00210         }
00211 
00212         return retVec;
00213     };
00214 
00215     //-----------------------------------------------------------------------
00216     void Zip::load() {
00217         struct stat tagStat;
00218         stat( mName.c_str(), &tagStat );
00219 
00220         if( ( tagStat.st_mode & S_IFDIR ) )
00221             Except( Exception::ERR_FILE_NOT_FOUND, "Zip archive " + mName + " not found.",
00222                 "Zip::load" );
00223         else
00224             mArchive = unzOpen( mName.c_str() );
00225 
00226         LogManager::getSingleton().logMessage( "Zip Archive codec for " + mName + " created.");
00227         mIsLoaded = true;
00228     };
00229 
00230     //-----------------------------------------------------------------------
00231     void Zip::unload() {
00232         if( mArchive )
00233             unzClose( mArchive );
00234 
00235         LogManager::getSingleton().logMessage( "Zip Archive Codec for " + mName + " unloaded." );
00236 
00237         delete this;
00238     };
00239 
00240     //-----------------------------------------------------------------------
00241     Zip::Zip() {}
00242 
00243     //-----------------------------------------------------------------------
00244     Zip::Zip( const String& name )
00245     {
00246         mName = name;
00247     }
00248 
00249     //-----------------------------------------------------------------------
00250     Zip::~Zip() {}
00251 
00252 }

Copyright © 2002-2003 by The OGRE Team
Last modified Wed Jan 21 00:10:32 2004