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
 

Wednesday, October 10, 2007

How to do Iframe auto resize according to aspx page contain

How to do Iframe auto resize according to aspx page contain
 
Supose you have one Iframe "IFrmRightMenu" and inside you are setting target for different aspx page, In this case the content of aspx page can have different height and width, If you want to set Iframe size according to aspx page contain height and width then use below function in onload attribute of iframe.
 
Source code
 
<script language="javascript">
 
function autoIFrameResize(id)
{
   var newheight;
   var the_width=572; //Fixed width      
       
   if(navigator.appName == "Microsoft Internet Explorer")
{            newheight=document.getElementById(id).contentWindow.document.body.scrollHeight;
           
      if (newheight == 0)
      {
           newheight =584; //Fixed height           
       }
        else
        {
newheight = document.getElementById(id).contentDocument.height;
        }
        
document.getElementById(id).height= (newheight + 16) + "px";
      document.getElementById(id).style.width=the_width+"px";       
window.parent.document.getElementById("IFrmRightMenu").style.height= (newheight + 16) + "px";
    }
 
</script>
 
How to use
 
<iframe frameborder="no" width="100%" height="100%" onload="autoResize('iframe1');" scrolling ="no" style="visibility:hidden;" id=" iframe1"></iframe>
 
How to redirect to Parent page from Child page (IFrame)
 
Let say you have one aspx page with two iframe (LeftIframe and RightIframe in one aspx page), now if you click any button in "RightIframe" and you want to redirect some error page or your want to redirect in home page then you can't simply write Reponse.Redirect ("error.aspx") or Reponse.Redirect ("Home.aspx"), this will display the page in "RightIframe" only.
 
Use below line of code to redirect page to Home.aspx or Error.aspx
 
Response.Write("<script>window.open('Error.aspx','_parent');</script>") OR
Response.Write("<script>window.open('Home.aspx','_parent');</script>")
 


Check out the hottest 2008 models today at Yahoo! Autos.