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.
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.Extension, ".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(SRootDir.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(sFullName.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
|
| Author: charles black 21 Jan 2006 | Member Level: Bronze Points : 0 |
There is an article on CodeProject web site (Bug when using the java.util.zip classes to write zip files ) at http://www.codeproject.com/buglist/jslibzipbug.asp that details a problem with using the java.util.zip.ZipOutputStream to write to zip files when adding binary files. It will throw an exception. Also I had trouble getting your CopyStream subroutine to work correctly. As far as I can see from the source code, you never close the OutputStreamWriter before exiting the CopyStream method. I couldn't get it to work until I added the writer.close() statement. Then it worked fine.
|
| Author: charles black 21 Jan 2006 | Member Level: Bronze Points : 0 |
That bug that would cause the java.util.zip.ZipOutputStream (in vsjlib) to throw an exception when zipping binary files seems to be corrected, at least in the latest dot net framework, 2.0. The version of vjslib.dll is 2.0.0.0.
|
| Author: charles black 21 Jan 2006 | Member Level: Bronze Points : 0 |
With respect the my earlier comment about CopyStream and your not closing the writer object. The writer object wraps the ZipOutputStream and if you are going to be calling CopyStream repeatedly for each file when zipping up a directory, you don't want to close the writer object. or you will get the exception, Zip file must contain at least one entry when you try to add the second entry. You must not close ZipOutputStream until you are done with everything.
|
| Author: relish 10 Jan 2007 | Member Level: Bronze Points : 0 |
When we use the code to zip the files,in the output, the modified date for each file is "1/1/1980"...Is there any way we can get the correct modified date of the files..?
|
| Author: Dilip Jain 26 Dec 2007 | Member Level: Bronze Points : 0 |
Hi, I am using .NET 2.0. I have incorporated the suggestion from comment 1 from Charles. The code works fine for a file of 3 GB however it fails when i use it for 7 GB. It throws an exception Illegal argument exception on closing the writer object. Can anyone let me know the maximum file size which i can zip? Is there any other way wherein I can zip such huge size?
|
| Author: Ganesh Shivshankar 25 Jan 2008 | Member Level: Bronze Points : 0 |
the zipped file gets created. but the .zip folder is empty.could you please help me with this?
|
| Author: Prak 28 Apr 2008 | Member Level: Bronze Points : 2 |
Hi,
I am successfully able to zip files and folders. But What I noticed was, I could not add or delete any files manually in to/from the archived files. An error message "the compressed (zipped)folder is invalid or corrupt" is displayed. I have closed the zipoutputstream after adding all files and folders. Can some tell what might be happening.
|