Rebex ZIP

ZIP and GZIP compression .NET library

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

Back to feature list...

Directory operations

Listing ZIP archive content (recursively) 

To get listing of ZIP archive contents, use GetItems method.

ZipItemCollection items;

// get all items in ZIP archive
items = zip.GetItems();

// get all ".txt" files of "/data" directory only
// (files from subdirectories are not returned)
items = zip.GetItems("/data/*.txt", TraversalMode.MatchFilesShallow);

// get all ".txt" files under "/data" directory
// (files from subdirectories are returned as well)
items = zip.GetItems("/data/*.txt", TraversalMode.MatchFilesDeep);

// initialize a file set
var fileSet = new Rebex.IO.FileSet("/data");
fileSet.Include("Web");
fileSet.Exclude("Web/Images");

// get all items defined by the file set
items = zip.GetItems(fileSet);

Tip: Check out more information on wildcards, traversal modes and file sets.

Listing directory content (non-recursively) 

To get listing of single directory contents, use GetItems method with "*" wildcard and TraversalMode.NonRecursive mode.

// get all items in the root of the ZIP archive
ZipItemCollection rootItems = zip.GetItems("*", TraversalMode.NonRecursive);

// get all items in "/data" directory
ZipItemCollection dataItems = zip.GetItems("/data/*", TraversalMode.NonRecursive);

Sorting content listings 

By default, GetItems method lists directory items in the original order (as present in the ZIP archive), which is often unsorted. To sort the results, use ZipItemCollection's Sort method and FileSystemItemComparer object.

// get all items in the ZIP archive
ZipItemCollection items = zip.GetItems();

// example 1: sort the collection by name (directories first)
items.Sort();

// example 2: large files first
items.Sort(new FileSystemItemComparer(FileSystemItemComparerType.Length));

// example 3: order by multiple fields
// (directories first, then order by time of last modification)
items.Sort(new MultiComparer(
        new FileSystemItemComparer(FileSystemItemComparerType.FileType),
        new FileSystemItemComparer(FileSystemItemComparerType.LastWriteTime)));

For more sorting options, see FileSystemItemComparerType enum.

LINQ support - IEnumerable<T> collection 

The ZipItemCollection object returned by GetItems method implements IEnumerable<ZipItem> to support LINQ queries.

Note: ZipItemCollection implements IEnumerable<ArchiveItem> as well, which means that casting it to IEnumerable<ZipItem> using Cast<ZipItem> might be necessary in order to use proper LINQ extension methods.

DateTime dt = DateTime.Now.Date.AddDays(-7);

// get all ".txt" files not older than 7 days sorted by path
// from current directory using LINQ methods
var files1 =
    zip.GetItems("*.txt", TraversalMode.Recursive).Cast<ZipItem>().
    Where(item => item.IsFile && item.LastWriteTime > dt).
    OrderBy(item => item.Path);

// get all ".txt" files not older than 7 days sorted by path
// anywhere under the current directory using a LINQ query
var items = zip.GetItems("*.txt", TraversalMode.MatchFilesDeep);
var files2 =
    from ZipItem item in items
    where item.IsFile && item.LastWriteTime > dt
    orderby item.Path
    select item;

Displaying listing progress 

The GetItems method raises a ProgressChanged event when a directory is processed (ArchiveOperationStep.DirectoryRetrieved). This is useful when working with huge directory listings because it makes it possible to display progress as they are listed.

Custom listing progress example:

// register an event handler which is raised
// when a directory listing is in progress
zip.ProgressChanged += (s, e) =>
{
    // display directory listing progress information
    if (e.OperationStep == ArchiveOperationStep.DirectoryRetrieved)
    {
        Console.WriteLine("Processed directory: {0}", e.ArchiveItemPath);
        Console.WriteLine(" - total items so far: {0}", e.FilesTotal);
    }
};

// retrieve specified files and display listing progress
var items = zip.GetItems("*.txt", TraversalMode.MatchFilesDeep);

Creating a directory 

To create a new directory in a ZIP archive, use CreateDirectory method. CreateDirectory method returns a ZipItem object that represents the newly-created directory.

// create a new directory in the ZIP archive
ZipItem newDir = zip.CreateDirectory("/data/Exports");

// access item info if needed
Console.WriteLine("New directory created: {0}", newDir.Path);

Deleting a directory 

To delete an empty ZIP archive directory, use Delete method.

// delete ZIP archive directory
// delete directory if it is empty
if (zip["/data/Exports"].IsEmptyDirectory)
{
    zip.Delete("/data/Exports", TraversalMode.NonRecursive);
}

Deleting directories recursively 

To delete non-empty ZIP archive directories or multiple files, pass TraversalMode.Recursive to the Delete method.

// delete whole ZIP archive directory
// (deletes the "/data/temp" directory as well)
zip.Delete("/data/temp", TraversalMode.Recursive);

// delete all ".txt" files in ZIP archive's "/data"
zip.Delete("/data/*.txt", TraversalMode.MatchFilesShallow);

// delete contents of ZIP archive's "/data" directory
// (keeps the empty "/data" directory)
zip.Delete("/data/*", TraversalMode.Recursive);

Renaming/moving a directory 

To rename or move a directory (or a file) within a ZIP archive, use Move method.

// rename a directory
zip["/data/Subdir1"].Rename("Renamed");

// move a directory
zip.Move("/data/Subdir2", "/Moved/Subdir2");

// move a directory and rename it as well
zip.Move("/data/Subdir3", "/Moved/Renamed");

Tip: Rebex ZIP makes it possible to move files and directories between a ZIP archive and the file system as well.

Checking directory existence 

To check whether a particular ZIP archive directory already exists, use DirectoryExists method.

// determine whether particular directories are in ZIP archive
bool hasExports = zip.DirectoryExists("/data/Exports");
bool hasTemp = zip.DirectoryExists("/Temp");

Back to feature list...