added full ExtractCallback errors managementd to YACReader

This commit is contained in:
Luis Ángel San Martín
2013-12-16 16:35:12 +01:00
commit 45b4bebce8
737 changed files with 67327 additions and 0 deletions

View File

@ -0,0 +1,65 @@
#ifndef _7Z_INCLUDES_H
#define _7Z_INCLUDES_H
//WIN includes
#ifdef Q_OS_WIN
#include "lib7zip/CPP/Common/StringConvert.h"
#include "lib7zip/CPP/Common/MyInitGuid.h"
#include "lib7zip/CPP/Common/MyCom.h"
#include "lib7zip/CPP/7zip/Common/FileStreams.h"
#include "lib7zip/CPP/7zip/Archive/IArchive.h"
#include "lib7zip/CPP/7zip/IStream.h"
#include "lib7zip/CPP/7zip/IPassword.h"
#include "lib7zip/CPP/7zip/MyVersion.h"
#include "lib7zip/C/Types.h"
#include "lib7zip/CPP/Windows/PropVariant.h"
#include "lib7zip/CPP/Windows/PropVariantConversions.h"
#include "lib7zip/CPP/7zip/Common/StreamObjects.h"
#include "lib7zip/CPP/7zip/Common/StreamUtils.h"
extern "C"
{
#include "lib7zip/C/Alloc.h"
}
#else
//POSIX includes
#include "libp7zip/CPP/myWindows/myPrivate.h"
#include "libp7zip/CPP/myWindows/config.h"
#include "libp7zip/CPP/Common/MyGuidDef.h"
#include "libp7zip/CPP/Common/MyWindows.h"
#include "libp7zip/CPP/Common/StringConvert.h"
#include "libp7zip/CPP/Common/MyInitGuid.h"
#include "libp7zip/CPP/Common/MyCom.h"
#include "libp7zip/CPP/7zip/Common/FileStreams.h"
#include "libp7zip/CPP/7zip/Archive/IArchive.h"
#include "libp7zip/CPP/7zip/IStream.h"
#include "libp7zip/CPP/7zip/IPassword.h"
#include "libp7zip/CPP/7zip/MyVersion.h"
#include "libp7zip/C/Types.h"
#include "libp7zip/CPP/Windows/Defs.h"
#include "libp7zip/CPP/Windows/PropVariant.h"
#include "libp7zip/CPP/Windows/PropVariantConversions.h"
#include "libp7zip/CPP/7zip/Common/StreamObjects.h"
#include "libp7zip/CPP/7zip/Common/StreamUtils.h"
#include "libp7zip/CPP/7zip/ICoder.h"
extern "C"
{
#include "libp7zip/C/Alloc.h"
}
#endif
#endif // _7Z_INCLUDES_H

View File

@ -0,0 +1,9 @@
// StdAfx.h
#ifndef __STDAFX_H
#define __STDAFX_H
#include <windows.h>
#include <stdio.h>
#endif

View File

@ -0,0 +1,10 @@
/*--------------------------------------------------------------------
*
* Due to issues with the dependencies checker within the IDE, it
* sometimes fails to recompile the PCH file, if we force the IDE to
* This file is auto-generated by qmake since no PRECOMPILED_SOURCE was
* specified, and is used as the common stdafx.cpp. The file is only
* command line compilations by nmake.
*
--------------------------------------------------------------------*/
#include "StdAfx.h"

View File

@ -0,0 +1,355 @@
#include "compressed_archive.h"
#include "extract_delegate.h"
#include <QLibrary>
#include <QDebug>
#include <QApplication>
#include "open_callbacks.h"
#include "extract_callbacks.h"
#include "yacreader_global.h"
//DEFINE_GUID(CLSID_CFormat7z,0x23170F69, 0x40C1, 0x278A, 0x10, 0x00, 0x00, 0x01, 0x10, 0x07, 0x00, 0x00);
//DEFINE_GUID(IArchiveKK,0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x06, 0x00, 0x60, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormat7z, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x07, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatRar, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x03, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatZip, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x01, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatTar, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xee, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatArj, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x04, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatBZip2, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x02, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatCab, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x08, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatChm, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xe9, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatCompound,0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xe5, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatCpio, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xed, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatDeb, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xec, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatGZip, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xef, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatIso, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xe7, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatLzh, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x06, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatLzma, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x0a, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatNsis, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x09, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatRpm, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xeb, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatSplit, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xea, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatWim, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0xe6, 0x00, 0x00);
DEFINE_GUID(CLSID_CFormatZ, 0x23170f69, 0x40c1, 0x278a, 0x10, 0x00, 0x00, 0x01, 0x10, 0x05, 0x00, 0x00);
#ifdef Q_OS_WIN
GUID _supportedFileFormats[] = {CLSID_CFormatRar,CLSID_CFormatZip,CLSID_CFormatTar,CLSID_CFormat7z,CLSID_CFormatArj};
#else
GUID _supportedFileFormats[] = {CLSID_CFormatZip,CLSID_CFormatTar,CLSID_CFormat7z,CLSID_CFormatArj};
#endif
std::vector<GUID> supportedFileFormats (_supportedFileFormats, _supportedFileFormats + sizeof(_supportedFileFormats) / sizeof(_supportedFileFormats[0]) );
DEFINE_GUID(IID_InArchive, 0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x06, 0x00, 0x60, 0x00, 0x00);
DEFINE_GUID(IID_ISetCompressCodecsInfo, 0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x04, 0x00, 0x61, 0x00, 0x00);
#ifdef Q_OS_UNIX
DEFINE_GUID(IID_IOutStream, 0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x03, 0x00, 0x04, 0x00, 0x00);
DEFINE_GUID(IID_IInStream, 0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00);
DEFINE_GUID(IID_IStreamGetSize, 0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x03, 0x00, 0x06, 0x00, 0x00);
DEFINE_GUID(IID_ISequentialInStream, 0x23170F69, 0x40C1, 0x278A, 0x00, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00);
#endif
struct SevenZipInterface {
CreateObjectFunc createObjectFunc;
GetMethodPropertyFunc getMethodPropertyFunc;
GetNumberOfMethodsFunc getNumberOfMethodsFunc;
GetNumberOfFormatsFunc getNumberOfFormatsFunc;
GetHandlerPropertyFunc getHandlerPropertyFunc;
GetHandlerPropertyFunc2 getHandlerPropertyFunc2;
SetLargePageModeFunc setLargePageModeFunc;
#ifdef Q_OS_UNIX
CreateObjectFunc createObjectFuncRar;
GetMethodPropertyFunc getMethodPropertyFuncRar;
GetNumberOfMethodsFunc getNumberOfMethodsFuncRar;
#endif
CMyComPtr<IInArchive> archive;
};
//SevenZipInterface * szInterface;
CompressedArchive::CompressedArchive(const QString & filePath, QObject *parent) :
QObject(parent),sevenzLib(0),valid(false),tools(false)
#ifdef Q_OS_UNIX
,isRar(false)
#endif
{
szInterface = new SevenZipInterface;
//load functions
if(!loadFunctions())
return;
tools = true;
//load file
if(szInterface->createObjectFunc != 0)
{
//QUuid CLSID_CFormat7z("23170f69-40c1-278a-1000-000110070000");
//se crea el objeto Archivo: formato,tipo,objeto
bool formatFound = false;
CInFileStream *fileSpec = new CInFileStream;
CMyComPtr<IInStream> file = fileSpec;
CArchiveOpenCallback *openCallbackSpec = new CArchiveOpenCallback;
CMyComPtr<IArchiveOpenCallback> openCallback = openCallbackSpec;
openCallbackSpec->PasswordIsDefined = false;
// openCallbackSpec->PasswordIsDefined = true;
// openCallbackSpec->Password = L"1";
for(unsigned int i=0;i<supportedFileFormats.size();i++)
{
//GUID uuid = supportedFileFormats[i];
//qDebug() << "trying : " << uuid << endl;
if (szInterface->createObjectFunc(&supportedFileFormats[i], &IID_InArchive, (void **)&szInterface->archive) != S_OK)
{
qDebug() << "wrong format";
continue;
}
if (!fileSpec->Open((LPCTSTR)filePath.toStdWString().data()))
{
qDebug() << "unable to load" + filePath;
continue;
}
//qDebug() << "Can not open archive file : " + filePath << endl;
if (szInterface->archive->Open(file, 0, openCallback) == S_OK)
{
valid = formatFound = true;
break;
}
else
qDebug() << "Can not open archive file : " + filePath << endl;
}
if(!formatFound)
{
#ifdef Q_OS_WIN
qDebug() << "Can not open archive" << endl;
#else
if (szInterface->createObjectFunc(&CLSID_CFormatRar, &IID_InArchive, (void **)&szInterface->archive) != S_OK)
{
qDebug() << "Error creating rar archive :" + filePath;
return;
}
CMyComPtr<ISetCompressCodecsInfo> codecsInfo;
if (szInterface->archive->QueryInterface(IID_ISetCompressCodecsInfo,(void **)&codecsInfo) != S_OK)
{
qDebug() << "Error getting rar codec :" + filePath;
return;
}
if (codecsInfo->SetCompressCodecsInfo(this) != S_OK)
{
qDebug() << "Error setting rar codec";
return;
}
if (!fileSpec->Open((LPCTSTR)filePath.toStdWString().data()))
{
qDebug() << "Error opening rar file :" + filePath;
return;
}
//qDebug() << "Can not open archive file : " + filePath << endl;
if (szInterface->archive->Open(file, 0, openCallback) == S_OK)
{
valid = formatFound = true;
isRar = true;
}
else
qDebug() << "Error opening rar archive";
#endif
}
}
}
CompressedArchive::~CompressedArchive()
{
#ifdef Q_OS_UNIX
if(isRar) //TODO: fix this!!! Posible memory leak. If AddRef is not used, a crash occurs in "delete szInterface"
szInterface->archive->AddRef();
#endif
delete szInterface;
#ifdef Q_OS_UNIX
delete rarLib;
#endif
delete sevenzLib;
}
bool CompressedArchive::loadFunctions()
{
//LOAD library
//TODO check if this works in OSX (7z.so instead of 7z.dylib)
// fix1: try to load "7z.so"
// fix2: rename 7z.so to 7z.dylib
if(sevenzLib == 0)
{
#ifdef Q_OS_UNIX
rarLib = new QLibrary(QApplication::applicationDirPath()+"/utils/Codecs/Rar29");
if(!rarLib->load())
{
qDebug() << "Error Loading Rar29.so : " + rarLib->errorString() << endl;
QApplication::exit(YACReader::SevenZNotFound);
return false;
}
#endif
sevenzLib = new QLibrary(QApplication::applicationDirPath()+"/utils/7z");
}
if(!sevenzLib->load())
{
qDebug() << "Error Loading 7z.dll : " + sevenzLib->errorString() << endl;
QApplication::exit(YACReader::SevenZNotFound);
return false;
}
else
{
qDebug() << "Loading functions" << endl;
if((szInterface->createObjectFunc = (CreateObjectFunc)sevenzLib->resolve("CreateObject")) == 0)
qDebug() << "fail loading function : CreateObject" << endl;
if((szInterface->getMethodPropertyFunc = (GetMethodPropertyFunc)sevenzLib->resolve("GetMethodProperty")) == 0)
qDebug() << "fail loading function : GetMethodProperty" << endl;
if((szInterface->getNumberOfMethodsFunc = (GetNumberOfMethodsFunc)sevenzLib->resolve("GetNumberOfMethods")) == 0)
qDebug() << "fail loading function : GetNumberOfMethods" << endl;
if((szInterface->getNumberOfFormatsFunc = (GetNumberOfFormatsFunc)sevenzLib->resolve("GetNumberOfFormats")) == 0)
qDebug() << "fail loading function : GetNumberOfFormats" << endl;
if((szInterface->getHandlerPropertyFunc = (GetHandlerPropertyFunc)sevenzLib->resolve("GetHandlerProperty")) == 0)
qDebug() << "fail loading function : GetHandlerProperty" << endl;
if((szInterface->getHandlerPropertyFunc2 = (GetHandlerPropertyFunc2)sevenzLib->resolve("GetHandlerProperty2")) == 0)
qDebug() << "fail loading function : GetHandlerProperty2" << endl;
if((szInterface->setLargePageModeFunc = (SetLargePageModeFunc)sevenzLib->resolve("SetLargePageMode")) == 0)
qDebug() << "fail loading function : SetLargePageMode" << endl;
#ifdef Q_OS_UNIX
if((szInterface->createObjectFuncRar = (CreateObjectFunc)rarLib->resolve("CreateObject")) == 0)
qDebug() << "fail loading function (rar) : CreateObject" << endl;
if((szInterface->getMethodPropertyFuncRar = (GetMethodPropertyFunc)rarLib->resolve("GetMethodProperty")) == 0)
qDebug() << "fail loading function (rar) : GetMethodProperty" << endl;
if((szInterface->getNumberOfMethodsFuncRar = (GetNumberOfMethodsFunc)rarLib->resolve("GetNumberOfMethods")) == 0)
qDebug() << "fail loading function (rar) : GetNumberOfMethods" << endl;
#endif
}
return true;
}
QList<QString> CompressedArchive::getFileNames()
{
QList<QString> files;
quint32 numItems = getNumFiles();
for (quint32 i = 0; i < numItems; i++)
{
{
// Get name of file
NWindows::NCOM::CPropVariant prop;
/*szInterface->archive->GetProperty(i, kpidIsDir, &prop);
bool isDir;
if (prop.vt == VT_BOOL)
isDir = VARIANT_BOOLToBool(prop.boolVal);
else if (prop.vt == VT_EMPTY)
isDir = false;
if(!isDir)
{*/
szInterface->archive->GetProperty(i, kpidPath, &prop);
UString s = ConvertPropVariantToString(prop);
const wchar_t * chars = s.operator const wchar_t *();
files.append(QString::fromWCharArray(chars));
//}
}
}
return files;
}
bool CompressedArchive::isValid()
{
return valid;
}
bool CompressedArchive::toolsLoaded()
{
return tools;
}
int CompressedArchive::getNumFiles()
{
quint32 numItems = 0;
szInterface->archive->GetNumberOfItems(&numItems);
return numItems;
}
QList<QByteArray> CompressedArchive::getAllData(const QVector<quint32> & indexes, ExtractDelegate * delegate)
{
CArchiveExtractCallback *extractCallbackSpec = new CArchiveExtractCallback(true,delegate);
CMyComPtr<IArchiveExtractCallback> extractCallback(extractCallbackSpec);
extractCallbackSpec->Init(szInterface->archive, L""); // second parameter is output folder path
extractCallbackSpec->PasswordIsDefined = false;
HRESULT result;
if(indexes.isEmpty())
result = szInterface->archive->Extract(NULL, -1, false, extractCallback);
else
result = szInterface->archive->Extract(indexes.data(), indexes.count(), false, extractCallback);
if (result != S_OK)
{
qDebug() << "Extract Error" << endl;
}
return extractCallbackSpec->allFiles;
}
QByteArray CompressedArchive::getRawDataAtIndex(int index)
{
if(index>=0 && index < getNumFiles())
{
CArchiveExtractCallback *extractCallbackSpec = new CArchiveExtractCallback;
CMyComPtr<IArchiveExtractCallback> extractCallback(extractCallbackSpec);
extractCallbackSpec->Init(szInterface->archive, L""); // second parameter is output folder path
extractCallbackSpec->PasswordIsDefined = false;
UInt32 indices[1];
indices[0] = index;
HRESULT result = szInterface->archive->Extract(indices, 1, false, extractCallback);
if (result != S_OK)
{
qDebug() << "Extract Error" << endl;
}
return QByteArray((char *)extractCallbackSpec->data,extractCallbackSpec->newFileSize);
}
return QByteArray();
}
#ifdef Q_OS_UNIX
STDMETHODIMP CompressedArchive::GetNumberOfMethods(UInt32 *numMethods)
{
return szInterface->getNumberOfMethodsFuncRar(numMethods);
}
STDMETHODIMP CompressedArchive::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *value)
{
return szInterface->getMethodPropertyFuncRar(index,propID,value);
}
int i = 0;
STDMETHODIMP CompressedArchive::CreateDecoder(UInt32 index, const GUID *interfaceID, void **coder)
{
NCOM::CPropVariant propVariant;
szInterface->getMethodPropertyFuncRar(index,NMethodPropID::kDecoder,&propVariant);
return szInterface->createObjectFuncRar((const GUID *)propVariant.bstrVal,interfaceID,coder);
}
STDMETHODIMP CompressedArchive::CreateEncoder(UInt32 index, const GUID *interfaceID, void **coder)
{
return S_OK;//szInterface->createObjectFuncRar(&CLSID_CFormatRar,interfaceID,coder);
}
#endif

View File

@ -0,0 +1,80 @@
#ifndef COMPRESSED_ARCHIVE_H
#define COMPRESSED_ARCHIVE_H
#include <QObject>
#ifdef Q_OS_UNIX
#include "libp7zip/CPP/7zip/ICoder.h"
#include "libp7zip/CPP/Common/MyCom.h"
#endif
class ExtractDelegate;
#ifdef Q_OS_WIN
#include "7z_includes.h"
#define _MY_WINAPI WINAPI
#else
#define _MY_WINAPI
#endif
typedef quint32 (_MY_WINAPI * CreateObjectFunc)(const GUID *clsID,const GUID *interfaceID,void **outObject);
typedef quint32 (_MY_WINAPI *GetMethodPropertyFunc)(quint32 index, PROPID propID, PROPVARIANT *value);
typedef quint32 (_MY_WINAPI *GetNumberOfMethodsFunc)(quint32 *numMethods);
typedef quint32 (_MY_WINAPI *GetNumberOfFormatsFunc)(quint32 *numFormats);
typedef quint32 (_MY_WINAPI *GetHandlerPropertyFunc)(PROPID propID, PROPVARIANT *value);
typedef quint32 (_MY_WINAPI *GetHandlerPropertyFunc2)(quint32 index, PROPID propID, PROPVARIANT *value);
typedef quint32 (_MY_WINAPI *SetLargePageModeFunc)();
class QLibrary;
#include <QString>
#include <QList>
struct SevenZipInterface;
class MyCodecs;
#ifdef Q_OS_UNIX
class CompressedArchive : public QObject, public ICompressCodecsInfo, public CMyUnknownImp
#else
class CompressedArchive : public QObject
#endif
{
Q_OBJECT
public:
explicit CompressedArchive(const QString & filePath, QObject *parent = 0);
~CompressedArchive();
#ifdef Q_OS_UNIX
MY_UNKNOWN_IMP
STDMETHOD(GetNumberOfMethods)(UInt32 *numMethods);
STDMETHOD(GetProperty)(UInt32 index, PROPID propID, PROPVARIANT *value);
STDMETHOD(CreateDecoder)(UInt32 index, const GUID *iid, void **coder);
STDMETHOD(CreateEncoder)(UInt32 index, const GUID *iid, void **coder);
bool isRar;
#endif
signals:
public slots:
int getNumFiles();
QList<QByteArray> getAllData(const QVector<quint32> & indexes, ExtractDelegate * delegate = 0);
QByteArray getRawDataAtIndex(int index);
QList<QString> getFileNames();
bool isValid();
bool toolsLoaded();
private:
SevenZipInterface * szInterface;
QLibrary * sevenzLib;
#ifdef Q_OS_UNIX
QLibrary * rarLib;
#endif
bool loadFunctions();
bool tools;
bool valid;
friend class MyCodecs;
};
#endif // COMPRESSED_ARCHIVE_H

View File

@ -0,0 +1,328 @@
#ifndef EXTRACT_CALLBACKS_H
#define EXTRACT_CALLBACKS_H
#include "7z_includes.h"
#include "extract_delegate.h"
#include <QDebug>
using namespace NWindows;
//////////////////////////////////////////////////////////////
// Archive Extracting callback class
static const wchar_t *kCantDeleteOutputFile = L"ERROR: Can not delete output file ";
static const char *kTestingString = "Testing ";
static const char *kExtractingString = "Extracting ";
static const char *kSkippingString = "Skipping ";
static const char *kUnsupportedMethod = "Unsupported Method";
static const char *kCRCFailed = "CRC Failed";
static const char *kDataError = "Data Error";
static const char *kUnknownError = "Unknown Error";
static const wchar_t *kEmptyFileAlias = L"[Content]";
static HRESULT IsArchiveItemProp(IInArchive *archive, UInt32 index, PROPID propID, bool &result)
{
NCOM::CPropVariant prop;
RINOK(archive->GetProperty(index, propID, &prop));
if (prop.vt == VT_BOOL)
result = VARIANT_BOOLToBool(prop.boolVal);
else if (prop.vt == VT_EMPTY)
result = false;
else
return E_FAIL;
return S_OK;
}
static HRESULT IsArchiveItemFolder(IInArchive *archive, UInt32 index, bool &result)
{
return IsArchiveItemProp(archive, index, kpidIsDir, result);
}
class CArchiveExtractCallback:
public IArchiveExtractCallback,
public ICryptoGetTextPassword,
public CMyUnknownImp
{
public:
MY_UNKNOWN_IMP1(ICryptoGetTextPassword)
// IProgress
STDMETHOD(SetTotal)(UInt64 size);
STDMETHOD(SetCompleted)(const UInt64 *completeValue);
// IArchiveExtractCallback
STDMETHOD(GetStream)(UInt32 index, ISequentialOutStream **outStream, Int32 askExtractMode);
STDMETHOD(PrepareOperation)(Int32 askExtractMode);
STDMETHOD(SetOperationResult)(Int32 resultEOperationResult);
// ICryptoGetTextPassword
STDMETHOD(CryptoGetTextPassword)(BSTR *aPassword);
private:
CMyComPtr<IInArchive> _archiveHandler;
UString _directoryPath; // Output directory
UString _filePath; // name inside arcvhive
UString _diskFilePath; // full path to file on disk
bool _extractMode;
bool all;
ExtractDelegate * delegate;
UInt32 _index;
struct CProcessedFileInfo
{
FILETIME MTime;
UInt32 Attrib;
bool isDir;
bool AttribDefined;
bool MTimeDefined;
} _processedFileInfo;
COutFileStream *_outFileStreamSpec;
CMyComPtr<ISequentialOutStream> _outFileStream;
public:
void Init(IInArchive *archiveHandler, const UString &directoryPath);
UInt64 NumErrors;
bool PasswordIsDefined;
QList<QByteArray> allFiles;
UString Password;
Byte * data;
UInt64 newFileSize;
CArchiveExtractCallback(bool c = false,ExtractDelegate * d = 0) : PasswordIsDefined(false),all(c),delegate(d) {}
~CArchiveExtractCallback() {MidFree(data);}
};
void CArchiveExtractCallback::Init(IInArchive *archiveHandler, const UString &directoryPath)
{
NumErrors = 0;
_archiveHandler = archiveHandler;
directoryPath;//unused
}
STDMETHODIMP CArchiveExtractCallback::SetTotal(UInt64 /* size */)
{
return S_OK;
}
STDMETHODIMP CArchiveExtractCallback::SetCompleted(const UInt64 * /* completeValue */)
{
return S_OK;
}
STDMETHODIMP CArchiveExtractCallback::GetStream(UInt32 index,
ISequentialOutStream **outStream, Int32 askExtractMode)
{
*outStream = 0;
_outFileStream.Release();
_index = index;
{
// Get Name
NCOM::CPropVariant prop;
RINOK(_archiveHandler->GetProperty(index, kpidPath, &prop));
UString fullPath;
if (prop.vt == VT_EMPTY)
fullPath = kEmptyFileAlias;
else
{
if (prop.vt != VT_BSTR)
return E_FAIL;
fullPath = prop.bstrVal;
}
_filePath = fullPath;
}
askExtractMode;//unused
//if (askExtractMode != NArchive::NExtract::NAskMode::kExtract)
//return S_OK;
{
// Get Attrib
NCOM::CPropVariant prop;
RINOK(_archiveHandler->GetProperty(index, kpidAttrib, &prop));
if (prop.vt == VT_EMPTY)
{
_processedFileInfo.Attrib = 0;
_processedFileInfo.AttribDefined = false;
}
else
{
if (prop.vt != VT_UI4)
return E_FAIL;
_processedFileInfo.Attrib = prop.ulVal;
_processedFileInfo.AttribDefined = true;
}
}
RINOK(IsArchiveItemFolder(_archiveHandler, index, _processedFileInfo.isDir));
{
// Get Modified Time
NCOM::CPropVariant prop;
RINOK(_archiveHandler->GetProperty(index, kpidMTime, &prop));
_processedFileInfo.MTimeDefined = false;
switch(prop.vt)
{
case VT_EMPTY:
// _processedFileInfo.MTime = _utcMTimeDefault;
break;
case VT_FILETIME:
_processedFileInfo.MTime = prop.filetime;
_processedFileInfo.MTimeDefined = true;
break;
default:
return E_FAIL;
}
}
//se necesita conocer el tama<6D>o del archivo para poder reservar suficiente memoria
bool newFileSizeDefined;
{
// Get Size
NCOM::CPropVariant prop;
RINOK(_archiveHandler->GetProperty(index, kpidSize, &prop));
newFileSizeDefined = (prop.vt != VT_EMPTY);
if (newFileSizeDefined)
newFileSize = ConvertPropVariantToUInt64(prop);
}
//No hay que crear ning<6E>n fichero, ni directorios intermedios
/*{
// Create folders for file
int slashPos = _filePath.ReverseFind(WCHAR_PATH_SEPARATOR);
if (slashPos >= 0)
NFile::NDirectory::CreateComplexDirectory(_directoryPath + _filePath.Left(slashPos));
}
UString fullProcessedPath = _directoryPath + _filePath;
_diskFilePath = fullProcessedPath;
*/
if (_processedFileInfo.isDir)
{
//NFile::NDirectory::CreateComplexDirectory(fullProcessedPath);
}
else
{
/*NFile::NFind::CFileInfoW fi;
if (fi.Find(fullProcessedPath))
{
if (!NFile::NDirectory::DeleteFileAlways(fullProcessedPath))
{
qDebug() <<(UString(kCantDeleteOutputFile) + fullProcessedPath);
return E_ABORT;
}
}*/
if(newFileSizeDefined)
{
CBufPtrSeqOutStream *outStreamSpec = new CBufPtrSeqOutStream;
CMyComPtr<CBufPtrSeqOutStream> outStreamLocal(outStreamSpec);
data = (Byte *)MidAlloc(newFileSize);
outStreamSpec->Init(data, newFileSize);
*outStream = outStreamLocal.Detach();
}
else
{
}
}
return S_OK;
}
STDMETHODIMP CArchiveExtractCallback::PrepareOperation(Int32 askExtractMode)
{
_extractMode = false;
switch (askExtractMode)
{
case NArchive::NExtract::NAskMode::kExtract: _extractMode = true; break;
};
/* switch (askExtractMode)
{
case NArchive::NExtract::NAskMode::kExtract: qDebug() << (kExtractingString); break;
case NArchive::NExtract::NAskMode::kTest: qDebug() <<(kTestingString); break;
case NArchive::NExtract::NAskMode::kSkip: qDebug() <<(kSkippingString); break;
};*/
//qDebug() << _filePath;
return S_OK;
}
STDMETHODIMP CArchiveExtractCallback::SetOperationResult(Int32 operationResult)
{
switch(operationResult)
{
case NArchive::NExtract::NOperationResult::kOK:
if(all && !_processedFileInfo.isDir)
{
QByteArray rawData((char *)data,newFileSize);
MidFree(data);
data = 0;
if(delegate != 0)
delegate->fileExtracted(_index,rawData);
else
{
allFiles.append(rawData);
}
}
break;
default:
{
NumErrors++;
qDebug() << " ";
switch(operationResult)
{
case NArchive::NExtract::NOperationResult::kUnSupportedMethod:
if(delegate != 0)
delegate->unknownError(_index);
qDebug() << kUnsupportedMethod;
break;
case NArchive::NExtract::NOperationResult::kCRCError:
if(delegate != 0)
delegate->crcError(_index);
qDebug() << kCRCFailed;
break;
case NArchive::NExtract::NOperationResult::kDataError:
if(delegate != 0)
delegate->unknownError(_index);
qDebug() << kDataError;
break;
default:
if(delegate != 0)
delegate->unknownError(_index);
qDebug() << kUnknownError;
}
}
}
/*
if (_outFileStream != NULL)
{
if (_processedFileInfo.MTimeDefined)
_outFileStreamSpec->SetMTime(&_processedFileInfo.MTime);
RINOK(_outFileStreamSpec->Close());
}
_outFileStream.Release();
if (_extractMode && _processedFileInfo.AttribDefined)
NFile::NDirectory::MySetFileAttributes(_diskFilePath, _processedFileInfo.Attrib);*/
//qDebug() << endl;
return S_OK;
}
STDMETHODIMP CArchiveExtractCallback::CryptoGetTextPassword(BSTR *password)
{
if (!PasswordIsDefined)
{
// You can ask real password here from user
// Password = GetPassword(OutStream);
// PasswordIsDefined = true;
qDebug() << "Password is not defined" << endl;
return E_ABORT;
}
return StringToBstr(Password, password);
}
#endif

View File

@ -0,0 +1,14 @@
#ifndef EXTRACT_DELEGATE_H
#define EXTRACT_DELEGATE_H
#include <QByteArray>
class ExtractDelegate
{
public:
virtual void fileExtracted(int index, const QByteArray & rawData) = 0;
virtual void crcError(int index) = 0;
virtual void unknownError(int index) = 0;
};
#endif //EXTRACT_DELEGATE_H

View File

@ -0,0 +1,54 @@
#ifndef OPEN_CALLBACKS_H
#define OPEN_CALLBACKS_H
#include "7z_includes.h"
#include <QDebug>
//////////////////////////////////////////////////////////////
// Archive Open callback class
class CArchiveOpenCallback:
public IArchiveOpenCallback,
public ICryptoGetTextPassword,
public CMyUnknownImp
{
public:
MY_UNKNOWN_IMP1(ICryptoGetTextPassword)
STDMETHOD(SetTotal)(const UInt64 *files, const UInt64 *bytes);
STDMETHOD(SetCompleted)(const UInt64 *files, const UInt64 *bytes);
STDMETHOD(CryptoGetTextPassword)(BSTR *password);
bool PasswordIsDefined;
UString Password;
CArchiveOpenCallback() : PasswordIsDefined(false) {}
};
STDMETHODIMP CArchiveOpenCallback::SetTotal(const UInt64 * /* files */, const UInt64 * /* bytes */)
{
return S_OK;
}
STDMETHODIMP CArchiveOpenCallback::SetCompleted(const UInt64 * /* files */, const UInt64 * /* bytes */)
{
return S_OK;
}
STDMETHODIMP CArchiveOpenCallback::CryptoGetTextPassword(BSTR *password)
{
if (!PasswordIsDefined)
{
// You can ask real password here from user
// Password = GetPassword(OutStream);
// PasswordIsDefined = true;
qDebug() << "Password is not defined" << endl;
return E_ABORT;
}
return StringToBstr(Password, password);
}
#endif

View File

@ -0,0 +1,110 @@
INCLUDEPATH += $$PWD
DEPENDPATH += $$PWD
CONFIG += precompile_header
win32 {PRECOMPILED_HEADER = $$PWD/StdAfx.h}
!win32 {PRECOMPILED_HEADER = $$PWD/libp7zip/CPP/myWindows/StdAfx.h}
win32 {
INCLUDEPATH += $$PWD/lib7zip/CPP/
DEFINES += _UNICODE _WIN32
SOURCES += $$PWD/compressed_archive.cpp \
$$PWD/lib7zip/CPP/Windows/FileIO.cpp \
$$PWD/lib7zip/CPP/Windows/PropVariant.cpp \
$$PWD/lib7zip/CPP/Windows/PropVariantConversions.cpp \
$$PWD/lib7zip/CPP/Common/IntToString.cpp \
$$PWD/lib7zip/CPP/Common/MyString.cpp \
$$PWD/lib7zip/CPP/Common/MyVector.cpp \
$$PWD/lib7zip/CPP/Common/StringConvert.cpp \
$$PWD/lib7zip/CPP/Common/Wildcard.cpp \
$$PWD/lib7zip/CPP/7zip/Common/FileStreams.cpp \
$$PWD/lib7zip/CPP/7zip/Common/StreamUtils.cpp \
$$PWD/lib7zip/C/Alloc.c \
$$PWD/lib7zip/CPP/7zip/Common/StreamObjects.cpp
HEADERS += $$PWD/compressed_archive.h \
$$PWD/extract_delegate.h \
$$PWD/7z_includes.h \
$$PWD/open_callbacks.h \
$$PWD/extract_callbacks.h\
$$PWD/lib7zip/CPP/Windows/FileIO.h \
$$PWD/lib7zip/CPP/Windows/PropVariant.h \
$$PWD/lib7zip/CPP/Windows/PropVariantConversions.h \
$$PWD/lib7zip/CPP/Common/IntToString.h \
$$PWD/lib7zip/CPP/Common/MyString.h \
$$PWD/lib7zip/CPP/Common/MyVector.h \
$$PWD/lib7zip/CPP/Common/StringConvert.h \
$$PWD/lib7zip/CPP/Common/Wildcard.h \
$$PWD/lib7zip/CPP/7zip/Common/FileStreams.h \
$$PWD/lib7zip/CPP/7zip/IStream.h \
$$PWD/lib7zip/CPP/7zip/Common/StreamUtils.h \
$$PWD/lib7zip/C/Alloc.h \
$$PWD/lib7zip/CPP/7zip/Common/StreamObjects.h
}
macx{
LIBS += -framework IOKit -framework CoreFoundation
DEFINES += UNICODE _UNICODE _FILE_OFFSET_BITS=64 _LARGEFILE_SOURCE \
NDEBUG _REENTRANT ENV_UNIX \
_7ZIP_LARGE_PAGES ENV_MACOSX _TCHAR_DEFINED
}
unix:!macx{
DEFINES += UNICODE _UNICODE _FILE_OFFSET_BITS=64 _LARGEFILE_SOURCE \
NDEBUG _REENTRANT ENV_UNIX \
_7ZIP_LARGE_PAGES
}
!win32 {
INCLUDEPATH += $$PWD/libp7zip/CPP/ \
$$PWD/libp7zip/CPP/myWindows/ \
$$PWD/libp7zip/CPP/include_windows/
SOURCES += $$PWD/compressed_archive.cpp \
$$PWD/libp7zip/CPP/Windows/FileIO.cpp \
$$PWD/libp7zip/CPP/Windows/FileFind.cpp \
$$PWD/libp7zip/CPP/Windows/PropVariant.cpp \
$$PWD/libp7zip/CPP/Windows/PropVariantConversions.cpp \
$$PWD/libp7zip/CPP/Common/IntToString.cpp \
$$PWD/libp7zip/CPP/Common/MyString.cpp \
$$PWD/libp7zip/CPP/Common/MyVector.cpp \
$$PWD/libp7zip/CPP/Common/StringConvert.cpp \
$$PWD/libp7zip/CPP/Common/Wildcard.cpp \
$$PWD/libp7zip/CPP/7zip/Common/FileStreams.cpp \
$$PWD/libp7zip/CPP/7zip/Common/StreamUtils.cpp \
$$PWD/libp7zip/C/Alloc.c \
$$PWD/libp7zip/CPP/7zip/Common/StreamObjects.cpp \
$$PWD/libp7zip/CPP/myWindows/wine_date_and_time.cpp \
$$PWD/libp7zip/CPP/Common/MyWindows.cpp
HEADERS += $$PWD/compressed_archive.h \
$$PWD/7z_includes.h \
$$PWD/open_callbacks.h \
$$PWD/extract_callbacks.h\
$$PWD/libp7zip/CPP/include_windows/windows.h \
$$PWD/libp7zip/CPP/include_windows/tchar.h \
$$PWD/libp7zip/CPP/include_windows/basetyps.h \
$$PWD/libp7zip/CPP/Windows/FileFind.h \
$$PWD/libp7zip/CPP/Windows/FileIO.h \
$$PWD/libp7zip/CPP/Windows/PropVariant.h \
$$PWD/libp7zip/CPP/Windows/PropVariantConversions.h \
$$PWD/libp7zip/CPP/Common/IntToString.h \
$$PWD/libp7zip/CPP/Common/MyString.h \
$$PWD/libp7zip/CPP/Common/MyVector.h \
$$PWD/libp7zip/CPP/Common/StringConvert.h \
$$PWD/libp7zip/CPP/Common/Wildcard.h \
$$PWD/libp7zip/CPP/7zip/Common/FileStreams.h \
$$PWD/libp7zip/CPP/7zip/IStream.h \
$$PWD/libp7zip/CPP/7zip/Common/StreamUtils.h \
$$PWD/libp7zip/C/Alloc.h \
$$PWD/libp7zip/CPP/7zip/Common/StreamObjects.h \
$$PWD/libp7zip/CPP/Common/MyWindows.h \
$$PWD/libp7zip/CPP/7zip/ICoder.h \
}