1 /*
2  *  SPDX-FileCopyrightText: 2011 Mario Bensi <mbensi@ipsquad.net>
3  *
4  *  SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 #include "k7zip.h"
8 
9 #include <QDebug>
10 
11 #include <stdio.h>
12 
recursive_print(const KArchiveDirectory * dir,const QString & path)13 void recursive_print(const KArchiveDirectory *dir, const QString &path)
14 {
15     QStringList l = dir->entries();
16     l.sort();
17     QStringList::ConstIterator it = l.constBegin();
18     for (; it != l.constEnd(); ++it) {
19         const KArchiveEntry *entry = dir->entry((*it));
20         printf("mode=%07o %s %s %s %s%s %lld isdir=%d\n",
21                entry->permissions(),
22                entry->date().toString(QStringLiteral("yyyy-MM-dd hh:mm:ss")).toLatin1().constData(),
23                entry->user().toLatin1().constData(),
24                entry->group().toLatin1().constData(),
25                path.toLatin1().constData(),
26                (*it).toLatin1().constData(),
27                entry->isFile() ? static_cast<const KArchiveFile *>(entry)->size() : 0,
28                entry->isDirectory());
29         if (!entry->symLinkTarget().isEmpty()) {
30             printf("  (symlink to %s)\n", qPrintable(entry->symLinkTarget()));
31         }
32         if (entry->isDirectory()) {
33             recursive_print((KArchiveDirectory *)entry, path + (*it) + '/');
34         }
35         if (entry->isFile()) {
36             const KArchiveFile *f = static_cast<const KArchiveFile *>(entry);
37             QByteArray arr(f->data());
38             qDebug() << "data" << arr;
39 
40             QIODevice *dev = f->createDevice();
41             QByteArray contents = dev->readAll();
42             qDebug() << "contents" << contents;
43             delete dev;
44         }
45     }
46 }
47 
48 // See karchivetest.cpp for the unittest that covers K7Zip.
49 
main(int argc,char ** argv)50 int main(int argc, char **argv)
51 {
52     if (argc != 2) {
53         printf(
54             "\n"
55             " Usage :\n"
56             " ./k7ziptest /path/to/existing_file.7z       tests listing an existing .7z\n");
57         return 1;
58     }
59 
60     K7Zip k7z(argv[1]);
61 
62     if (!k7z.open(QIODevice::ReadOnly)) {
63         printf("Could not open %s for reading\n", argv[1]);
64         return 1;
65     }
66 
67     const KArchiveDirectory *dir = k7z.directory();
68 
69     // printf("calling recursive_print\n");
70     recursive_print(dir, QLatin1String(""));
71     // printf("recursive_print called\n");
72 
73     k7z.close();
74 
75     return 0;
76 }
77