2016-02-25 20:19:27 +00:00
|
|
|
// Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
|
|
|
|
// Copyright (C) 2010 Winch Gate Property Limited
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include "stdpch.h"
|
2016-05-16 09:10:01 +00:00
|
|
|
#include "filesextractor.h"
|
|
|
|
#include "operation.h"
|
2016-02-25 20:19:27 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
#include "nel/misc/big_file.h"
|
|
|
|
#include "nel/misc/callback.h"
|
|
|
|
#include "nel/misc/file.h"
|
|
|
|
#include "nel/misc/path.h"
|
|
|
|
|
|
|
|
#include "7z.h"
|
|
|
|
#include "7zAlloc.h"
|
|
|
|
#include "7zBuf.h"
|
|
|
|
#include "7zCrc.h"
|
|
|
|
|
|
|
|
#include "qzipreader.h"
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2016-05-14 16:52:40 +00:00
|
|
|
#ifndef FILE_ATTRIBUTE_READONLY
|
2016-02-27 17:44:09 +00:00
|
|
|
#define FILE_ATTRIBUTE_READONLY 0x1
|
2016-05-14 16:52:40 +00:00
|
|
|
#endif
|
|
|
|
#ifndef FILE_ATTRIBUTE_HIDDEN
|
2016-02-27 17:44:09 +00:00
|
|
|
#define FILE_ATTRIBUTE_HIDDEN 0x2
|
2016-05-14 16:52:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FILE_ATTRIBUTE_SYSTEM
|
2016-02-27 17:44:09 +00:00
|
|
|
#define FILE_ATTRIBUTE_SYSTEM 0x4
|
2016-05-14 16:52:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FILE_ATTRIBUTE_DIRECTORY
|
2016-02-27 17:44:09 +00:00
|
|
|
#define FILE_ATTRIBUTE_DIRECTORY 0x10
|
2016-05-14 16:52:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FILE_ATTRIBUTE_ARCHIVE
|
2016-02-27 17:44:09 +00:00
|
|
|
#define FILE_ATTRIBUTE_ARCHIVE 0x20
|
2016-05-14 16:52:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FILE_ATTRIBUTE_DEVICE
|
2016-02-27 17:44:09 +00:00
|
|
|
#define FILE_ATTRIBUTE_DEVICE 0x40
|
2016-05-14 16:52:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FILE_ATTRIBUTE_NORMAL
|
2016-02-27 17:44:09 +00:00
|
|
|
#define FILE_ATTRIBUTE_NORMAL 0x80
|
2016-05-14 16:52:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FILE_ATTRIBUTE_TEMPORARY
|
2016-02-27 17:44:09 +00:00
|
|
|
#define FILE_ATTRIBUTE_TEMPORARY 0x100
|
2016-05-14 16:52:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FILE_ATTRIBUTE_SPARSE_FILE
|
2016-02-27 17:44:09 +00:00
|
|
|
#define FILE_ATTRIBUTE_SPARSE_FILE 0x200
|
2016-05-14 16:52:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FILE_ATTRIBUTE_REPARSE_POINT
|
2016-02-27 17:44:09 +00:00
|
|
|
#define FILE_ATTRIBUTE_REPARSE_POINT 0x400
|
2016-05-14 16:52:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FILE_ATTRIBUTE_COMPRESSED
|
2016-02-27 17:44:09 +00:00
|
|
|
#define FILE_ATTRIBUTE_COMPRESSED 0x800
|
2016-05-14 16:52:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FILE_ATTRIBUTE_OFFLINE
|
2016-02-25 20:19:27 +00:00
|
|
|
#define FILE_ATTRIBUTE_OFFLINE 0x1000
|
2016-05-14 16:52:40 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FILE_ATTRIBUTE_ENCRYPTED
|
2016-02-25 20:19:27 +00:00
|
|
|
#define FILE_ATTRIBUTE_ENCRYPTED 0x4000
|
2016-05-14 16:52:40 +00:00
|
|
|
#endif
|
|
|
|
|
2016-02-25 20:19:27 +00:00
|
|
|
#define FILE_ATTRIBUTE_UNIX_EXTENSION 0x8000 /* trick for Unix */
|
|
|
|
|
2016-02-27 17:44:09 +00:00
|
|
|
#define FILE_ATTRIBUTE_WINDOWS 0x5fff
|
|
|
|
#define FILE_ATTRIBUTE_UNIX 0xffff0000
|
|
|
|
|
2016-02-25 20:19:27 +00:00
|
|
|
bool Set7zFileAttrib(const QString &filename, uint32 fileAttributes)
|
|
|
|
{
|
2016-05-16 16:16:33 +00:00
|
|
|
bool attrReadOnly = (fileAttributes & FILE_ATTRIBUTE_READONLY) != 0;
|
|
|
|
bool attrHidden = (fileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0;
|
|
|
|
bool attrSystem = (fileAttributes & FILE_ATTRIBUTE_SYSTEM) != 0;
|
|
|
|
bool attrDir = (fileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
|
|
|
bool attrArchive = (fileAttributes & FILE_ATTRIBUTE_ARCHIVE) != 0;
|
|
|
|
bool attrDevice = (fileAttributes & FILE_ATTRIBUTE_DEVICE) != 0;
|
|
|
|
bool attrNormal = (fileAttributes & FILE_ATTRIBUTE_NORMAL) != 0;
|
|
|
|
bool attrTemp = (fileAttributes & FILE_ATTRIBUTE_TEMPORARY) != 0;
|
|
|
|
bool attrSparceFile = (fileAttributes & FILE_ATTRIBUTE_SPARSE_FILE) != 0;
|
|
|
|
bool attrReparsePoint = (fileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0;
|
|
|
|
bool attrCompressed = (fileAttributes & FILE_ATTRIBUTE_COMPRESSED) != 0;
|
|
|
|
bool attrOffline = (fileAttributes & FILE_ATTRIBUTE_OFFLINE) != 0;
|
|
|
|
bool attrEncrypted = (fileAttributes & FILE_ATTRIBUTE_ENCRYPTED) != 0;
|
|
|
|
bool attrUnix = (fileAttributes & FILE_ATTRIBUTE_UNIX_EXTENSION) != 0;
|
2016-02-25 20:19:27 +00:00
|
|
|
|
2016-02-27 17:44:09 +00:00
|
|
|
uint32 unixAttributes = (fileAttributes & FILE_ATTRIBUTE_UNIX) >> 16;
|
|
|
|
uint32 windowsAttributes = fileAttributes & FILE_ATTRIBUTE_WINDOWS;
|
|
|
|
|
2016-05-16 16:16:33 +00:00
|
|
|
// qDebug() << "attribs" << QByteArray::fromRawData((const char*)&fileAttributes, 4).toHex();
|
2016-02-25 20:19:27 +00:00
|
|
|
|
2016-05-16 16:19:23 +00:00
|
|
|
#ifdef Q_OS_WIN
|
2016-02-27 17:44:09 +00:00
|
|
|
SetFileAttributesW((wchar_t*)filename.utf16(), windowsAttributes);
|
2016-02-25 20:19:27 +00:00
|
|
|
#else
|
2016-02-27 17:44:09 +00:00
|
|
|
const char *name = filename.toUtf8().constData();
|
2016-02-25 20:19:27 +00:00
|
|
|
|
2016-05-16 16:16:33 +00:00
|
|
|
mode_t current_umask = umask(0); // get and set the umask
|
|
|
|
umask(current_umask); // restore the umask
|
|
|
|
mode_t mask = 0777 & (~current_umask);
|
|
|
|
|
2016-02-25 20:19:27 +00:00
|
|
|
struct stat stat_info;
|
2016-05-16 16:16:33 +00:00
|
|
|
|
|
|
|
if (lstat(name, &stat_info) != 0)
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
2016-05-16 16:16:33 +00:00
|
|
|
nlwarning("Unable to get file attributes for %s", name);
|
2016-02-25 20:19:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-27 17:44:09 +00:00
|
|
|
if (attrUnix)
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
2016-02-27 17:44:09 +00:00
|
|
|
stat_info.st_mode = unixAttributes;
|
2016-02-25 20:19:27 +00:00
|
|
|
|
2016-05-16 16:16:33 +00:00
|
|
|
// ignore symbolic links
|
|
|
|
if (!S_ISLNK(stat_info.st_mode))
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
2016-05-16 16:16:33 +00:00
|
|
|
if (S_ISREG(stat_info.st_mode))
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
2016-05-16 16:16:33 +00:00
|
|
|
chmod(name, stat_info.st_mode & mask);
|
|
|
|
}
|
|
|
|
else if (S_ISDIR(stat_info.st_mode))
|
|
|
|
{
|
|
|
|
// user/7za must be able to create files in this directory
|
|
|
|
stat_info.st_mode |= (S_IRUSR | S_IWUSR | S_IXUSR);
|
|
|
|
chmod(name, stat_info.st_mode & mask);
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 16:16:33 +00:00
|
|
|
else if (!S_ISLNK(stat_info.st_mode) && !S_ISDIR(stat_info.st_mode) && attrReadOnly)
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
|
|
|
// do not use chmod on a link
|
2016-05-16 16:16:33 +00:00
|
|
|
// Remark : FILE_ATTRIBUTE_READONLY ignored for directory.
|
2016-02-25 20:19:27 +00:00
|
|
|
// Only Windows Attributes
|
2016-05-16 16:16:33 +00:00
|
|
|
|
|
|
|
// octal!, clear write permission bits
|
|
|
|
stat_info.st_mode &= ~0222;
|
|
|
|
|
|
|
|
chmod(name, stat_info.st_mode & mask);
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_NEW
|
|
|
|
#define new DEBUG_NEW
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define SZ_ERROR_INTERRUPTED 18
|
|
|
|
|
|
|
|
class Q7zFile : public ISeekInStream
|
|
|
|
{
|
|
|
|
QFile m_file;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Q7zFile(const QString &filename):m_file(filename)
|
|
|
|
{
|
|
|
|
Read = readFunc;
|
|
|
|
Seek = seekFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Q7zFile()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool open()
|
|
|
|
{
|
|
|
|
return m_file.open(QFile::ReadOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
// the read function called by 7zip to read data
|
|
|
|
static SRes readFunc(void *object, void *buffer, size_t *size)
|
|
|
|
{
|
|
|
|
Q7zFile *me = (Q7zFile*)object;
|
|
|
|
qint64 len = *size;
|
|
|
|
len = me->m_file.read((char*)buffer, len);
|
|
|
|
|
|
|
|
if (len == *size)
|
|
|
|
{
|
|
|
|
*size = len;
|
|
|
|
return SZ_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return SZ_ERROR_READ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the seek function called by seven zip to seek inside stream
|
|
|
|
static SRes seekFunc(void *object, Int64 *pos, ESzSeek origin)
|
|
|
|
{
|
|
|
|
Q7zFile *me = (Q7zFile*)object;
|
|
|
|
qint64 newPos;
|
|
|
|
|
|
|
|
switch(origin)
|
|
|
|
{
|
|
|
|
case SZ_SEEK_SET: newPos = *pos; break;
|
|
|
|
case SZ_SEEK_CUR: newPos = me->m_file.pos() + *pos; break;
|
|
|
|
case SZ_SEEK_END: newPos = me->m_file.size() - *pos; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (me->m_file.seek(newPos))
|
|
|
|
{
|
|
|
|
*pos = newPos;
|
|
|
|
return SZ_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return SZ_ERROR_READ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
CFilesExtractor::CFilesExtractor(IOperationProgressListener *listener):m_listener(listener)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CFilesExtractor::~CFilesExtractor()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFilesExtractor::setSourceFile(const QString &src)
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
m_sourceFile = src;
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
void CFilesExtractor::setDesinationDirectory(const QString &dst)
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
m_destinationDirectory = dst;
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
bool CFilesExtractor::exec()
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_sourceFile.isEmpty() || m_destinationDirectory.isEmpty()) return false;
|
2016-02-25 20:19:27 +00:00
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener) m_listener->operationPrepare();
|
|
|
|
|
|
|
|
QFile file(m_sourceFile);
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
// open archive file to check format
|
|
|
|
if (!file.open(QFile::ReadOnly)) return false;
|
|
|
|
|
|
|
|
// read 2 first bytes
|
|
|
|
QByteArray header = file.read(2);
|
|
|
|
|
|
|
|
// close file
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
// create destination directory
|
|
|
|
QDir dir;
|
2016-05-16 09:10:01 +00:00
|
|
|
dir.mkpath(m_destinationDirectory);
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
// compare to supported formats and call the appropriate decompressor
|
|
|
|
if (header == "7z")
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
return extract7z();
|
2016-05-14 16:50:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
if (header == "PK")
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
return extractZip();
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
if (QFileInfo(m_sourceFile).suffix().toLower() == "bnp")
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
return extractBnp();
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
qDebug() << "Unsupported format";
|
|
|
|
return false;
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
bool CFilesExtractor::extract7z()
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
Q7zFile inFile(m_sourceFile);
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
if (!inFile.open())
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener) m_listener->operationFail(QApplication::tr("Unable to open %1").arg(m_sourceFile));
|
2016-02-25 20:19:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
UInt16 *temp = NULL;
|
|
|
|
size_t tempSize = 0;
|
|
|
|
|
|
|
|
// register the files read handlers
|
|
|
|
CLookToRead lookStream;
|
|
|
|
lookStream.realStream = &inFile;
|
|
|
|
LookToRead_CreateVTable(&lookStream, False);
|
|
|
|
LookToRead_Init(&lookStream);
|
|
|
|
|
|
|
|
// init CRC table
|
|
|
|
CrcGenerateTable();
|
|
|
|
|
|
|
|
// init 7z
|
|
|
|
CSzArEx db;
|
|
|
|
SzArEx_Init(&db);
|
|
|
|
|
|
|
|
// register allocators
|
|
|
|
ISzAlloc allocImp;
|
|
|
|
allocImp.Alloc = SzAlloc;
|
|
|
|
allocImp.Free = SzFree;
|
|
|
|
|
|
|
|
ISzAlloc allocTempImp;
|
|
|
|
allocTempImp.Alloc = SzAllocTemp;
|
|
|
|
allocTempImp.Free = SzFreeTemp;
|
|
|
|
|
|
|
|
qint64 total = 0, totalUncompressed = 0;
|
|
|
|
QString error;
|
|
|
|
|
|
|
|
// open 7z acrhive
|
|
|
|
SRes res = SzArEx_Open(&db, &lookStream.s, &allocImp, &allocTempImp);
|
|
|
|
|
|
|
|
if (res == SZ_OK)
|
|
|
|
{
|
|
|
|
// process each file in archive
|
|
|
|
for (UInt32 i = 0; i < db.NumFiles; ++i)
|
|
|
|
{
|
|
|
|
bool isDir = SzArEx_IsDir(&db, i) != 0;
|
|
|
|
|
|
|
|
if (!isDir) total += SzArEx_GetFileSize(&db, i);
|
|
|
|
}
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener)
|
|
|
|
{
|
|
|
|
m_listener->operationInit(0, total);
|
|
|
|
m_listener->operationStart();
|
|
|
|
}
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
// variables used for decompression
|
|
|
|
UInt32 blockIndex = 0xFFFFFFFF;
|
|
|
|
Byte *outBuffer = NULL;
|
|
|
|
size_t outBufferSize = 0;
|
|
|
|
|
|
|
|
// process each file in archive
|
|
|
|
for (UInt32 i = 0; i < db.NumFiles; ++i)
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener && m_listener->operationShouldStop())
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
|
|
|
res = SZ_ERROR_INTERRUPTED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t offset = 0;
|
|
|
|
size_t outSizeProcessed = 0;
|
|
|
|
|
|
|
|
bool isDir = SzArEx_IsDir(&db, i) != 0;
|
|
|
|
|
|
|
|
size_t len = SzArEx_GetFileNameUtf16(&db, i, NULL);
|
|
|
|
|
|
|
|
if (len > tempSize)
|
|
|
|
{
|
|
|
|
SzFree(NULL, temp);
|
|
|
|
tempSize = len;
|
|
|
|
temp = (UInt16 *)SzAlloc(NULL, tempSize * sizeof(temp[0]));
|
|
|
|
if (!temp)
|
|
|
|
{
|
|
|
|
res = SZ_ERROR_MEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SzArEx_GetFileNameUtf16(&db, i, temp);
|
|
|
|
|
|
|
|
QString path = QString::fromUtf16(temp);
|
|
|
|
QString filename = QFileInfo(path).fileName();
|
|
|
|
|
|
|
|
if (!isDir)
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener) m_listener->operationProgress(totalUncompressed, filename);
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
res = SzArEx_Extract(&db, &lookStream.s, i, &blockIndex, &outBuffer, &outBufferSize,
|
|
|
|
&offset, &outSizeProcessed, &allocImp, &allocTempImp);
|
|
|
|
|
|
|
|
if (res != SZ_OK) break;
|
|
|
|
}
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
QString destPath = m_destinationDirectory + '/' + path;
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
QDir dir;
|
|
|
|
|
|
|
|
if (isDir)
|
|
|
|
{
|
|
|
|
dir.mkpath(destPath);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir.mkpath(QFileInfo(destPath).absolutePath());
|
|
|
|
|
|
|
|
QFile outFile(destPath);
|
|
|
|
|
|
|
|
if (!outFile.open(QFile::WriteOnly))
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
error = QApplication::tr("Unable to open output file");
|
2016-02-25 20:19:27 +00:00
|
|
|
res = SZ_ERROR_FAIL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t processedSize = outFile.write((const char*)(outBuffer + offset), outSizeProcessed);
|
|
|
|
|
|
|
|
if (processedSize != outSizeProcessed)
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
error = QApplication::tr("Unable to write output file");
|
2016-02-25 20:19:27 +00:00
|
|
|
res = SZ_ERROR_FAIL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
outFile.close();
|
|
|
|
|
|
|
|
totalUncompressed += SzArEx_GetFileSize(&db, i);
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener) m_listener->operationProgress(totalUncompressed, filename);
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
if (SzBitWithVals_Check(&db.Attribs, i))
|
|
|
|
Set7zFileAttrib(destPath, db.Attribs.Vals[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
IAlloc_Free(&allocImp, outBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
SzArEx_Free(&db, &allocImp);
|
|
|
|
SzFree(NULL, temp);
|
|
|
|
|
|
|
|
switch(res)
|
|
|
|
{
|
|
|
|
case SZ_OK:
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener)
|
|
|
|
{
|
|
|
|
m_listener->operationSuccess(totalUncompressed);
|
|
|
|
}
|
2016-02-25 20:19:27 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case SZ_ERROR_INTERRUPTED:
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener) m_listener->operationStop();
|
2016-02-25 20:19:27 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case SZ_ERROR_UNSUPPORTED:
|
2016-05-16 09:10:01 +00:00
|
|
|
error = QApplication::tr("7zip decoder doesn't support this archive");
|
2016-02-25 20:19:27 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SZ_ERROR_MEM:
|
2016-05-16 09:10:01 +00:00
|
|
|
error = QApplication::tr("Unable to allocate memory");
|
2016-02-25 20:19:27 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SZ_ERROR_CRC:
|
2016-05-16 09:10:01 +00:00
|
|
|
error = QApplication::tr("7zip decoder doesn't support this archive");
|
2016-02-25 20:19:27 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SZ_ERROR_FAIL:
|
|
|
|
// error already defined
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-05-16 09:10:01 +00:00
|
|
|
error = QApplication::tr("Error %1").arg(res);
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener) m_listener->operationFail(error);
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
bool CFilesExtractor::extractZip()
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
QZipReader reader(m_sourceFile);
|
2016-02-25 20:19:27 +00:00
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
QDir baseDir(m_destinationDirectory);
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
// create directories first
|
|
|
|
QList<QZipReader::FileInfo> allFiles = reader.fileInfoList();
|
|
|
|
|
|
|
|
qint64 totalSize = 0, currentSize = 0;
|
|
|
|
|
|
|
|
foreach (const QZipReader::FileInfo &fi, allFiles)
|
|
|
|
{
|
|
|
|
if (fi.isDir)
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
const QString absPath = m_destinationDirectory + QDir::separator() + fi.filePath;
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
if (!baseDir.mkpath(fi.filePath))
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener) m_listener->operationFail(QApplication::tr("Unable to create directory %1").arg(absPath));
|
2016-02-25 20:19:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!QFile::setPermissions(absPath, fi.permissions))
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener) m_listener->operationFail(QApplication::tr("Unable to set permissions of %1").arg(absPath));
|
2016-02-25 20:19:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
totalSize += fi.size;
|
|
|
|
}
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener)
|
|
|
|
{
|
|
|
|
m_listener->operationInit(0, totalSize);
|
|
|
|
m_listener->operationStart();
|
|
|
|
}
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
// client won't use symbolic links so don't process them
|
|
|
|
|
|
|
|
foreach (const QZipReader::FileInfo &fi, allFiles)
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
const QString absPath = m_destinationDirectory + QDir::separator() + fi.filePath;
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
if (fi.isFile)
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener && m_listener->operationShouldStop())
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
m_listener->operationStop();
|
2016-02-25 20:19:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile f(absPath);
|
|
|
|
|
|
|
|
if (!f.open(QIODevice::WriteOnly))
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener) m_listener->operationFail(QApplication::tr("Unable to open %1").arg(absPath));
|
2016-02-25 20:19:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentSize += f.write(reader.fileData(fi.filePath));
|
|
|
|
|
|
|
|
f.setPermissions(fi.permissions);
|
|
|
|
f.close();
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener) m_listener->operationProgress(currentSize, QFileInfo(absPath).fileName());
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener)
|
|
|
|
{
|
|
|
|
m_listener->operationSuccess(totalSize);
|
|
|
|
}
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
bool CFilesExtractor::progress(const std::string &filename, uint32 currentSize, uint32 totalSize)
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener && m_listener->operationShouldStop())
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
m_listener->operationStop();
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentSize == 0)
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener)
|
|
|
|
{
|
|
|
|
m_listener->operationInit(0, (qint64)totalSize);
|
|
|
|
m_listener->operationStart();
|
|
|
|
}
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener) m_listener->operationProgress((qint64)currentSize, qFromUtf8(filename));
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
if (currentSize == totalSize)
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener)
|
|
|
|
{
|
|
|
|
m_listener->operationSuccess((qint64)totalSize);
|
|
|
|
}
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
bool CFilesExtractor::extractBnp()
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
|
|
|
QString error;
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
NLMISC::CBigFile::TUnpackProgressCallback cbMethod = NLMISC::CBigFile::TUnpackProgressCallback(this, &CFilesExtractor::progress);
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
if (NLMISC::CBigFile::unpack(qToUtf8(m_sourceFile), qToUtf8(m_destinationDirectory), &cbMethod))
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener && m_listener->operationShouldStop())
|
2016-02-25 20:19:27 +00:00
|
|
|
{
|
|
|
|
// stopped
|
2016-05-16 12:55:46 +00:00
|
|
|
m_listener->operationStop();
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
error.clear();
|
|
|
|
}
|
|
|
|
catch(const NLMISC::EDiskFullError &e)
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
error = QApplication::tr("disk full");
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
|
|
|
catch(const NLMISC::EWriteError &e)
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
error = QApplication::tr("unable to write %1").arg(qFromUtf8(e.Filename));
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
|
|
|
catch(const NLMISC::EReadError &e)
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
error = QApplication::tr("unable to read %1").arg(qFromUtf8(e.Filename));
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2016-05-16 09:10:01 +00:00
|
|
|
error = QApplication::tr("failed (%1)").arg(qFromUtf8(e.what()));
|
2016-02-25 20:19:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 09:10:01 +00:00
|
|
|
if (m_listener) m_listener->operationFail(QApplication::tr("Unable to unpack %1 to %2: %3").arg(m_sourceFile).arg(m_destinationDirectory).arg(error));
|
2016-02-25 20:19:27 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|