Menu

Unzip

2024-07-27 - Kompresja
//---------------------------------------------------------------------------
void __fastcall TForm1::UnZipFile(wchar_t *SourceFile, wchar_t *DestDir)
{
  try
  {
    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))
    {
        VariantInit(&variantDir);
        variantDir.vt = VT_BSTR;
        variantDir.bstrVal = DestDir;
        hResult = pIShellDispatch->NameSpace(variantDir, &pToFolder);
 
        if  (SUCCEEDED(hResult))
        {
            Folder *pFromFolder = NULL;
            VariantInit(&variantFile);
            variantFile.vt = VT_BSTR;
            variantFile.bstrVal = SourceFile;
            pIShellDispatch->NameSpace(variantFile, &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();
  }catch(Exception &e){
    Application->MessageBox(e.Message.c_str(), L"Błąd", MB_ICONERROR | MB_OK);
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    UnZipFile(L"C:\\Test\\Plik.zip", L"C:\\Test");
}
//---------------------------------------------------------------------------