Search code examples
c#.netpinvoke

How to decompress a single file from an ZipFile using "SHFILEOPSTRUCT"?


i know this question is asked 1000 times but I don't want to use any external tools like (DotNetZip or SharpZipLib).

I have currently this working piece of code:

string _from = Path.Combine(apk, "*.*") + '\0';
string _to = destination + '\0' + Path.Combine(destination, "*.*") + '\0';
NativeMethods.SHFILEOPSTRUCT fileop = new NativeMethods.SHFILEOPSTRUCT();
fileop.pFrom = _from;
fileop.pTo = _to;
fileop.wFunc = NativeMethods.FO_Type.FO_COPY;
fileop.fFlags = NativeMethods.FOF_Flag.FOF_WANTNUKEWARNING;
NativeMethods.SHFileOperation(ref fileop);

My problem is that I can't extract/copy a single file/folder. If i replace the *.* for example with res\\drawable\\icon.png it will do nothing.

Can someone tell me how to extract a single file/folder using the SHFILEOPSTRUCT? Do I miss something?

The code supports:

Framework Version: .Net Client 4

OS: WinXP, 8, 8.1, 10

EDIT (Notice):

When you use this piece of code in an C# thread you'll always get an 'Path length exceeded' error... In this case you need to use an 'Dispatcher'.


Solution

  • SHFileOperation is not capable of extracting individual items from a ZIP file. You options as I see them are:

    1. Include a 3rd party ZIP library.
    2. Switch to .net 4.5 and use the framework supplied ZIP library.
    3. See if ZipPackage from System.IO.Packaging has enough functionality to meet your needs.
    4. Write your own ZIP code.
    5. Extract the entire ZIP file to a temporary directory and then pick out the parts you need.