# Easily Zip / Unzip Files using Windows Shell32

, 22 Oct 2013
 Rate this:
Easily zip / unzip files using Windows Shell32

## Introduction

If you want to zip / unzip files and folders without using third-party libraries, and you need a simple way of achieving that, 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#).

## Preparation

As I mentioned above, Visual Basic 2010 with .NET Framework 2.0 is used:

1. Create a new project.
3. Select the COM tab and search for "Microsoft Shell Controls and Automation".

## So, How Does It Work?

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

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()
'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, option 4 was used. To use other options, please refer to this page: http://msdn.microsoft.com/en-us/library/windows/desktop/bb787866(v=vs.85).aspx.

An alternative way of using the Shell32 (instead of importing a reference) is to directly get it through:

    Dim sc As Object = CreateObject("Shell.Application")

But then, the next lines need to be slightly modified.

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

The only problem with this method is that you don't have a direct control with the zipping or unzipping operations, since these operations are carried out by the Shell32 itself. 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!

## You may also be interested in...

Student
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.

Best Regards

 First PrevNext
 C# code to unzip !! oruganti12 5-Feb-14 9:43
 Re: C# code to unzip !! Bashar Tahir 5-Feb-14 18:00
 Smart and brilliant! Gun Gun Febrianza 16-Nov-13 7:10
 Re: Smart and brilliant! Bashar Yassin Tahir 17-Nov-13 1:34
 My vote of 5 Alan Tuscano 16-Jul-13 21:15
 Re: My vote of 5 Bashar Yassin Tahir 17-Jul-13 4:28
 Easily zip / unzip files using Windows Shell32 Alan Tuscano 15-Jul-13 23:46
 Re: Easily zip / unzip files using Windows Shell32 Alan Tuscano 16-Jul-13 21:14
 Empty Output Member 9488126 11-Jun-13 0:19
 Re: Empty Output Bashar Yassin Tahir 11-Jun-13 2:24
 Re: Empty Output Bashar Yassin Tahir 11-Jun-13 2:27
 Re: Empty Output Member 9488126 11-Jun-13 2:51
 Re: Empty Output Bashar Yassin Tahir 11-Jun-13 6:45
 My vote of 5 Jim Meadors 3-Mar-13 20:01
 Re: My vote of 5 Bashar Tahir 18-Mar-14 23:02
 ZIP and UNZIP using CopyHere judy kamber 22-Feb-13 4:27
 Re: ZIP and UNZIP using CopyHere Bashar Yassin Tahir 22-Feb-13 6:51
 To add a file to the existing zip file Member 9782402 13-Feb-13 21:31
 Re: To add a file to the existing zip file Bashar Yassin Tahir 14-Feb-13 2:04
 Re: To add a file to the existing zip file Member 9782402 14-Feb-13 4:25
 Re: To add a file to the existing zip file Bashar Yassin Tahir 14-Feb-13 4:49
 Re: To add a file to the existing zip file Member 9782402 14-Feb-13 23:11
 Last Visit: 31-Dec-99 18:00     Last Update: 1-Sep-14 14:26 Refresh 12345 Next »