File indexing completed on 2024-05-19 05:47:09

0001 /*  This file is part of the KDE project
0002 
0003     Copyright (C) 2014 Maarten De Meyer <de.meyer.maarten@gmail.com>
0004 
0005     You may use this file under the terms of the BSD license as follows:
0006 
0007     Redistribution and use in source and binary forms, with or without
0008     modification, are permitted provided that the following conditions
0009     are met:
0010 
0011     1. Redistributions of source code must retain the above copyright
0012       notice, this list of conditions and the following disclaimer.
0013     2. Redistributions in binary form must reproduce the above copyright
0014       notice, this list of conditions and the following disclaimer in the
0015       documentation and/or other materials provided with the distribution.
0016 
0017     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0018     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0019     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0020     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0021     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0022     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0023     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0024     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0025     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0026     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0027 */
0028 
0029 /*
0030  * Unzipper
0031  * This example shows how to extract all files from a zip archive.
0032  *
0033  * api: KArchive::directory()
0034  * api: KArchiveDirectory::copyTo(QString destination, bool recursive)
0035  *
0036  * Usage: ./unzipper <archive>
0037 */
0038 
0039 #include <QCoreApplication>
0040 #include <QDir>
0041 
0042 #include <kzip.h>
0043 
0044 int main(int argc, char *argv[])
0045 {
0046     QCoreApplication app(argc, argv);
0047     QStringList args(app.arguments());
0048 
0049     if (args.size() != 2) {
0050         // Too many or too few arguments
0051         qWarning("Usage: ./unzipper <archive.zip>");
0052         return 1;
0053     }
0054 
0055     QString file = args.at(1);
0056     KZip archive(file);
0057 
0058     // Open the archive
0059     if (!archive.open(QIODevice::ReadOnly)) {
0060         qWarning("Cannot open " + file.toLatin1());
0061         qWarning("Is it a valid zip file?");
0062         return 1;
0063     }
0064 
0065     // Take the root folder from the archive and create a KArchiveDirectory object.
0066     // KArchiveDirectory represents a directory in a KArchive.
0067     const KArchiveDirectory *root = archive.directory();
0068 
0069     // We can extract all contents from a KArchiveDirectory to a destination.
0070     // recursive true will also extract subdirectories.
0071     QString destination = QDir::currentPath();
0072     bool recursive = true;
0073     root->copyTo(destination, recursive);
0074 
0075     archive.close();
0076     return 0;
0077 }