Click here to Skip to main content
Click here to Skip to main content

Easily zip / unzip files using Windows Shell32

By , 22 Oct 2013
Rate this:
Please Sign up or sign in to vote.

Introduction

If you want to zip / unzip files without using third party libraries, and you want a method that can be used easily and works fine, you can use the Windows Shell32. I've written the code in VB 2010 using .NET Framework 2.0, but it's simple and can be easily converted to any other language like C#.

Using the Code

As I mentioned above, I'm going to use Visual Basic 2010 with .NET Framework 2.0.

  1. Create a new project.
  2. From the main menu, select Project -> Add Reference.
  3. Select the COM tab and search for Microsoft Shell Controls and Automation.

257193/02.png

How It Works

We will use the CopyHere command. We need to create an empty zip file, then we will use Shell32 to copy the files that we want to compress into the output Zip file. Or use it to unzip the files we have compressed previously to an output folder.

Imports Shell32
 
Public Class Form1
 
    Sub Zip()
        '1) Lets create an empty Zip File .
        'The following data represents an empty zip file .

        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} 
        ' Data for an empty zip file .
        FileIO.FileSystem.WriteAllBytes("d:\empty.zip", startBuffer, False)
 
        'We have successfully made the empty zip file .

        '2) Use the Shell32 to zip your files .
        ' Declare new shell class
        Dim sc As New Shell32.Shell()
        'Declare the folder which contains the files you want to zip .
        Dim input As Shell32.Folder = sc.NameSpace("D:\neededFiles")
        'Declare  your created empty zip file as folder  .
        Dim output As Shell32.Folder = sc.NameSpace("D:\empty.zip")
        'Copy the files into the empty zip file using the CopyHere command .
        output.CopyHere(input.Items, 4)
 
    End Sub
 
    Sub UnZip()
        Dim sc As New Shell32.Shell()
        ''UPDATE !!
        'Create directory in which you will unzip your files .
        IO.Directory.CreateDirectory("D:\extractedFiles") 
        'Declare the folder where the files will be extracted
        Dim output As Shell32.Folder = sc.NameSpace("D:\extractedFiles")
        'Declare your input zip file as folder  .
        Dim input As Shell32.Folder = sc.NameSpace("d:\myzip.zip")
        'Extract the files from the zip file using the CopyHere command .
        output.CopyHere(input.Items, 4)
 
    End Sub
 
End Class

In the CopyHere command, I have used option = 4. To use other options, please refer to this page: http://msdn.microsoft.com/en-us/library/windows/desktop/bb787866(v=vs.85).aspx.

I hope I have provided something useful.

License

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

About the Author

Bashar Tahir
Student
Iraq Iraq
Hello Everyone ,
 
I didn't really study programming at college or something , I've just 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 will be glad to help you out!
 
Best Regards

Comments and Discussions

 
QuestionC# code to unzip !! Pinmemberoruganti125-Feb-14 9:43 
AnswerRe: C# code to unzip !! PinmemberBashar Tahir5-Feb-14 18:00 
QuestionSmart and brilliant! Pinmember Gun Gun Febrianza16-Nov-13 7:10 
AnswerRe: Smart and brilliant! PinmemberBashar Yassin Tahir17-Nov-13 1:34 
GeneralMy vote of 5 PinmemberAlan Tuscano16-Jul-13 21:15 
GeneralRe: My vote of 5 PinmemberBashar Yassin Tahir17-Jul-13 4:28 
QuestionEasily zip / unzip files using Windows Shell32 PinmemberAlan Tuscano15-Jul-13 23:46 
AnswerRe: Easily zip / unzip files using Windows Shell32 PinmemberBashar Yassin Tahir16-Jul-13 4:32 
GeneralRe: Easily zip / unzip files using Windows Shell32 PinmemberAlan Tuscano16-Jul-13 21:14 
GeneralRe: Easily zip / unzip files using Windows Shell32 PinmemberBashar Yassin Tahir17-Jul-13 4:23 
QuestionEmpty Output PinmemberMember 948812611-Jun-13 0:19 
AnswerRe: Empty Output PinmemberBashar Yassin Tahir11-Jun-13 2:24 
AnswerRe: Empty Output PinmemberBashar Yassin Tahir11-Jun-13 2:27 
GeneralRe: Empty Output PinmemberMember 948812611-Jun-13 2:51 
GeneralRe: Empty Output PinmemberBashar Yassin Tahir11-Jun-13 6:45 
GeneralMy vote of 5 PinmemberJim Meadors3-Mar-13 20:01 
GeneralRe: My vote of 5 PinmemberBashar Tahir18-Mar-14 23:02 
QuestionZIP and UNZIP using CopyHere Pinmemberjudy kamber22-Feb-13 4:27 
AnswerRe: ZIP and UNZIP using CopyHere PinmemberBashar Yassin Tahir22-Feb-13 6:51 
QuestionTo add a file to the existing zip file PinmemberMember 978240213-Feb-13 21:31 
AnswerRe: To add a file to the existing zip file PinmemberBashar Yassin Tahir14-Feb-13 2:04 
GeneralRe: To add a file to the existing zip file [modified] PinmemberMember 978240214-Feb-13 4:12 
GeneralRe: To add a file to the existing zip file PinmemberMember 978240214-Feb-13 4:25 
GeneralRe: To add a file to the existing zip file PinmemberBashar Yassin Tahir14-Feb-13 4:49 
GeneralRe: To add a file to the existing zip file PinmemberMember 978240214-Feb-13 23:11 
GeneralRe: To add a file to the existing zip file PinmemberBashar Yassin Tahir15-Feb-13 3:15 
GeneralRe: To add a file to the existing zip file PinmemberMember 978240218-Feb-13 19:19 
GeneralRe: To add a file to the existing zip file PinmemberBashar Yassin Tahir18-Feb-13 21:18 
GeneralRe: To add a file to the existing zip file Pinmembergerald_b20-Feb-14 8:43 
QuestionExtracting Pinmemberanopem7-Feb-13 3:02 
AnswerRe: Extracting PinmemberBashar Yassin Tahir7-Feb-13 5:55 
GeneralRe: Extracting Pinmemberanopem7-Feb-13 8:37 
GeneralRe: Extracting Pinmemberanopem13-Feb-13 0:06 
GeneralRe: Extracting PinmemberBashar Yassin Tahir13-Feb-13 2:28 
GeneralRe: Extracting Pinmembergerald_b20-Feb-14 8:49 
GeneralRe: Extracting PinmemberBashar Tahir20-Feb-14 23:09 
Questioncan you possibly make this overwrite existing file to ? PinmemberMattias Ghodsian6-Jan-13 20:25 
AnswerRe: can you possibly make this overwrite existing file to ? PinmemberBashar Yassin Tahir19-Jan-13 9:44 
GeneralMy vote of 3 PinmemberPankaj130719-Dec-12 4:29 
QuestionCall Sub Zip() PinmemberHunterTTP19-Nov-12 1:20 
AnswerRe: Call Sub Zip() PinmemberBashar Yassin Tahir19-Nov-12 2:37 
hai ,
 
I hope i got you right.
 
the sub you need to create is this
 
 
    Sub unZIP(ByVal inputZip As String, ByVal outputFolder As String)
        Dim sc As New Shell32.Shell()
        IO.Directory.CreateDirectory(outputFolder)
        Dim output As Shell32.Folder = sc.NameSpace(outputFolder)
        Dim input As Shell32.Folder = sc.NameSpace(inputZip)
        output.CopyHere(input.Items, 16)
    End Sub
 
So when you want to unzip a file you simply provide input zip file and output folder :
 
 
       unZIP(Downloads & "\test.zip",AppData & "\testdirectory")
 
 
So the code for unzipping the two of your files is :
 
 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 
       unZIP(Downloads & "\test.zip",AppData & "\testdirectory")
 
       unZIP(AppData & "\HMMI\test.2.zip",AppData & "\HMMI\test")
 
    End Sub
 

 
If this is not what you are asking for then please inform me.
 
Best Regards
GeneralRe: Call Sub Zip() [modified] PinmemberHunterTTP19-Nov-12 4:39 
GeneralRe: Call Sub Zip() PinmemberBashar Yassin Tahir19-Nov-12 7:19 
QuestionWait for completion PinmemberHunterTTP18-Nov-12 11:36 
AnswerRe: Wait for completion PinmemberBashar Yassin Tahir18-Nov-12 23:58 
GeneralRe: Wait for completion PinmemberHunterTTP19-Nov-12 1:06 
GeneralRe: Wait for completion PinmemberBashar Yassin Tahir19-Nov-12 2:39 
GeneralRe: Wait for completion PinmemberHunterTTP19-Nov-12 7:40 
GeneralRe: Wait for completion PinmemberBashar Yassin Tahir19-Nov-12 10:14 
GeneralRe: Wait for completion [modified] PinmemberHunterTTP19-Nov-12 10:21 

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 | Mobile
Web03 | 2.8.140415.2 | Last Updated 22 Oct 2013
Article Copyright 2011 by Bashar Tahir
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid