Hack Forums
[.NET] [TUT] Zip and UnZip Files in VB.NET - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] [TUT] Zip and UnZip Files in VB.NET (/showthread.php?tid=1523026)



[TUT] Zip and UnZip Files in VB.NET - Washi - Jul 25, 2011

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/report.html?id=5fef4f5e67128ed39c16c35195d125368d23c64e480b7b221d7e7787b8bc6800-1311628040


* - Funhouse - Jul 25, 2011

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.


* - Coleak - Jul 25, 2011

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.


* - Penthos - Jul 25, 2011

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.


* - nathan72389 - Jul 25, 2011

you may want to mention that "windows base" is only available in 3.0 and up.



* - Logic_Bomber - Jul 25, 2011

This sounds interesting, will definately try it out.


* - Washi - Jul 25, 2011

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 👍


* - Penthos - Sep 20, 2011

(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??




* - Vysse - Sep 20, 2011

you can use the Gzip library to achieve the same thing...


* - Washi - Sep 20, 2011

(Sep 20, 2011 04:18 PM)TrinityCoder 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.


* - desi hacker - Sep 20, 2011

nice tutorial ... this is work well ... superb...


* - adiiforu - Sep 20, 2011

there is also a method to compress you file in 7zip its already posted in hf. Nice share.


* - Penthos - Sep 21, 2011

(Sep 20, 2011 10:10 PM)TheUnknownProgrammer 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 ..


* - bob42 - Sep 21, 2011

Thank you for the share, great job. ^_^


* - Penthos - Sep 21, 2011

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