Thursday, October 11, 2007

How to get Zip files contents in C# using J# libraries

How to get Zip files contents in C# using J# libraries
Let's assume this example, how to know whether .xml file present inside requested zip file or not
Reference
Right click your project in Server Explorer and click on "Add Reference" -> Select the .Net tab -> Scroll down and select "vjslib" -> Click OK and you are there.
Namespace
using java.util;
using java.util.zip;
using java.io;
Source Code
private Boolean GetZipFiles(ZipFile zipfil)
{
Enumeration zipEnum = zipfil.entries();
while (zipEnum.hasMoreElements())
{
ZipEntry zip = (ZipEntry)zipEnum.nextElement();
if (zip.ToString().ToLower().IndexOf(".xml") != -1)
{
//folder have xml file inside
return true;
}
}
return false; //folder does NOT have xml file inside
}
How to Use
page_Load()
{
ZipFile zipfile = new ZipFile(@"C:\temp.zip");
GetZipFiles(zipfile)
}
Some more functionality using J# Library
Create a ZIP file
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();
}
Extract a ZIP file
private void Extract(string zipFileName, string destinationPath)
{
ZipFile zipfile = new ZipFile(zipFileName);
List < ZipEntry > zipFiles = GetZippedFiles(zipfile);
foreach (ZipEntry zipFile in zipFiles)
  {
    if (!zipFile.isDirectory())
    {
      InputStream s = zipfile.getInputStream(zipFile);
      try
      {
        Directory.CreateDirectory(destinationPath + "\\" +
        Path.GetDirectoryName(zipFile.getName()));
        FileOutputStream dest = new FileOutputStream(Path.Combine
          (destinationPath + "\\" + Path.GetDirectoryName(zipFile.getName()),
          Path.GetFileName(zipFile.getName())));
        try
        {
          int len = 0;
          sbyte[]buffer = new sbyte[7168];
          while ((len = s.read(buffer)) >  = 0)
          {
            dest.write(buffer, 0, len);
          }
        }
        finally
        {
          dest.close();
        }   }
      finally {
        s.close();
     }    }  }  }
Get the contents of a ZIP file
private List < ZipEntry > GetZipFiles(ZipFile zipfil)
{
  List < ZipEntry > lstZip = new List < ZipEntry > ();
  Enumeration zipEnum = zipfil.entries();
  while (zipEnum.hasMoreElements())
  {
    ZipEntry zip = (ZipEntry)zipEnum.nextElement();
    lstZip.Add(zip);
  }
  return lstZip;
}
Reference sites

1)
http://www.c-sharpcorner.com/UploadFile/neo_matrix/SharpZip04242007001341AM/SharpZip.aspx
2) http://aspalliance.com/articleViewer.aspx?aId=1269&pId=-1
 

No comments: