none
Using IFileOperation CopyItems for a destination ZIP file does not work RRS feed

  • Question

  • I am using IFileOperation interface's CopyItems method to copy one more files from source folder to a destination folder. This works without any error as long as the destination folder is a standard file system directory. However, my main motivation of using IFileOperation instead of SHFileOperation() method is to allow copying items from / into non-file-system objects as the documentation says (https://msdn.microsoft.com/en-us/library/windows/desktop/bb775771%28v=vs.85%29.aspx).

    The following code works perfectly for file-system destination e.g. "g:\\my folder" but does not work if the destination is a ZIP file e.g. "g:\\Super Cars.zip". For this quick sample please overlook the resource leaks :).

    Please note the ZIP file is an existing ZIP file creating using standard Windows built-in ZIP capability and it does not contain the file is am trying to copy.

    #include "stdafx.h"
    #include <Windows.h>
    #include <shellapi.h>
    #include <Shlobj.h>
    #include <iostream>
    #include <atlbase.h>

    void CopyFile(LPCTSTR strSource, LPCTSTR strDestination)
    {
        PIDLIST_ABSOLUTE abSourcePidl = nullptr;
        SFGAOF attrs;
        
        HRESULT hr = SHParseDisplayName(strSource, nullptr, &abSourcePidl, 0, &attrs);    
        if (FAILED(hr))
        {
            std::wcout << _T("SHParseDisplayName failed for ") << strSource << std::endl;
            return;
        }
        
        PIDLIST_ABSOLUTE abDestPidl = nullptr;
        
        hr = SHParseDisplayName(strDestination, nullptr, &abDestPidl, 0, &attrs);
        if (FAILED(hr))
        {
            std::wcout << _T("SHParseDisplayName failed for ") << strDestination << std::endl;
            return;
        }

        IShellItem2* pDestShellItem;
        
        hr = SHCreateItemFromIDList(abDestPidl, IID_IShellItem2, reinterpret_cast<LPVOID*>(&pDestShellItem));
        if (FAILED(hr))
        {
            std::wcout << _T("SHCreateItemFromIDList failed") << std::endl;
            return;
        }

        PIDLIST_ABSOLUTE* pPidlRawArray = new PIDLIST_ABSOLUTE[1];
        pPidlRawArray[0] = abSourcePidl;
        
        IShellItemArray* pShellItemArr = nullptr;
        hr = SHCreateShellItemArrayFromIDLists(1, (LPCITEMIDLIST*)pPidlRawArray, &pShellItemArr);
        if (FAILED(hr))
        {
            std::wcout << _T("SHCreateShellItemArrayFromIDLists failed") << std::endl;
            return;
        }
        
        CComPtr<IFileOperation> fileOp;
        hr = fileOp.CoCreateInstance(CLSID_FileOperation);
        if (FAILED(hr))
        {
            std::wcout << _T("Creation of IFileOperation failed") << std::endl;
            return;
        }

        hr = fileOp->CopyItems(pShellItemArr, pDestShellItem);
        if (FAILED(hr))
        {
            std::wcout << _T("CopyItems fall failed") << std::endl;
            return;
        }

        hr = fileOp->PerformOperations();
        if (FAILED(hr))
        {
            std::wcout << _T("PerformOperations call failed") << std::endl;
            return;
        }

        std::wcout << _T("File copied successfully") << std::endl;
    }


    int _tmain(int argc, _TCHAR* argv[])
    {
        CoInitialize(NULL);

        CopyFile(_T("D:\\TEMP\\COMTIME.TXT"), _T("G:\\My Folder")); // WORKS!!
        CopyFile(_T("D:\\TEMP\\COMTIME.TXT"), _T("G:\\Super Cars.zip")); // DOES NOT WORK!!

        CoUninitialize();

        return 0;
    }




    • Edited by Siddharth Barman Sunday, April 26, 2015 6:06 PM Additional information
    Sunday, April 26, 2015 5:55 PM

Answers

All replies