Telerik blogs

Have you ever wondered how to increase the responsiveness of your applications by compressing the data that you send over the internet? The Q2 2011 release of the RadControls for Silverlight and WPF comes with a new RadZipLibrary which allows you to easily compress data like images, docx or pdf files and send them over the wire. Thus, you will achieve fast and secure transactions.

This is a list with short descriptions of the top-of-the-line features of Telerik’s Zip Library:

  • Easy to use API – the Zip Library exposes flexible and easy to use API to provide you with full control over the compressed data
  • Load or create ZIP files – you can load data from existing ZIP files or create and edit ZIPs that can be used by other applications. You can also create ZIP files in memory or add data to ZIP file from stream
  • Store your data in the Isolated Storage – you can store the compressed data in the Isolated Storage
  • Support for large files – the Zip Library works seamlessly with large files

Basically when you want to work with the Telerik ZipLibrary, you need to create a ZipPackage object. In order to do so, you can take advantage of the ZipPackage class static methods that allow you to create or open a zip package:

ZipPackage Class Static Methods

  • Create(Stream stream) – this method is used to create a ZipPackage from a stream. It returns a ZipPackage object.
  • CreateFile(string fileName) – this method is used to create a ZipPackage with a given fileName. It returns a ZipPackage object.
  • IsZipFile(Stream stream) – this method evaluates the file with the given stream to check if it is a zip file.
  • IsZipFile(string fileName) – this method evaluates the file with the given fileName to check if it is a zip file.
  • Open(Stream stream, FileAccess access) – this method is used to open a ZipPackage from a stream. It returns a ZipPackage object.
  • OpenFile(string fileName, FileAccess access) – this method is used to open a ZipPackage with a given file name. It returns a ZipPackage object.

ZipPackage object

When you create a ZipPackage object using the above methods, it populates its ZipPackageEntries collection of ZipPackageEntry elements. You can further manipulate the ZipPackage and its ZipPackageEntries collection using the following methods:

  • Add(string fileName) – this method adds a file with the passed file name in a ZipPackage.
  • Add(string[] fileNames) – this method adds the files from the array of file names in a ZipPackage.
  • Add(string fileName, string fileNameInZip) – this method adds a file with the given fileName in a ZipPackage and associates it with a given fileNameInZip.
  • Add(string fileName, string fileNameInZip, DateTime dateTime) – this method adds a file with the given fileName in a ZipPackage, associates it with a given fileNameInZip and sets a DateTime value for the entry.
  • AddStream(string fileNameInZip, Stream stream) – this method adds a stream in the ZipPackage and associates it with a given fileNameInZip.
  • AddStream(ZipCompression method, string fileNameInZip, Stream stream, DateTime dateTime) – this method adds a stream in a ZipPackage, compresses it with a given compress method, associates it with a given fileNameInZip and sets a DateTime value for the entry.
  • Close(bool shouldCloseStream) – this method closes a ZipPackage. If the shouldCloseStream value is set to false, the method only resets the internal values.
  • IndexOf(string fileNameInZip) – this method gets the index of the entry in the list of entries of a ZipPackage.
  • RemoveEntry(ZipPackageEntry zipPackageEntry) – this method removes a given ZipPackageEntry from a ZipPackage.

If you need to get the file name of a ZipPackage, you can use the ZipPackage object FileName property.

ZipPackageEntry

The ZipPackageEntry class describes each file in the ZipPackage object. It exposes the following properties:

  • Attributes – this property is of type FileAttributes and it gets the attributes that describe the entry.
  • CompressedSize – this property is of type int and it gets the compressed size of the entry.
  • FileNameInZip – this property is of type string and it gets the file name under which the entry is added in the ZipPackage.
  • UncompressedSize – this property is of type int and it gets the uncompressed size of the entry.

If you want to get the stream that represents the ZipPackageEntry, you can take advantage of the ZipPackageEntry.OpenInputStream() method which returns the Stream of the entry.

Telerik Zip Library provides two classes that are designed to compress and decompress streams – ZipOutputStream and ZipInputStream.

Compressing a Stream

The ZipOutputStream class allows you to compress a stream. You can create a compressed stream by initializing a new instance of the ZipOutputStream class and passing the uncompressed stream as a parameter:

ZipOutputStream zipOutputStream = new ZipOutputStream(memoryStream);

While initializing an instance of the ZipOutputStream class you can also pass as a parameter a ZipCompression method:

ZipOutputStream zipOutputStream = new ZipOutputStream(memoryStream, ZipCompression.Deflate64);

The ZipCompression method defines the level of compression to be used while compressing the stream.

ZipOutputStream properties

The ZipOutputStream class derives from the Stream class and therefore it supports all Stream class properties. However, it also exposes a set of properties that provide further information about the compressed stream:

  • BaseStream – gets the stream that is compressed. This property is of type Stream.
  • Checksum – gets the checksum of the compressed stream. This property is of type int.
  • CompressedSize – gets the compressed size of the stream. This property is of type int.
  • UncompressedSize – gets the uncompressed size of the stream. This property is of type int.

ZipOutputStream methods

Since the ZipOutputStream class derives from the Stream class it supports all Stream class methods. It also exposes a Cancel() method that can be used to stop the compression.

Decompressing a Stream

The ZipInputStream class allows you to decompress a stream. In order to decompress a Stream object, you can create a new instance of the ZipInputStream class passing the compressed stream as a parameter:

ZipInputStream inputStream = new ZipInputStream(compressedStream);

ZipInputStream properties

The ZipInputStream class derives from the Stream class and therefore it supports all Stream class properties. However, it also exposes a set of properties that proved further information about the decompressed stream:

  • BaseStream – gets the stream that is decompressed. This property is of type Stream.
  • CompressedSize – gets the compressed size of the stream. This property is of type int.
  • UncompressedSize – gets the uncompressed size of the stream. This property is of type int.

Conclusion

The new RadZipLibrary provides an easy way to manipulate zip files and to compress and decompress streams in your Web or LOB applications, so you can give it a try and feel free to share your feedback in order to make the product better.

Here you can find some examples using the RadZipLibrary


About the Author

Viktor Tsvetkov

Software Developer,
WinCore Team

Related Posts

Comments

Comments are disabled in preview mode.