Compressing a File with MS Visual J# .NET

The Microsoft Visual J# .NET 1.1 Class Library (vjslib.dll) contains methods you can use to compress a file. The following code demonstrates how to use them.

/* Code to zip a file/folder using Visual J# library */using System;using System.IO;using java.util.zip;using java.util;using java.io;namespace Zipper{ /// <summary> /// This Class provides methods to Zip a file vjslib /// c:windowsmicrosoft.netframeworkv1.1.4322vjslib.dll ///   public class Zipper {   #region Constructor     public Zipper(){}   #endregion Constructor		   #region Void Methods    /*CreateZipFile Method expects the path to be zipped.     * The path could be a directory or a single file*/   public void CreateZipFile(string sPath)   {     FileOutputStream stream1 = null;     ZipOutputStream stream2 = null;     if (System.IO.File.Exists(sPath))     {	FileInfo info2 = new FileInfo(sPath);        stream1 = new FileOutputStream(sPath.Replace                                      (info2.Extension, ".zip"));	stream2 = new ZipOutputStream(stream1);			this.ZipOneFile(stream1, stream2,  sPath);     }     else if (Directory.Exists(sPath))     {	stream1 = new FileOutputStream(sPath + ".zip");	stream2 = new ZipOutputStream(stream1);	DirectoryInfo info1 = new DirectoryInfo(sPath);	this.ZipDirectory(stream1, stream2, info1, sPath);     }     stream2.close();     stream1.close();     stream2.flush();     stream1.flush();    }    private void CopyStream(FileInputStream src, ZipOutputStream                                                             dest)    {	InputStreamReader reader1 = new InputStreamReader(src);	OutputStreamWriter writer1 = new OutputStreamWriter(dest);	while (reader1.ready())	{	  writer1.write(reader1.read());	}	writer1.flush();    }		    /*ZipDirectory Method zips the entire directory*/    private void ZipDirectory(FileOutputStream fos, ZipOutputStream                              zos, DirectoryInfo di, string SRootDir)    {	FileInfo[] infoArray2 = di.GetFiles();	foreach (FileInfo info2 in infoArray2)	{           ZipEntry entry1 = new ZipEntry               (info2.FullName.Substring(SRootDir.LastIndexOf(@"")));	   entry1.setMethod(8);	   zos.putNextEntry(entry1);	   FileInputStream stream1 = new FileInputStream                                       (info2.FullName);       	   this.CopyStream(stream1, zos);	   zos.closeEntry();	   stream1.close();	}	DirectoryInfo[] infoArray1 = di.GetDirectories();	foreach (DirectoryInfo info1 in infoArray1)	{	   this.ZipDirectory(fos, zos, info1, SRootDir);	}     }     /*ZipOneFile Method zips the file*/     private void ZipOneFile(FileOutputStream fos, ZipOutputStream                                              zos, string sFullName)     {	ZipEntry entry1 = new ZipEntry(sFullName.Substring                            (sFullName.LastIndexOf(@""))); 	entry1.setMethod(8);	zos.putNextEntry(entry1);	FileInputStream stream1 = new FileInputStream(sFullName);	this.CopyStream(stream1, zos);	zos.closeEntry();	stream1.close();     }     #endregion Void Methods  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: