Skip to content

Commit

Permalink
Test callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Nov 20, 2017
1 parent 78753e3 commit 428e5f4
Show file tree
Hide file tree
Showing 4 changed files with 402 additions and 4 deletions.
132 changes: 132 additions & 0 deletions Lib7Zip/7ZipArchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,32 @@ class CCustomArchiveExtractCallback:
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:
C7ZipExtractCallback *m_pCallback;

C7ZipSequentialOutStreamWrap * _outFileStreamSpec;
CMyComPtr<ISequentialOutStream> _outFileStream;
C7ZipSequentialOutStream * m_pSequentialOutStream;
public:
CCustomArchiveExtractCallback(C7ZipExtractCallback *pCallback) :
m_pCallback(pCallback) {

}
};


Expand All @@ -135,6 +160,7 @@ class C7ZipArchiveImpl : public virtual C7ZipArchive
virtual bool Extract(unsigned int index, C7ZipSequentialOutStream * pSequentialOutStream);
virtual bool Extract(unsigned int index, C7ZipSequentialOutStream * pSequentialOutStream, const wstring & pwd);
virtual bool Extract(const C7ZipArchiveItem * pArchiveItem, C7ZipSequentialOutStream * pSequentialOutStream);
virtual bool ExtractSeveral(unsigned int *indexList, int numIndices, C7ZipExtractCallback *extractCallback);

virtual void Close();

Expand Down Expand Up @@ -223,6 +249,21 @@ bool C7ZipArchiveImpl::Extract(const C7ZipArchiveItem * pArchiveItem, C7ZipSeque
return m_pInArchive->Extract(&nArchiveIndex, 1, false, extractCallbackSpec) == S_OK;
}

bool C7ZipArchiveImpl::ExtractSeveral(unsigned int *indexList, int numIndices, C7ZipExtractCallback *pCallback) {
CCustomArchiveExtractCallback *extractCallbackSpec = new CCustomArchiveExtractCallback(pCallback);
CMyComPtr<IArchiveExtractCallback> extractCallback(extractCallbackSpec);

fprintf(stderr, "C7ZipArchiveImpl: passed %d indices\n", numIndices);
fflush(stderr);

for (int i = 0; i < numIndices; i++) {
fprintf(stderr, "C7ZipArchiveImpl: index %d = %u\n", i, indexList[i]);
fflush(stderr);
}

return m_pInArchive->Extract(indexList, numIndices, false, extractCallbackSpec) == S_OK;
}

void C7ZipArchiveImpl::Close()
{
m_pInArchive->Close();
Expand Down Expand Up @@ -279,6 +320,94 @@ bool Create7ZipArchive(C7ZipLibrary * pLibrary, IInArchive * pInArchive, C7ZipAr
return false;
}

//-------------------------------

STDMETHODIMP CCustomArchiveExtractCallback::SetTotal(UInt64 size)
{
m_pCallback->SetTotal(size);
return S_OK;
}

STDMETHODIMP CCustomArchiveExtractCallback::SetCompleted(const UInt64 * completeValue)
{
m_pCallback->SetCompleted(completeValue);
return S_OK;
}

STDMETHODIMP CCustomArchiveExtractCallback::GetStream(UInt32 index,
ISequentialOutStream **outStream, Int32 askExtractMode)
{
fprintf(stderr, "CCustomArchive::GetStream(%u)\n", index);
fflush(stderr);

if (askExtractMode != NArchive::NExtract::NAskMode::kExtract) {
fprintf(stderr, "CCustomArchive::GetStream(%u) - not extract (passed %d, wanted %d)\n", index,
askExtractMode, NArchive::NExtract::NAskMode::kExtract);
fflush(stderr);

// nothing to do then!
return S_OK;
}

m_pSequentialOutStream = m_pCallback->GetStream(index);
if (!m_pSequentialOutStream) {
fprintf(stderr, "CCustomArchive::GetStream - null callback->GetStream() result\n", index);
fflush(stderr);

// then just don't extract it I guess?
return S_OK;
}

fprintf(stderr, "CCustomArchive::GetStream - doing a wraperoo!\n", index);
fflush(stderr);

_outFileStreamSpec = new C7ZipSequentialOutStreamWrap(m_pSequentialOutStream);
CMyComPtr<ISequentialOutStream> outStreamLoc(_outFileStreamSpec);

_outFileStream = outStreamLoc;
*outStream = outStreamLoc.Detach();
return S_OK;
}

STDMETHODIMP CCustomArchiveExtractCallback::PrepareOperation(Int32 askExtractMode)
{
return S_OK;
}

STDMETHODIMP CCustomArchiveExtractCallback::SetOperationResult(Int32 operationResult)
{
switch(operationResult)
{
case NArchive::NExtract::NOperationResult::kOK:
break;
default:
{
switch(operationResult)
{
default:
break;
}
}
}

_outFileStream.Release();
delete m_pSequentialOutStream;
m_pSequentialOutStream = NULL;

m_pCallback->SetOperationResult(operationResult);

return S_OK;
}

STDMETHODIMP CCustomArchiveExtractCallback::CryptoGetTextPassword(BSTR *password)
{
fprintf(stderr, "CryptoGetTextPassword called!\n");
fflush(stderr);
return E_NOTIMPL;
}

// ------------------------------

STDMETHODIMP CArchiveExtractCallback::SetTotal(UInt64 /* size */)
{
return S_OK;
Expand All @@ -292,6 +421,9 @@ STDMETHODIMP CArchiveExtractCallback::SetCompleted(const UInt64 * /* completeVal
STDMETHODIMP CArchiveExtractCallback::GetStream(UInt32 index,
ISequentialOutStream **outStream, Int32 askExtractMode)
{
fprintf(stderr, "CArchive::GetStream(%u) - (passed %d, want %d)\n", index, askExtractMode, NArchive::NExtract::NAskMode::kExtract);
fflush(stderr);

if (askExtractMode != NArchive::NExtract::NAskMode::kExtract)
return S_OK;

Expand Down
15 changes: 11 additions & 4 deletions Lib7Zip/lib7zip.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,25 +143,31 @@ class C7ZipSequentialOutStream
{
public:
virtual int Write(const void *data, unsigned int size, unsigned int *processedSize) = 0;
virtual ~C7ZipSequentialOutStream() {
// muffin
}
};

class C7ZipOutStream : public virtual C7ZipSequentialOutStream
{
public:
virtual int Seek(__int64 offset, unsigned int seekOrigin, unsigned __int64 *newPosition) = 0;
virtual int SetSize(unsigned __int64 size) = 0;
virtual ~C7ZipOutStream() {
// muffin
}
};

class C7ZipExtractCallback
{
public:
// IProgress
virtual void SetTotal(unsigned __int64 size);
virtual void SetCompleted(const unsigned __int64 *completeValue);
virtual void SetTotal(unsigned __int64 size) = 0;
virtual void SetCompleted(const unsigned __int64 *completeValue) = 0;

// IArchiveExtractCallback
virtual C7ZipOutStream *GetStream(int index);
virtual void SetOperationResult(int operationResult);
virtual C7ZipSequentialOutStream *GetStream(int index) = 0;
virtual void SetOperationResult(int operationResult) = 0;
};

class C7ZipArchive : public virtual C7ZipObject
Expand All @@ -176,6 +182,7 @@ class C7ZipArchive : public virtual C7ZipObject
virtual bool Extract(unsigned int index, C7ZipSequentialOutStream * pSequentialOutStream) = 0;
virtual bool Extract(unsigned int index, C7ZipSequentialOutStream * pSequentialOutStream, const wstring & pwd) = 0;
virtual bool Extract(const C7ZipArchiveItem * pArchiveItem, C7ZipSequentialOutStream * pSequentialOutStream) = 0;
virtual bool ExtractSeveral(unsigned int *indexList, int numIndices, C7ZipExtractCallback *extractCallback) = 0;
virtual wstring GetArchivePassword() const = 0;
virtual void SetArchivePassword(const wstring & password) = 0;
virtual bool IsPasswordSet() const = 0;
Expand Down
8 changes: 8 additions & 0 deletions Test7Zip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ set(test7ziprar5_SOURCES
${SRC}/Test7ZipRar5.cpp
)

set(test7zipcallback_SOURCES
${SRC}/stdafx.cpp
${SRC}/Test7ZipCallback.cpp
)

add_executable(test7zip ${test7zip_SOURCES})
target_link_libraries(test7zip 7zip)

Expand All @@ -57,6 +62,9 @@ target_link_libraries(test7zipsig 7zip)
add_executable(test7ziprar5 ${test7ziprar5_SOURCES})
target_link_libraries(test7ziprar5 7zip)

add_executable(test7zipcallback ${test7zipcallback_SOURCES})
target_link_libraries(test7zipcallback 7zip)

configure_file(${SRC}/Test7Zip.7z ${CMAKE_CURRENT_BINARY_DIR}/Test7Zip.7z COPYONLY)
configure_file(${SRC}/Test7Zip.zip ${CMAKE_CURRENT_BINARY_DIR}/Test7Zip.zip COPYONLY)
configure_file(${SRC}/Test7ZipCryptFileName.7z ${CMAKE_CURRENT_BINARY_DIR}/Test7ZipCryptFileName.7z COPYONLY)
Expand Down
Loading

0 comments on commit 428e5f4

Please sign in to comment.