Delete Files in a ZIP Archive in C# .NET

While working with archives from within your .NET applications, you may need to manipulate files inside a ZIP. In the previous post, you have seen how to add files/folders in a ZIP archive. In this article, you will learn how to delete files inside the ZIP archives programmatically in C#. So let’s begin.

Delete Files in ZIP with C# - API Installation

Aspose.ZIP for .NET is a powerful API that provides a wide range of archiving features. It lets you create and manipulate archives of popular formats including ZIP, TAR, 7z, and GZip. We will use this API to delete the files inside ZIP archives dynamically. You can either install the API from NuGet or download the DLL and reference it manually.

PM> Install-Package Aspose.Zip 

C# Delete Files in a ZIP Archive

There are two ways to delete files inside a ZIP archive using Aspose.ZIP for .NET. One is using the index of the file while the other is using the ArchiveEntry class. Let’s see how to use both of the aforementioned methods.

Remove a File in ZIP with ArchiveEntry

Aspose.ZIP for .NET represents each file/folder inside a ZIP as an ArchiveEntry object and all the files and folders make a collection of ArchiveEntry objects. Thus, to delete a file from ZIP, you can access and delete the relevant ArchiveEntry object from the collection. The following are the steps to delete a file from a ZIP archive in C#.

The following code sample shows how to delete files inside a ZIP archive in C#.

C# Remove a File in ZIP using Index

You can also delete the ZIP entries using their index. The following are the steps to remove a file/folder from a ZIP archive using its index.

The following C# code sample shows how to remove a file in a ZIP archive using its index.

C# API to Remove Files in ZIP - Get a Free License

You can remove files inside ZIP archives without evaluation limitations by getting a free temporary license.

Conclusion

In this article, you have learned how to delete files from ZIP archives in C#. With the help of code samples, we have seen how to delete files using the ArchiveEntry class or the index. In case you want to learn more about Aspose.ZIP for .NET, you can visit documentation. Also, you can ask your questions via our forum.

See Also