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.
C# .NET API to Delete Files Inside ZIP Archives
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
Delete Files Inside ZIP Archives in C#
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.
Delete a File in ZIP using 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#.
- Load the ZIP archive using Archive class.
- Loop through the entries in Archive.Entries collection.
- Filter the desired files/folders and add them to a list.
- Delete each entry in the list using Archive.DeleteEntry(ArchvieEntry) method.
- Save the updated ZIP archive using Archive.Save(string) method.
The following code sample shows how to delete files inside a ZIP archive in C#.
Delete 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.
- Load the ZIP archive using Archive class.
- Delete the file by specifying index in Archive.DeleteEntry(int) method.
- Save the updated ZIP archive using Archive.Save(string) method.
The following C# code sample shows how to remove a file in a ZIP archive using its index.
Get a Free API License
You can use Aspose.ZIP for .NET 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.