Skip navigation

Xceed Zip for .NET

Extend Your Application’s Zipping and Unzipping Capabilities

asp:review

 

Xceed Zip for .NET

Extend Your Application's Zipping and Unzipping Capabilities

 

By Dino Esposito

 

Xceed Zip for .NET, a new class library from Xceed Software Inc., extends the .NET Framework with several types of data-compression functions. Xceed Zip gives any .NET application flexible zipping and unzipping capabilities as well as data-streaming and data-compressing features. Furthermore, a rather innovative file-system object model enables you to access zipped files as easily as you can access other files and folders. The library contains three groups of classes: classes to zip and unzip; classes to access file-system elements transparently, irrespective of their compression status; and a set of stream classes to provide the ability to serialize and de-serialize data to and from compressed streams.

 

Xceed Zip for .NET is the fifth-generation compression library from Xceed Software. Xceed Zip for .NET has been completely re-architected and redesigned to match the requirements of .NET applications. The library is made up of 100 percent managed code and provides classes to extend existing classes in the framework. From a programmer's perspective, the library is easy to use. Key operations such as zipping and unzipping require only one or two lines of code. For more complex tasks, Xceed's classes closely mirror the style of the .NET Framework's base classes and thus should be familiar to most .NET developers.

 

The Library

The library contains three namespaces, each corresponding to a distinct area of functionality:

  • Xceed.Zip
  • Xceed.FileSystem
  • Xceed.Compression

 

The Xceed.Zip namespace exposes the simple yet powerful QuickZip class, which provides static methods to zip and unzip files and folders. Zipping a file is a piece of cake, as the following Visual Basic code snippet demonstrates:

 

Imports Xceed.Zip

QuickZip.Zip ("c:\test.zip", "c:\temp\*.*")

 

Unzipping is also incredibly easy. You call the Unzip static method and pass it the name of the ZIP file to process, the destination folder, and the list of the file names to unzip and extract from there. Existing files will be overwritten, and any directory structure defined in the zip file will be restored in the destination folder. In case you need to accomplish operations that are more complex, such as adding files to a zipped subfolder, or you need to check whether a file exists within the zip, you can use the ZippedFolder and ZippedFile classes rather than QuickZip.

 

The object model available in the Xceed.FileSystem namespace (see FIGURE 1) represents zipped folders and files more or less as they are rendered in the .NET Framework. Actually, the .NET Framework does not have classes like Folder and File, but it still provides access to folder and file information through classes such as DirectoryInfo and FileInfo.

 


FIGURE 1: The classes of the Xceed.FileSystem namespace. The zipped classes occur as a special case of more general classes managing files and folders. Interestingly, File and Folder classes return their unzipped content by using the same programming interface.

 

Xceed's base classes DiskFolder and DiskFile represent a directory and a file, respectively. But, as soon as you replace those classes with the derived classes ZippedFolder and ZippedFile, the same directory and file become compressed. One of the sample applications, MiniExplorer, makes it particularly evident how interchangeable normal and zipped files are when you're using the Xceed class library (see FIGURE 2). The sample demonstrates how to navigate within the file system in an Explorer-like fashion, considering each zip file as though it's a directory you can expand to show the contents.

 


FIGURE 2: You can use the MiniExplorer sample application to navigate zip files. This sample application illustrates the use of File and Folder classes and allows you to visit zipped files by using a folder-like system.

 

Compressed Streams

The CompressedStream class acts as a wrapper class around any of the .NET predefined stream classes. The constructor of CompressedStream takes as its input an instance of a valid Stream object or any other class that inherits from Stream. Next, the class pre- and post-processes the data so that the data is compressed when it's stored and is decompressed when it's read back. The CompressedStream class constitutes the main interface between the caller application and the storage medium where data actually is stored. The CompressedStream class does not care about the nature of the medium lying behind the stream. The .NET object used to initialize the compressed stream is responsible for maintaining the connection with the physical data source. Using Visual Basic .NET, the CompressedStream is defined as follows:

 

Public Class CompressedStream

 Inherits Stream

 Implements IDisposable

 

The class constructor allows you to indicate the stream to work on as well as the compression mode and the level of compression. One of the extra properties CompressedStream exposes is Transient, which indicates whether the compressor stream should close the stream at the end of its activity.

 

The code in FIGURE 3 shows how to set up a compressed stream and read data out of it.

 

Dim sourceStream As New FileStream(in_file, _

 FileMode.Open, FileAccess.Read)

Dim destStream As New FileStream(out_File, _

 FileMode.Create, FileAccess.Write)

 

Dim compStream As New CompressedStream(destStream)

 

' Copy data between input and output streams

 

Try

  Dim bytesRead As Integer

  Dim buffer(32768) As Byte

 

  bytesRead = sourceStream.Read(buffer, 0, buffer.Length)

 

  While (bytesRead > 0)

    compStream.Write(buffer, 0, bytesRead)

    bytesRead = sourceStream.Read(buffer, 0, buffer.Length)

  End While

 

Finally

  sourceStream.Close()

  compStream.Close()

End Try

FIGURE 3: This code demonstrates how to copy data from a source stream to a compressed stream, working on top of the specified output file. The compression takes place transparently under the hood of the CompressedStream object.

 

Xceed has done a wonderful job of designing the classes for Xceed Zip for .NET. They're so good, you should keep them in mind when building your own project-wide, custom classes. For instance, the CompressedStream class is a wrapper class that extends the base class Stream by applying compression and decompression algorithms to incoming and outgoing data. In practice, the class does little more than just zipping while serializing and unzipping while de-serializing, but these operations are made transparent to the programmer and are buried under the familiar and common stream-based API.

 

Setup and Licensing

The setup for Xceed Zip for .NET requires Microsoft Windows Installer 2.0. This is not a very strict requirement because .NET requires Windows Installer and will install it if necessary. Deploying the library means three DLLs will be copied onto your machine, one for each namespace. Assemblies automatically are copied into the Global Assembly Cache, and there is no need to reboot the machine to make the code work. Just launch Visual Studio .NET and try the code. If you use any of the sample applications (available both in C# and VB), you'll initially have an error because the sample folders have no files with the extension sln. However, just clicking OK in the dialog box will cause Visual Studio .NET to create a solution file and fix everything.

 

You need a license to use Xceed classes. By downloading the library code (a trial edition is available), you automatically get a free 20-day license. After that, you will need to buy the product.

 

The product is simple to use and is effective. It instantly can solve any problem with data compression you may have. The documentation is clear enough and provides links to the official Microsoft .NET documentation. The base price for the library is US$299.95, but registered users of other Xceed products receive a significant discount.

 

Dino Esposito is a trainer and consultant for Wintellect (http:// www.wintellect.com), where he manages the ADO.NET class. Dino writes a monthly column for asp.netPRO. The author of Building Web Solutions with ASP.NET and ADO.NET from Microsoft Press, Dino is also the cofounder of http://www.VB2TheMax.com. Write to him at mailto:[email protected].

 

asp:factfile

Xceed Zip for .NET is a general-purpose product that makes it easy to integrate zip and unzip functions into any .NET application.

 

Xceed Software Inc.

1555 Boul. Jean-Paul-Vincent, Suite 180

Lonqueuil, Quebec

Canada J4N 1L6

 

Phone: (450) 442-2626 or (800) 865-2626

E-Mail: mailto:[email protected]

Web Site: http://www.xceedsoft.com

Price: US$299.95 for a single developer license; US$599.95 for a four-developer license; US$899.95 for an eight-developer license; and US$1,499.95 for a site license.

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish