Often, you get a ZIP archive that contains other ZIP archives inside it forming a nested structure of the archives. In such cases, you may want to get a flat structure by extracting all the inner ZIP archives into the outer archive. To perform this operation programmatically, this article shows how to create a flat ZIP archive in C#.
C# .NET API to Create Flat ZIP Archives
To create the flat ZIP archives, we will use Aspose.ZIP for .NET. It is an archiving API that is designed to create and extract popular archive formats including ZIP, TAR, GZip, and 7z. You can either install the API from NuGet or download its DLL from the downloads section.
PM> Install-Package Aspose.Zip
Create a Flat ZIP Archive in C#
To understand the structure of a flat ZIP archive, let’s take an example. The following is a ZIP archive that contains another ZIP archive inside it.
parent.zip ├first.txt ├inner.zip │ ├game.exe │ └subitem.bin └picture.gif
After transforming this ZIP archive to a flat ZIP, all the entries of the inner ZIP will be extracted into the parent ZIP. Finally, we’ll get the following structure of the parent ZIP.
flatten.zip ├first.txt ├picture.gif ├game.exe └subitem.bin
Let’s see how to perform this transformation programmatically. The following are the steps to create a flat ZIP archive in C#.
- First, load the parent ZIP archive using Archive class.
- Then, create lists to store the inner ZIP entries to delete from parent ZIP, extracted entries and their names.
- After that, loop through each ArchiveEntry in the parent ZIP using Archive.Entries collection.
- For each entry, perform the following operations:
- Check if entry is a ZIP archive using ArchiveEntry.Name.EndsWith(".zip", StringComparison.InvariantCultureIgnoreCase)) method.
- Then, add entry to the list of entries to be deleted.
- Open entry into a MemoryStream object using ArchiveEntry.Open().CopyTo(MemoryStream) method.
- Iterate through the entries of inner ZIP archive and in each iteration, perform following steps.
- Add name of entry into list of entries to be added.
- Then, load entry to MemoryStream using ArchiveEntry.Open().CopyTo(MemoryStream) method.
- Finally, add entry to the list of entries to be added to parent ZIP.
- Then, loop through the list of inner ZIP archives and delete each entry using Archive.DeleteEntry(ArchiveEntry) method.
- Loop through the list of entries to be added to parent ZIP and add each entry using Archive.CreateEntry(String, Stream) method.
- Finally, save the parent ZIP archive using Archive.Save(String) method.
The following code sample shows how to create a flat ZIP archive in C#.
Get a Free API License
You can get a free temporary license to use Aspose.ZIP for .NET without evaluation limitations.
In this article, you have learned how to create a flat ZIP archive programmatically in C#. Particularly, we have demonstrated how to make a flat ZIP by extracting the inner ZIP archives into the parent ZIP. Apart from that, you can visit the documentation to read more about Aspose.ZIP for .NET. Also, you can share your queries with us via our forum.