File indexing completed on 2024-04-14 03:50:30

0001 /*
0002  *  SPDX-FileCopyrightText: 2011 Mario Bensi <mbensi@ipsquad.net>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "k7zip.h"
0008 
0009 #include <QDebug>
0010 
0011 #include <stdio.h>
0012 
0013 void recursive_print(const KArchiveDirectory *dir, const QString &path)
0014 {
0015     QStringList l = dir->entries();
0016     l.sort();
0017     QStringList::ConstIterator it = l.constBegin();
0018     for (; it != l.constEnd(); ++it) {
0019         const KArchiveEntry *entry = dir->entry((*it));
0020         printf("mode=%07o %s %s %s %s%s %lld isdir=%d\n",
0021                entry->permissions(),
0022                entry->date().toString(QStringLiteral("yyyy-MM-dd hh:mm:ss")).toLatin1().constData(),
0023                entry->user().toLatin1().constData(),
0024                entry->group().toLatin1().constData(),
0025                path.toLatin1().constData(),
0026                (*it).toLatin1().constData(),
0027                entry->isFile() ? static_cast<const KArchiveFile *>(entry)->size() : 0,
0028                entry->isDirectory());
0029         if (!entry->symLinkTarget().isEmpty()) {
0030             printf("  (symlink to %s)\n", qPrintable(entry->symLinkTarget()));
0031         }
0032         if (entry->isDirectory()) {
0033             recursive_print((KArchiveDirectory *)entry, path + (*it) + '/');
0034         }
0035         if (entry->isFile()) {
0036             const KArchiveFile *f = static_cast<const KArchiveFile *>(entry);
0037             QByteArray arr(f->data());
0038             qDebug() << "data" << arr;
0039 
0040             QIODevice *dev = f->createDevice();
0041             QByteArray contents = dev->readAll();
0042             qDebug() << "contents" << contents;
0043             delete dev;
0044         }
0045     }
0046 }
0047 
0048 // See karchivetest.cpp for the unittest that covers K7Zip.
0049 
0050 int main(int argc, char **argv)
0051 {
0052     if (argc != 2) {
0053         printf(
0054             "\n"
0055             " Usage :\n"
0056             " ./k7ziptest /path/to/existing_file.7z       tests listing an existing .7z\n");
0057         return 1;
0058     }
0059 
0060     K7Zip k7z(argv[1]);
0061 
0062     if (!k7z.open(QIODevice::ReadOnly)) {
0063         printf("Could not open %s for reading\n", argv[1]);
0064         return 1;
0065     }
0066 
0067     const KArchiveDirectory *dir = k7z.directory();
0068 
0069     // printf("calling recursive_print\n");
0070     recursive_print(dir, QLatin1String(""));
0071     // printf("recursive_print called\n");
0072 
0073     k7z.close();
0074 
0075     return 0;
0076 }