# Update ZipArchive

With **RadZipLibrary** you can update an existing ZIP archive to add new entries to it, delete entries, or update existing ones.

The `ZipArchive` class provides three modes: `Read`, `Create`, and `Update`. More information on creating and reading an archive is available in the [RadZipLibrary Getting Started](https://www.telerik.com/document-processing-libraries/documentation/libraries/radziplibrary/gettingstarted) article.

The code snippet from **Example 1** opens a ZIP archive in update mode using the `ZipArchive` class.

**Example 1: Open for update**

```C#
using (Stream stream = File.Open("test.zip", FileMode.Open))
{
	using (ZipArchive archive = ZipArchive.Update(stream, null))
	{
		//Display the list of the files in the selected zip file using the ZipArchive.Entries property.
	}
}
```

## Add Entry

To add a new entry into the ZIP archive, perform the following steps:

1. Use the `CreateEntry()` method of the `ZipArchive` object to create a new entry.

2. Open the entry to get a stream for writing.

3. Write the necessary information into the entry.

4. Dispose the entry when all necessary information is written. In the `Update` mode this step is optional. You can omit it if you need to add, delete, or update other entries in the archive.

> note More information about `ZipArchiveEntry` is available in the [ZipArchiveEntry](https://www.telerik.com/document-processing-libraries/documentation/libraries/radziplibrary/features/ziparchive-entry) help article.

**Example 2: Add entry**

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

## Delete Entry

The `ZipArchive` class provides a `GetEntry()` method, which allows you access to a particular entry in the archive.

**Example 3** shows how to get an entry and delete it from the ZIP archive using the `Delete()` method.

**Example 3: Delete entry**

```C#
ZipArchiveEntry entry = archive.GetEntry("text.txt");
			if (entry != null)
			{
				entry.Delete();
			}
```

## Update Entry

To update an existing entry in the ZIP archive, perform the following steps:

1. Use the `GetEntry()` method of the `ZipArchive` object to get an existing entry.

2. Open the entry to get a stream for reading and writing.

3. Read or write the necessary information from or to the entry.

4. Dispose the entry when all necessary information is written. In the `Update` mode this step is optional. You can omit it if you need to add, delete, or update other entries in the archive.

**Example 4: Update entry**

```C#
ZipArchiveEntry entry = archive.GetEntry("text.txt");
if (entry != null)
{
	Stream entryStream = entry.Open();
	StreamReader reader = new StreamReader(entryStream);
	string content = reader.ReadToEnd();

	entryStream.Seek(0, SeekOrigin.End);
	StreamWriter writer = new StreamWriter(entryStream);
	writer.WriteLine("Updated line.");
	writer.Flush();
}
```

## Copy Entry

Starting with the **Q2 2025** release, **RadZipLibrary** introduces a new extension method `CopyTo` for the `Stream` class. Use this method to copy data from one stream to another with an optional timeout parameter for more control over long-running operations. This is particularly useful when you work with large files in ZIP archives.

The following example shows how to use the `CopyTo` method to copy the contents of a large file from a ZIP archive to a memory stream with a specified timeout.

**Example 5: Copy entry with timeout**

```C#
using (Stream zipStream = File.Open("input.zip", FileMode.Open))
{
    using (ZipArchive archive = ZipArchive.Read(zipStream))
    {
        ZipArchiveEntry entry = archive.Entries.ElementAt(2); // Access the third entry in the archive
        using (Stream entryStream = entry.Open())

        using (MemoryStream memoryStream = new MemoryStream())
        {
            // Copy the entry's content to the memory stream with a timeout of 5 seconds
            entryStream.CopyTo(memoryStream);
        }
    }
}
```

This method ensures that the operation does not hang indefinitely. It is useful for scenarios that involve large files or limited resources.

## See Also

* [RadZipLibrary Getting Started](https://www.telerik.com/document-processing-libraries/documentation/libraries/radziplibrary/gettingstarted)
