|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionRecently I had to build a private online photo album application. I planned to provide the users option to upload images as a zip file. I needed this option to be implemented in ASP.NET 1.1, as my hosting service provider didn't have .NET 2.0 support. An online search lead me to a blog with this wonderful idea of using the zip option in J# and it really attracted me. I wanted to try it out and the results were pretty cool. Even though this sample application has been written in .NET 2.0, the .NET Framework 2.0 has libraries to work with the compressing and uncompressing of files. I will soon come up with an article to illustrate the use of .NET 2.0 libraries with compressing and uncompressing functionalities. In this article I will explain the usage of zip functionality in J# from C# code. The code in this application has been designed to reuse in a copy/paste fashion and not as a library. BackgroundThis application consumes J# classes internally. For this we must first refer to the J#.NET library. Physically it resides as a file named vjslib.dll. If you are not very sure how to refer to a library in your project please follow the below steps: 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. Now you can refer the Java library classes within your application. This was the first time trying to refer to the J# classes and it was a moment in my programming life that I will never forget. Import the following namespaces for ease of coding. java.util;
java.util.zip;
java.io;
Programmatically, a Using the codeBelow is a code listing how to 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();
}
The above
The The Taking a deeper look into the
code, a The newly created 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();
}
}
}
}
The During the extraction, the original folder
structure is maintained. In the 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;
}
The Apart from the above listed code, the downloadable source code for the sample application, contains some extra code to handle the UI part of the application, i.e., entries made to the ListBox and handling the progress bar. I haven't included them in this article because I didn't want to lose focus from the main objective of the article. The code is comprehensive, but the UI controls can be handled in better ways, keeping performance and usability in mind. One good option might be to keep our zip functionality separate from the UI thread, so that interactivity is well maintained. Points of InterestOne interesting fact we note while using the Java library is the difference in naming conventions, especially the naming of methods. For C# coders, methods give the feel of variable names. Also, the way the classes are named is a distinguishable factor. Even though we can't use the features of Java beyond a point because runtime decides the main advantages of a platform; we would take advantage of using the Java libraries. This gives an upper hand of Java over C#. I hope Microsoft continues to support J#.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||