KArchive

kcompressiondevice.h
1 /* This file is part of the KDE libraries
2  SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
3  SPDX-FileCopyrightText: 2011 Mario Bensi <mbensi@ipsquad.net>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 #ifndef __kcompressiondevice_h
8 #define __kcompressiondevice_h
9 
10 #include <karchive_export.h>
11 
12 #include <QFileDevice>
13 #include <QIODevice>
14 #include <QMetaType>
15 #include <QString>
16 
17 class KCompressionDevicePrivate;
18 
19 class KFilterBase;
20 
21 /**
22  * @class KCompressionDevice kcompressiondevice.h KCompressionDevice
23  *
24  * A class for reading and writing compressed data onto a device
25  * (e.g. file, but other usages are possible, like a buffer or a socket).
26  *
27  * Use this class to read/write compressed files.
28  */
29 
30 class KARCHIVE_EXPORT KCompressionDevice : public QIODevice // KF6 TODO: consider inheriting from QFileDevice, so apps can use error() generically ?
31 {
32  Q_OBJECT
33 public:
35  GZip,
36  BZip2,
37  Xz,
38  None,
39  Zstd, ///< @since 5.82
40  };
41 
42  /**
43  * Constructs a KCompressionDevice for a given CompressionType (e.g. GZip, BZip2 etc.).
44  * @param inputDevice input device.
45  * @param autoDeleteInputDevice if true, @p inputDevice will be deleted automatically
46  * @param type the CompressionType to use.
47  */
48  KCompressionDevice(QIODevice *inputDevice, bool autoDeleteInputDevice, CompressionType type);
49 
50  /**
51  * Constructs a KCompressionDevice for a given CompressionType (e.g. GZip, BZip2 etc.).
52  * @param fileName the name of the file to filter.
53  * @param type the CompressionType to use.
54  */
55  KCompressionDevice(const QString &fileName, CompressionType type);
56 
57  /**
58  * Constructs a KCompressionDevice for a given @p fileName.
59  * @param fileName the name of the file to filter.
60  * @since 5.85
61  */
62  explicit KCompressionDevice(const QString &fileName);
63 
64  /**
65  * Destructs the KCompressionDevice.
66  * Calls close() if the filter device is still open.
67  */
68  ~KCompressionDevice() override;
69 
70  /**
71  * The compression actually used by this device.
72  * If the support for the compression requested in the constructor
73  * is not available, then the device will use None.
74  */
75  CompressionType compressionType() const;
76 
77  /**
78  * Open for reading or writing.
79  */
80  bool open(QIODevice::OpenMode mode) override;
81 
82  /**
83  * Close after reading or writing.
84  */
85  void close() override;
86 
87  /**
88  * For writing gzip compressed files only:
89  * set the name of the original file, to be used in the gzip header.
90  * @param fileName the name of the original file
91  */
92  void setOrigFileName(const QByteArray &fileName);
93 
94  /**
95  * Call this let this device skip the gzip headers when reading/writing.
96  * This way KCompressionDevice (with gzip filter) can be used as a direct wrapper
97  * around zlib - this is used by KZip.
98  */
99  void setSkipHeaders();
100 
101  /**
102  * That one can be quite slow, when going back. Use with care.
103  */
104  bool seek(qint64) override;
105 
106  bool atEnd() const override;
107 
108  /**
109  * Call this to create the appropriate filter for the CompressionType
110  * named @p type.
111  * @param type the type of the compression filter
112  * @return the filter for the @p type, or 0 if not found
113  */
114  static KFilterBase *filterForCompressionType(CompressionType type);
115 
116  /**
117  * Returns the compression type for the given MIME type, if possible. Otherwise returns None.
118  * This handles simple cases like application/gzip, but also application/x-compressed-tar, and inheritance.
119  * @since 5.85
120  */
121  static CompressionType compressionTypeForMimeType(const QString &mimetype);
122 
123  /**
124  * Returns the error code from the last failing operation.
125  * This is especially useful after calling close(), which unfortunately returns void
126  * (see https://bugreports.qt.io/browse/QTBUG-70033), to see if the flushing done by close
127  * was able to write all the data to disk.
128  */
129  QFileDevice::FileError error() const;
130 
131 protected:
132  friend class K7Zip;
133 
134  qint64 readData(char *data, qint64 maxlen) override;
135  qint64 writeData(const char *data, qint64 len) override;
136 
137  KFilterBase *filterBase();
138 
139 private:
140  friend KCompressionDevicePrivate;
141  KCompressionDevicePrivate *const d;
142 };
143 
144 Q_DECLARE_METATYPE(KCompressionDevice::CompressionType)
145 
146 #endif
typedef OpenMode
virtual bool open(QIODevice::OpenMode mode)
virtual bool seek(qint64 pos)
virtual bool atEnd() const const
virtual void close()
virtual qint64 writeData(const char *data, qint64 maxSize)=0
virtual qint64 readData(char *data, qint64 maxSize)=0
Definition: k7zip.h:18
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 04:05:36 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.