File indexing completed on 2024-04-28 03:52:32

0001 /*  This file is part of the KDE project
0002 
0003     SPDX-FileCopyrightText: 2014 Maarten De Meyer <de.meyer.maarten@gmail.com>
0004 
0005     SPDX-License-Identifier: BSD-2-Clause
0006 */
0007 
0008 /*
0009  * bzip2gzip
0010  * This example shows the usage of KCompressionDevice.
0011  * It converts BZip2 files to GZip archives.
0012  *
0013  * api: KCompressionDevice(QIODevice * inputDevice, bool autoDeleteInputDevice, CompressionType type)
0014  * api: KCompressionDevice(const QString & fileName, CompressionType type)
0015  * api: QIODevice::readAll()
0016  * api: QIODevice::read(qint64 maxSize)
0017  * api: QIODevice::write(const QByteArray &data)
0018  *
0019  * Usage: ./bzip2gzip <archive.bz2>
0020  */
0021 
0022 #include <QCoreApplication>
0023 #include <QFile>
0024 #include <QFileInfo>
0025 #include <QStringList>
0026 
0027 #include <KCompressionDevice>
0028 
0029 int main(int argc, char *argv[])
0030 {
0031     QCoreApplication app(argc, argv);
0032     QStringList args(app.arguments());
0033 
0034     if (args.size() != 2) {
0035         qWarning("Usage: ./bzip2gzip <archive.bz2>");
0036         return 1;
0037     }
0038 
0039     QString inputFile = args.at(1);
0040     QFile file(inputFile);
0041     QFileInfo info(inputFile);
0042 
0043     if (info.suffix() != QLatin1String("bz2")) {
0044         qCritical("Error: not a valid BZip2 file!");
0045         return 1;
0046     }
0047 
0048     //@@snippet_begin(kcompressiondevice_example)
0049     // Open the input archive
0050     KCompressionDevice input(&file, false, KCompressionDevice::BZip2);
0051     input.open(QIODevice::ReadOnly);
0052 
0053     QString outputFile = (info.completeBaseName() + QLatin1String(".gz"));
0054 
0055     // Open the new output file
0056     KCompressionDevice output(outputFile, KCompressionDevice::GZip);
0057     output.open(QIODevice::WriteOnly);
0058 
0059     while (!input.atEnd()) {
0060         // Read and uncompress the data
0061         QByteArray data = input.read(512);
0062 
0063         // Write data like you would to any other QIODevice
0064         output.write(data);
0065     }
0066 
0067     input.close();
0068     output.close();
0069     //@@snippet_end
0070 
0071     return 0;
0072 }