1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
/* This file is part of the KDE project
SPDX-FileCopyrightText: 2014 Maarten De Meyer <de.meyer.maarten@gmail.com>
SPDX-License-Identifier: BSD-2-Clause
*/
/*
* bzip2gzip
* This example shows the usage of KCompressionDevice.
* It converts BZip2 files to GZip archives.
*
* api: KCompressionDevice(QIODevice * inputDevice, bool autoDeleteInputDevice, CompressionType type)
* api: KCompressionDevice(const QString & fileName, CompressionType type)
* api: QIODevice::readAll()
* api: QIODevice::read(qint64 maxSize)
* api: QIODevice::write(const QByteArray &data)
*
* Usage: ./bzip2gzip <archive.bz2>
*/
#include <QCoreApplication>
#include <QFile>
#include <QFileInfo>
#include <QStringList>
#include <KCompressionDevice>
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QStringList args(app.arguments());
if (args.size() != 2) {
qWarning("Usage: ./bzip2gzip <archive.bz2>");
return 1;
}
QString inputFile = args.at(1);
QFile file(inputFile);
QFileInfo info(inputFile);
if (info.suffix() != QLatin1String("bz2")) {
qCritical("Error: not a valid BZip2 file!");
return 1;
}
//@@snippet_begin(kcompressiondevice_example)
// Open the input archive
KCompressionDevice input(&file, false, KCompressionDevice::BZip2);
input.open(QIODevice::ReadOnly);
QString outputFile = (info.completeBaseName() + QLatin1String(".gz"));
// Open the new output file
KCompressionDevice output(outputFile, KCompressionDevice::GZip);
output.open(QIODevice::WriteOnly);
while (!input.atEnd()) {
// Read and uncompress the data
QByteArray data = input.read(512);
// Write data like you would to any other QIODevice
output.write(data);
}
input.close();
output.close();
//@@snippet_end
return 0;
}
|