Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

[TUT] Zip and UnZip Files in VB.NET

Hi all.

This is my tutorial to zip and unzip files.

First of all, create a new project and name it wathever you want.

Then the first thing what you have to do is add the reference that is called WindowBase
[img]http://jersoft.site88.net/1.png[/img]

Then Customize your form.. I just let it simple
[img]http://jersoft.site88.net/2.png[/img]

Now, double click on the Zip button to switch to your code window.
At the top, type in:

Code
Imports System.IO.Packaging

Then in the Button1_Click sub from the Zip Button, add this code:
Code
'The Path of the Zip file you want to create
Dim zippath As String = IO.Path.Combine(Application.StartupPath, "test.zip")

'Create New Zip File
Dim zip As ZipPackage = ZipPackage.Open(zippath, IO.FileMode.Create, IO.FileAccess.ReadWrite)

To add a file or folder, use this:

Code
Dim part As PackagePart = zip.CreatePart(New Uri("/subfolder/calc.exe", UriKind.Relative), _
                                         Net.Mime.MediaTypeNames.Application.Zip, CompressionOption.Normal)

Dim bytes() As Byte = My.Computer.FileSystem.ReadAllBytes("c:\windows\system32\calc.exe")
part.GetStream.Write(bytes, 0, bytes.Length)

And finally, when you are done with adding files and/or folders close the zip creation.

Code
zip.close()


Now you have the zip code, test your program and you will see something like this:

[img]http://jersoft.site88.net/3.png[/img]


Congratulations!
But it's not finished yet. We have to unzip the file as well.
To do that, double click on the Unzip Button and the Button2_Click Sub will be generated.

Inside that sub, type this:

Code
Dim zippath As String = IO.Path.Combine(Application.StartupPath, "test.zip")
Dim zip As ZipPackage = ZipPackage.Open(zippath, IO.FileMode.Open, IO.FileAccess.ReadWrite)

'Loop through every Packagepart
For Each p As PackagePart In zip.GetParts

    'Get Filename without path
    Dim FileName As String = p.Uri.ToString.Substring(p.Uri.ToString.LastIndexOf("/") + 1)

    'Create a new FileStream
    Dim tempstream As New IO.FileStream(Application.StartupPath & "\" & FileName, IO.FileMode.Create)

    'Copy stream to the tempstream
    p.GetStream.CopyTo(tempstream)

    tempstream.Close()
Next

'And at last
zip.Close()

Now test your program and if you did all things right, you will get something like this:

[img]http://jersoft.site88.net/4.png[/img]

And that's all you have to do 👍

You can download the project here:
http://www.mediafire.com/?1hbffjodefwbj8i

Virustotal scan
http://www.virustotal.com/file-scan/repo...1311628040
https://github.com/Washi1337

"I'll tell you this UDP joke, and I don't care if you will get it or not."
I've been looking for something along these lines for a file management system, along with an FTP client management system. Thanks for the share, bro. I appreciate it.
This would be really good if you were uploading a file to a FTP. Good tutorial, home to see some more comments on this quality thread.
Nice post.

Although i have only just seen it and its too late to start now, tomorrow i will give it a go :P

Starting to really get into VB now, and all tuts are welcome as i can feel my mindz expanding with each new tut.

Following the series on youtube vb.net really good for beginners like myself. So yea thanks again.
you may want to mention that "windows base" is only available in 3.0 and up.
This sounds interesting, will definately try it out.
Thank you all for the nice replies!

(Jul 25, 2011 02:28 PM)Coleak Wrote:  This would be really good if you were uploading a file to a FTP. Good tutorial, home to see some more comments on this quality thread.

Yea I hope so to 😉

(Jul 25, 2011 06:29 PM)nathan72389 Wrote:  you may want to mention that "windows base" is only available in 3.0 and up.

Okay, I didn't know. Thank you for the information 👍
https://github.com/Washi1337

"I'll tell you this UDP joke, and I don't care if you will get it or not."
(Jul 25, 2011 06:29 PM)nathan72389 Wrote:  you may want to mention that "windows base" is only available in 3.0 and up.

When I tried this guide with framework it failed on line

P.getstream.copyto(thempstrem) - whatever that line is can't see posting from phone atm.

Only worked with framework 4.0

Any work around??

you can use the Gzip library to achieve the same thing...
Bitcoin Address: 1KQCBvevk1sA4wSj7gugv3sf5yjUygAzcv
(Sep 20, 2011 04:18 PM)Penthos Wrote:  When I tried this guide with framework it failed on line

P.getstream.copyto(thempstrem) - whatever that line is can't see posting from phone atm.

Only worked with framework 4.0

Any work around??

Check that you don't have any spaces or other symbols in the destination path.
https://github.com/Washi1337

"I'll tell you this UDP joke, and I don't care if you will get it or not."
nice tutorial ... this is work well ... superb...
there is also a method to compress you file in 7zip its already posted in hf. Nice share.
(Sep 20, 2011 10:10 PM)Washi Wrote:  Check that you don't have any spaces or other symbols in the destination path.

It was due to copyto not being in the framework for 3.5

As soon as I changed framework and nothing else it worked.

Nothing to do with destination ..
Thank you for the share, great job. ^_^
Properly no need for this but i had fun :P

Thanks op!

[img]http://i1141.photobucket.com/albums/n590/Richard_Jones1984/zipit.png[/img]

Download Source : http://www.mediafire.com/?8hvho57a2v30fl7

Edit: now to look into this Gzip....


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by Johan CruyffSep 27, 2017 09:07 AM 0 165 Sep 27, 2017 09:07 AM
Last Post: Johan Cruyff
[Shell] by mralihaiderAug 6, 2017 11:39 AM 7 360 Aug 6, 2017 11:39 AM
Last Post: GoDZMoDZ
[C#] by Pim FortuynMar 18, 2017 10:49 AM 12 484 Mar 18, 2017 10:49 AM
Last Post: Pim Fortuyn