Available for: UI for ASP.NET MVC | UI for ASP.NET AJAX | UI for WPF | UI for WinForms

New to Telerik Document Processing? Download free 30-day trial

ZipArchive Entry

ZipArchiveEntry represents every single compressed file within a zip archive.

What Is ZipArchiveEntry

The root element of the RadZipLibrary is the ZipArchive. The ZipArchive, on the other hand, consists of ZipArchiveEntries hosting all entries in the zip archive. You can Get ZipArchiveEntry or Create ZipArchiveEntry using the ZipArchive instance.

Properties

  • CompressedLength: Gets the compressed size of the entry in the zip archive.

    The archive compressed length can be obtained only after the ZipArchiveEntry is disposed of, so in the following example, we are ensuring the correct disposing of the object by using statement.

    Example 1: Get entry's compressed length

        ZipArchiveEntry entry; 
        using (entry = archive.CreateEntry("file.txt")) 
        { 
            StreamWriter writer = new StreamWriter(entry.Open()); 
            writer.WriteLine("Progress!"); 
            writer.Flush(); 
        } 
     
        long compressedLength = entry.CompressedLength; 
    
  • ExternalAttributes: Gets or sets external file attributes.

    Example 2: Set entry's external attributes

        entry.ExternalAttributes = (int)File.GetAttributes(sourceFileName); 
    
  • FullName: Gets the relative path of the entry in the zip archive.

    Example 3: Get entry's relative path

        string fullName = entry.FullName; 
    
  • LastWriteTime: Gets or sets the last time the entry in the zip archive was changed.

    Example 4: Set last entry's write time

        DateTime lastWriteTime = File.GetLastWriteTime(sourceFileName); 
        entry.LastWriteTime = lastWriteTime; 
    
  • Length: Gets the uncompressed size of the entry in the zip archive.

    The archive length can be obtained only after the ZipArchiveEntry is disposed of, so in the following example, we are ensuring the correct disposing of the object by using statement.

    Example 5: Get entry's length

        ZipArchiveEntry entry; 
        using (entry = archive.CreateEntry("file.txt")) 
        { 
            StreamWriter writer = new StreamWriter(entry.Open()); 
            writer.WriteLine("Progress!"); 
            writer.Flush(); 
        } 
     
        long length = entry.CompressedLength; 
    
  • Name: Gets the file name of the entry in the zip archive.

    Example 6: Get entry's name

        string name = entry.Name; 
    

Methods

  • Delete: Deletes the entry from the zip archive.
  • Dispose: Releases the resources used by the current instance of the ZipArchiveEntry class.
  • Open: Opens the entry from the zip archive.

Events

  • PropertyChanged: Occurs when a property value changes.

ZipArchive and ZipArchiveEntry

There are several operations, which you can execute over a ZipArchive instance.

Get ZipArchiveEntry

Retrieves a wrapper for the specified entry in the zip archive.

Example 7: Get ZipArchiveEntry

ZipArchiveEntry entry = archive.GetEntry("file.txt"); 

Create ZipArchiveEntry

Creates an empty entry that has the specified path and entry name in the zip archive.

Example 8: Create ZipArchiveEntry

ZipArchiveEntry entry = archive.CreateEntry("file.txt"); 
 
// OR 
 
ZipArchiveEntry entry = archive.CreateEntry("file.txt", compressionSettings); 

Extract ZipArchiveEntry

Extract an entry to a specific folder/directory.

Example 9: Extracting a ZipArchiveEntry to folder/directory

ZipArchiveEntry entry = archive.GetEntry("file.txt"); 
string path = Path.Combine(RootDirectory, entry.FullName); 
 
using (Stream fileStream = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None)) 
{ 
    using (Stream entryStream = entry.Open()) 
    { 
        entryStream.CopyTo(fileStream); 
    } 
}    

This functionality could be achieved by using the Zip Extensions' ExtractToFile method as well.

Operating with ZipArchiveEntry

Specific examples of using the ZipArchiveEntry.

Using ZipArchiveEntry Properties

A complete example including all the properties discussed above.

Example 10: Complete example

string[] files = Directory.GetFiles("SampleFiles"); 
string zipFileName = "ZipArchive.zip"; 
 
using (Stream stream = File.Open(zipFileName, FileMode.Create)) 
{ 
    using (ZipArchive archive = new ZipArchive.Create(stream, null)) 
    { 
        foreach (string file in files) 
        { 
            string sourceFileName = file; 
            string fileName = file.Split(new string[] { "\" }, StringSplitOptions.None).Last(); 
 
            ZipArchiveEntry entry; 
            using (entry = archive.CreateEntry(fileName)) 
            { 
                using (Stream fileStream = File.Open(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read)) 
                { 
                    // Setting the ExternalAttributes property 
                    entry.ExternalAttributes = (int)File.GetAttributes(sourceFileName); 
 
                    DateTime lastWriteTime = File.GetLastWriteTime(sourceFileName); 
                    // Setting the LastWriteTime property 
                    entry.LastWriteTime = lastWriteTime; 
 
                    // Getting the FullName property 
                    string fullName = entry.FullName; 
 
                    // Getting the Name property 
                    string name = entry.Name; 
 
                    using (Stream entryStream = entry.Open()) 
                    { 
                        fileStream.CopyTo(entryStream); 
                    } 
                } 
            } 
 
            // Getting the CompressedLength property 
            long compressedLength = entry.CompressedLength; 
 
            // Getting the Length property 
            long length = entry.Length; 
        } 
    } 
} 

Working with Folders/Directories

Example 11: Creating folders/directories

ZipArchiveEntry entry = archive.CreateEntry("Documents/Sample/"); 

Example 12: Opening Files in folders/directories

ZipArchiveEntry entry = archive.GetEntry("Documents/Sample/file.txt"); 

See Also

In this article