Click here to Skip to main content
15,884,836 members
Articles / Programming Languages / C#
Alternative
Article

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

Rate me:
Please Sign up or sign in to vote.
4.88/5 (10 votes)
29 Aug 2013CPOL2 min read 76.2K   6K   26   12
This is an alternative for "Compress Zip files with Windows Shell API and C#"

Introduction

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.

Background

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.

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

C#
string _sourceFile = "Template.zip";
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.

History

  • Original post: 29 August, 2013

License

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


Written By
Software Developer (Senior)
Nepal Nepal
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
srilekhamenon8-Nov-17 1:43
professionalsrilekhamenon8-Nov-17 1:43 
Nice Work
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 patel17-Jul-16 23:07
nitin c patel17-Jul-16 23:07 
GeneralMy vote of 5 Pin
Alexander Tobias Heinrich10-Jul-16 2:53
Alexander Tobias Heinrich10-Jul-16 2:53 
GeneralMy vote of 5 Pin
Debopam Pal29-Nov-13 4:44
professionalDebopam Pal29-Nov-13 4:44 
GeneralMy vote of 5 Pin
xilione12-Sep-13 5:38
xilione12-Sep-13 5:38 
GeneralRe: My vote of 5 Pin
Kumar Kisalaya12-Sep-13 6:10
Kumar Kisalaya12-Sep-13 6:10 
QuestionTo compress several single files stored in an array Pin
Member 102079671-Sep-13 5:40
Member 102079671-Sep-13 5:40 
GeneralMy vote of 5 Pin
Member 1020796731-Aug-13 6:54
Member 1020796731-Aug-13 6:54 
GeneralRe: My vote of 5 Pin
Kumar Kisalaya31-Aug-13 16:37
Kumar Kisalaya31-Aug-13 16:37 
QuestionWhy wouldn't you just use the Compression namespace Pin
Sacha Barber29-Aug-13 23:09
Sacha Barber29-Aug-13 23:09 
AnswerRe: Why wouldn't you just use the Compression namespace Pin
Kumar Kisalaya29-Aug-13 23:51
Kumar Kisalaya29-Aug-13 23:51 
GeneralRe: Why wouldn't you just use the Compression namespace Pin
Sacha Barber1-Sep-13 21:48
Sacha Barber1-Sep-13 21: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.