Click here to Skip to main content
Click here to Skip to main content
 

Decompress Zip files with Windows Shell API and C#

By , 3 Oct 2005
 

Sample Image

Introduction

With this code you can use the Windows Shell API in C# to decompress Zip files and do so without having to show the Copy Progress window shown above. Normally when you use the Shell API to decompress a Zip file it will show a Copy Progress window even when you set the options to tell Windows not to show it. To get around this, you move the Shell API code to a separate executable and then launch that executable using the .NET Process class being sure to set the process window style to 'Hidden'.

Background

Ever needed to decompress Zip files and needed a better Zip than what comes with many of the free compression libraries out there? I.e. you needed to compress folders and subfolders as well as files. Windows Zipping can compress more than just individual files. All you need is a way to programmatically get Windows to silently decompress these Zip files. Of course, you could spend $300 on one of the commercial Zip components, but it's hard to beat free if all you need is to decompress folder hierarchies.

Using the code

The following code shows how to use the Windows Shell API to decompress a Zip file. The source folder points to a Zip file. The destination folder points to an output folder. This code as is will decompress the Zip file, however it will also show the Copy Progress window. To make this code work, you will also need to set a reference to a COM library. In the References window, go to the COM tab and select the library labeled 'Microsoft Shell Controls And Automation'.

Shell32.ShellClass sc = new Shell32.ShellClass();
Shell32.Folder SrcFlder = sc.NameSpace(strSrcPath);
Shell32.Folder DestFlder = sc.NameSpace(strDestPath);
Shell32.FolderItems items = SrcFlder.Items();
DestFlder.CopyHere(items, 20);

The sample solution included with this article shows how to put this code into a console application and then launch this console app to decompress the Zip without showing the Copy Progress window.

The code below shows a button click event handler that contains the code used to launch the console application so that there is no UI during the decompress.

private void btnUnzip_Click(object sender, System.EventArgs e)
{
    System.Diagnostics.ProcessStartInfo i = new 
      System.Diagnostics.ProcessStartInfo(
      AppDomain.CurrentDomain.BaseDirectory + "unzip.exe");
    i.CreateNoWindow = true;
    string args = "";

    if(txtSource.Text.IndexOf(" ") != -1)
    {
        //we got a space in the path so wrap it in double qoutes
        args += "\"" + txtSource.Text + "\"";
    }
    else
    {
        args += txtSource.Text;
    }

    if(txtDestination.Text.IndexOf(" ") != -1)
    {
        //we got a space in the path so wrap it in double qoutes
        args += " " + "\"" + txtDestination.Text + "\"";
    }
    else
    {
        args += " " + txtDestination.Text;
    }
    i.Arguments = args;

    //Mark the process window as hidden so 
    //that the progress copy window doesn't show
    i.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    System.Diagnostics.Process p = System.Diagnostics.Process.Start(i);
    p.WaitForExit();
    MessageBox.Show("Complete");
}

Points of Interest

  • It's free!
  • You can use Windows to create the Zip file instead of an expensive Zip library to work with folder hierarchies.
  • Works with or without showing the Copy Progress window.
  • Uses the Windows Shell API which has a lot of of interesting Windows integration possibilities.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Gerald Gibson Jr
Web Developer
United States United States
Member
I have been coding since 4th grade in elementary school back in 1981. I concentrate mainly on Microsoft technologies from the old MSDOS/MSBASIC to coding in VC++ then Visual Basic and now .Net. I try to stay on top of the trends coming from Microsoft such as the Millennium Project, Windows DNA, and my new favorite Windows clustering servers.
 
I have dabbled in making games in my initial VC++ days, but spent most of the past 10 years working on business apps in VB, C++, and the past 7 years in C#.
 
I eventually hope to get my own company supporting me so I can concentrate on my real dream of creating clusters of automated robots for use in various hazardous industries.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionNot unziping filememberM Thirmal Reddy25 May '12 - 3:14 
Hi,
 
i have written windows service this service will Extart ZIP file and place into one folde. i have impletend using below code
 
Shell32.Shell sc = new Shell32.Shell();
 
Shell32.Folder SrcFlder = sc.NameSpace(sourcePath);
 
Shell32.Folder DestFlder = sc.NameSpace(destination);
 
Shell32.FolderItems items = SrcFlder.Items();

DestFlder.CopyHere(items, 20);
 
System.Threading.Thread.Sleep(1000);
 
in my Service but it is not working.
 
If i run this code in Widows application it is working fine.
Please can you help me out in this issue.
AnswerRe: Not unziping filememberGerald Gibson Jr26 May '12 - 13:54 
Shell commands are not normally accessible to Services. The user the service runs under must have all the desktop access that a regular user would (which is why it works for you when you run a desktop app as a normal user). You can try messing around with the user account the service runs under and see if you can get it to work.
GeneralMy vote of 1memberJABch30 Dec '11 - 23:31 
This doesn't work with Net v4.
The demo project crashes.
QuestionInternal ZIP API is not documented because it is copyrightedmemberMember 47756929 Jun '11 - 12:16 
A license is required to use the internal zip API that is included by Microsoft for Zip Folders support. The Inner Media, Inc. owns the copyright and controls the licensing.
General.NET 4 Can't create new Shell32.ShellClass()memberBob Hall14 Jun '11 - 23:40 
For those want to use in .NET4 the interop dll doesn't work generated from win7.
 
Use reflection to create an object and cast to the interface:
 
Type shellType = Type.GetTypeFromProgID("Shell.Application");
object oShell = Activator.CreateInstance(shellType);
Shell32.IShellDispatch5 sc = (Shell32.IShellDispatch5)oShell;
 
//then use sc.... as normal.
GeneralMy vote of 5memberUma BSNL19 Apr '11 - 21:36 
I tried many third party components for unziping. Which has thrown many errors
General64 and 32 bit supportmemberGladson WiIlliam19 Apr '11 - 0:31 
Is Interop.Shell32.dll compatible for 32 and 64 bit machines. If no, where to get Interop.Shell32.dll for 64 bit?
GeneralMy vote of 5membersuman chowdhury19 Jan '11 - 1:31 
very nice explanation and solved my problem
Questioncode not working from windows service on windows server 2k8memberTarunMehta12 Oct '10 - 5:34 
Thanks for your code. It is working on my local machine (XP) but it is not working on windows server 2008. The same Code works fine if i try in a sample application which is not a windows service.
 
Any idea?
AnswerRe: code not working from windows service on windows server 2k8memberMember 794559831 May '11 - 1:48 
Looks like a 32bit/64bit issue, but I am not sure.
I faced the same problem with win7 64-bit...

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 3 Oct 2005
Article Copyright 2005 by Gerald Gibson Jr
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid