Krita Source Code Documentation
Loading...
Searching...
No Matches
KoQuaZipStore Class Reference

#include <KoQuaZipStore.h>

+ Inheritance diagram for KoQuaZipStore:

Classes

struct  Private
 

Public Member Functions

QStringList directoryList () const override
 
 KoQuaZipStore (const QString &_filename, Mode _mode, const QByteArray &appIdentification, bool writeMimetype=true)
 
 KoQuaZipStore (QIODevice *dev, Mode mode, const QByteArray &appIdentification, bool writeMimetype=true)
 
void setCompressionEnabled (bool enabled) override
 
qint64 write (const char *_data, qint64 _len) override
 
 ~KoQuaZipStore () override
 
- Public Member Functions inherited from KoStore
bool atEnd () const
 
bool bad () const
 
bool close ()
 
QString currentPath () const
 
QIODevice * device () const
 
virtual bool enterDirectory (const QString &directory)
 
bool extractFile (const QString &sourceName, QByteArray &data)
 
bool finalize ()
 
bool hasDirectory (const QString &directoryName)
 
bool hasFile (const QString &fileName) const
 
bool isOpen () const
 
bool leaveDirectory ()
 
Mode mode () const
 
bool open (const QString &name)
 
void popDirectory ()
 
qint64 pos () const
 
void pushDirectory ()
 
qint64 read (char *buffer, qint64 length)
 
QByteArray read (qint64 max)
 
bool seek (qint64 pos)
 See QIODevice.
 
void setSubstitution (const QString &name, const QString &substitution)
 When reading, in the paths in the store where name occurs, substitution is used.
 
qint64 size () const
 
qint64 write (const QByteArray &data)
 
virtual ~KoStore ()
 

Protected Member Functions

bool closeRead () override
 
bool closeWrite () override
 
bool doFinalize () override
 
bool enterAbsoluteDirectory (const QString &path) override
 
bool enterRelativeDirectory (const QString &dirName) override
 
bool fileExists (const QString &absPath) const override
 
void init (const QByteArray &appIdentification)
 
bool openRead (const QString &name) override
 
bool openWrite (const QString &name) override
 
- Protected Member Functions inherited from KoStore
 KoStore (Mode mode, bool writeMimetype=true)
 

Private Attributes

const QScopedPointer< Privatedd
 

Additional Inherited Members

- Public Types inherited from KoStore
enum  Backend { Auto , Zip , Directory }
 
enum  Mode { Read , Write }
 
- Static Public Member Functions inherited from KoStore
static KoStorecreateStore (const QString &fileName, Mode mode, const QByteArray &appIdentification=QByteArray(), Backend backend=Auto, bool writeMimetype=true)
 
static KoStorecreateStore (QIODevice *device, Mode mode, const QByteArray &appIdentification=QByteArray(), Backend backend=Auto, bool writeMimetype=true)
 
- Protected Attributes inherited from KoStore
KoStorePrivated_ptr
 

Detailed Description

Definition at line 15 of file KoQuaZipStore.h.

Constructor & Destructor Documentation

◆ KoQuaZipStore() [1/2]

KoQuaZipStore::KoQuaZipStore ( const QString & _filename,
KoStore::Mode _mode,
const QByteArray & appIdentification,
bool writeMimetype = true )

Definition at line 43 of file KoQuaZipStore.cpp.

44 : KoStore(_mode, writeMimetype)
45 , dd(new Private())
46{
47 Q_D(KoStore);
48 d->localFileName = _filename;
49 dd->archive = new QuaZip(_filename);
50 init(appIdentification);
51
52}
void init(const QByteArray &appIdentification)
const QScopedPointer< Private > dd
KoStore(Mode mode, bool writeMimetype=true)
Definition KoStore.cpp:98

References dd, and init().

◆ KoQuaZipStore() [2/2]

KoQuaZipStore::KoQuaZipStore ( QIODevice * dev,
KoStore::Mode _mode,
const QByteArray & appIdentification,
bool writeMimetype = true )

Definition at line 54 of file KoQuaZipStore.cpp.

55 : KoStore(_mode, writeMimetype)
56 , dd(new Private())
57{
58 dd->archive = new QuaZip(dev);
59 init(appIdentification);
60}

References dd, and init().

◆ ~KoQuaZipStore()

KoQuaZipStore::~KoQuaZipStore ( )
override

Definition at line 62 of file KoQuaZipStore.cpp.

63{
64 Q_D(KoStore);
65
66 if (d->good && dd->currentFile && dd->currentFile->isOpen()) {
67 dd->currentFile->close();
68 }
69
70 if (!d->finalized) {
71 finalize();
72 }
73
74 delete dd->archive;
75
76 if (dd->currentFile) {
77 delete dd->currentFile;
78 }
79}
bool finalize()
Definition KoStore.cpp:395

References dd, and KoStore::finalize().

Member Function Documentation

◆ closeRead()

bool KoQuaZipStore::closeRead ( )
overrideprotectedvirtual
Returns
true on success

Implements KoStore.

Definition at line 256 of file KoQuaZipStore.cpp.

257{
258 Q_D(KoStore);
259 d->stream = 0;
260 return true;
261}

◆ closeWrite()

bool KoQuaZipStore::closeWrite ( )
overrideprotectedvirtual
Returns
true on success

Implements KoStore.

Definition at line 239 of file KoQuaZipStore.cpp.

240{
241 Q_D(KoStore);
242
243 bool r = true;
244 if (dd->currentFile->write(dd->cache) != dd->cache.size()) {
245 // write() returns number of bytes written, or -1 in case of error
246 // let's allow write 0 bytes in the cache, when needed
247 qWarning() << "Could not write buffer to the file";
248 r = false;
249 }
250 dd->buffer.close();
251 dd->currentFile->close();
252 d->stream = 0;
253 return (r && dd->currentFile->getZipError() == ZIP_OK);
254}

References dd.

◆ directoryList()

QStringList KoQuaZipStore::directoryList ( ) const
overridevirtual

If an store is opened for reading, then the directories of the store can be accessed via this function.

Returns
a stringlist with all directories found

Reimplemented from KoStore.

Definition at line 112 of file KoQuaZipStore.cpp.

113{
114 // If in Read mode, we can assume the directory listing won't change between invocations.
115 if(mode() == Read) {
116 if(!dd->directoryListCached) {
117 dd->directoryListCache = dd->archive->getFileNameList();
118 dd->directoryListCached = true;
119 }
120 return dd->directoryListCache;
121 }
122 else {
123 return dd->archive->getFileNameList();
124 }
125}
@ Read
Definition KoStore.h:29
Mode mode() const
Definition KoStore.cpp:420

References dd, KoStore::mode(), and KoStore::Read.

◆ doFinalize()

bool KoQuaZipStore::doFinalize ( )
overrideprotectedvirtual

Finalize store - called by finalize.

Returns
true on success

Reimplemented from KoStore.

Definition at line 167 of file KoQuaZipStore.cpp.

168{
169 Q_D(KoStore);
170
171 d->stream = 0;
172 if (d->good && !dd->usingSaveFile) {
173 dd->archive->close();
174 }
175 return dd->archive->getZipError() == ZIP_OK;
176
177}

References dd.

◆ enterAbsoluteDirectory()

bool KoQuaZipStore::enterAbsoluteDirectory ( const QString & path)
overrideprotectedvirtual

Enter a directory where we've been before. It is guaranteed to always exist.

Implements KoStore.

Definition at line 268 of file KoQuaZipStore.cpp.

269{
270 QString fixedPath = path;
271 fixedPath.replace("//", "/");
272
273 if (fixedPath.isEmpty()) {
274 fixedPath = "/";
275 }
276
277 QuaZipDir currentDir (dd->archive, fixedPath);
278
279 return currentDir.exists();
280}

References dd.

◆ enterRelativeDirectory()

bool KoQuaZipStore::enterRelativeDirectory ( const QString & dirName)
overrideprotectedvirtual

Enter a subdirectory of the current directory. The directory might not exist yet in Write mode.

Implements KoStore.

Definition at line 263 of file KoQuaZipStore.cpp.

264{
265 return true;
266}

◆ fileExists()

bool KoQuaZipStore::fileExists ( const QString & absPath) const
overrideprotectedvirtual

Check if a file exists inside the store.

Parameters
absPaththe absolute path inside the store, i.e. not relative to the current directory

Implements KoStore.

Definition at line 282 of file KoQuaZipStore.cpp.

283{
284 Q_D(const KoStore);
285
286 QString fixedPath = absPath;
287 fixedPath.replace("//", "/");
288
289 if (!d->substituteThis.isEmpty()) {
290 fixedPath = fixedPath.replace(d->substituteThis, d->substituteWith);
291 }
292
293 return directoryList().contains(fixedPath);
294}
QStringList directoryList() const override

References directoryList().

◆ init()

void KoQuaZipStore::init ( const QByteArray & appIdentification)
protected

Definition at line 127 of file KoQuaZipStore.cpp.

128{
129 Q_D(KoStore);
130
131 bool enableZip64 = false;
132 if (appIdentification == "application/x-krita") {
133 enableZip64 = KSharedConfig::openConfig()->group("").readEntry<bool>("UseZip64", false);
134 }
135
136 dd->archive->setDataDescriptorWritingEnabled(false);
137 dd->archive->setZip64Enabled(enableZip64);
138 dd->archive->setFileNameCodec("UTF-8");
139 dd->usingSaveFile = dd->archive->getIoDevice() && dd->archive->getIoDevice()->inherits("QSaveFile");
140 dd->archive->setAutoClose(!dd->usingSaveFile);
141
142 d->good = dd->archive->open(d->mode == Write ? QuaZip::mdCreate : QuaZip::mdUnzip);
143
144 if (!d->good) {
145 return;
146 }
147
148 if (d->mode == Write) {
149 if (d->writeMimetype) {
150 QuaZipFile f(dd->archive);
151 QuaZipNewInfo newInfo("mimetype");
152 newInfo.setPermissions(QFileDevice::ReadOwner | QFileDevice::ReadGroup | QFileDevice::ReadOther);
153 if (!f.open(QIODevice::WriteOnly, newInfo, 0, 0, 0, Z_NO_COMPRESSION)) {
154 d->good = false;
155 return;
156 }
157 f.write(appIdentification);
158 f.close();
159 }
160 }
161 else {
162 debugStore << dd->archive->getEntriesCount() << directoryList();
163 d->good = dd->archive->getEntriesCount();
164 }
165}
#define debugStore
Definition StoreDebug.h:15
@ Write
Definition KoStore.h:29

References dd, debugStore, directoryList(), and KoStore::Write.

◆ openRead()

bool KoQuaZipStore::openRead ( const QString & name)
overrideprotectedvirtual

Open the file name in the store, for reading. On success, this method must set m_stream to a stream from which we can read, as well as setting m_iSize to the size of the file.

Parameters
name"absolute path" (in the archive) to the file to open
Returns
true on success

Implements KoStore.

Definition at line 204 of file KoQuaZipStore.cpp.

205{
206 Q_D(KoStore);
207
208 QString fixedPath = name;
209 fixedPath.replace("//", "/");
210
211 delete d->stream;
212 d->stream = 0;
213 delete dd->currentFile;
214 dd->currentFile = 0;
215
216 if (!currentPath().isEmpty() && !fixedPath.startsWith(currentPath())) {
217 fixedPath = currentPath() + '/' + fixedPath;
218 }
219
220 if (!d->substituteThis.isEmpty()) {
221 fixedPath = fixedPath.replace(d->substituteThis, d->substituteWith);
222 }
223
224 if (!dd->archive->setCurrentFile(fixedPath)) {
225 qWarning() << "\t\tCould not set current file" << dd->archive->getZipError() << fixedPath;
226 return false;
227 }
228
229 dd->currentFile = new QuaZipFile(dd->archive);
230 if (!dd->currentFile->open(QIODevice::ReadOnly)) {
231 qWarning() << "\t\t\tBut could not open!!!" << dd->archive->getZipError();
232 return false;
233 }
234 d->stream = dd->currentFile;
235 d->size = dd->currentFile->size();
236 return true;
237}
QString currentPath() const
Definition KoStore.cpp:281
const char * name(StandardAction id)

References KoStore::currentPath(), and dd.

◆ openWrite()

bool KoQuaZipStore::openWrite ( const QString & name)
overrideprotectedvirtual

Open the file name in the store, for writing On success, this method must set m_stream to a stream in which we can write.

Parameters
name"absolute path" (in the archive) to the file to open
Returns
true on success

Implements KoStore.

Definition at line 179 of file KoQuaZipStore.cpp.

180{
181 Q_D(KoStore);
182 QString fixedPath = name;
183 fixedPath.replace("//", "/");
184
185 delete d->stream;
186 d->stream = 0; // Not used when writing
187
188 delete dd->currentFile;
189 dd->currentFile = new QuaZipFile(dd->archive);
190 QuaZipNewInfo newInfo(fixedPath);
191 newInfo.setPermissions(QFileDevice::ReadOwner | QFileDevice::ReadGroup | QFileDevice::ReadOther);
192 bool r = dd->currentFile->open(QIODevice::WriteOnly, newInfo, 0, 0, Z_DEFLATED, dd->compressionLevel);
193 if (!r) {
194 qWarning() << "Could not open" << name << dd->currentFile->getZipError();
195 }
196
197 dd->cache = QByteArray();
198 dd->buffer.setBuffer(&dd->cache);
199 dd->buffer.open(QBuffer::WriteOnly);
200
201 return r;
202}

References dd.

◆ setCompressionEnabled()

void KoQuaZipStore::setCompressionEnabled ( bool e)
overridevirtual

Allow to enable or disable compression of the files. Only supported by the ZIP backend.

Reimplemented from KoStore.

Definition at line 81 of file KoQuaZipStore.cpp.

82{
83
84 if (enabled) {
85 dd->compressionLevel = Z_DEFAULT_COMPRESSION;
86 }
87 else {
88 dd->compressionLevel = Z_NO_COMPRESSION;
89 }
90}

References dd.

◆ write()

qint64 KoQuaZipStore::write ( const char * data,
qint64 length )
overridevirtual

Write data into the currently opened file. You can also use the streams for this.

Reimplemented from KoStore.

Definition at line 92 of file KoQuaZipStore.cpp.

93{
94 Q_D(KoStore);
95 if (_len == 0) return 0;
96
97 if (!d->isOpen) {
98 errorStore << "KoStore: You must open before writing" << Qt::endl;
99 return 0;
100 }
101
102 if (d->mode != Write) {
103 errorStore << "KoStore: Can not write to store that is opened for reading" << Qt::endl;
104 return 0;
105 }
106
107 qint64 nwritten = dd->buffer.write(_data, _len);
108 d->size += nwritten;
109 return nwritten;
110}
#define errorStore
Definition StoreDebug.h:17

References dd, errorStore, and KoStore::Write.

Member Data Documentation

◆ dd

const QScopedPointer<Private> KoQuaZipStore::dd
private

Definition at line 44 of file KoQuaZipStore.h.


The documentation for this class was generated from the following files: