Rebex ZIP

ZIP and GZIP compression .NET library

Download 30-day free trial Buy from $199
More .NET libraries

Back to feature list...

Single file operations

Single-file compression and decompression 

Use AddFile method to add a single file to a ZIP archive, and ExtractFile method to unpack it.

using (var zip = new ZipArchive(@"C:\MyData\archive.zip"))
{
    // add a file to the root of the ZIP archive
    zip.AddFile(@"C:\MyData\file1.txt");

    // extract a file from the ZIP archive
    // (new name "extracted.txt" is used for extracted file)
    zip.ExtractFile("file1.txt", @"C:\MyData\Out\extracted.txt");
}

An exception is thrown if a file with the same name already exists in the target location. To choose a different behavior, pass a suitable ActionOnExistingFiles argument.

To check whether a file exists in a ZIP archive, use FileExists method. See Checking file existence for details.

Stream-based compression and decompression 

Both AddFile and ExtractFile methods accept an instance of System.IO.Stream class, making it possible to implement stream-based data processing.

For example, .NET's MemoryStream is useful for processing short files in memory without having to create temporary files in the local file system.

Add memory-based file data:

// prepare content in memory
string content = "Some text";
var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));

// add a file from stream
zip.AddFile(stream, "file1.txt");

Extract packed file into a memory stream:

// prepare stream for extracted data
var stream = new MemoryStream();

// extract a file into memory
zip.ExtractFile("file1.txt", stream);

Deleting a file 

To delete a file from a ZIP archive, use DeleteFile method.

// delete a file in ZIP archive
zip.DeleteFile("file1.txt");

Tip: To delete multiple files or non-empty directories, use Delete method.

Renaming or moving a file 

To rename a file (or a directory) in a ZIP archive, use ZipItem object's Rename method. Alternatively, use the Move method which also makes it possible to move file to another directory inside the ZIP archive.

// rename a file
zip["file1.txt"].Rename("renamed.txt");

// move a file
zip.Move("/file2.txt", "/moved/file2.txt");

// move a file and rename it as well
zip.Move("/file3.txt", "/moved/renamed.txt");

Checking file existence 

To check whether a file already exists in a ZIP archive, use FileExists method.

This is useful, for example, when you are about to add a new file, but would like to ask the user whether to overwrite an existing target file first.

// determine whether a particular files are in the ZIP archive
bool hasReadme = zip.FileExists("readme.txt");
bool hasWebDefault = zip.FileExists("/web/default.html");

Getting file length 

To determine a length of a file in a ZIP archive, use ZipItem object's Length property. There is also a CompressedLength property that holds the compressed file length.

// get uncompressed file size (in bytes)
long fileByteSize = zip["file1.txt"].Length;

// get compressed size (in bytes) of a file in the ZIP archive
long compressedByteSize = zip["file1.txt"].CompressedLength;

// display compression ratio
double ratio = 100.0 * compressedByteSize / fileByteSize;
Console.WriteLine("Compressed to {0:0.00}%", ratio);

Getting and setting file date/time 

To get or set dates and times of an item in a ZIP archive, use CreationTime, LastWriteTime and LastAccessTime properties of the ZipItem class.

Getting date and time:

// get information about dates and times of a file
DateTime? lastAccess = zip["file1.txt"].LastAccessTime;
DateTime? lastWrite = zip["file1.txt"].LastWriteTime;
DateTime? creation = zip["file1.txt"].CreationTime;

Setting date and time:

// set dates and times of a file
zip["file1.txt"].CreationTime = DateTime.Now;
zip["file1.txt"].LastWriteTime = DateTime.Now;
zip["file1.txt"].LastAccessTime = DateTime.Now;

Checksums and data integrity 

ZIP format uses CRC-32 checksums to ensure data integrity. A check is performed automatically during extraction of each file. However, it's possible to access the checksum using ZipItem object's Crc32 property.

// get CRC-32 checksum of a file
long checksum = zip["file1.txt"].Crc32;

Tip: To perform CRC-32 check after addition of files as well, set ZipArchive.Options.ChecksumVerificationMode property to ChecksumVerificationMode.AddAndExtract.

Resolving symlinks 

To check if a particular file is a symbolic link, use ZipItem object's IsLink property. To determine the link target, use LinkTarget property.

// check whether a file is a symlink
if (zip["file1.txt"].IsLink)
{
    // get link target
    string target = zip["file1.txt"].LinkTarget;

    // process the link
    // ...
}

Back to feature list...