20#include "KoZipStore.h"
27#include <tdeio/netaccess.h>
29KoZipStore::KoZipStore(
const TQString & _filename, Mode _mode,
const TQCString & appIdentification )
31 kdDebug(s_area) <<
"KoZipStore Constructor filename = " << _filename
32 <<
" mode = " << int(_mode)
33 <<
" mimetype = " << appIdentification << endl;
35 m_pZip =
new KZip( _filename );
36 m_bGood = init( _mode, appIdentification );
39KoZipStore::KoZipStore( TQIODevice *dev, Mode mode,
const TQCString & appIdentification )
41 m_pZip =
new KZip( dev );
45KoZipStore::KoZipStore( TQWidget* window,
const KURL & _url,
const TQString & _filename, Mode _mode,
const TQCString & appIdentification )
47 kdDebug(s_area) <<
"KoZipStore Constructor url" << _url.prettyURL()
48 <<
" filename = " << _filename
49 <<
" mode = " << int(_mode)
50 <<
" mimetype = " << appIdentification << endl;
55 if ( _mode == KoStore::Read )
57 m_fileMode = KoStoreBase::RemoteRead;
58 m_localFileName = _filename;
63 m_fileMode = KoStoreBase::RemoteWrite;
64 m_localFileName =
"/tmp/kozip";
67 m_pZip =
new KZip( m_localFileName );
68 m_bGood = init( _mode, appIdentification );
71KoZipStore::~KoZipStore()
73 kdDebug(s_area) <<
"KoZipStore::~KoZipStore" << endl;
78 if ( m_fileMode == KoStoreBase::RemoteRead )
80 TDEIO::NetAccess::removeTempFile( m_localFileName );
82 else if ( m_fileMode == KoStoreBase::RemoteWrite )
84 TDEIO::NetAccess::upload( m_localFileName,
m_url, m_window );
89bool KoZipStore::init( Mode _mode,
const TQCString& appIdentification )
93 bool good = m_pZip->open( _mode == Write ? IO_WriteOnly : IO_ReadOnly );
95 if ( good && _mode == Read )
96 good = m_pZip->directory() != 0;
97 else if ( good && _mode == Write )
101 m_pZip->setCompression( KZip::NoCompression );
102 m_pZip->setExtraField( KZip::NoExtraField );
104 (void)m_pZip->writeFile(
"mimetype",
"",
"", appIdentification.length(), appIdentification.data() );
105 m_pZip->setCompression( KZip::DeflateCompression );
111bool KoZipStore::openWrite(
const TQString& name )
115 m_byteArray.resize( 0 );
116 m_stream =
new TQBuffer( m_byteArray );
121 return m_pZip->prepareWriting( name,
"",
"" , 0 );
124bool KoZipStore::openRead(
const TQString& name )
126 const KArchiveEntry * entry = m_pZip->directory()->entry( name );
133 if ( entry->isDirectory() )
135 kdWarning(s_area) <<
name <<
" is a directory !" << endl;
140 const KZipFileEntry * f =
static_cast<const KZipFileEntry *
>(entry);
147TQ_LONG KoZipStore::write(
const char* _data, TQ_ULONG _len )
149 if ( _len == 0L )
return 0;
154 kdError(s_area) <<
"KoStore: You must open before writing" << endl;
157 if ( m_mode != Write )
159 kdError(s_area) <<
"KoStore: Can not write to store that is opened for reading" << endl;
164 if ( m_pZip->writeData( _data, _len ) )
171 kdDebug(s_area) <<
"Wrote file " <<
m_sName <<
" into ZIP archive. size "
173 return m_pZip->doneWriting(
m_iSize );
175 if ( !m_pZip->writeFile(
m_sName ,
"user",
"group",
m_iSize, m_byteArray.data() ) )
176 kdWarning( s_area ) <<
"Failed to write " <<
m_sName << endl;
177 m_byteArray.resize( 0 );
182bool KoZipStore::enterRelativeDirectory(
const TQString& dirName )
184 if ( m_mode == Read ) {
185 if ( !m_currentDir ) {
186 m_currentDir = m_pZip->directory();
189 const KArchiveEntry *entry = m_currentDir->entry( dirName );
190 if ( entry && entry->isDirectory() ) {
191 m_currentDir =
dynamic_cast<const KArchiveDirectory*
>( entry );
192 return m_currentDir != 0;
200bool KoZipStore::enterAbsoluteDirectory(
const TQString& path )
202 if ( path.isEmpty() )
207 m_currentDir =
dynamic_cast<const KArchiveDirectory*
>( m_pZip->directory()->entry( path ) );
208 Q_ASSERT( m_currentDir );
209 return m_currentDir != 0;
212bool KoZipStore::fileExists(
const TQString& absPath )
const
214 const KArchiveEntry *entry = m_pZip->directory()->entry( absPath );
215 return entry && entry->isFile();
KURL m_url
original URL of the remote file (undefined for a local file)
virtual bool closeWrite()=0
TQStringList m_currentPath
The "current directory" (path).
bool m_bGood
Must be set by the constructor.
TQString m_sName
Current filename (between an open() and a close()).
TQIODevice::Offset m_iSize
Current size of the file named m_sName.
virtual bool init(Mode mode)
Init store - called by constructor.
TQIODevice * m_stream
The stream for the current read or write operation.
const char * name(StdAction id)