// You should have received a copy of the GNU General Public License
// along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
-#include <QFile>
+#include <QFileInfo>
#include <QDebug>
#include "unziphelper.h"
#include "../dataobjects/mediaimage.h"
const QString UnzipHelper::UNZIP_COMMAND = "unzip ";
const QString UnzipHelper::UNZIP_LIST_ARGS = "-lv ";
+const QString UnzipHelper::UNZIP_EXTRACT_ARGS= "-qqo ";
UnzipHelper::UnzipHelper(QObject *parent) :
ProcessHelper(parent)
{
}
-QList<MediaImage*> UnzipHelper::listContents(const QString filePath, const FilePathObject *fp)
+/* Throws EmuFrontException */
+QMap<QString, EmuFrontObject*> UnzipHelper::listContents(const QString filePath, const FilePathObject *fp)
{
if (!fp->getSetup()){
throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
QFile fl(filePath);
if (!fl.open(QIODevice::ReadOnly)) {
- throw new EmuFrontException(tr("Couldn't read file %1.").arg(filePath));
+ throw EmuFrontException(tr("Couldn't read file %1.").arg(filePath));
}
- Setup *sup = fp->getSetup();
- QList<MediaImage*> fileList;
+ //Setup *sup = fp->getSetup();
+ QMap<QString, EmuFrontObject*> fileList;
- QProcess proc(this);
QString command;
command.append(UNZIP_COMMAND);
command.append(UNZIP_LIST_ARGS);
command.append("\"");
command.append(filePath);
command.append("\"");
- qDebug() << command;
- proc.start(command);
+ //qDebug() << command;
+ start(command);
// TODO: slot(s) for (start and) error signal(s)
- bool procOk = proc.waitForFinished();
+ bool procOk = waitForFinished();
if (!procOk) {
- throw new EmuFrontException(tr("Listing information from file %1 failed with unzip.").arg(filePath));
+ throw EmuFrontException(tr("Listing information from file %1 failed with unzip.").arg(filePath));
}
- QString err = proc.readAllStandardError();
- QString msg = proc.readAllStandardOutput();
- qDebug() << "\nErrors:\n" << err << "\nMessage:\n" << msg;
+ QString err = readAllStandardError();
+ QString msg = readAllStandardOutput();
+ //qDebug() << "\nErrors:\n" << err << "\nMessage:\n" << msg;
/*
//QRegExp("^\\s+\\d+\\s+[A-Za-z:]*\\s+\\d+\\s+\\d{1,3}%\\s+\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}\\s+[0-9a-f]{8}\\s+.+$")
);
QStringList entries;
- QRegExp test("^\\s+\\d+\\s+[A-Za-z:]*\\s+\\d+\\s+\\d{1,3}%\\s+\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}\\s+[0-9a-f]{8}\\s+.+$");
- QRegExp regExEntries("^\\s+(\\d+)\\s+[A-Za-z:]*\\s+\\d+\\s+\\d{1,3}%\\s+\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}\\s+([0-9a-f]{8})\\s+(\\S.+)$");
+ //QRegExp test("^\\s+\\d+\\s+[A-Za-z:]*\\s+\\d+\\s+\\d{1,3}%\\s+\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}\\s+[0-9a-f]{8}\\s+.+$");
+ QRegExp regExEntries(
+ "^" // line starts
+ "\\s*" // 1st empty space is optional!
+ "(\\d+)" // uncompressed "Length" in digits
+ "\\s+"
+ "[A-Za-z:]*" // "Method"
+ "\\s+"
+ "\\d+" // compressed "Size"
+ "\\s+"
+ "\\d{1,3}%" // compression ratio
+ "\\s+"
+ "\\d{2,4}-\\d{2}-\\d{2,4}" // date
+ "\\s+"
+ "\\d{2}:\\d{2}" // time
+ "\\s+"
+ "([0-9a-f]{8})" // CRC-32
+ "\\s+"
+ "(\\S.*)" // at least one non whitespace character + optional other chars (including whitespace)
+ "$" // line ends
+ );
foreach(QString ln, lines) {
- if (!test.exactMatch(ln)) continue;
+ //if (!test.exactMatch(ln)) continue;
+ //qDebug() << "Current line is " << ln;
int pos = regExEntries.indexIn(ln);
+ if (pos == -1) {
+ //qDebug() << "Regex didn't match any entries.";
+ continue; // > no entries
+ }
entries = regExEntries.capturedTexts();
+ //qDebug() << "Got " << entries.count() << " entries.";
if (entries.count() < 4) continue;
QString filename = entries[3];
QString checksum = entries[2];
QString lenStr = entries[1];
bool ok;
int length = lenStr.toInt(&ok);
+ /*qDebug() << "Filename is " << filename << " checksum "
+ << checksum << " length " << length;*/
if (!ok) continue;
MediaImage *effo = new MediaImage(filename, checksum, length);
- fileList << effo;
+ fileList[checksum] = effo;
}
- /*UnZip uz;
- UnZip::ErrorCode ec = uz.openArchive(filePath);
- if (ec != UnZip::Ok)
- throw EmuFrontException(tr("Error while opening zip-file %1, error code %2").arg(filePath).arg(ec));
-
- QList<UnZip::ZipEntry> list = uz.entryList();
- foreach(UnZip::ZipEntry entry, list)
- {
- qDebug() << "Zip entry " << entry.filename;
- if (isSupportedFile(entry.filename, sup->getSupportedFileTypeExtensions()))
- {
- QString checksum = QString("%1").arg(entry.crc32, 0, 16);
- qDebug() << "Checksum " << checksum;
- MediaImage *effo = new MediaImage(entry.filename,
- checksum, entry.uncompressedSize);
- fileList << effo;
- }
- }*/
-
- qDebug() << "File list has " << fileList.size() << " entries.";
+ //qDebug() << "File list has " << fileList.size() << " entries.";
return fileList;
-
}
+/*
+ Returns the exit code of the unzip process.
+ Throws EmuFrontException if filePath is not readable
+ or targetPath is not writable.
+*/
+int UnzipHelper::extractAll(QString filePath, QString targetPath)
+{
+ QFileInfo fp(filePath);
+ if (!fp.isReadable()) {
+ throw EmuFrontException(tr("Cannot read file %1.").arg(filePath));
+ }
+ QFileInfo tp(targetPath);
+ if (!tp.isWritable()) {
+ throw EmuFrontException(tr("Cannot write to %1.").arg(targetPath));
+ }
+
+ // unzip filepath -d targetpath
+ QString command;
+ command.append(UNZIP_COMMAND);
+ command.append(UNZIP_EXTRACT_ARGS);
+ command.append("\"");
+ command.append(filePath);
+ command.append("\"");
+ command.append(" -d ");
+ command.append(targetPath);
+ //qDebug() << "Starting unzip command: " << command;
+ start(command);
+ bool procOk = waitForFinished( ); // TODO: set timeout, now using default 30000ms
+ if (!procOk) {
+ throw EmuFrontException(tr("Failed unzipping file '%1' to '%2'.").arg(filePath).arg(targetPath));
+ }
+ return exitCode();
+}