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  * Unzipper
0010  * This example shows how to extract all files from a zip archive.
0011  *
0012  * api: KArchive::directory()
0013  * api: KArchiveDirectory::copyTo(QString destination, bool recursive)
0014  *
0015  * Usage: ./unzipper <archive>
0016  */
0017 
0018 #include <QCoreApplication>
0019 #include <QDir>
0020 
0021 #include <kzip.h>
0022 
0023 int main(int argc, char *argv[])
0024 {
0025     QCoreApplication app(argc, argv);
0026     QStringList args(app.arguments());
0027 
0028     if (args.size() != 2) {
0029         // Too many or too few arguments
0030         qWarning("Usage: ./unzipper <archive.zip>");
0031         return 1;
0032     }
0033 
0034     QString file = args.at(1);
0035     KZip archive(file);
0036 
0037     // Open the archive
0038     if (!archive.open(QIODevice::ReadOnly)) {
0039         qWarning("Cannot open " + file.toLatin1());
0040         qWarning("Is it a valid zip file?");
0041         return 1;
0042     }
0043 
0044     // Take the root folder from the archive and create a KArchiveDirectory object.
0045     // KArchiveDirectory represents a directory in a KArchive.
0046     const KArchiveDirectory *root = archive.directory();
0047 
0048     // We can extract all contents from a KArchiveDirectory to a destination.
0049     // recursive true will also extract subdirectories.
0050     QString destination = QDir::currentPath();
0051     bool recursive = true;
0052     root->copyTo(destination, recursive);
0053 
0054     archive.close();
0055     return 0;
0056 }