242 lines
6.5 KiB
C++
242 lines
6.5 KiB
C++
// Zip.cpp : Implementation of CZip
|
|
|
|
#include "stdafx.h"
|
|
#include "Zip.h"
|
|
|
|
#define DATA_BUFFER_BYTES 4096
|
|
|
|
#define VERYSECRET ("H&4!0jKd")
|
|
|
|
// CZip
|
|
|
|
STDMETHODIMP CZip::InterfaceSupportsErrorInfo(REFIID riid)
|
|
{
|
|
static const IID* arr[] =
|
|
{
|
|
&IID_IZip
|
|
};
|
|
|
|
for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
|
|
{
|
|
if (InlineIsEqualGUID(*arr[i],riid))
|
|
return S_OK;
|
|
}
|
|
return S_FALSE;
|
|
}
|
|
|
|
STDMETHODIMP CZip::Open(BSTR ZipPath)
|
|
{
|
|
if (m_pZipFile)
|
|
return myAtlReportError (GetObjectCLSID(), "\nCZip::Open zipfile is already open.");
|
|
|
|
try
|
|
{
|
|
DWFCore::DWFFile oZipFilename( ZipPath );
|
|
|
|
m_pZipFile = new DWFCore::DWFZipFileDescriptor(oZipFilename, DWFZipFileDescriptor::eUnzip);
|
|
m_pZipFile->open();
|
|
}
|
|
catch (DWFException& ex)
|
|
{
|
|
CString err;
|
|
err.Format("%ls\n%ls\n%s\n%ls(%d)",ex.type(),ex.message(),ex.function(),ex.file(),ex.line());
|
|
return myAtlReportError (GetObjectCLSID(), "\nCZip::Open('%ls')\n%s", ZipPath, err);
|
|
}
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
STDMETHODIMP CZip::New(BSTR ZipPath)
|
|
{
|
|
if (m_pZipFile)
|
|
return myAtlReportError (GetObjectCLSID(), "\nCZip::New zipfile is already open.");
|
|
|
|
try
|
|
{
|
|
DWFCore::DWFFile oZipFilename( ZipPath );
|
|
|
|
m_pZipFile = new DWFCore::DWFZipFileDescriptor(oZipFilename, DWFZipFileDescriptor::eZip);
|
|
m_pZipFile->open();
|
|
|
|
}
|
|
catch (DWFException& ex)
|
|
{
|
|
CString err;
|
|
err.Format("%ls\n%ls\n%s\n%ls(%d)",ex.type(),ex.message(),ex.function(),ex.file(),ex.line());
|
|
return myAtlReportError (GetObjectCLSID(), "\nCZip::New('%ls')\n%s", ZipPath, err);
|
|
}
|
|
return S_OK;
|
|
}
|
|
|
|
STDMETHODIMP CZip::DecryptToStream(VARIANT pStream, BSTR filepath)
|
|
{
|
|
return UnzipToStream(pStream, filepath, CComBSTR ( VERYSECRET ));
|
|
}
|
|
|
|
// Gebruik vanuit ASP: oZIP.UnzipToStream(Response, "Geheim");
|
|
// want het Response Object implementeerd mooi de IStream interface
|
|
STDMETHODIMP CZip::UnzipToStream(VARIANT pStream, BSTR filepath, BSTR Password/*=L""*/)
|
|
{
|
|
if (!m_pZipFile)
|
|
return myAtlReportError (GetObjectCLSID(), "\nCZip::UnzipToStream zipfile not open.");
|
|
|
|
myTRACE("\nEntering CZip::UnzipToStream");
|
|
|
|
CComQIPtr<IStream> pIStream;
|
|
|
|
if (pStream.vt!=VT_ERROR)
|
|
{
|
|
VARIANT *var2 = &pStream;
|
|
if (var2->vt==(VT_VARIANT|VT_BYREF)) // ByRef vanuit VBScript
|
|
var2 = (VARIANT *)var2->pvarVal;
|
|
|
|
if (var2->vt==VT_DISPATCH)
|
|
pIStream = var2->pdispVal;
|
|
else if (var2->vt==(VT_DISPATCH|VT_BYREF)) // ByRef vanuit VB
|
|
pIStream = *(var2->ppdispVal);
|
|
|
|
if (!pIStream)
|
|
return E_INVALIDARG;
|
|
|
|
try
|
|
{
|
|
unsigned char* pDataBuffer = DWFCORE_ALLOC_MEMORY( unsigned char, DATA_BUFFER_BYTES );
|
|
if (pDataBuffer == NULL)
|
|
{
|
|
_DWFCORE_THROW( DWFMemoryException, L"No memory for data buffer" );
|
|
}
|
|
|
|
DWFCore::DWFInputStream* pUnzipStream =
|
|
m_pZipFile->unzip((LPCSTR)CString(filepath),(LPCSTR)CString(Password));
|
|
|
|
while (pUnzipStream->available() > 0)
|
|
{
|
|
size_t nBytes;
|
|
nBytes = pUnzipStream->read( pDataBuffer, DATA_BUFFER_BYTES );
|
|
ULONG cb;
|
|
pIStream->Write((void*)pDataBuffer, (ULONG)nBytes, &cb);
|
|
}
|
|
|
|
DWFCORE_FREE_OBJECT (pUnzipStream);
|
|
DWFCORE_FREE_MEMORY( pDataBuffer )
|
|
}
|
|
catch (DWFException& ex)
|
|
{
|
|
CString err;
|
|
err.Format("%ls\n%ls\n%s\n%ls(%d)",ex.type(),ex.message(),ex.function(),ex.file(),ex.line());
|
|
return myAtlReportError (GetObjectCLSID(), "\nCZip::UnzipToStream()\n%s", err);
|
|
}
|
|
}
|
|
|
|
return S_OK;
|
|
}
|
|
|
|
STDMETHODIMP CZip::EncryptFromString(BSTR filepath, BSTR data)
|
|
{
|
|
return ZipFromString( filepath, data, CComBSTR ( VERYSECRET ));
|
|
}
|
|
|
|
// filepath naam waaronder opgeslagen binnen de ZIP
|
|
// data Visual Basic string
|
|
// Password Optioneel
|
|
STDMETHODIMP CZip::ZipFromStream(BSTR filepath, VARIANT pStream, BSTR Password)
|
|
{
|
|
if (!m_pZipFile)
|
|
return myAtlReportError (GetObjectCLSID(), "\nCZip::ZipFromStream zipfile not open.");
|
|
|
|
myTRACE("\nEntering CZip::ZipFromStream");
|
|
|
|
CComQIPtr<IStream> pIStream;
|
|
|
|
if (pStream.vt!=VT_ERROR)
|
|
{
|
|
VARIANT *var2 = &pStream;
|
|
if (var2->vt==(VT_VARIANT|VT_BYREF)) // ByRef vanuit VBScript
|
|
var2 = (VARIANT *)var2->pvarVal;
|
|
|
|
if (var2->vt==VT_DISPATCH)
|
|
pIStream = var2->pdispVal;
|
|
else if (var2->vt==(VT_DISPATCH|VT_BYREF)) // ByRef vanuit VB
|
|
pIStream = *(var2->ppdispVal);
|
|
|
|
if (!pIStream)
|
|
return E_INVALIDARG;
|
|
|
|
try
|
|
{
|
|
// Naam waaronder binnen de zip is opgeslagen
|
|
DWFCore::DWFString file((LPCSTR)CString(filepath));
|
|
DWFCore::DWFOutputStream* pZipStream;
|
|
|
|
pZipStream = m_pZipFile->zip(file,(LPCSTR)CString(Password));
|
|
|
|
unsigned char* pDataBuffer = DWFCORE_ALLOC_MEMORY( unsigned char, DATA_BUFFER_BYTES );
|
|
if (pDataBuffer == NULL)
|
|
{
|
|
_DWFCORE_THROW( DWFMemoryException, L"No memory for data buffer" );
|
|
}
|
|
|
|
ULONG cbRead;
|
|
do
|
|
{
|
|
pIStream->Read((void*)pDataBuffer, DATA_BUFFER_BYTES, &cbRead);
|
|
if (cbRead>0)
|
|
pZipStream->write( pDataBuffer, cbRead);
|
|
}
|
|
while (cbRead == DATA_BUFFER_BYTES);
|
|
|
|
pZipStream->flush();
|
|
|
|
DWFCORE_FREE_MEMORY( pDataBuffer )
|
|
DWFCORE_FREE_OBJECT (pZipStream);
|
|
}
|
|
catch (DWFException& ex)
|
|
{
|
|
CString err;
|
|
err.Format("%ls\n%ls\n%s\n%ls(%d)",ex.type(),ex.message(),ex.function(),ex.file(),ex.line());
|
|
return myAtlReportError (GetObjectCLSID(), "\nCZip::ZipFromString('%ls')\n%s", filepath, err);
|
|
}
|
|
}
|
|
return S_OK;
|
|
}
|
|
|
|
// filepath naam waaronder opgeslagen binnen de ZIP
|
|
// data Visual Basic string
|
|
// Password Optioneel
|
|
STDMETHODIMP CZip::ZipFromString(BSTR filepath, BSTR data, BSTR Password)
|
|
{
|
|
if (!m_pZipFile)
|
|
return myAtlReportError (GetObjectCLSID(), "\nCZip::ZipFromString zipfile not open.");
|
|
|
|
myTRACE("\nEntering CZip::ZipFromString met data lengte: %d\n", SysStringByteLen(data));
|
|
try
|
|
{
|
|
// Naam waaronder binnen de zip is opgeslagen
|
|
DWFCore::DWFString file((LPCSTR)CString(filepath));
|
|
DWFCore::DWFOutputStream* pZipStream;
|
|
|
|
pZipStream = m_pZipFile->zip(file,(LPCSTR)CString(Password));
|
|
size_t nBytes=pZipStream->write( data, SysStringByteLen(data));
|
|
pZipStream->flush();
|
|
DWFCORE_FREE_OBJECT (pZipStream);
|
|
}
|
|
catch (DWFException& ex)
|
|
{
|
|
CString err;
|
|
err.Format("%ls\n%ls\n%s\n%ls(%d)",ex.type(),ex.message(),ex.function(),ex.file(),ex.line());
|
|
return myAtlReportError (GetObjectCLSID(), "\nCZip::ZipFromString('%ls')\n%s", filepath, err);
|
|
}
|
|
return S_OK;
|
|
}
|
|
|
|
STDMETHODIMP CZip::Close(void)
|
|
{
|
|
if (!m_pZipFile)
|
|
return myAtlReportError (GetObjectCLSID(), "\nCZip::Close zipfile not open.");
|
|
|
|
m_pZipFile->close();
|
|
delete m_pZipFile;
|
|
m_pZipFile = NULL;
|
|
return S_OK;
|
|
}
|