Rebex ZIP

ZIP and GZIP compression .NET library

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

Back to feature list...

ZipItem class

ZipItem properties 

To access a particular file (or other item) stored within a ZIP archive, use the ZipItem class. It provides various information about the item:

// open a ZIP archive
var zip = new ZipArchive(path, ArchiveOpenMode.Open);

// get instance of "/data/file1.txt" ZIP item
ZipItem item = zip["/data/file1.txt"];

// display general info
Console.WriteLine("Path: {0} (name: {1})", item.Path, item.Name);
Console.WriteLine("Length: {0} B (compressed: {1} B)", item.Length, item.CompressedLength);
Console.WriteLine("Comment: {0}", item.Comment);
Console.WriteLine("Compression method: {0}", item.CompressionMethod);
if (item.IsEncrypted)
{
    Console.WriteLine("Encryption method: {0}", item.EncryptionAlgorithm);
}
Console.WriteLine("CRC-32: {0:X8}", item.Crc32);
Console.WriteLine("Already deleted: {0}", item.IsDeleted);
Console.WriteLine("Can be extracted: {0}", item.CanExtract);

// display type info
Console.WriteLine("Type: {0}", item.ItemType);
Console.WriteLine("  Is file: {0}", item.IsFile);
Console.WriteLine("  Is directory: {0}", item.IsDirectory);
Console.WriteLine("  Is root: {0}", item.IsRootDirectory);
Console.WriteLine("  Is empty: {0}", item.IsEmptyDirectory);
Console.WriteLine("  Is link: {0} (target: '{1}')", item.IsLink, item.LinkTarget);

// display time info
Console.WriteLine("Accessed: {0}", item.LastAccessTime);
Console.WriteLine("Modified: {0}", item.LastWriteTime);
Console.WriteLine("Created: {0}", item.CreationTime);

// display UNIX permissions
if (item.UnixPermissions.HasValue)
{
    Console.WriteLine("UNIX Permissions: {0}", item.UnixPermissions);
    Console.WriteLine("UNIX Group ID: {0}", item.UnixGroupId);
    Console.WriteLine("UNIX Owner ID: {0}", item.UnixOwnerId);
}

Tip: Comment property and all DateTime properties can also be used to update respective ZIP archive file metadata.

Extracting to anywhere 

ZipItem data can be accessed using many different ways:

// get instance of "/data/file1.txt" ZIP item
ZipItem item = zip["/data/file1.txt"];

// extract the file into a byte array
// (could cause OutOfMemoryException if the file was too long)
byte[] byteArrayFile = item.ExtractToArray();

// extract the file into a directory
item.ExtractToDirectory(@"C:\MyData\Out");

// extract the file to a different file name
item.ExtractToFile(@"C:\MyData\Out\archived.txt", ActionOnExistingFiles.OverwriteAll);

// extract the file to a stream
using (var writer = new StreamWriter(stream))
{
    // write header
    writer.WriteLine("Content of '{0}' follows:", item.Path);
    writer.Flush();

    // write data
    item.ExtractToStream(writer.BaseStream);
}

Stream-based data access 

To access decompressed data as a .NET Stream, use the ZipItem.Open() method. This will provide a readable and non-seekable stream from which you can read the data.

// get instance of "/data/file1.txt" ZIP item
ZipItem item = zip["/data/file1.txt"];

// construct a stream reader from the ZIP item
// (suitable for text files; use a different approach for binary files)
using (var reader = new StreamReader(item.Open()))
{
    // read all data
    string content = reader.ReadToEnd();

    // display the data
    Console.WriteLine("Content of '{0}' follows:", item.Path);
    Console.WriteLine(content);
}
Note: While the stream is open, the parent ZipArchive object is locked and cannot be manipulated until the ZipItem's stream is closed.

Back to feature list...