Click here to Skip to main content
15,903,201 members
Articles / Programming Languages / C#
Article

Decompress Zip files with Windows Shell API and C#

Rate me:
Please Sign up or sign in to vote.
4.53/5 (22 votes)
3 Oct 20052 min read 304.9K   3.3K   48   61
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'.

C#
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.

C#
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


Written By
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

 
GeneralRe: Decompressing password protected zip file Pin
sakthivenkatesh22-Feb-07 13:32
sakthivenkatesh22-Feb-07 13:32 
Generallazy morning... Pin
kodegar15-Jan-07 20:16
kodegar15-Jan-07 20:16 
GeneralCrash on Win 2K Advanced Server Pin
girilv21-Sep-06 10:04
girilv21-Sep-06 10:04 
AnswerRe: Crash on Win 2K Advanced Server Pin
Gerald Gibson Jr21-Sep-06 10:37
Gerald Gibson Jr21-Sep-06 10:37 
Question.CopyHere(items, 20) Options Pin
e-dub13-Mar-06 6:10
e-dub13-Mar-06 6:10 
AnswerRe: .CopyHere(items, 20) Options Pin
Gerald Gibson Jr13-Mar-06 7:17
Gerald Gibson Jr13-Mar-06 7:17 
GeneralRe: .CopyHere(items, 20) Options Pin
SiliconShadow18-Jul-06 7:04
SiliconShadow18-Jul-06 7:04 
GeneralRe: .CopyHere(items, 20) Options [modified] Pin
Gerald Gibson Jr18-Jul-06 7:21
Gerald Gibson Jr18-Jul-06 7:21 
What I have found is the vOptions parameter does not work. This is what led me to making this article in the first place. I wanted to use the vOptions paramter '4' to tell it to NOT display a dialog box at all... but it simply does not work. To get around this I used the console app approach where I can tell it to not show any output.

You probably have already found this page, but just in case ... Microsoft's reference says...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/objects/folder/copyhere.asp[^]

vOptions Optional. Specifies options for the copy operation. This value can be zero or a combination of the following values. These values are based upon flags defined for use with the fFlags member of the C++ SHFILEOPSTRUCT structure. These flags are not defined as such for Microsoft Visual Basic, Visual Basic Scripting Edition (VBScript), or Microsoft JScript, so you must define them yourself or use their numeric equivalents.

4 Do not display a progress dialog box.
8 Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.
16 Respond with "Yes to All" for any dialog box that is displayed.
64 Preserve undo information, if possible.
128 Perform the operation on files only if a wildcard file name (*.*) is specified.
256 Display a progress dialog box but do not show the file names.
512 Do not confirm the creation of a new directory if the operation requires one to be created.
1024 Do not display a user interface if an error occurs.
2048 Version 4.71. Do not copy the security attributes of the file.
4096 Only operate in the local directory. Don't operate recursively into subdirectories.
9182 Version 5.0. Do not copy connected files as a group. Only copy the specified files.




-- modified at 13:22 Tuesday 18th July, 2006
GeneralRe: .CopyHere(items, 20) Options Pin
SiliconShadow18-Jul-06 7:30
SiliconShadow18-Jul-06 7:30 
GeneralRe: .CopyHere(items, 20) Options Pin
FartKnocker9-Jul-07 16:28
FartKnocker9-Jul-07 16:28 
GeneralProblems zipping large files Pin
rvxnet15-Feb-06 1:22
rvxnet15-Feb-06 1:22 
GeneralRe: Problems zipping large files Pin
Gerald Gibson Jr2-Mar-06 8:27
Gerald Gibson Jr2-Mar-06 8:27 
GeneralRe: Problems zipping large files Pin
Gerald Gibson Jr13-Mar-06 7:20
Gerald Gibson Jr13-Mar-06 7:20 
GeneralHaving a problem... Pin
yertle13-Feb-06 8:40
yertle13-Feb-06 8:40 
GeneralRe: Having a problem... Pin
Gerald Gibson Jr13-Feb-06 8:49
Gerald Gibson Jr13-Feb-06 8:49 
GeneralRe: Having a problem... Pin
rvxnet15-Feb-06 1:17
rvxnet15-Feb-06 1:17 
GeneralRe: Having a problem... Pin
yertle15-Feb-06 4:09
yertle15-Feb-06 4:09 
QuestionRemaining temporary folder Pin
pgrange29-Nov-05 4:19
pgrange29-Nov-05 4:19 
AnswerRe: Remaining temporary folder Pin
Gerald Gibson Jr29-Nov-05 7:20
Gerald Gibson Jr29-Nov-05 7:20 
GeneralI've found good zip utils fo C# in java.util.zip Pin
Krzysztof Marzencki11-Oct-05 19:53
Krzysztof Marzencki11-Oct-05 19:53 
GeneralRe: I've found good zip utils fo C# in java.util.zip Pin
Gerald Gibson Jr13-Oct-05 7:19
Gerald Gibson Jr13-Oct-05 7:19 
GeneralRe: I've found good zip utils fo C# in java.util.zip Pin
emilioarp3-Nov-05 5:17
emilioarp3-Nov-05 5:17 
GeneralCompress files with Windows Pin
anthonya10-Oct-05 20:51
anthonya10-Oct-05 20:51 
AnswerRe: Compress files with Windows Pin
Gerald Gibson Jr13-Oct-05 8:00
Gerald Gibson Jr13-Oct-05 8:00 
GeneralRe: Compress files with Windows Pin
anthonya13-Oct-05 20:40
anthonya13-Oct-05 20:40 

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

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