KCompressionDevice Class

Header: #include <KCompressionDevice>
CMake: find_package(KF6 REQUIRED COMPONENTS Archive)
target_link_libraries(mytarget PRIVATE KF6::Archive)
Inherits: QIODevice

Public Types

enum CompressionType { GZip, BZip2, Xz, None, Zstd, Lz }

Public Functions

KCompressionDevice(const QString &fileName)
KCompressionDevice(const QString &fileName, KCompressionDevice::CompressionType type)
KCompressionDevice(QIODevice *inputDevice, bool autoDeleteInputDevice, KCompressionDevice::CompressionType type)
(since 6.16) KCompressionDevice(std::unique_ptr<QIODevice> inputDevice, KCompressionDevice::CompressionType type, std::optional<qint64> size = {})
virtual ~KCompressionDevice() override
KCompressionDevice::CompressionType compressionType() const
QFileDevice::FileError error() const
void setOrigFileName(const QByteArray &fileName)
void setSkipHeaders()

Reimplemented Public Functions

virtual bool seek(qint64) override

Static Public Members

KCompressionDevice::CompressionType compressionTypeForMimeType(const QString &mimetype)
KFilterBase *filterForCompressionType(KCompressionDevice::CompressionType type)

Detailed Description

A class for reading and writing compressed data onto a device (e.g. file, but other usages are possible, like a buffer or a socket).

Use this class to read/write compressed files.

Member Type Documentation

enum KCompressionDevice::CompressionType

ConstantValue
KCompressionDevice::GZip0
KCompressionDevice::BZip21
KCompressionDevice::Xz2
KCompressionDevice::None3
KCompressionDevice::Zstd (since 5.82)4
KCompressionDevice::Lz (since 6.15)5

Member Function Documentation

[explicit] KCompressionDevice::KCompressionDevice(const QString &fileName)

Constructs a KCompressionDevice for a given fileName.

fileName the name of the file to filter.

KCompressionDevice::KCompressionDevice(const QString &fileName, KCompressionDevice::CompressionType type)

Constructs a KCompressionDevice for a given CompressionType (e.g. GZip, BZip2 etc.).

fileName the name of the file to filter.

type the CompressionType to use.

KCompressionDevice::KCompressionDevice(QIODevice *inputDevice, bool autoDeleteInputDevice, KCompressionDevice::CompressionType type)

Constructs a KCompressionDevice for a given CompressionType (e.g. GZip, BZip2 etc.).

inputDevice input device.

autoDeleteInputDevice if true, inputDevice will be deleted automatically

type the CompressionType to use.

[since 6.16] KCompressionDevice::KCompressionDevice(std::unique_ptr<QIODevice> inputDevice, KCompressionDevice::CompressionType type, std::optional<qint64> size = {})

Constructs a KCompressionDevice for a given CompressionType (e.g. GZip, BZip2 etc.).

inputDevice input device.

type the CompressionType to use.

size the size we know the inputDevice with CompressionType type has. If we know it.

This function was introduced in 6.16.

[override virtual noexcept] KCompressionDevice::~KCompressionDevice()

Destructs the KCompressionDevice.

Calls close() if the filter device is still open.

KCompressionDevice::CompressionType KCompressionDevice::compressionType() const

The compression actually used by this device.

If the support for the compression requested in the constructor is not available, then the device will use None.

[static] KCompressionDevice::CompressionType KCompressionDevice::compressionTypeForMimeType(const QString &mimetype)

Returns the compression type for the given MIME type, if possible. Otherwise returns None.

This handles simple cases like application/gzip, but also application/x-compressed-tar, and inheritance.

QFileDevice::FileError KCompressionDevice::error() const

Returns the error code from the last failing operation. This is especially useful after calling close(), which unfortunately returns void (see https://bugreports.qt.io/browse/QTBUG-70033), to see if the flushing done by close was able to write all the data to disk.

[static] KFilterBase *KCompressionDevice::filterForCompressionType(KCompressionDevice::CompressionType type)

Call this to create the appropriate filter for the CompressionType named type.

type the type of the compression filter

Returns the filter for the type, or 0 if not found

[override virtual] bool KCompressionDevice::seek(qint64)

Reimplements: QIODevice::seek(qint64 pos).

That one can be quite slow, when going back. Use with care.

void KCompressionDevice::setOrigFileName(const QByteArray &fileName)

For writing gzip compressed files only: set the name of the original file, to be used in the gzip header.

fileName the name of the original file

void KCompressionDevice::setSkipHeaders()

Call this let this device skip the gzip headers when reading/writing. This way KCompressionDevice (with gzip filter) can be used as a direct wrapper around zlib - this is used by KZip.