Xceed Zip for .NET

Extend Your Application’s Zipping and Unzipping Capabilities

Dino Esposito

October 30, 2009

6 Min Read
ITPro Today logo in a gray background | ITPro Today

asp:review

 

Xceed Zipfor .NET

ExtendYour Application's Zipping and Unzipping Capabilities

 

By DinoEsposito

 

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

 

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

 

The Library

Thelibrary contains three namespaces, each corresponding to a distinct area offunctionality:

  • Xceed.Zip

  • Xceed.FileSystem

  • Xceed.Compression

 

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

 

Imports Xceed.Zip

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

 

Unzippingis also incredibly easy. You call the Unzipstatic method and pass it the name of the ZIP file to process, the destinationfolder, and the list of the file names to unzip and extract from there.Existing files will be overwritten, and any directory structure defined in thezip file will be restored in the destination folder. In case you need toaccomplish operations that are more complex, such as adding files to a zippedsubfolder, or you need to check whether a file exists within the zip, you canuse the ZippedFolder and ZippedFile classes rather than QuickZip.

 

Theobject model available in the Xceed.FileSystemnamespace (see FIGURE 1) represents zipped folders and files more or less asthey are rendered in the .NET Framework. Actually, the .NET Framework does nothave classes like Folder and File, but it still provides access to folder and file informationthrough classes such as DirectoryInfoand FileInfo.

 


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

 

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

 


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

 

Compressed Streams

The CompressedStream class acts as awrapper class around any of the .NET predefined stream classes. The constructorof CompressedStream takes as itsinput an instance of a valid Streamobject or any other class that inherits from Stream. Next, the class pre- and post-processes the data so thatthe data is compressed when it's stored and is decompressed when it's readback. The CompressedStream classconstitutes the main interface between the caller application and the storagemedium where data actually is stored. The CompressedStreamclass does not care about the nature of the medium lying behind the stream. The.NET object used to initialize the compressed stream is responsible formaintaining the connection with the physical data source. Using Visual Basic.NET, the CompressedStream isdefined as follows:

 

Public Class CompressedStream

 Inherits Stream

 Implements IDisposable

 

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

 

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

 

Dim sourceStream As New FileStream(in_file, _

 FileMode.Open, FileAccess.Read)

Dim destStreamAs 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

FIGURE3: This codedemonstrates how to copy data from a source stream to a compressed stream, workingon top of the specified output file. The compression takes place transparentlyunder the hood of the CompressedStream object.

 

Xceedhas 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 ownproject-wide, custom classes. For instance, the CompressedStream class is a wrapper class that extends the baseclass Stream by applying compressionand decompression algorithms to incoming and outgoing data. In practice, theclass does little more than just zipping while serializing and unzipping whilede-serializing, but these operations are made transparent to the programmer andare buried under the familiar and common stream-based API.

 

Setup and Licensing

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

 

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

 

Theproduct is simple to use and is effective. It instantly can solve any problemwith data compression you may have. The documentation is clear enough andprovides links to the official Microsoft .NET documentation. The base price forthe library is US$299.95, but registered users of other Xceed products receivea significant discount.

 

Dino Espositois 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 alsothe cofounder of http://www.VB2TheMax.com.Write to him at mailto:[email protected].

 

asp:factfile

XceedZip for .NET is a general-purpose product that makes it easy to integrate zipand unzip functions into any .NET application.

 

Xceed Software Inc.

1555 Boul. Jean-Paul-Vincent, Suite 180

Lonqueuil,Quebec

CanadaJ4N 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 developerlicense; US$599.95 for a four-developer license; US$899.95 for an eight-developerlicense; and US$1,499.95 for a site license.

 

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like