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

0001 /*  This file is part of the KDE project
0002 
0003     SPDX-FileCopyrightText: 2013 Maarten De Meyer <de.meyer.maarten@gmail.com>
0004 
0005     SPDX-License-Identifier: BSD-2-Clause
0006 */
0007 
0008 /*
0009  * HelloWorld
0010  *
0011  * Example to show very basic usage of KArchive with CMake
0012  *
0013  * Usage:
0014  *      mkdir build && cd build
0015  *      cmake ..
0016  *      make
0017  *      ./helloworld
0018  */
0019 
0020 #include <QDebug>
0021 #include <kzip.h>
0022 
0023 int main()
0024 {
0025     //@@snippet_begin(helloworld)
0026     // Create a zip archive
0027     KZip archive(QStringLiteral("hello.zip"));
0028 
0029     // Open our archive for writing
0030     if (archive.open(QIODevice::WriteOnly)) {
0031         // The archive is open, we can now write data
0032         archive.writeFile(QStringLiteral("world"), // File name
0033                           QByteArray("The whole world inside a hello."), // Data
0034                           0100644, // Permissions
0035                           QStringLiteral("owner"), // Owner
0036                           QStringLiteral("users")); // Group
0037 
0038         // Don't forget to close!
0039         archive.close();
0040     }
0041 
0042     if (archive.open(QIODevice::ReadOnly)) {
0043         const KArchiveDirectory *dir = archive.directory();
0044 
0045         const KArchiveEntry *e = dir->entry("world");
0046         if (!e) {
0047             qDebug() << "File not found!";
0048             return -1;
0049         }
0050         const KArchiveFile *f = static_cast<const KArchiveFile *>(e);
0051         QByteArray arr(f->data());
0052         qDebug() << arr; // the file contents
0053 
0054         // To avoid reading everything into memory in one go, we can use createDevice() instead
0055         QIODevice *dev = f->createDevice();
0056         while (!dev->atEnd()) {
0057             qDebug() << dev->readLine();
0058         }
0059         delete dev;
0060     }
0061     //@@snippet_end
0062 
0063     return 0;
0064 }