Click here to Skip to main content
11,429,154 members (67,838 online)
Click here to Skip to main content

Easily Zip / Unzip Files using Windows Shell32

, 22 Oct 2013 CPOL
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

Comments and Discussions

 
QuestionExtracting Pin
anopem7-Feb-13 4:02
memberanopem7-Feb-13 4:02 
AnswerRe: Extracting Pin
Bashar Yassin Tahir7-Feb-13 6:55
memberBashar Yassin Tahir7-Feb-13 6:55 
GeneralRe: Extracting Pin
anopem7-Feb-13 9:37
memberanopem7-Feb-13 9:37 
GeneralRe: Extracting Pin
anopem13-Feb-13 1:06
memberanopem13-Feb-13 1:06 
GeneralRe: Extracting Pin
Bashar Yassin Tahir13-Feb-13 3:28
memberBashar Yassin Tahir13-Feb-13 3:28 
GeneralRe: Extracting Pin
gerald_b20-Feb-14 9:49
membergerald_b20-Feb-14 9:49 
GeneralRe: Extracting Pin
Bashar Tahir21-Feb-14 0:09
memberBashar Tahir21-Feb-14 0:09 
Questioncan you possibly make this overwrite existing file to ? Pin
Mattias Ghodsian6-Jan-13 21:25
memberMattias Ghodsian6-Jan-13 21:25 
AnswerRe: can you possibly make this overwrite existing file to ? Pin
Bashar Yassin Tahir19-Jan-13 10:44
memberBashar Yassin Tahir19-Jan-13 10:44 
GeneralMy vote of 3 Pin
Pankaj130719-Dec-12 5:29
memberPankaj130719-Dec-12 5:29 
QuestionCall Sub Zip() Pin
HunterTTP19-Nov-12 2:20
memberHunterTTP19-Nov-12 2:20 
AnswerRe: Call Sub Zip() Pin
Bashar Yassin Tahir19-Nov-12 3:37
memberBashar Yassin Tahir19-Nov-12 3:37 
GeneralRe: Call Sub Zip() [modified] Pin
HunterTTP19-Nov-12 5:39
memberHunterTTP19-Nov-12 5:39 
GeneralRe: Call Sub Zip() Pin
Bashar Yassin Tahir19-Nov-12 8:19
memberBashar Yassin Tahir19-Nov-12 8:19 
the zipping sub should be :

 
    Sub Zip(ByVal inputFolder As String, ByVal outputZip As String)
 
        Dim startBuffer() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, _
                                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
 
        FileIO.FileSystem.WriteAllBytes(outputZip, startBuffer, False)
        Dim sc As New Shell32.Shell()
        Dim input As Shell32.Folder = sc.NameSpace(inputFolder)
        Dim output As Shell32.Folder = sc.NameSpace(outputZip)
        output.CopyHere(input.Items, 16)
 
    End Sub
 


Calling it is just as simple as calling the unzip() sub :

 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 
        Zip("D:\myfiles", "D:\myZip.zip")
 
    End Sub

QuestionWait for completion Pin
HunterTTP18-Nov-12 12:36
memberHunterTTP18-Nov-12 12:36 
AnswerRe: Wait for completion Pin
Bashar Yassin Tahir19-Nov-12 0:58
memberBashar Yassin Tahir19-Nov-12 0:58 
GeneralRe: Wait for completion Pin
HunterTTP19-Nov-12 2:06
memberHunterTTP19-Nov-12 2:06 
GeneralRe: Wait for completion Pin
Bashar Yassin Tahir19-Nov-12 3:39
memberBashar Yassin Tahir19-Nov-12 3:39 
GeneralRe: Wait for completion Pin
HunterTTP19-Nov-12 8:40
memberHunterTTP19-Nov-12 8:40 
GeneralRe: Wait for completion Pin
Bashar Yassin Tahir19-Nov-12 11:14
memberBashar Yassin Tahir19-Nov-12 11:14 
GeneralRe: Wait for completion [modified] Pin
HunterTTP19-Nov-12 11:21
memberHunterTTP19-Nov-12 11:21 
GeneralRe: Wait for completion Pin
Bashar Yassin Tahir20-Nov-12 3:21
memberBashar Yassin Tahir20-Nov-12 3:21 
GeneralRe: Wait for completion Pin
HunterTTP20-Nov-12 13:02
memberHunterTTP20-Nov-12 13:02 
GeneralRe: Wait for completion Pin
Bashar Yassin Tahir21-Nov-12 4:44
memberBashar Yassin Tahir21-Nov-12 4:44 
GeneralRe: Wait for completion [modified] Pin
HunterTTP21-Nov-12 6:53
memberHunterTTP21-Nov-12 6:53 
GeneralRe: Wait for completion Pin
Bashar Yassin Tahir21-Nov-12 10:29
memberBashar Yassin Tahir21-Nov-12 10:29 
GeneralRe: Wait for completion Pin
HunterTTP21-Nov-12 10:42
memberHunterTTP21-Nov-12 10:42 
GeneralRe: Wait for completion Pin
Bashar Yassin Tahir21-Nov-12 10:59
memberBashar Yassin Tahir21-Nov-12 10:59 
GeneralRe: Wait for completion Pin
Bashar Yassin Tahir21-Nov-12 11:04
memberBashar Yassin Tahir21-Nov-12 11:04 
GeneralRe: Wait for completion Pin
HunterTTP21-Nov-12 11:15
memberHunterTTP21-Nov-12 11:15 
GeneralRe: Wait for completion Pin
Bashar Yassin Tahir21-Nov-12 11:19
memberBashar Yassin Tahir21-Nov-12 11:19 
GeneralRe: Wait for completion Pin
HunterTTP21-Nov-12 11:50
memberHunterTTP21-Nov-12 11:50 
GeneralRe: Wait for completion [modified] Pin
HunterTTP21-Nov-12 12:16
memberHunterTTP21-Nov-12 12:16 
GeneralRe: Wait for completion Pin
HunterTTP21-Nov-12 13:08
memberHunterTTP21-Nov-12 13:08 
GeneralRe: Wait for completion [modified] Pin
Bashar Yassin Tahir22-Nov-12 4:11
memberBashar Yassin Tahir22-Nov-12 4:11 
GeneralRe: Wait for completion Pin
HunterTTP22-Nov-12 6:13
memberHunterTTP22-Nov-12 6:13 
GeneralRe: Wait for completion Pin
Bashar Yassin Tahir22-Nov-12 10:01
memberBashar Yassin Tahir22-Nov-12 10:01 
GeneralRe: Wait for completion [modified] Pin
HunterTTP22-Nov-12 14:41
memberHunterTTP22-Nov-12 14:41 
GeneralRe: Wait for completion Pin
Bashar Yassin Tahir22-Nov-12 22:11
memberBashar Yassin Tahir22-Nov-12 22:11 
GeneralRe: Wait for completion Pin
HunterTTP23-Nov-12 4:18
memberHunterTTP23-Nov-12 4:18 
GeneralRe: Wait for completion Pin
Bashar Yassin Tahir23-Nov-12 4:41
memberBashar Yassin Tahir23-Nov-12 4:41 
GeneralRe: Wait for completion Pin
HunterTTP23-Nov-12 4:48
memberHunterTTP23-Nov-12 4:48 
GeneralRe: Wait for completion Pin
Bashar Yassin Tahir23-Nov-12 4:50
memberBashar Yassin Tahir23-Nov-12 4:50 
GeneralRe: Wait for completion Pin
HunterTTP23-Nov-12 5:07
memberHunterTTP23-Nov-12 5:07 
GeneralRe: Wait for completion Pin
Bashar Yassin Tahir23-Nov-12 6:49
memberBashar Yassin Tahir23-Nov-12 6:49 
GeneralRe: Wait for completion Pin
Pankaj130719-Dec-12 6:03
memberPankaj130719-Dec-12 6:03 
GeneralRe: Wait for completion Pin
HunterTTP19-Dec-12 6:51
memberHunterTTP19-Dec-12 6:51 
GeneralRe: Wait for completion Pin
Pankaj130719-Dec-12 6:59
memberPankaj130719-Dec-12 6:59 
GeneralRe: Wait for completion Pin
Bashar Yassin Tahir19-Dec-12 7:00
memberBashar Yassin Tahir19-Dec-12 7:00 
QuestionMultiple uses in a single sub? (VB.Net) Pin
HunterTTP17-Nov-12 16:25
memberHunterTTP17-Nov-12 16:25 

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