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

Easily zip / unzip files using Windows Shell32

By , 11 Oct 2012
 

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 Yassin Tahir
Student Free Grounds
Iraq Iraq
Member
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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberJim Meadors3 Mar '13 - 20:01 
Thanks. This is a very simple way to do exactly what I want without having to add other libraries, etc.
QuestionZIP and UNZIP using CopyHerememberjudy kamber22 Feb '13 - 4:27 
This code is wonderful and so appreciated.
I have one question, please.
When I ZIP the file from a directory like C:\abc\def, the ZIP file has the DEF as the path. Then, when I want to unzip to another folder, it first creates a DEF folder then unzips the files inside. How do I either avoid putting the path into the ZIP file or avoid using in when unzipping?
 

Thanks again!
AnswerRe: ZIP and UNZIP using CopyHerememberBashar Yassin Tahir22 Feb '13 - 6:51 
Thanks for your reply.
 
Sorry but i couldn't understand your problem.
Normally it should extract/zip to the path you choose ( folder name you want).
If you can explain it in another way maybe i could help you.
 

Best Regards
QuestionTo add a file to the existing zip filememberMember 978240213 Feb '13 - 21:31 
how do i add a file to an existing zip file.
 
I already have the extract1.zip. now i want to add a file to the extract1.zip.
 
After creating thr zip file i tried this
 
Dim sc As New Shell32.Shell()
Dim output As Shell32.Folder = sc.NameSpace(outputZIP)
output.CopyHere(pathForZipFiles + "/ctrl.txt", 1024)
 
But this is not working. i want to add this text file into the zip archive. Please help me asap.
AnswerRe: To add a file to the existing zip filememberBashar Yassin Tahir14 Feb '13 - 2:04 
Just Replace the "/" at the CopyHere with the correct one "\" .
 
 
output.CopyHere(pathForZipFiles + "\ctrl.txt", 1024)
 
 
Best Regards
GeneralRe: To add a file to the existing zip file [modified]memberMember 978240214 Feb '13 - 4:12 
Hi
 
Thanks for you help.
 
Its working now.

modified 14 Feb '13 - 10:26.

GeneralRe: To add a file to the existing zip filememberMember 978240214 Feb '13 - 4:25 
Hi
 
Thanks for your help.
 
I changed all the slashes to "\" and now its working.
GeneralRe: To add a file to the existing zip filememberBashar Yassin Tahir14 Feb '13 - 4:49 
Good Job Smile | :)
GeneralRe: To add a file to the existing zip filememberMember 978240214 Feb '13 - 23:11 
Hi
 
I have the path in the app.config file.
 
add key="Path" value = "c:\images\"
 
In the code i am using output.copyhere(configurationmanager.appsettings("path") + "\out.txt",1024)
 
so now the path will be "c:\images\\out.txt". In this case the file is not moved to the zip archive.
 
But the path is a valid path.
 
App.config is not in our control as the user might give the path with last slash.
 
how to overcome this problem?
GeneralRe: To add a file to the existing zip filememberBashar Yassin Tahir15 Feb '13 - 3:15 
Ok we will just use a simple method to check if the last char is "\" or not .
 
So your code will be like this :
 
 

 
 Dim tempString As String = configurationmanager.appsettings("path")
 
 If tempString(tempString.Length - 1) = "\" Then tempString = tempString.Remove(tempString.Length - 1)
 
 output.copyhere(tempString + "\out.txt",1024)
 
 
The line :
If tempString(tempString.Length - 1) = "\" Then tempString = tempString.Remove(tempString.Length - 1)
will simply check the last char. If it's a "\" it will be removed, if not then nothing will happen and the original string will be passed =D .

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 12 Oct 2012
Article Copyright 2011 by Bashar Yassin Tahir
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid