Click here to Skip to main content
11,500,844 members (81,890 online)
Click here to Skip to main content

Decompress Zip files with Windows Shell API and C#

, 3 Oct 2005 223.1K 2.9K 46
Rate this:
Please Sign up or sign in to vote.
Use Windows shell API in C# to decompress Zip files, without showing the Copy Progress window.

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

Share

About the Author

Gerald Gibson Jr
Web Developer
United States United States
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.

Comments and Discussions

 
QuestionNot unziping file Pin
M Thirmal Reddy25-May-12 3:14
memberM Thirmal Reddy25-May-12 3:14 
AnswerRe: Not unziping file Pin
Gerald Gibson Jr26-May-12 13:54
memberGerald Gibson Jr26-May-12 13:54 
GeneralMy vote of 1 Pin
JABch30-Dec-11 23:31
memberJABch30-Dec-11 23:31 
QuestionInternal ZIP API is not documented because it is copyrighted Pin
Member 47756929-Jun-11 12:16
memberMember 47756929-Jun-11 12:16 
General.NET 4 Can't create new Shell32.ShellClass() Pin
Bob Hall14-Jun-11 23:40
memberBob Hall14-Jun-11 23:40 
GeneralMy vote of 5 Pin
Uma BSNL19-Apr-11 21:36
memberUma BSNL19-Apr-11 21:36 
General64 and 32 bit support Pin
Gladson WiIlliam19-Apr-11 0:31
memberGladson WiIlliam19-Apr-11 0:31 
GeneralMy vote of 5 Pin
suman chowdhury19-Jan-11 1:31
membersuman chowdhury19-Jan-11 1:31 
Questioncode not working from windows service on windows server 2k8 Pin
TarunMehta12-Oct-10 5:34
memberTarunMehta12-Oct-10 5:34 
AnswerRe: code not working from windows service on windows server 2k8 Pin
Member 794559831-May-11 1:48
memberMember 794559831-May-11 1:48 
GeneralI cannot do it Pin
zhtway15-Jan-10 8:14
memberzhtway15-Jan-10 8:14 
GeneralUnzip using Shell with C# Pin
Ricky Murphy15-Sep-09 15:50
memberRicky Murphy15-Sep-09 15:50 
GeneralRe: Unzip using Shell with C# Pin
Gerald Gibson Jr15-Sep-09 19:13
memberGerald Gibson Jr15-Sep-09 19:13 
Questionhello Pin
secretyavuz14-Jul-09 10:40
membersecretyavuz14-Jul-09 10:40 
AnswerRe: hello Pin
Gerald Gibson Jr14-Jul-09 10:42
memberGerald Gibson Jr14-Jul-09 10:42 
GeneralRe: hello Pin
secretyavuz14-Jul-09 11:10
membersecretyavuz14-Jul-09 11:10 
GeneralRe: hello Pin
Gerald Gibson Jr14-Jul-09 11:15
memberGerald Gibson Jr14-Jul-09 11:15 
GeneralRe: hello Pin
secretyavuz14-Jul-09 11:31
membersecretyavuz14-Jul-09 11:31 
GeneralRe: hello Pin
Gerald Gibson Jr14-Jul-09 11:35
memberGerald Gibson Jr14-Jul-09 11:35 
GeneralDecompressing zip file with other extension than .zip Pin
Member 304827824-Feb-09 4:57
memberMember 304827824-Feb-09 4:57 
GeneralRe: Decompressing zip file with other extension than .zip Pin
Gerald Gibson Jr24-Feb-09 4:59
memberGerald Gibson Jr24-Feb-09 4:59 
GeneralRe: Decompressing zip file with other extension than .zip Pin
Member 304827824-Feb-09 5:47
memberMember 304827824-Feb-09 5:47 
GeneralNameSpace call results in file not found error Pin
M. Washington19-Aug-08 13:53
memberM. Washington19-Aug-08 13:53 
GeneralRe: NameSpace call results in file not found error Pin
Gerald Gibson Jr19-Aug-08 18:26
memberGerald Gibson Jr19-Aug-08 18:26 
GeneralTips on debugging Pin
pw19725-Feb-08 9:25
memberpw19725-Feb-08 9:25 
GeneralRe: Tips on debugging Pin
Gerald Gibson Jr5-Feb-08 9:57
memberGerald Gibson Jr5-Feb-08 9:57 
QuestionThe sample code is giving error Pin
Amit Sec12-Dec-07 21:08
memberAmit Sec12-Dec-07 21:08 
GeneralIgnoring Subfolders & Tar Files Pin
Fresh Mexican Food Fan31-May-07 9:06
memberFresh Mexican Food Fan31-May-07 9:06 
GeneralRe: Ignoring Subfolders & Tar Files Pin
Gerald Gibson Jr31-May-07 9:25
memberGerald Gibson Jr31-May-07 9:25 
GeneralRe: Ignoring Subfolders & Tar Files Pin
Fresh Mexican Food Fan31-May-07 11:40
memberFresh Mexican Food Fan31-May-07 11:40 
GeneralDecompressing password protected zip file Pin
sakthivenkatesh22-Feb-07 12:38
membersakthivenkatesh22-Feb-07 12:38 
GeneralRe: Decompressing password protected zip file [modified] Pin
Gerald Gibson Jr22-Feb-07 13:26
memberGerald Gibson Jr22-Feb-07 13:26 
GeneralRe: Decompressing password protected zip file Pin
sakthivenkatesh22-Feb-07 13:32
membersakthivenkatesh22-Feb-07 13:32 
Generallazy morning... Pin
kodegar15-Jan-07 20:16
memberkodegar15-Jan-07 20:16 
GeneralCrash on Win 2K Advanced Server Pin
girilv21-Sep-06 10:04
membergirilv21-Sep-06 10:04 
AnswerRe: Crash on Win 2K Advanced Server Pin
Gerald Gibson Jr21-Sep-06 10:37
memberGerald Gibson Jr21-Sep-06 10:37 
Question.CopyHere(items, 20) Options Pin
e-dub13-Mar-06 6:10
membere-dub13-Mar-06 6:10 
AnswerRe: .CopyHere(items, 20) Options Pin
Gerald Gibson Jr13-Mar-06 7:17
memberGerald Gibson Jr13-Mar-06 7:17 
GeneralRe: .CopyHere(items, 20) Options Pin
SiliconShadow18-Jul-06 7:04
memberSiliconShadow18-Jul-06 7:04 
GeneralRe: .CopyHere(items, 20) Options [modified] Pin
Gerald Gibson Jr18-Jul-06 7:21
memberGerald Gibson Jr18-Jul-06 7:21 
GeneralRe: .CopyHere(items, 20) Options Pin
SiliconShadow18-Jul-06 7:30
memberSiliconShadow18-Jul-06 7:30 
GeneralRe: .CopyHere(items, 20) Options Pin
FartKnocker9-Jul-07 16:28
memberFartKnocker9-Jul-07 16:28 
GeneralProblems zipping large files Pin
rvxnet15-Feb-06 1:22
memberrvxnet15-Feb-06 1:22 
GeneralRe: Problems zipping large files Pin
Gerald Gibson Jr2-Mar-06 8:27
memberGerald Gibson Jr2-Mar-06 8:27 
GeneralRe: Problems zipping large files Pin
Gerald Gibson Jr13-Mar-06 7:20
memberGerald Gibson Jr13-Mar-06 7:20 
GeneralHaving a problem... Pin
yertle13-Feb-06 8:40
memberyertle13-Feb-06 8:40 
GeneralRe: Having a problem... Pin
Gerald Gibson Jr13-Feb-06 8:49
memberGerald Gibson Jr13-Feb-06 8:49 
GeneralRe: Having a problem... Pin
rvxnet15-Feb-06 1:17
memberrvxnet15-Feb-06 1:17 
GeneralRe: Having a problem... Pin
yertle15-Feb-06 4:09
memberyertle15-Feb-06 4:09 
QuestionRemaining temporary folder Pin
pgrange29-Nov-05 4:19
memberpgrange29-Nov-05 4:19 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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