Connecting Tech Pros Worldwide Help | Site Map

VB.NET Class File For Zip a File or Directory

Newbie
 
Join Date: Dec 2007
Posts: 1
#1   Dec 29 '07
This article is used to zip a file or directory using vb.net. The classes and method to zip a file is availale in java.io, java.util, java.util.zip class library.To import these you have to add a reference vsjlib Library in .net component.


.NET Classes used :


Imports System.IO
Imports java.io
Imports java.util
Imports java.util.zip

Introduction


This article is used to create a zip file in easy way.
What you have to do is just create an object for the class that i have written in below and call the procedrue CreateZipFile(FolderToZip) or CreateZipFile(FileToZip).



Class to zip file or directory


Imports System.IO
Imports java.io
Imports java.util
Imports java.util.zip

Public Class clsZip

Public Sub CreateZipFile(ByVal sPath As String)
Dim fos As java.io.FileOutputStream
Dim zos As java.util.zip.ZipOutputStream
Dim di As System.IO.DirectoryInfo

'check , is it a file existing in this path, if true then zip a file
If System.IO.File.Exists(sPath) Then
Dim fInfo As New FileInfo(sPath)

'create a zip file with same name in the same path
fos = New java.io.FileOutputStream(sPath.Replace(fInfo.Exten sion, ".zip"))
zos = New java.util.zip.ZipOutputStream(fos)

'procedure to zip one File
ZipOneFile(fos, zos, sPath)

'check , is it a directory existing in this path,if true then zip a directory
ElseIf System.IO.Directory.Exists(sPath) Then

'create a zip file with same name in the same path
fos = New java.io.FileOutputStream(sPath & ".zip")
zos = New java.util.zip.ZipOutputStream(fos)
di = New System.IO.DirectoryInfo(sPath)

'procedure to zip a directory
ZipDirectory(fos, zos, di, sPath)
End If


zos.close()
fos.close()
zos.flush()
fos.flush()
End Sub

Private Sub ZipDirectory(ByVal fos As java.io.FileOutputStream, ByVal zos As java.util.zip.ZipOutputStream, ByVal di As System.IO.DirectoryInfo, ByVal SRootDir As String)
Dim fis As java.io.FileInputStream
Dim ze As java.util.zip.ZipEntry

'to get file info from the directory
Dim fInfos As System.IO.FileInfo() = di.GetFiles
Dim fInfo As System.IO.FileInfo

For Each fInfo In fInfos
'give the zip entry or the folder arrangement for the file
ze = New java.util.zip.ZipEntry(fInfo.FullName.Substring(SR ootDir.LastIndexOf("\")))

'The DEFLATED method is the one of the methods to zip a file
ze.setMethod(ze.DEFLATED)
zos.putNextEntry(ze)

'Input stream for the file to zip
fis = New java.io.FileInputStream(fInfo.FullName)

'Copy stream is a simple method to read a file input stream (file to zip) and write it to a file output stream(new zip file)
CopyStream(fis, zos)

zos.closeEntry()
fis.close()
Next

'If the directory contains the sub directory the call the same procedure
Dim dinfos As System.IO.DirectoryInfo() = di.GetDirectories()
Dim dinfo As System.IO.DirectoryInfo
For Each dinfo In dinfos
ZipDirectory(fos, zos, dinfo, SRootDir)
Next

End Sub

Private Sub ZipOneFile(ByVal fos As java.io.FileOutputStream, ByVal zos As java.util.zip.ZipOutputStream, ByVal sFullName As String)
Dim fis As java.io.FileInputStream
Dim ze As java.util.zip.ZipEntry
'give the zip entry or the folder arrangement for the file
ze = New java.util.zip.ZipEntry(sFullName.Substring(sFullNa me.LastIndexOf("\")))
'The DEFLATED method is the one of the methods to zip a file
ze.setMethod(ze.DEFLATED)
zos.putNextEntry(ze)
'Input stream for the file to zip
fis = New java.io.FileInputStream(sFullName)
'Copy stream is a simple method to read a file input stream (file to zip) and write it to a file output stream(new zip file)
CopyStream(fis, zos)
zos.closeEntry()
fis.close()
End Sub

Private Sub CopyStream(ByVal src As java.io.FileInputStream, ByVal dest As java.util.zip.ZipOutputStream)
Dim reader As New java.io.InputStreamReader(src)
Dim writer As New java.io.OutputStreamWriter(dest)
While reader.ready
writer.write(reader.read)
End While
writer.flush()
End Sub


End Class



CALL THE PROCEDURE TO ZIP A FILE


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim obj As New clsZip
obj.CreateZipFile(TextBox1.Text)
MsgBox("New Zip file is created for " & TextBox1.Text, MsgBoxStyle.Information, "Zip File") End Sub



Step by Step process

1. Create a new vb.net project
2. Create a class clsZip
3. Copy the code CLASS TO ZIP A FILE OR DIRECTORY
4. In the solution explorer-->Reference, right click,
select vsjlib library in the .Net library Tab Page.
5. See the sample CALL THE PROCEDURE TO ZIP A FILE


Thanx
Sandhya.



Newbie
 
Join Date: Oct 2008
Posts: 1
#2   Oct 15 '08

re: VB.NET Class File For Zip a File or Directory


you miswrited the name of the library to reference:
vjslib
should be the correct one...
Reply