00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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 );
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
00060
00061 m_pZip->setCompression( KZip::NoCompression );
00062
#if KDE_IS_VERSION(3,1,93)
00063
m_pZip->setExtraField( KZip::NoExtraField );
00064
#endif
00065
00066 (
void)m_pZip->writeFile(
"mimetype",
"",
"", appIdentification.length(), appIdentification.data() );
00067 m_pZip->setCompression( KZip::DeflateCompression );
00068
00069 }
00070
return good;
00071 }
00072
00073
bool KoZipStore::openWrite(
const QString& name )
00074 {
00075
#if 0
00076
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;
00083
return m_pZip->prepareWriting( name,
"",
"" , 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
00092
00093
return false;
00094 }
00095
if ( entry->isDirectory() )
00096 {
00097 kdWarning(s_area) << name <<
" is a directory !" << endl;
00098
00099
return false;
00100 }
00101
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
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 ) )
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 );
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();
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
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 }