Zip Unzip files Programmatically in C#

Posted by Techie Cocktail | 7:54 AM | | 0 comments »

C# and VB.Net does not have support to perform Zip and UnZip operations. But you can achieve this in .Net using Visual J# in C#. Its very easy. Check below.



Steps:

a. Ensure that you have Visual J# in your Visual Studio version. In VS 2005, Visual J# used to come by default, but it does not comes by default in VS 2008. So you need to install it to your VS 2008.

You can download Visual J# Redistributable Packages from -

this link.



b. After installing V J#, add a reference to 'vjslib.dll' in your project.

c. Refer to these namespaces in your code - java.util, java.util.zip, java.io;



d. Follow this code below. Works perfect.



Source Code:



public static void CreateZipFile(string filename, string[] items)

{

FileOutputStream fout = new FileOutputStream(filename);

ZipOutputStream zout = new ZipOutputStream(fout);

zout.close();

ZipFile zipfile = new ZipFile(filename);

AddEntries(zipfile, items);

}





private static void AddEntries(ZipFile file,string[] newFiles)

{

string fileName = file.getName();

string tempFileName = Path.GetTempFileName();

ZipOutputStream destination = new ZipOutputStream

(new FileOutputStream(tempFileName));

try

{

CopyEntries(file, destination);

if (newFiles != null)

{

foreach (string f in newFiles)

{

ZipEntry z = new ZipEntry(f.Remove

(0,Path.GetPathRoot(f).Length));

z.setMethod(ZipEntry.DEFLATED);

destination.putNextEntry(z);

try

{

FileInputStream s = new FileInputStream(f);

try

{

CopyStream(s, destination);

}

finally

{

s.close();

}

}

finally

{

destination.closeEntry();

}

}

}

}

finally

{

destination.close();

}

file.close();

System.IO.File.Copy(tempFileName, fileName, true);

System.IO.File.Delete(tempFileName);

}





private static void CopyEntries(ZipFile source,

ZipOutputStream destination)

{

List entries = GetZippedItems(source);

foreach (ZipEntry entry in entries)

{

destination.putNextEntry(entry);

InputStream s = source.getInputStream(entry);



CopyStream(s, destination);

destination.closeEntry();

s.close();

}

}





private static void CopyEntries(ZipFile source,

ZipOutputStream destination,string[] entryNames)

{

List entries = GetZippedItems(source);

for(int i=0;i
{

foreach (ZipEntry entry in entries)

{

if (entry.getName() == entryNames[i])

{

destination.putNextEntry(entry);

InputStream s = from.getInputStream(entry);



CopyStream(s, destination);

destination.closeEntry();

s.close();

}

}

}

}





private static void CopyStream(InputStream source,

OutputStream destination)

{

sbyte[] buffer = new sbyte[8000];

int data;

while (true)

{

try

{

data = source.read(buffer, 0, buffer.Length);

if (data > 0)

{

destination.write(buffer, 0, data);

}

else

{

return;

}

}

catch (Exception ex)

{

string msg = ex.Message;

}

}

}





private static List GetZippedItems(ZipFile file)

{

List entries = new List();

Enumeration e = file.entries();

while (true)

{

if (e.hasMoreElements())

{

ZipEntry entry = (ZipEntry)e.nextElement();

entries.Add(entry);

}

else

{

break;

}

}

return entries;

}





public static void ExtractZipFile(string zipfilename,

string destination)

{

ZipFile zipfile = new ZipFile(zipfilename);

List entries = GetZippedItems(zipfile);

foreach (ZipEntry entry in entries)

{

if (!entry.isDirectory())

{

InputStream s = zipfile.getInputStream(entry);

try

{

string fname = System.IO.Path.GetFileName(entry.getName());

string dir=System.IO.Path.GetDirectoryName(entry.getName());

string newpath = destination + @"\" + dir;

System.IO.Directory.CreateDirectory(newpath);

FileOutputStream dest = new FileOutputStream

(System.IO.Path.Combine(newpath, fname));

try

{

CopyStream(s, dest);

}

finally

{

dest.close();

}

}

finally

{

s.close();

}

}

}

}



For more information on this topic check this reference website: DotNetBips



Other method to zip files:



private void Zip(string zipFileName, string[] sourceFile)

{

FileOutputStream filOpStrm = new FileOutputStream(zipFileName);

ZipOutputStream zipOpStrm = new ZipOutputStream(filOpStrm);

FileInputStream filIpStrm = null;

foreach(string strFilName in sourceFile)

{

filIpStrm = new FileInputStream(strFilName);

ZipEntry ze = new ZipEntry(Path.GetFileName(strFilName));

zipOpStrm.putNextEntry(ze);

sbyte[] buffer = new

sbyte[1024];

int len = 0;

while ((len =

filIpStrm.read(buffer)) > = 0)

{

zipOpStrm.write(buffer, 0, len);

}

}

zipOpStrm.closeEntry();

filIpStrm.close();

zipOpStrm.close();

filOpStrm.close();

}

Reference: http://69.10.233.10/KB/cs/unzipC_usingJ_.aspx

0 comments