By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,016 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

VB.NET Class File For Zip a File or Directory

P: 1
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 :

Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  2. Imports java.io
  3. Imports java.util
  4. 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


Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  2. Imports java.io
  3. Imports java.util
  4. Imports java.util.zip
  5.  
  6. Public Class clsZip
  7.  
  8.    Public Sub CreateZipFile(ByVal sPath As String)
  9.        Dim fos As java.io.FileOutputStream
  10.        Dim zos As java.util.zip.ZipOutputStream
  11.        Dim di As System.IO.DirectoryInfo
  12.  
  13.        'check , is it a file existing in this path,   if true then zip a file
  14.        If System.IO.File.Exists(sPath) Then
  15.            Dim fInfo As New FileInfo(sPath)
  16.  
  17.            'create a zip file with same name in the same path
  18.            fos = New java.io.FileOutputStream(sPath.Replace(fInfo.Extension, ".zip"))
  19.            zos = New java.util.zip.ZipOutputStream(fos)
  20.  
  21.            'procedure to zip one File
  22.            ZipOneFile(fos, zos, sPath)
  23.  
  24.            'check , is it a directory existing in this path,if true then zip a directory
  25.        ElseIf System.IO.Directory.Exists(sPath) Then
  26.  
  27.            'create a zip file with same name in the same path
  28.            fos = New java.io.FileOutputStream(sPath & ".zip")
  29.            zos = New java.util.zip.ZipOutputStream(fos)
  30.            di = New System.IO.DirectoryInfo(sPath)
  31.  
  32.            'procedure to zip a directory
  33.            ZipDirectory(fos, zos, di, sPath)
  34.        End If
  35.  
  36.  
  37.        zos.close()
  38.        fos.close()
  39.        zos.flush()
  40.        fos.flush()
  41.    End Sub
  42.  
  43.    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)
  44.        Dim fis As java.io.FileInputStream
  45.        Dim ze As java.util.zip.ZipEntry
  46.  
  47.        'to get file info from the directory
  48.        Dim fInfos As System.IO.FileInfo() = di.GetFiles
  49.        Dim fInfo As System.IO.FileInfo
  50.  
  51.        For Each fInfo In fInfos
  52.            'give the zip entry or the folder arrangement for the file
  53.            ze = New java.util.zip.ZipEntry(fInfo.FullName.Substring(SRootDir.LastIndexOf("\")))
  54.  
  55.            'The DEFLATED method is the one of the methods to zip a file
  56.            ze.setMethod(ze.DEFLATED)
  57.            zos.putNextEntry(ze)
  58.  
  59.            'Input stream for the file to zip
  60.            fis = New java.io.FileInputStream(fInfo.FullName)
  61.  
  62.            '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)
  63.            CopyStream(fis, zos)
  64.  
  65.            zos.closeEntry()
  66.            fis.close()
  67.        Next
  68.  
  69.        'If the directory contains the sub directory the call the same procedure
  70.        Dim dinfos As System.IO.DirectoryInfo() = di.GetDirectories()
  71.        Dim dinfo As System.IO.DirectoryInfo
  72.        For Each dinfo In dinfos
  73.            ZipDirectory(fos, zos, dinfo, SRootDir)
  74.        Next
  75.  
  76.    End Sub
  77.  
  78.    Private Sub ZipOneFile(ByVal fos As java.io.FileOutputStream, ByVal zos As java.util.zip.ZipOutputStream, ByVal sFullName As String)
  79.        Dim fis As java.io.FileInputStream
  80.        Dim ze As java.util.zip.ZipEntry
  81.        'give the zip entry or the folder arrangement for the file
  82.        ze = New java.util.zip.ZipEntry(sFullName.Substring(sFullName.LastIndexOf("\")))
  83.        'The DEFLATED method is the one of the methods to zip a file
  84.        ze.setMethod(ze.DEFLATED)
  85.        zos.putNextEntry(ze)
  86.        'Input stream for the file to zip
  87.        fis = New java.io.FileInputStream(sFullName)
  88.        '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)
  89.        CopyStream(fis, zos)
  90.        zos.closeEntry()
  91.        fis.close()
  92.    End Sub
  93.  
  94.    Private Sub CopyStream(ByVal src As java.io.FileInputStream, ByVal dest As java.util.zip.ZipOutputStream)
  95.        Dim reader As New java.io.InputStreamReader(src)
  96.        Dim writer As New java.io.OutputStreamWriter(dest)
  97.        While reader.ready
  98.            writer.write(reader.read)
  99.        End While
  100.        writer.flush()
  101.    End Sub
  102.  
  103.  
  104. End Class


CALL THE PROCEDURE TO ZIP A FILE


Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.        Dim obj As New clsZip
  3.       obj.CreateZipFile(TextBox1.Text)
  4.        MsgBox("New Zip file is created for " & TextBox1.Text, MsgBoxStyle.Information, "Zip File")    
  5. 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.
Dec 29 '07 #1
Share this Article
Share on Google+
1 Comment


P: 1
you miswrited the name of the library to reference:
vjslib
should be the correct one...
Oct 15 '08 #2