| 1 | /***************************************************************************** |
| 2 | ** QNapi |
| 3 | ** Copyright (C) 2008-2017 Piotr KrzemiĆski <[email protected]> |
| 4 | ** |
| 5 | ** This program is free software; you can redistribute it and/or modify |
| 6 | ** it under the terms of the GNU General Public License as published by |
| 7 | ** the Free Software Foundation; either version 2 of the License, or |
| 8 | ** (at your option) any later version. |
| 9 | ** |
| 10 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
| 11 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | ** |
| 13 | *****************************************************************************/ |
| 14 | |
| 15 | #include "p7zipdecoder.h" |
| 16 | |
| 17 | #include <QProcess> |
| 18 | |
| 19 | P7ZipDecoder::P7ZipDecoder(const QString& p7zipPath, int operationTimeoutMsecs) |
| 20 | : p7zipPath(p7zipPath), operationTimeoutMsecs(operationTimeoutMsecs) {} |
| 21 | |
| 22 | QStringList P7ZipDecoder::listArchiveFiles(const QString& archivePath) const { |
| 23 | QString stdoutBuff = readP7ZipOutput({"l", "-slt", archivePath}); |
| 24 | |
| 25 | #ifdef Q_OS_WIN |
| 26 | QRegExp r("\r\nPath = ([^\r\n]*)\r\n"); |
| 27 | #else |
| 28 | QRegExp r("\nPath = ([^\n]*)\n"); |
| 29 | #endif |
| 30 | r.setPatternSyntax(QRegExp::RegExp2); |
| 31 | |
| 32 | QStringList files; |
| 33 | int offset = 0; |
| 34 | |
| 35 | while ((offset = r.indexIn(stdoutBuff, offset)) != -1) { |
| 36 | files << r.cap(1); |
| 37 | offset += r.matchedLength(); |
| 38 | } |
| 39 | |
| 40 | return files; |
| 41 | } |
| 42 | |
| 43 | bool P7ZipDecoder::unpackArchiveFile(const QString& archivePath, |
| 44 | const QString& filename, |
| 45 | const QString& targetPath) const { |
| 46 | return runP7Zip({"e", "-y", "-o" + targetPath, archivePath, filename}); |
| 47 | } |
| 48 | |
| 49 | bool P7ZipDecoder::unpackArchiveFiles(const QString& archivePath, |
| 50 | const QString& targetPath) const { |
| 51 | return runP7Zip({"e", "-y", "-o" + targetPath, archivePath}); |
| 52 | } |
| 53 | |
| 54 | bool P7ZipDecoder::unpackSecureArchiveFiles(const QString& archivePath, |
| 55 | const QString& password, |
| 56 | const QString& targetPath) const { |
| 57 | return runP7Zip({"e", "-y", "-p" + password, "-o" + targetPath, archivePath}); |
| 58 | } |
| 59 | |
| 60 | bool P7ZipDecoder::runP7Zip(const QStringList& args) const { |
| 61 | QProcess p7zip; |
| 62 | p7zip.start(p7zipPath, args); |
| 63 | |
| 64 | bool result = p7zip.waitForFinished(operationTimeoutMsecs); |
| 65 | |
| 66 | p7zip.close(); |
| 67 | |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | QString P7ZipDecoder::readP7ZipOutput(const QStringList& args) const { |
| 72 | QProcess p7zip; |
| 73 | p7zip.start(p7zipPath, args); |
| 74 | |
| 75 | p7zip.waitForFinished(operationTimeoutMsecs); |
| 76 | |
| 77 | QString stdoutBuff = QByteArray(p7zip.readAll()); |
| 78 | |
| 79 | p7zip.close(); |
| 80 | |
| 81 | return stdoutBuff; |
| 82 | } |