SmartZip Archive File Library – Creating and Using Archive Files - Other Formats
(Page 4 of 4 )
As I said above, the SmartZip library supports writing to other formats. These are GZip, BZip2, and Tar. For GZip, there are only the two GZipInputStream and GZipOutputStream classes for writing out to and reading in from streams. For BZip2, the actual BZip2 class can be used to compress one stream into another or vice versa; it really does nothing else, unlike the ZipFile class mentioned above. Otherwise, both BZip2 and GZip have only the two input and output stream classes.
The Tar implementation works a little bit differently from the other namespaces. When you create a TarArchive class, class is created for reading or writing only; you are not allowed to do both on the same object. In order to do this, you should use the public static factory methods called CreateInputTarArchive and CreateOutputTarArchive. Both of these methods take a stream as a parameter. In general, you can give CreateInputTarArchive any type of stream that contains Tar formatted data. This can be from a file, network, or anywhere else from where you can get a stream.
If you wish to extract the contents of a Tar file, assuming you’ve created a TarArchive object from the CreateInputTarArchive method, you then simply use the ExtractContents method to dump the entire TarArchive to a directory. This directory is represented by a string given to the ExtractContents method as a parameter. If you wish to write TarEntrys to an archive, assuming you’ve created a TarArchive object using the CreateOutputTarArchive method, you can use the WriteEntry method to add new entries to the archive.
In order to use the WriteEntry method, you must create TarEntry objects. These can be instantiated in one of two ways. You instantiate either by passing the constructor an array of bytes that constitutes the actual Tar header, or by passing the constructor a TarHeader object, which contains the same information, but in a more easily accessible manner.
When you instantiate a TarHeader object, it is created with the default settings for all of the many fields available. Many of these fields are read-only or constant, and as such don’t often concern the programmer. Some fields you will want to set are: name, size, and version. It is important to set the size field correctly, as the write method on a TarOutputStream (which TarArchive uses) will throw an exception if you attempt to write data beyond the header’s size parameter. However, to get around all of this confusing class hierarchy, it is possible to use the public static factory function CreateEntryFromFile to automatically create the TarEntry and appropriate header information given just a string representing the filename from which the object is to be created.
Both of the above processes can be accomplished using the TarInputStream and TarOutputStream objects as well, using much the same process as was discussed earlier with Zip files. Of course, after you’ve used the Tar classes to create a single file from many files, you can then run that file through the GzipOutputStream to compress it.
Conclusion
In general, these classes make creating and working with archive files easy and intuitive. With just a few class instantiations and method calls, you can easily create and open Zip archives. This could potentially make sending large amounts of data over a network much simpler and faster, if you are writing a network or Web application. You can find the library at www.icsharpcode.com.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |