KArchive

kzip.h
1 /* This file is part of the KDE libraries
2  SPDX-FileCopyrightText: 2002 Holger Schroeder <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #ifndef KZIP_H
7 #define KZIP_H
8 
9 #include <karchive.h>
10 
11 #include "kzipfileentry.h" // for source compat
12 
13 class KZipFileEntry;
14 /**
15  * @class KZip zip.h KZip
16  *
17  * A class for reading / writing zip archives.
18  *
19  * You can use it in QIODevice::ReadOnly or in QIODevice::WriteOnly mode, and it
20  * behaves just as expected.
21  * It can also be used in QIODevice::ReadWrite mode, in this case one can
22  * append files to an existing zip archive. When you append new files, which
23  * are not yet in the zip, it works as expected, i.e. the files are appended at the end.
24  * When you append a file, which is already in the file, the reference to the
25  * old file is dropped and the new one is added to the zip - but the
26  * old data from the file itself is not deleted, it is still in the
27  * zipfile. So when you want to have a small and garbage-free zipfile,
28  * just read the contents of the appended zip file and write it to a new one
29  * in QIODevice::WriteOnly mode. This is especially important when you don't want
30  * to leak information of how intermediate versions of files in the zip
31  * were looking.
32  *
33  * For more information on the zip fileformat go to
34  * http://www.pkware.com/products/enterprise/white_papers/appnote.html
35  * @author Holger Schroeder <[email protected]>
36  */
37 class KARCHIVE_EXPORT KZip : public KArchive
38 {
39  Q_DECLARE_TR_FUNCTIONS(KZip)
40 
41 public:
42  /**
43  * Creates an instance that operates on the given filename.
44  * using the compression filter associated to given mimetype.
45  *
46  * @param filename is a local path (e.g. "/home/holger/myfile.zip")
47  */
48  KZip(const QString &filename);
49 
50  /**
51  * Creates an instance that operates on the given device.
52  * The device can be compressed (KCompressionDevice) or not (QFile, etc.).
53  * @warning Do not assume that giving a QFile here will decompress the file,
54  * in case it's compressed!
55  * @param dev the device to access
56  */
57  KZip(QIODevice *dev);
58 
59  /**
60  * If the zip file is still opened, then it will be
61  * closed automatically by the destructor.
62  */
63  ~KZip() override;
64 
65  /**
66  * Describes the contents of the "extra field" for a given file in the Zip archive.
67  */
68  enum ExtraField {
69  NoExtraField = 0, ///< No extra field
70  ModificationTime = 1, ///< Modification time ("extended timestamp" header)
71  DefaultExtraField = 1, // alias of ModificationTime
72  };
73 
74  /**
75  * Call this before writeFile or prepareWriting, to define what the next
76  * file to be written should have in its extra field.
77  * @param ef the type of "extra field"
78  * @see extraField()
79  */
80  void setExtraField(ExtraField ef);
81 
82  /**
83  * The current type of "extra field" that will be used for new files.
84  * @return the current type of "extra field"
85  * @see setExtraField()
86  */
87  ExtraField extraField() const;
88 
89  /**
90  * Describes the compression type for a given file in the Zip archive.
91  */
92  enum Compression {
93  NoCompression = 0, ///< Uncompressed.
94  DeflateCompression = 1, ///< Deflate compression method.
95  };
96 
97  /**
98  * Call this before writeFile or prepareWriting, to define whether the next
99  * files to be written should be compressed or not.
100  * @param c the new compression mode
101  * @see compression()
102  */
103  void setCompression(Compression c);
104 
105  /**
106  * The current compression mode that will be used for new files.
107  * @return the current compression mode
108  * @see setCompression()
109  */
110  Compression compression() const;
111 
112  /**
113  * Write data to a file that has been created using prepareWriting().
114  * @param data a pointer to the data
115  * @param size the size of the chunk
116  * @return true if successful, false otherwise
117  */
118  bool writeData(const char *data, qint64 size) override;
119 
120 protected:
121  /// Reimplemented from KArchive
122  bool doWriteSymLink(const QString &name,
123  const QString &target,
124  const QString &user,
125  const QString &group,
126  mode_t perm,
127  const QDateTime &atime,
128  const QDateTime &mtime,
129  const QDateTime &ctime) override;
130  /// Reimplemented from KArchive
131  bool doPrepareWriting(const QString &name,
132  const QString &user,
133  const QString &group,
134  qint64 size,
135  mode_t perm,
136  const QDateTime &atime,
137  const QDateTime &mtime,
138  const QDateTime &creationTime) override;
139 
140  /**
141  * Write data to a file that has been created using prepareWriting().
142  * @param size the size of the file
143  * @return true if successful, false otherwise
144  */
145  bool doFinishWriting(qint64 size) override;
146 
147  /**
148  * Opens the archive for reading.
149  * Parses the directory listing of the archive
150  * and creates the KArchiveDirectory/KArchiveFile entries.
151  * @param mode the mode of the file
152  */
153  bool openArchive(QIODevice::OpenMode mode) override;
154 
155  /// Closes the archive
156  bool closeArchive() override;
157 
158  /// Reimplemented from KArchive
159  bool doWriteDir(const QString &name,
160  const QString &user,
161  const QString &group,
162  mode_t perm,
163  const QDateTime &atime,
164  const QDateTime &mtime,
165  const QDateTime &ctime) override;
166 
167 protected:
168  void virtual_hook(int id, void *data) override;
169 
170 private:
171  class KZipPrivate;
172  KZipPrivate *const d;
173 };
174 
175 #endif
virtual bool doWriteDir(const QString &name, const QString &user, const QString &group, mode_t perm, const QDateTime &atime, const QDateTime &mtime, const QDateTime &ctime)=0
Write a directory to the archive.
virtual bool openArchive(QIODevice::OpenMode mode)=0
Opens an archive for reading or writing.
typedef OpenMode
Compression
Describes the compression type for a given file in the Zip archive.
Definition: kzip.h:92
virtual bool writeData(const char *data, qint64 size)
Write data into the current file - to be called after calling prepareWriting.
Definition: karchive.cpp:405
generic class for reading/writing archives
Definition: karchive.h:39
virtual bool doPrepareWriting(const QString &name, const QString &user, const QString &group, qint64 size, mode_t perm, const QDateTime &atime, const QDateTime &mtime, const QDateTime &ctime)=0
This virtual method must be implemented by subclasses.
Definition: kzip.h:37
virtual bool doFinishWriting(qint64 size)=0
Called after writing the data.
virtual bool closeArchive()=0
Closes the archive.
virtual bool doWriteSymLink(const QString &name, const QString &target, const QString &user, const QString &group, mode_t perm, const QDateTime &atime, const QDateTime &mtime, const QDateTime &ctime)=0
Writes a symbolic link to the archive.
ExtraField
Describes the contents of the "extra field" for a given file in the Zip archive.
Definition: kzip.h:68
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu May 26 2022 04:03:46 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.