April 19, 2004
Java-Worth Another Look?Richard Grimes
Don't worry, Richard isn't suggesting that you abandon .NET, but if the .NET Framework library is missing a feature you need, you might find the solution in the JDK. Richard shows how to tap into Java's zip file utilities from within your .NET code
.NET is made up of two important parts, each as important as the other: the runtime and the framework library. If the framework library lacks the features that developers want to use, it does not matter how good the runtime is, developers will not use it. Of course, there are gaps in the library, and this has allowed third-party library companies to flourish. But before you get out your credit card to buy a library, take a look at what Java can do. Version 1.1 of .NET provided a Java-like language called J#, and Microsoft's intention was to persuade Java developers to move over to .NET by providing a basic version of the JDK implemented as a .NET library. For example, you will find that in the standard .NET library, there are no classes to manipulate ZIP files, but the JDK has classes in the java.util.zip package to read and write ZIP files. Microsoft's version of the JDK can be found in the vjslib.dll assembly and if you use ILDASM, you'll see that there is a java.util.zip namespace that contains the classes in the java.util.zip Java package. Using these classes is straightforward. Here's the code for compressing a file: void Zip(string inStr, string outStr)
{
FileOutputStream fos = new FileOutputStream(outStr);
FileInputStream fis = new FileInputStream(inStr);
ZipOutputStream zos = new ZipOutputStream(fos);
zos.putNextEntry(new ZipEntry(inStr));
try
{
sbyte[] buffer = new sbyte[1024];
int len;
while((len = fis.read(buffer)) >= 0)
{
zos.write(buffer, 0, len);
}
zos.closeEntry();
}
finally
{
if (fis != null) fis.close();
if (zos != null) zos.close();
}
}
This code uses the Java FileInputStream and FileOutputStream
to access the file that contains the data to be zipped and the zip file that
is created. These classes are essentially the read and write methods, respectively,
of the .NET FileStream class. The important class in this code is the
ZipOutputStream, which is constructed from an OutputStream object
(which is the base class of FileOutputStream) and is used to compress
files. ZipOutputStream can add one or more entries into a zip file, and
this code calls putNewEntry and closeEntry to indicate that the
output file will have a single entry.
The java.util.zip namespace has several other useful classes: the ZipInputStream
allows you to read the contents from a ZIP file, GZIPInputStream and
GZIPOutputStream are used to read and write GZIP files, and Deflator
and Inflator are used to read and write ZLIB data. Furthermore, you may
have a requirement to generate a cyclic redundancy count (CRC) to check whether
data has been corrupted. The java.util.zip namespace provides the CRC32 class
to allow you to generate a CRC as you read or write the data:
long GetCRC(byte[] data)
{
CRC32 crc = new CRC32();
crc.update(data);
return crc.getValue();
}
If you want to calculate the CRC while reading from or writing to a stream, you can use the CheckedInputStream and CheckedOutputStream classes. Bear in mind that if you use these classes, you have to have a reference to the vjslib.dll assembly. Also, remember that since these classes are JDK classes, they use JDK types as parameters. The previous example shows what I meanthe ZIP classes are based on OutputStream and InputStream objects, not .NET FileStream objects.
Richard Grimes speaks at conferences and writes extensively on .NET, COM, and
COM+. He is the author of Developing Applications with Visual Studio .NET
(Addison-Wesley, 2002). If you have comments about this topic, Richard can be
reached at richard@richardgrimes.com.
|
|
||||||||||||||||||||||||||||
|
|
|
|