# ZipArchive Entry

`ZipArchiveEntry` represents each compressed file within a ZIP archive.

## What Is ZipArchiveEntry

The root element of the [RadZipLibrary](https://www.telerik.com/document-processing-libraries/documentation/libraries/radziplibrary/overview) is the [`ZipArchive`](https://docs.telerik.com/devtools/document-processing/api/telerik.windows.zip.ziparchive). The `ZipArchive` consists of `ZipArchiveEntry` objects that host all entries in the ZIP archive. You can [get a ZipArchiveEntry](https://www.telerik.com/document-processing-libraries/documentation/libraries/radziplibrary/features/ziparchive-entry#get-ziparchiveentry) or [create a ZipArchiveEntry](https://www.telerik.com/document-processing-libraries/documentation/libraries/radziplibrary/features/ziparchive-entry#create-ziparchiveentry) by using the `ZipArchive` instance.

### Properties

The following table describes the properties of `ZipArchiveEntry`:

| Property             | Description                                                                                                                       |
| -------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| `CompressedLength`   | Gets the compressed size of the entry in the ZIP archive. This value is available only after the `ZipArchiveEntry` is disposed.   |
| `ExternalAttributes` | Gets or sets external [file attributes](https://learn.microsoft.com/en-us/dotnet/api/system.io.fileattributes).                   |
| `FullName`           | Gets the relative path of the entry in the ZIP archive.                                                                           |
| `LastWriteTime`      | Gets or sets the last time the entry in the ZIP archive was changed.                                                              |
| `Length`             | Gets the uncompressed size of the entry in the ZIP archive. This value is available only after the `ZipArchiveEntry` is disposed. |
| `Name`               | Gets the filename of the entry in the ZIP archive.                                                                                |

**Example 1: Get the compressed length of an entry**

```C#
ZipArchiveEntry entry;
using (entry = archive.CreateEntry("file.txt"))
{
    StreamWriter writer = new StreamWriter(entry.Open());
    writer.WriteLine("Progress!");
    writer.Flush();
}

long compressedLength = entry.CompressedLength;
```

**Example 2: Set external attributes of an entry**

```C#
entry.ExternalAttributes = (int)File.GetAttributes(sourceFileName);
```

**Example 3: Get the relative path of an entry**

```C#
string fullName = entry.FullName;
```

**Example 4: Set the last write time of an entry**

```C#
DateTime lastWriteTime = File.GetLastWriteTime(sourceFileName);
entry.LastWriteTime = lastWriteTime;
```

**Example 5: Get the length of an entry**

```C#
ZipArchiveEntry entry;
using (entry = archive.CreateEntry("file.txt"))
{
    StreamWriter writer = new StreamWriter(entry.Open());
    writer.WriteLine("Progress!");
    writer.Flush();
}

long length = entry.CompressedLength;
```

**Example 6: Get the name of an entry**

```C#
string name = entry.Name;
```

### Methods

The following table describes the methods of `ZipArchiveEntry`:

| Method    | Description                                                                         |
| --------- | ----------------------------------------------------------------------------------- |
| `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

| Event             | Description                           |
| ----------------- | ------------------------------------- |
| `PropertyChanged` | Occurs when a property value changes. |

## ZipArchive and ZipArchiveEntry

You can perform several operations on a `ZipArchive` instance.

### Get ZipArchiveEntry

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

**Example 7: Get ZipArchiveEntry**

```C#
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**

```C#
ZipArchiveEntry entry1 = archive.CreateEntry("file.txt");

//OR

ZipArchiveEntry entry2 = archive.CreateEntry("file.txt", compressionSettings);
```

### Extract ZipArchiveEntry

Extracts an entry to a specific folder or directory.

**Example 9: Extract a ZipArchiveEntry to a folder**

```C#
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);
    }
}
```

> tip You can also achieve this by using the [Zip Extensions](https://www.telerik.com/document-processing-libraries/documentation/libraries/radziplibrary/features/zip-extensions) `ExtractToFile` method.

## Operating with ZipArchiveEntry

The following examples show specific uses of `ZipArchiveEntry`.

### Using ZipArchiveEntry Properties

The following example includes all the properties discussed in the previous section.

**Example 10: Complete example**

```C#
string[] files = Directory.GetFiles("SampleFiles");
string zipFileName = "ZipArchive.zip";

using (Stream stream = File.Open(zipFileName, FileMode.Create))
{
    using (ZipArchive archive = 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 and Directories

**Example 11: Create folders and directories**

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

**Example 12: Open files in folders and directories**

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

## See Also

* [RadZipLibrary Getting Started](https://www.telerik.com/document-processing-libraries/documentation/libraries/radziplibrary/gettingstarted)
* [Update ZipArchive](https://www.telerik.com/document-processing-libraries/documentation/libraries/radziplibrary/features/update-ziparchive)
* [Protect ZipArchive](https://www.telerik.com/document-processing-libraries/documentation/libraries/radziplibrary/features/protect-ziparchive)
* [Compress Stream](https://www.telerik.com/document-processing-libraries/documentation/libraries/radziplibrary/features/compress-stream)
* [Compression Settings](https://www.telerik.com/document-processing-libraries/documentation/libraries/radziplibrary/features/compression-settings)
