Click here to Skip to main content
12,696,179 members (29,864 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked

Compress/Decompress Zip Files without Third-party Libraries in C#

, 29 Aug 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "Compress Zip files with Windows Shell API and C#"


This is a follow-up to the article "Compress Zip files with Windows Shell API and C#" by "Gerald Gibson Jr".

I needed a similar functionality for a very basic compression/decompression and didn't want to use any third party libraries. Out of the many options that I came up with, I thought the Windows Shell API served best for my purpose.

Granted, that this is not the best way to archive/un-archive files, but I found this to be one of the simplest ways to achieve what I needed.


What I'm presenting here is a simple wrapper class for archiving/un-archiving using the Windows Shell API with (very) minimal error handling. Additionally, I've also included a function to copy the file permissions from one file to another. This might be useful in some cases.

Using the Code

As the class uses the Shell32 namespace, a reference to Microsoft Shell Controls and Automation should be added to the project. This would come under the COM references section. The wrapper class is named ArchiveManager and has a few public static methods that expose the functionalities.

public static bool Archive(string archiveFile, string unArchiveFolder);
public static bool UnArchive(string archiveFile);
public static bool UnArchive(string archiveFile, string unArchiveFolder);
public static bool CopyPermissions(string sourceFile, string destFile) ;

The method names are pretty self explanatory, however a sample console application has been included in the accompanying code which shows sample usage of the wrapper class.

string _sourceFile = "";
if (!ArchiveManager.UnArchive(_sourceFile))
   Console.WriteLine("ERROR: " + ArchiveManager.LastError);

Other methods can be used in a similar way.

Points of Interest

One of the issues with this approach is that the Shell32 APIs used to create/extract an archive are asynchronous. Meaning, our main thread cannot determine when the archiving/extracting methods are complete.

A rather crude approach has been used to get around this problem. I've used a wait loop that keeps sleeping till the number of items in the source folder (or zip file) and destination folder (or zip file) are equal, or, a timeout (configurable) occurs.


  • Original post: 29 August, 2013


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kumar Kisalaya
Software Developer (Senior)
Nepal Nepal
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionThrowing an Error "Unable to cast COM object of type 'Shell32.ShellClass' to interface type 'Shell32.IShellDispatch6'. This operation failed because the QueryInterface call on the COM component for the interface" Pin
nitin c patel18-Jul-16 0:07
membernitin c patel18-Jul-16 0:07 
GeneralMy vote of 5 Pin
Alexander Tobias Heinrich10-Jul-16 3:53
memberAlexander Tobias Heinrich10-Jul-16 3:53 
GeneralMy vote of 5 Pin
Debopam Pal29-Nov-13 5:44
memberDebopam Pal29-Nov-13 5:44 
GeneralMy vote of 5 Pin
xilione12-Sep-13 6:38
memberxilione12-Sep-13 6:38 
GeneralRe: My vote of 5 Pin
Kumar Kisalaya12-Sep-13 7:10
memberKumar Kisalaya12-Sep-13 7:10 
QuestionTo compress several single files stored in an array Pin
Member 102079671-Sep-13 6:40
memberMember 102079671-Sep-13 6:40 
GeneralMy vote of 5 Pin
Member 1020796731-Aug-13 7:54
memberMember 1020796731-Aug-13 7:54 
GeneralRe: My vote of 5 Pin
Kumar Kisalaya31-Aug-13 17:37
memberKumar Kisalaya31-Aug-13 17:37 
QuestionWhy wouldn't you just use the Compression namespace Pin
Sacha Barber30-Aug-13 0:09
mvpSacha Barber30-Aug-13 0:09 
AnswerRe: Why wouldn't you just use the Compression namespace Pin
Kumar Kisalaya30-Aug-13 0:51
memberKumar Kisalaya30-Aug-13 0:51 
GeneralRe: Why wouldn't you just use the Compression namespace Pin
Sacha Barber1-Sep-13 22:48
mvpSacha Barber1-Sep-13 22:48 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 29 Aug 2013
Article Copyright 2013 by Kumar Kisalaya
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid