Using C#.Net Create Zip File

From C# code, files can be easily compressed in Zip format by getting the advantage of  Zip functionality available in J#. First of all we have to add reference of the J# .NET library in our project. Physically, it resides as a file named vjslib.dll

Add Reference vjslib (J# .Net Library):

In Solution Explorer Right click your project on References and click on “Add Reference” -> select the .NET tab -> scroll down and select “vjslib” -> click OK.
After adding reference, the Java library classes could be refered  within your application.

C# Code:


using java.util;
using java.util.zip;
using java.io;



 private void CreateZipFile(string DestinationZipFileName, string SourceFileToZip)
 {

 FileOutputStream fileOutStream;
 fileOutStream = new FileOutputStream(DestinationZipFileName);
 ZipOutputStream zipOutStream ;
 zipOutStream = new ZipOutputStream(fileOutStream);

 FileInputStream fileInStream ;
 fileInStream = new FileInputStream(SourceFileToZip);

 ZipEntry zipEntry;
 zipEntry = new ZipEntry(Path.GetFileName(SourceFileToZip));

 zipOutStream.putNextEntry(zipEntry);

 sbyte[] buffer = new sbyte[1024];
 int len = 0;
 while ((len = fileInStream.read(buffer)) >= 0)
 {
 zipOutStream.write(buffer, 0, len);
 }

 zipOutStream.closeEntry();
 fileInStream.close();
 zipOutStream.close();
 fileOutStream.close();

 }



I hope this simple example will help you a lot.


Leave a Reply

Your email address will not be published. Required fields are marked *