Commit be2d7fbd authored by Dennis Rassmann's avatar Dennis Rassmann

flasher: highly optimize extract function

Now works more than 3x as fast
and also creates subdirectories.
Also add error handling if file could not be extracted.
Signed-off-by: Dennis Rassmann's avatarshowp1984 <showp1984@gmail.com>
parent 2e936553
......@@ -4,6 +4,7 @@
#include "quazip/quazip.h"
#include "quazip/quazipfile.h"
#include "quazip/JlCompress.h"
flasher::flasher(QWidget *parent) :
QMainWindow(parent),
......@@ -120,7 +121,7 @@ void flasher::on_txt_out_textChanged()
ui->txt_out->setTextCursor(c);
}
static bool extract(const QString & filePath, const QString & extDirPath, const QString & singleFileName = QString(""))
bool flasher::extract_zip(const QString & filePath, const QString & extDirPath, const QString & singleFileName = QString(""))
{
QuaZip zip(filePath);
......@@ -140,40 +141,46 @@ static bool extract(const QString & filePath, const QString & extDirPath, const
QFile out;
QString name;
char c;
QString path;
QString temppath;
QString basepath = qApp->applicationDirPath();
QStringList splitpath = QStringList() << "" << "" << "" << "" << "" << "" << "" << "" << "" << "" << "" << "" << "" << "";
QByteArray cnt;
QDir filedir;
for (bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) {
if (!zip.getCurrentFileInfo(&info)) {
qWarning("testRead(): getCurrentFileInfo(): %d\n", zip.getZipError());
return false;
}
if (!singleFileName.isEmpty())
if (!info.name.contains(singleFileName))
continue;
if (!file.open(QIODevice::ReadOnly)) {
if (!file.open(QuaZipFile::ReadOnly)) {
qWarning("testRead(): file.open(): %d", file.getZipError());
return false;
}
name = QString("%1/%2").arg(extDirPath).arg(file.getActualFileName());
splitpath = file.getActualFileName().split("/");
temppath = splitpath.takeLast();
temppath = splitpath.join("/");
path = QString("%1/%2/%3").arg(basepath).arg(extDirPath).arg(temppath);
name = QString("%1/%2/%3").arg(basepath).arg(extDirPath).arg(file.getActualFileName());
if (file.getZipError() != UNZ_OK) {
qWarning("testRead(): file.getFileName(): %d", file.getZipError());
return false;
}
//out.setFileName("out/" + name);
out.setFileName(name);
filedir.setPath(path);
if (!filedir.exists()) {
filedir.mkpath(path);
}
// this will fail if "name" contains subdirectories, but we don't mind that
out.setFileName(name);
cnt=file.readAll();
qDebug()<<"name: "<<name;
qDebug()<<"writing "<<cnt.size()<<" bytes";
out.open(QIODevice::WriteOnly);
// Slow like hell (on GNU/Linux at least), but it is not my fault.
// Not ZIP/UNZIP package's fault either.
// The slowest thing here is out.putChar(c).
while (file.getChar(&c)) out.putChar(c);
out.write(cnt);
out.close();
if (file.getZipError() != UNZ_OK) {
......@@ -193,14 +200,11 @@ static bool extract(const QString & filePath, const QString & extDirPath, const
return false;
}
}
zip.close();
if (zip.getZipError() != UNZ_OK) {
qWarning("testRead(): zip.close(): %d", zip.getZipError());
return false;
}
return true;
}
......@@ -276,7 +280,7 @@ void flasher::flash_device(void)
int flasher::extract(void)
{
unsigned int rnd = 0, max = 999999;
bool success = false;
bool success = false, xtr_return = false;
while (!success) {
rnd = qrand() % max;
......@@ -288,10 +292,15 @@ int flasher::extract(void)
}
ui->txt_out->append("Created temporary directory: " + tmp_folder);
xtr_return = extract_zip(filepath, tmp_folder);
if (!xtr_return) {
ui->txt_out->append("Error! Could not extract zip file.");
return RELEASE_CONTROLS;
}
QDir().rmdir(tmp_folder);
ui->txt_out->append("Deleted temporary directory: " + tmp_folder);
//QDir().rmdir(tmp_folder);
//ui->txt_out->append("Deleted temporary directory: " + tmp_folder);
//skip to detect
return DETECT;
}
......
......@@ -70,6 +70,7 @@ private slots:
int detect_device(void);
int get_booted(void);
int push_files(void);
bool extract_zip(const QString&, const QString&, const QString&);
};
#endif // FLASHER_H
/****************************************************************************
** Meta object code from reading C++ file 'detect.h'
**
** Created: Mon Jul 2 01:03:26 2012
** Created: Mon Jul 2 02:27:48 2012
** by: The Qt Meta Object Compiler version 62 (Qt 4.6.2)
**
** WARNING! All changes made in this file will be lost!
......
/****************************************************************************
** Meta object code from reading C++ file 'dragons.h'
**
** Created: Mon Jul 2 01:03:24 2012
** Created: Mon Jul 2 02:27:46 2012
** by: The Qt Meta Object Compiler version 62 (Qt 4.6.2)
**
** WARNING! All changes made in this file will be lost!
......
/****************************************************************************
** Meta object code from reading C++ file 'flasher.h'
**
** Created: Mon Jul 2 01:03:27 2012
** Created: Mon Jul 2 02:27:49 2012
** by: The Qt Meta Object Compiler version 62 (Qt 4.6.2)
**
** WARNING! All changes made in this file will be lost!
......@@ -23,7 +23,7 @@ static const uint qt_meta_data_flasher[] = {
4, // revision
0, // classname
0, 0, // classinfo
10, 14, // methods
11, 14, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
......@@ -41,6 +41,7 @@ static const uint qt_meta_data_flasher[] = {
160, 8, 146, 8, 0x08,
176, 8, 146, 8, 0x08,
189, 8, 146, 8, 0x08,
210, 207, 202, 8, 0x08,
0 // eod
};
......@@ -51,7 +52,8 @@ static const char qt_meta_stringdata_flasher[] = {
"on_actionInfo_triggered()\0"
"on_actionQuit_triggered()\0flash_device()\0"
"int\0extract()\0detect_device()\0"
"get_booted()\0push_files()\0"
"get_booted()\0push_files()\0bool\0,,\0"
"extract_zip(QString,QString,QString)\0"
};
const QMetaObject flasher::staticMetaObject = {
......@@ -97,9 +99,11 @@ int flasher::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
if (_a[0]) *reinterpret_cast< int*>(_a[0]) = _r; } break;
case 9: { int _r = push_files();
if (_a[0]) *reinterpret_cast< int*>(_a[0]) = _r; } break;
case 10: { bool _r = extract_zip((*reinterpret_cast< const QString(*)>(_a[1])),(*reinterpret_cast< const QString(*)>(_a[2])),(*reinterpret_cast< const QString(*)>(_a[3])));
if (_a[0]) *reinterpret_cast< bool*>(_a[0]) = _r; } break;
default: ;
}
_id -= 10;
_id -= 11;
}
return _id;
}
......
/****************************************************************************
** Meta object code from reading C++ file 'mainwindow.h'
**
** Created: Mon Jul 2 01:03:22 2012
** Created: Mon Jul 2 02:27:45 2012
** by: The Qt Meta Object Compiler version 62 (Qt 4.6.2)
**
** WARNING! All changes made in this file will be lost!
......
/****************************************************************************
** Meta object code from reading C++ file 'openfile.h'
**
** Created: Mon Jul 2 01:03:29 2012
** Created: Mon Jul 2 02:27:51 2012
** by: The Qt Meta Object Compiler version 62 (Qt 4.6.2)
**
** WARNING! All changes made in this file will be lost!
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment