Click here to Skip to main content
11,715,022 members (69,374 online)
Click here to Skip to main content

Easily Zip / Unzip Files using Windows Shell32

, 22 Oct 2013 CPOL 154.9K 63
Rate this:
Please Sign up or sign in to vote.
Easily zip / unzip files using Windows Shell32

(NOTE: The code has been updated for better compatibility on Windows Vista and 8.)

Introduction

If you want to zip / unzip files and folders without using third-party libraries, and you need a simple way to do it, then the Windows Shell32 is your choice.

The code is written with Visual Basic 2010 using .NET Framework 2.0 (running on Windows 7 x64), but it's simple and can be easily converted to any other language (like C#).

The Code

The first step is to create an empty ZIP file. Then, by using the CopyHere method, the files can be copied (or compressed) into the ZIP file. Also, using the same method, we can copy (or extract) the compressed files.

Public Class Form1
 
    Dim outputZip As String = "output zip file path"
    Dim inputZip As String = "input zip file path"
    Dim inputFolder As String = "input folder path"
    Dim outputFolder As String = "output folder path"
    
    'Declare the shell object
    Dim shObj As Object = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"))

    Sub Zip()

        'Lets create an empty Zip File .
        'The following data represents an empty zip file.
        Dim startBytes() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, _
                                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        'Data for an empty zip file.

        FileIO.FileSystem.WriteAllBytes(outputZip, startBytes, False)
        'We have successfully created the empty zip file.

        'Declare the folder which contains the items (files/folders) that you want to zip.
        Dim input As Object = shObj.NameSpace((inputFolder))

        'Declare the created empty zip file.
        Dim output As Object = shObj.NameSpace((outputZip))

        'Compress the items into the zip file.
        output.CopyHere((input.Items), 4)

    End Sub

    Sub UnZip()

        'Create directory in which you will unzip your items.
        IO.Directory.CreateDirectory(outputFolder)

        'Declare the folder where the items will be extracted.
        Dim output As Object = shObj.NameSpace((outputFolder))

        'Declare the input zip file.
        Dim input As Object = shObj.NameSpace((inputZip))

        'Extract the items from the zip file.
        output.CopyHere((input.Items), 4)

    End Sub
 
End Class

In the CopyHere method, option 4 was used, but it seems that it's actually ignored. Please refer to this page for more details: http://msdn.microsoft.com/en-us/library/windows/desktop/bb787866(v=vs.85).aspx.

Note that when using the methods NameSpace and CopyHere, the parameters were passed through additional parentheses. For some reason, it seems that there's a problem passing the variables themselves, therefore, the parentheses were used to pass "New" parameters which are created from and equivalent to the original ones. Otherwise, you would receive errors.

(Thanks to Thomas Tsigkonis for reporting the issues he faced on Windows 8.)

For those wondering if this method also compresses files (reduces their size): Yes! It does.

Also, you need to know that since the zipping/unzipping operations are carried-out by the Shell32 itself, you do not have a direct control on them. But, there are couple of ways to monitor the process, some of them are mentioned in the comments below.

At the end, this is an easy and fast-to-implement method for adding zipping / unzipping capabilities to your Windows application.

Best regards!

License

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

Share

About the Author

Bashar Tahir
Student
Iraq Iraq
Hello Everyone,

I didn't study programming at academic levels, but rather learned it by myself at home.
From time to time i will try to post some code that's rarely being used and it does prove useful.

Please anytime if you need help, don't hesitate to contact me. I'll be glad to help you out!

Best Regards

You may also be interested in...

Comments and Discussions

 
QuestionERROR!!! Pin
DNA18013-Jun-15 1:22
memberDNA18013-Jun-15 1:22 
AnswerRe: ERROR!!! Pin
Bashar Tahir15-Jun-15 23:26
memberBashar Tahir15-Jun-15 23:26 
GeneralRe: ERROR!!! Pin
DNA18016-Jun-15 0:43
memberDNA18016-Jun-15 0:43 
QuestionA track of progress Pin
Rockford Sol2-May-15 9:57
memberRockford Sol2-May-15 9:57 
AnswerRe: A track of progress Pin
Bashar Tahir5-May-15 7:59
memberBashar Tahir5-May-15 7:59 
QuestionSet Password? Pin
Boy Balantoy21-Dec-14 6:39
memberBoy Balantoy21-Dec-14 6:39 
AnswerRe: Set Password? Pin
Bashar Tahir21-Dec-14 8:41
memberBashar Tahir21-Dec-14 8:41 
AnswerRe: Set Password? Pin
Bashar Tahir21-Dec-14 8:44
memberBashar Tahir21-Dec-14 8:44 
QuestionIs it possible to create the zip for files using wildcards Pin
PuWii9-Nov-14 7:06
groupPuWii9-Nov-14 7:06 
AnswerRe: Is it possible to create the zip for files using wildcards Pin
Bashar Tahir10-Nov-14 2:18
memberBashar Tahir10-Nov-14 2:18 
GeneralRe: Is it possible to create the zip for files using wildcards Pin
PuWii10-Nov-14 3:56
groupPuWii10-Nov-14 3:56 
GeneralRe: Is it possible to create the zip for files using wildcards Pin
PuWii10-Nov-14 18:36
groupPuWii10-Nov-14 18:36 
GeneralRe: Is it possible to create the zip for files using wildcards Pin
Bashar Tahir10-Nov-14 20:41
memberBashar Tahir10-Nov-14 20:41 
QuestionExcellent! Pin
Gray-Notes Software23-Sep-14 20:25
memberGray-Notes Software23-Sep-14 20:25 
AnswerRe: Excellent! Pin
Bashar Tahir23-Sep-14 22:09
memberBashar Tahir23-Sep-14 22:09 
AnswerRe: Excellent! Pin
Bashar Tahir25-Sep-14 2:41
memberBashar Tahir25-Sep-14 2:41 
AnswerRe: Excellent! Pin
Bashar Tahir1-Oct-14 20:09
memberBashar Tahir1-Oct-14 20:09 
QuestionError Code in Shell32 Pin
Thomas Tsigkonis13-Sep-14 9:56
memberThomas Tsigkonis13-Sep-14 9:56 
AnswerRe: Error Code in Shell32 Pin
Bashar Tahir13-Sep-14 11:48
memberBashar Tahir13-Sep-14 11:48 
AnswerRe: Error Code in Shell32 Pin
Thomas Tsigkonis13-Sep-14 12:12
memberThomas Tsigkonis13-Sep-14 12:12 
GeneralRe: Error Code in Shell32 Pin
Bashar Tahir13-Sep-14 20:06
memberBashar Tahir13-Sep-14 20:06 
GeneralRe: Error Code in Shell32 Pin
Thomas Tsigkonis14-Sep-14 5:42
memberThomas Tsigkonis14-Sep-14 5:42 
GeneralRe: Error Code in Shell32 Pin
Bashar Tahir14-Sep-14 6:13
memberBashar Tahir14-Sep-14 6:13 
GeneralRe: Error Code in Shell32 Pin
Thomas Tsigkonis14-Sep-14 14:33
memberThomas Tsigkonis14-Sep-14 14:33 
GeneralRe: Error Code in Shell32 Pin
Bashar Tahir14-Sep-14 19:53
memberBashar Tahir14-Sep-14 19:53 
GeneralRe: Error Code in Shell32 Pin
Thomas Tsigkonis15-Sep-14 6:49
memberThomas Tsigkonis15-Sep-14 6:49 
GeneralRe: Error Code in Shell32 Pin
Bashar Tahir15-Sep-14 10:18
memberBashar Tahir15-Sep-14 10:18 
GeneralRe: Error Code in Shell32 Pin
Thomas Tsigkonis15-Sep-14 12:35
memberThomas Tsigkonis15-Sep-14 12:35 
GeneralRe: Error Code in Shell32 Pin
Bashar Tahir15-Sep-14 20:57
memberBashar Tahir15-Sep-14 20:57 
GeneralRe: Error Code in Shell32 Pin
Thomas Tsigkonis16-Sep-14 10:10
memberThomas Tsigkonis16-Sep-14 10:10 
QuestionC# code to unzip !! Pin
oruganti125-Feb-14 9:43
memberoruganti125-Feb-14 9:43 
AnswerRe: C# code to unzip !! Pin
Bashar Tahir5-Feb-14 18:00
memberBashar Tahir5-Feb-14 18:00 
QuestionSmart and brilliant! Pin
Gun Gun Febrianza16-Nov-13 7:10
member Gun Gun Febrianza16-Nov-13 7:10 
AnswerRe: Smart and brilliant! Pin
Bashar Yassin Tahir17-Nov-13 1:34
memberBashar Yassin Tahir17-Nov-13 1:34 
GeneralMy vote of 5 Pin
Alan Tuscano16-Jul-13 21:15
memberAlan Tuscano16-Jul-13 21:15 
GeneralRe: My vote of 5 Pin
Bashar Yassin Tahir17-Jul-13 4:28
memberBashar Yassin Tahir17-Jul-13 4:28 
QuestionEasily zip / unzip files using Windows Shell32 Pin
Alan Tuscano15-Jul-13 23:46
memberAlan Tuscano15-Jul-13 23:46 
AnswerRe: Easily zip / unzip files using Windows Shell32 Pin
Bashar Yassin Tahir16-Jul-13 4:32
memberBashar Yassin Tahir16-Jul-13 4:32 
GeneralRe: Easily zip / unzip files using Windows Shell32 Pin
Alan Tuscano16-Jul-13 21:14
memberAlan Tuscano16-Jul-13 21:14 
GeneralRe: Easily zip / unzip files using Windows Shell32 Pin
Bashar Yassin Tahir17-Jul-13 4:23
memberBashar Yassin Tahir17-Jul-13 4:23 
QuestionEmpty Output Pin
Member 948812611-Jun-13 0:19
memberMember 948812611-Jun-13 0:19 
AnswerRe: Empty Output Pin
Bashar Yassin Tahir11-Jun-13 2:24
memberBashar Yassin Tahir11-Jun-13 2:24 
AnswerRe: Empty Output Pin
Bashar Yassin Tahir11-Jun-13 2:27
memberBashar Yassin Tahir11-Jun-13 2:27 
GeneralRe: Empty Output Pin
Member 948812611-Jun-13 2:51
memberMember 948812611-Jun-13 2:51 
GeneralRe: Empty Output Pin
Bashar Yassin Tahir11-Jun-13 6:45
memberBashar Yassin Tahir11-Jun-13 6:45 
GeneralMy vote of 5 Pin
Jim Meadors3-Mar-13 20:01
memberJim Meadors3-Mar-13 20:01 
GeneralRe: My vote of 5 Pin
Bashar Tahir18-Mar-14 23:02
memberBashar Tahir18-Mar-14 23:02 
QuestionZIP and UNZIP using CopyHere Pin
judy kamber22-Feb-13 4:27
memberjudy kamber22-Feb-13 4:27 
AnswerRe: ZIP and UNZIP using CopyHere Pin
Bashar Yassin Tahir22-Feb-13 6:51
memberBashar Yassin Tahir22-Feb-13 6:51 
QuestionTo add a file to the existing zip file Pin
Member 978240213-Feb-13 21:31
memberMember 978240213-Feb-13 21:31 

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
Web02 | 2.8.150901.1 | Last Updated 22 Oct 2013
Article Copyright 2011 by Bashar Tahir
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid