koZipStore.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "koZipStore.h"
00021
00022 #include <qbuffer.h>
00023
00024 #include <kzip.h>
00025 #include <kdebug.h>
00026 #include <kdeversion.h>
00027 #include <kurl.h>
00028 #include <kio/netaccess.h>
00029 #if ! KDE_IS_VERSION( 3, 4, 1 )
00030 #include <qdir.h>
00031 #include <qfileinfo.h>
00032 #endif
00033
00034 KoZipStore::KoZipStore( const QString & _filename, Mode _mode, const QCString & appIdentification )
00035 {
00036 kdDebug(s_area) << "KoZipStore Constructor filename = " << _filename
00037 << " mode = " << int(_mode)
00038 << " mimetype = " << appIdentification << endl;
00039
00040 m_pZip = new KZip( _filename );
00041
00042 #if ! KDE_IS_VERSION( 3, 4, 1 )
00043
00044
00045 QDir dir( QFileInfo( _filename ).dir() );
00046 if (_mode == Write && !QFileInfo( dir.path() ).isWritable() )
00047 {
00048 kdWarning(s_area) << dir.path() << " isn't writable" << endl;
00049 m_bGood = false;
00050 m_currentDir = 0;
00051 KoStore::init( _mode );
00052 }
00053 else
00054 #endif
00055 {
00056 m_bGood = init( _mode, appIdentification );
00057 }
00058 }
00059
00060 KoZipStore::KoZipStore( QIODevice *dev, Mode mode, const QCString & appIdentification )
00061 {
00062 m_pZip = new KZip( dev );
00063 m_bGood = init( mode, appIdentification );
00064 }
00065
00066 KoZipStore::KoZipStore( QWidget* window, const KURL & _url, const QString & _filename, Mode _mode, const QCString & appIdentification )
00067 {
00068 kdDebug(s_area) << "KoZipStore Constructor url" << _url.prettyURL()
00069 << " filename = " << _filename
00070 << " mode = " << int(_mode)
00071 << " mimetype = " << appIdentification << endl;
00072
00073 m_url = _url;
00074 m_window = window;
00075
00076 if ( _mode == KoStore::Read )
00077 {
00078 m_fileMode = KoStoreBase::RemoteRead;
00079 m_localFileName = _filename;
00080
00081 }
00082 else
00083 {
00084 m_fileMode = KoStoreBase::RemoteWrite;
00085 m_localFileName = "/tmp/kozip";
00086 }
00087
00088 m_pZip = new KZip( m_localFileName );
00089 m_bGood = init( _mode, appIdentification );
00090 }
00091
00092 KoZipStore::~KoZipStore()
00093 {
00094 kdDebug(s_area) << "KoZipStore::~KoZipStore" << endl;
00095 m_pZip->close();
00096 delete m_pZip;
00097
00098
00099 if ( m_fileMode == KoStoreBase::RemoteRead )
00100 {
00101 KIO::NetAccess::removeTempFile( m_localFileName );
00102 }
00103 else if ( m_fileMode == KoStoreBase::RemoteWrite )
00104 {
00105 KIO::NetAccess::upload( m_localFileName, m_url, m_window );
00106
00107 }
00108 }
00109
00110 bool KoZipStore::init( Mode _mode, const QCString& appIdentification )
00111 {
00112 KoStore::init( _mode );
00113 m_currentDir = 0;
00114 bool good = m_pZip->open( _mode == Write ? IO_WriteOnly : IO_ReadOnly );
00115
00116 if ( good && _mode == Read )
00117 good = m_pZip->directory() != 0;
00118 else if ( good && _mode == Write )
00119 {
00120
00121
00122 m_pZip->setCompression( KZip::NoCompression );
00123 m_pZip->setExtraField( KZip::NoExtraField );
00124
00125 (void)m_pZip->writeFile( "mimetype", "", "", appIdentification.length(), appIdentification.data() );
00126 m_pZip->setCompression( KZip::DeflateCompression );
00127
00128 }
00129 return good;
00130 }
00131
00132 bool KoZipStore::openWrite( const QString& name )
00133 {
00134 #if 0
00135
00136 m_byteArray.resize( 0 );
00137 m_stream = new QBuffer( m_byteArray );
00138 m_stream->open( IO_WriteOnly );
00139 return true;
00140 #endif
00141 m_stream = 0L;
00142 return m_pZip->prepareWriting( name, "", "" , 0 );
00143 }
00144
00145 bool KoZipStore::openRead( const QString& name )
00146 {
00147 const KArchiveEntry * entry = m_pZip->directory()->entry( name );
00148 if ( entry == 0L )
00149 {
00150
00151
00152 return false;
00153 }
00154 if ( entry->isDirectory() )
00155 {
00156 kdWarning(s_area) << name << " is a directory !" << endl;
00157
00158 return false;
00159 }
00160
00161 const KZipFileEntry * f = static_cast<const KZipFileEntry *>(entry);
00162 delete m_stream;
00163 m_stream = f->device();
00164 m_iSize = f->size();
00165 return true;
00166 }
00167
00168 Q_LONG KoZipStore::write( const char* _data, Q_ULONG _len )
00169 {
00170 if ( _len == 0L ) return 0;
00171
00172
00173 if ( !m_bIsOpen )
00174 {
00175 kdError(s_area) << "KoStore: You must open before writing" << endl;
00176 return 0L;
00177 }
00178 if ( m_mode != Write )
00179 {
00180 kdError(s_area) << "KoStore: Can not write to store that is opened for reading" << endl;
00181 return 0L;
00182 }
00183
00184 m_iSize += _len;
00185 if ( m_pZip->writeData( _data, _len ) )
00186 return _len;
00187 return 0L;
00188 }
00189
00190 bool KoZipStore::closeWrite()
00191 {
00192 kdDebug(s_area) << "Wrote file " << m_sName << " into ZIP archive. size "
00193 << m_iSize << endl;
00194 return m_pZip->doneWriting( m_iSize );
00195 #if 0
00196 if ( !m_pZip->writeFile( m_sName , "user", "group", m_iSize, m_byteArray.data() ) )
00197 kdWarning( s_area ) << "Failed to write " << m_sName << endl;
00198 m_byteArray.resize( 0 );
00199 return true;
00200 #endif
00201 }
00202
00203 bool KoZipStore::enterRelativeDirectory( const QString& dirName )
00204 {
00205 if ( m_mode == Read ) {
00206 if ( !m_currentDir ) {
00207 m_currentDir = m_pZip->directory();
00208 Q_ASSERT( m_currentPath.isEmpty() );
00209 }
00210 const KArchiveEntry *entry = m_currentDir->entry( dirName );
00211 if ( entry && entry->isDirectory() ) {
00212 m_currentDir = dynamic_cast<const KArchiveDirectory*>( entry );
00213 return m_currentDir != 0;
00214 }
00215 return false;
00216 }
00217 else
00218 return true;
00219 }
00220
00221 bool KoZipStore::enterAbsoluteDirectory( const QString& path )
00222 {
00223 if ( path.isEmpty() )
00224 {
00225 m_currentDir = 0;
00226 return true;
00227 }
00228 m_currentDir = dynamic_cast<const KArchiveDirectory*>( m_pZip->directory()->entry( path ) );
00229 Q_ASSERT( m_currentDir );
00230 return m_currentDir != 0;
00231 }
00232
00233 bool KoZipStore::fileExists( const QString& absPath ) const
00234 {
00235 const KArchiveEntry *entry = m_pZip->directory()->entry( absPath );
00236 return entry && entry->isFile();
00237 }
This file is part of the documentation for lib Library Version 1.4.2.