lib Library API Documentation

koZipStore.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2000-2002 David Faure <david@mandrakesoft.com>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "koZipStore.h"
00021 #include <kzip.h>
00022 #include <kdebug.h>
00023 #include <kdeversion.h>
00024 #include <qbuffer.h>
00025 
00026 KoZipStore::KoZipStore( const QString & _filename, Mode _mode, const QCString & appIdentification )
00027 {
00028     kdDebug(s_area) << "KoZipStore Constructor filename = " << _filename
00029                     << " mode = " << int(_mode)
00030                     << " mimetype = " << appIdentification << endl;
00031 
00032     m_pZip = new KZip( _filename );
00033     m_bGood = init( _mode, appIdentification ); // open the zip file and init some vars
00034 }
00035 
00036 KoZipStore::KoZipStore( QIODevice *dev, Mode mode, const QCString & appIdentification )
00037 {
00038     m_pZip = new KZip( dev );
00039     m_bGood = init( mode, appIdentification );
00040 }
00041 
00042 KoZipStore::~KoZipStore()
00043 {
00044     kdDebug(s_area) << "KoZipStore::~KoZipStore" << endl;
00045     m_pZip->close();
00046     delete m_pZip;
00047 }
00048 
00049 bool KoZipStore::init( Mode _mode, const QCString& appIdentification )
00050 {
00051     KoStore::init( _mode );
00052     m_currentDir = 0;
00053     bool good = m_pZip->open( _mode == Write ? IO_WriteOnly : IO_ReadOnly );
00054 
00055     if ( good && _mode == Read )
00056         good = m_pZip->directory() != 0;
00057     else if ( good && _mode == Write )
00058     {
00059         //kdDebug(s_area) << "KoZipStore::init writing mimetype " << appIdentification << endl;
00060 
00061         m_pZip->setCompression( KZip::NoCompression );
00062 #if KDE_IS_VERSION(3,1,93)
00063         m_pZip->setExtraField( KZip::NoExtraField );
00064 #endif
00065         // Write identification
00066         (void)m_pZip->writeFile( "mimetype", "", "", appIdentification.length(), appIdentification.data() );
00067         m_pZip->setCompression( KZip::DeflateCompression );
00068         // We don't need the extra field in KOffice - so we leave it as "no extra field".
00069     }
00070     return good;
00071 }
00072 
00073 bool KoZipStore::openWrite( const QString& name )
00074 {
00075 #if 0
00076     // Prepare memory buffer for writing
00077     m_byteArray.resize( 0 );
00078     m_stream = new QBuffer( m_byteArray );
00079     m_stream->open( IO_WriteOnly );
00080     return true;
00081 #endif
00082     m_stream = 0L; // Don't use!
00083     return m_pZip->prepareWriting( name, "", "" /*m_pZip->rootDir()->user(), m_pZip->rootDir()->group()*/, 0 );
00084 }
00085 
00086 bool KoZipStore::openRead( const QString& name )
00087 {
00088     const KArchiveEntry * entry = m_pZip->directory()->entry( name );
00089     if ( entry == 0L )
00090     {
00091         //kdWarning(s_area) << "Unknown filename " << name << endl;
00092         //return KIO::ERR_DOES_NOT_EXIST;
00093         return false;
00094     }
00095     if ( entry->isDirectory() )
00096     {
00097         kdWarning(s_area) << name << " is a directory !" << endl;
00098         //return KIO::ERR_IS_DIRECTORY;
00099         return false;
00100     }
00101     // Must cast to KZipFileEntry, not only KArchiveFile, because device() isn't virtual!
00102     const KZipFileEntry * f = static_cast<const KZipFileEntry *>(entry);
00103     delete m_stream;
00104     m_stream = f->device();
00105     m_iSize = f->size();
00106     return true;
00107 }
00108 
00109 Q_LONG KoZipStore::write( const char* _data, Q_ULONG _len )
00110 {
00111   if ( _len == 0L ) return 0;
00112   //kdDebug(s_area) << "KoZipStore::write " << _len << endl;
00113 
00114   if ( !m_bIsOpen )
00115   {
00116     kdError(s_area) << "KoStore: You must open before writing" << endl;
00117     return 0L;
00118   }
00119   if ( m_mode != Write  )
00120   {
00121     kdError(s_area) << "KoStore: Can not write to store that is opened for reading" << endl;
00122     return 0L;
00123   }
00124 
00125   m_iSize += _len;
00126   if ( m_pZip->writeData( _data, _len ) ) // writeData returns a bool!
00127       return _len;
00128   return 0L;
00129 }
00130 
00131 bool KoZipStore::closeWrite()
00132 {
00133     kdDebug(s_area) << "Wrote file " << m_sName << " into ZIP archive. size "
00134                     << m_iSize << endl;
00135     return m_pZip->doneWriting( m_iSize );
00136 #if 0
00137     if ( !m_pZip->writeFile( m_sName , "user", "group", m_iSize, m_byteArray.data() ) )
00138         kdWarning( s_area ) << "Failed to write " << m_sName << endl;
00139     m_byteArray.resize( 0 ); // save memory
00140     return true;
00141 #endif
00142 }
00143 
00144 bool KoZipStore::enterRelativeDirectory( const QString& dirName )
00145 {
00146     if ( m_mode == Read ) {
00147         if ( !m_currentDir ) {
00148             m_currentDir = m_pZip->directory(); // initialize
00149             Q_ASSERT( m_currentPath.isEmpty() );
00150         }
00151         const KArchiveEntry *entry = m_currentDir->entry( dirName );
00152         if ( entry && entry->isDirectory() ) {
00153             m_currentDir = dynamic_cast<const KArchiveDirectory*>( entry );
00154             return m_currentDir != 0;
00155         }
00156         return false;
00157     }
00158     else  // Write, no checking here
00159         return true;
00160 }
00161 
00162 bool KoZipStore::enterAbsoluteDirectory( const QString& path )
00163 {
00164     if ( path.isEmpty() )
00165     {
00166         m_currentDir = 0;
00167         return true;
00168     }
00169     m_currentDir = dynamic_cast<const KArchiveDirectory*>( m_pZip->directory()->entry( path ) );
00170     Q_ASSERT( m_currentDir );
00171     return m_currentDir != 0;
00172 }
00173 
00174 bool KoZipStore::fileExists( const QString& absPath )
00175 {
00176     return m_pZip->directory()->entry( absPath ) != 0;
00177 }
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Mar 20 14:25:28 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003