So I have a very simple job to do: decompress a zip file. Thought I'd find a simple solution withing 5 seconds online but I'm still struggling.
I've obviously read these posts:
But the answers suggest to make use of zlib
and libzip
or miniz
.
I'm sure these approaches works just fine. However it seems that it is not straightforward trying to apply this approach in existing VS2013 solution.
Then I came across this simple solution, ref1, ref2, that make use of IShellDispatch object
I rushed to implement it:
bool DecompressZIP(_In_ const wpath& pathFile, _In_ const wpath& pathDstDir)
{
BSTR source = _bstr_t(pathFile.string().c_str());
BSTR dest = _bstr_t(pathDstDir.string().c_str());
HRESULT hResult = S_FALSE;
IShellDispatch *pIShellDispatch = NULL;
Folder *pToFolder = NULL;
VARIANT variantDir, variantFile, variantOpt;
CoInitialize(NULL);
hResult = CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER,
IID_IShellDispatch, (void **)&pIShellDispatch);
if (SUCCEEDED(hResult) && NULL != pIShellDispatch)
{
VariantInit(&variantDir);
variantDir.vt = VT_BSTR;
variantDir.bstrVal = dest;
hResult = pIShellDispatch->NameSpace(variantDir, &pToFolder);
if (SUCCEEDED(hResult) && NULL != pToFolder)
{
Folder *pFromFolder = NULL;
VariantInit(&variantFile);
variantFile.vt = VT_BSTR;
variantFile.bstrVal = source;
hResult = pIShellDispatch->NameSpace(variantFile, &pFromFolder);
if (SUCCEEDED(hResult) && NULL != pFromFolder)
{
FolderItems *fi = NULL;
pFromFolder->Items(&fi);
VariantInit(&variantOpt);
variantOpt.vt = VT_I4;
variantOpt.lVal = FOF_NO_UI;
VARIANT newV;
VariantInit(&newV);
newV.vt = VT_DISPATCH;
newV.pdispVal = fi;
hResult = pToFolder->CopyHere(newV, variantOpt);
Sleep(1000);
pFromFolder->Release();
pToFolder->Release();
}
}
pIShellDispatch->Release();
}
CoUninitialize();
return true;
}
BUT IT DOES NOT WORK !
line:
hResult = pIShellDispatch->NameSpace(variantFile, &pFromFolder);
always result in pFromFolder == NULL
hResult
isS_FALSE
SUCCEEDED(hResult)
is trueGetLastError
is 0
Question
what am I doing wrong?