Project Description

Small C# class to store and extract uncompressed and deflated files in new or existing Zip files, without any external library.
Works with .net 2.0 and up, and Compact Framework.

Introduction

There are many techniques to produce Zip files in a .NET 2.0 environment, like the following:
  • Using the java.util.zip namespace
  • Invoking Shell API features
  • Using a third-party .NET library
  • Wrapping and marshalling a non-.NET library
  • Invoking a compression tool at command-line

I have tested most of them, each one has pros and cons, but sometimes it is needed just a tiny library to store files in a Zip with basic compression or plain storing. I built my own minimalistic class to create Zip files and store/retrieve files to/from it, firstly with uncompressed storing capabilities and now with Deflate algorithm, no other compression methods supported.
Moreover, notice that the new .NET 3.0 and 3.5 Frameworks come with the ZipPackage class, but it is not available in .NET 2.0 Framework or Compact Framework (any version). The restriction of ZipPackage is that you cannot avoid to generate an extra file inside named [Content_Types].xml.

Background

The following diagram depicts a Zip file structure; it looks a bit redundant because of its double directory approach (local and central). This is because it is designed to support creation in a sequential-access-only device.

Zip_Structure.png
The contents of each section can vary depending on the Operating System and hardware platform.

Using the Code

The ZipStorer class is the unique one needed to create the zip file. It contains a nested structure (ZipFileEntry) to collect each directory entry. The class has been declared inside the System.IO namespace. The following diagram describes all the ZipStorer class members:

Class_Diagram.png

There is no default constructor. There are two ways to construct a new ZipStorer instance, depending on specific needs: use either Create() or Open() static methods. To create a new Zip file, use the Create() method like this:

ZipStorer zip = ZipStorer.Create(filename, comment); // file-oriented version
ZipStorer zip = ZipStorer.Create(stream, comment); // stream-oriented version
It is required to specify the full path for the new zip file, or pass a valid stream, and optionally add a comment. To open an existing zip file for appending, the Open() method is required, like the following:

ZipStorer zip = ZipStorer.Open(filename, fileaccess);  // file-oriented version
ZipStorer zip = ZipStorer.Open(stream, fileaccess);  // stream-oriented version

Where fileaccess should be of type System.IO.FileAccess enumeration type. Also, as now ZipStorer is derived from IDisposable interface, the using keyword can be used to ensure proper disposing of the storage resource:

using (ZipStorer zip = ZipStorer.Create(filename, comment))
{
    // some operations with zip object
    //
} // automatic close operation here

To add files into an opened zip storage, there are two available methods:

public void AddFile(ZipStorer.Compress _method, string _pathname, string _filenameInZip, string _comment);
public void AddStream(ZipStorer.Compress _method, string _filenameInZip, Stream _source, DateTime _modTime, string _comment);
The first method allows to add an existing file to the storage. The first argument receives the compression method; it can be Store or Deflate enum values. The second argument admits the physical path name, the third one allows to change the path or file name to be stored in the Zip, and the last argument inserts a comment in the storage. Notice that the folder path in the _pathname argument is not saved in the Zip file. Use the _filenameInZip argument instead to specify the folder path and filename. It can be expressed with both slashes or backslashes.
The second method allows to add data from any kind of stream object derived from the System.IO.Stream class. Internally, the first method opens a FileStream and calls the second method.
Finally, it is required to close the storage with the Close() method. This will save the central directory information too.

Sample Application

Zip_Storer_new.png

The provided sample application will ask for files and store the path names in a ListBox, along with the operation type: creating or appending, and compression method. Once the Proceed button is pressed, the following code snippet will be executed:

ZipStorer zip;

if (this.RadioCreate.Checked)
    // Creates a new zip file
    zip = ZipStorer.Create(TextStorage.Text, "Generated by ZipStorer class");
else
    // Creates a new zip file
    zip = ZipStorer.Open(TextStorage.Text, FileAccess.Write);

// Stores all the files into the zip file
foreach (string path in listBox1.Items)
{
    zip.AddFile(this.checkCompress.Checked ? ZipStorer.Compression.Deflate : ZipStorer.Compression.Store, 
         path, Path.GetFileName(path), "");
}

// Creates a memory stream with text
MemoryStream readme = new MemoryStream(
System.Text.Encoding.UTF8.GetBytes(
    string.Format("{0}\r\nThis file has been {1} using the ZipStorer class, by Jaime Olivares.",
    DateTime.Now, this.RadioCreate.Checked ? "created" : "appended")));

// Stores a new file directly from the stream
zip.AddStream(ZipStorer.Compression.Store, "readme.txt", readme, DateTime.Now, "Please read");
readme.Close();

// Updates and closes the zip file
zip.Close();

This code snippet shows how to add both physical files and a little readme text from a memory stream.
Notice that the sample has been produced with Visual Studio 2008. The solution cannot be loaded directly with Visual Studio 2005, but a new solution can be created and the project file attached to it without problems.

Extracting Stored Files

To extract a file, the zip directory shall be read first , by using the ReadCentralDir() method, and then the ExtractStoredFile() method, like in the following minimal sample code:

// Open an existing zip file for reading
ZipStorer zip = ZipStorer.Open(@"c:\data\sample.zip", FileAccesss.Read);

// Read the central directory collection
List<ZipStorer.ZipFileEntry> dir = zip.ReadCentralDir();

// Look for the desired file
foreach (ZipStorer.ZipFileEntry entry in dir)
{
    if (Path.GetFileName(entry.FilenameInZip) == "sample.jpg")
    {
        // File found, extract it
        zip.ExtractStoredFile(entry, @"c:\data\sample.jpg");
        break;
    }
}
zip.Close();

Removal of Entries

Removal of entries in a zip file is a resource-consuming task. The simplest way is to copy all non-removed files into a new zip storage. The RemoveEntries() static method will do this exactly and will construct the ZipStorer object again. For the sake of efficiency, RemoveEntries() will accept many entry references in a single call, as in the following example:

List<ZipStorer.ZipFileEntry> removeList = new List<ZipStorer.ZipFileEntry>();

foreach (object sel in listBox4.SelectedItems)
{
    removeList.Add((ZipStorer.ZipFileEntry)sel);
}

ZipStorer.RemoveEntries(ref zip, removeList);

Files or streams?

The current release of ZipStorer supports both files and streams for creating and opening a zip storage. Several methods are overloaded for this dual support. The advantage of file-oriented methods is simplicity, since those methods will open or create files internally. On the other hand, stream-oriented methods are more flexible by allowing to manage zip storages in streams different than files. File-oriented methods will invoke internally to equivalent stream-oriented methods.
Notice that not all streams will apply, because the library requires the streams to be randomly accessed (CanSeek = true). The RemoveEntries method will work only if the zip storage is a file.

// File-oriented methods:
        public static ZipStorer Create(string _filename, string _comment)
        public static ZipStorer Open(string _filename, FileAccess _access)
        public void AddFile(Compression _method, string _pathname, string _filenameInZip, string _comment)
        public bool ExtractFile(ZipFileEntry _zfe, string _filename)
        public static bool RemoveEntries(ref ZipStorer _zip, List<ZipFileEntry> _zfes)  // No stream-oriented equivalent

// Stream-oriented methods:
        public static ZipStorer Create(Stream _stream, string _comment)
        public static ZipStorer Open(Stream _stream, FileAccess _access)
        public void AddStream(Compression _method, string _filenameInZip, Stream _source, DateTime _modTime, string _comment)
        public bool ExtractFile(ZipFileEntry _zfe, Stream _stream)

Filename encoding

Traditionally, the ZIP format supported DOS encoding system (a.k.a IBM Code Page 437) for filenames in header records, which is a serious limitation for using non-occidental and even some occidental characters. Since 2007, the ZIP format specification was improved to support Unicode's UTF-8 encoding system.
ZipStorer class detects UTF-8 encoding by reading the proper flag in each file's header information. To enforce filenames to be encoded with UTF-8 system, set the EncodeUTF8 member of ZipStorer class to true. All new filenames added will be encoded with UTF8. Notice this doesn't affect stored file contents at all. Also be aware that Windows Explorer's embedded Zip format facility does not recognize well the UTF-8 encoding system, like it does WinZip or WinRAR.

Compatibility with ePUB & OCF

The ZipStorer library has been adjusted to comply with Open Container Format Specification (OCF), one of the standards required to produce ePUB Digital Books. There are some specific requirements to fulfill the OCF specification:
  • The storage shall have the .epub extension instead of .zip
  • The first file in storage must be non-compressed and shall be called mimetypes, containing the string application/epub+zip
  • Do not use comments in zip file entries or zip storage
  • The filenames shall be encoded in UTF8. Set the storage field EncodeUTF8 to true

Advantages and Usage

ZipStorer has the following advantages:
  • It is a short and monolithic C# class that can be embedded as source code in any project (1 source file of 33KB, 700+ lines)
  • No external libraries, no extra DLLs in application deployments
  • No Interop calls, increments portability, maybe to Mono
  • Can also be implemented with .NET Compact Framework 3.5
  • Fast storing and extracting, because the code is simple and short
  • UTF-8 Encoding support and ePUB format compatibility

Old ZipStorer with support for Silverlight

Previous version of ZipStorer (v1.0) doesn't compress, just stores, but it works with Silverlight, while version 2.0 and up won't work because Silverlight framework doesn't support the DeflateStream class. If you want to download it, go to ZipStorer 1.01

Disclaimer

The code/library supplied is totally free for either personal or commercial use, with no warranties.
Please use the "Powered by ZipStorer" logo (below) wherever is possible, pointing to this site.

Editor's award Powered by ZipStorer ePUB Compatible

Last edited Oct 5, 2010 at 2:34 AM by jaime_olivares, version 31