Search code examples
c#zipshell32shell32.dll

c# Shell32 zip / unzip code, CopyHere not working


I'm having some issues with the Shell32 CopyHere method.

First of all, I'm working from this page: http://www.codeproject.com/Tips/257193/Easily-zip-unzip-files-using-Windows-Shell32

I was hoping to rewrite something similar in C# just because I hardly ever work in VB.NET.

I'm passing an input directory i that has one text file in it as my test. At the bottom, just so I see that it's in fact grabbing my files, I write the count of input.Items() and I get 1, so I think it's seeing my directory with the one text file in it. However, while my code creates the empty zip file just fine, and at least from that console output appears to be grabbing my files, it doesn't actually copy anything into the zip file. No errors are thrown, it's just as if nothing happens.

static void Zip(string i, string o)
{
        //Turn relative paths into full paths for Shell.NameSpace method.
        string ifp = Path.GetFullPath(i);
        string ofp = Path.GetFullPath(o);

        //Validate parameters.
        if (!(Directory.Exists(ifp)) || (Directory.GetFiles(ifp).Count() <= 0))
            throw new Exception("Input directory " + i + " was invalid.  " + i + " was either empty or doesn't exist.");
        string ext = ofp.Substring(ofp.Length - 4, 4);
        if (ext != ".zip")
            throw new Exception("Output zip directory " + o + " was invalid.  File extension was " + ext + " when it should be .zip.");
        if (File.Exists(ofp))
            throw new Exception("Output zip directory " + o + " was invalid.  Zip file already exists.");

        //The following data represents an empty zip file.
        byte[] startBuffer = { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        //Create empty zip file.
        File.WriteAllBytes(ofp, startBuffer);
        if (!File.Exists(ofp))
            throw new Exception("Output zip directory " + o + " was unable to be created.");

        Shell sc = new Shell();
        //Folder which contains the files you want to zip.
        Folder input = sc.NameSpace(ifp);
        //Empty zip file as folder.
        Folder output = sc.NameSpace(ofp);
        //Copy the files into the empty zip file.
        output.CopyHere(input.Items(), 4);
        Console.WriteLine(input.Items().Count);
}

Is there something wrong with how I'm using the Shell32 methods?

EDIT:

Sorry for the slow response, I'm trying to work with some of the code DJ Kraze put up.

To clarify some things, unfortunately I can't use a third party tool and I'm on .NET 4.0. I'm going to try taking DJ's advice and see if I can get either the VB version working or the C# that he kindly posted.

Thanks for the help everyone.


Solution

  • the code here will be the C# equivalent to the VB.NET code from the codeproject page

    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    public class Form1
    {
    
        public void Zip()
        {
            //1) Lets create an empty Zip File .
            //The following data represents an empty zip file .
    
            byte[] startBuffer = {
                80,
                75,
                5,
                6,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0
            };
            // Data for an empty zip file .
            FileIO.FileSystem.WriteAllBytes("d:\\empty.zip", startBuffer, false);
    
            //We have successfully made the empty zip file .
    
            //2) Use the Shell32 to zip your files .
            // Declare new shell class
            Shell32.Shell sc = new Shell32.Shell();
            //Declare the folder which contains the files you want to zip .
            Shell32.Folder input = sc.NameSpace("D:\\neededFiles");
            //Declare  your created empty zip file as folder  .
            Shell32.Folder output = sc.NameSpace("D:\\empty.zip");
            //Copy the files into the empty zip file using the CopyHere command .
            output.CopyHere(input.Items, 4);
    
        }
    
        public void UnZip()
        {
            Shell32.Shell sc = new Shell32.Shell();
            //'UPDATE !!
            //Create directory in which you will unzip your files .
            IO.Directory.CreateDirectory("D:\\extractedFiles");
            //Declare the folder where the files will be extracted
            Shell32.Folder output = sc.NameSpace("D:\\extractedFiles");
            //Declare your input zip file as folder  .
            Shell32.Folder input = sc.NameSpace("d:\\myzip.zip");
            //Extract the files from the zip file using the CopyHere command .
            output.CopyHere(input.Items, 4);
    
        }
    
    }