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.zipfirst.txtinner.zip
 │ ├game.exe
 │ └subitem.binpicture.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.zipfirst.txtpicture.gifgame.exesubitem.bin

Let’s see how to perform this transformation programmatically. The following are the steps to create a flat ZIP archive in C#.

The following code sample shows how to create a flat ZIP archive in C#.

// Load ZIP archive
using (Archive outer = new Archive("Archives/nested-archive.zip"))
{
// Create a list to delete inner ZIP entries after extraction
List<ArchiveEntry> entriesToDelete = new List<ArchiveEntry>();
// Create a list to add names of files in inner ZIP archives
List<string> namesToInsert = new List<string>();
// Create a list to add stream objects of files in inner ZIP archives
List<MemoryStream> contentToInsert = new List<MemoryStream>();
// Loop through entries in the parent ZIP
foreach (ArchiveEntry entry in outer.Entries)
{
// Check if entry is a ZIP file
if (entry.Name.EndsWith(".zip", StringComparison.InvariantCultureIgnoreCase))
{
// Add entry to list to keep reference in order to remove it from the archive later
entriesToDelete.Add(entry);
// Create stream object to open the ZIP archive
MemoryStream innerCompressed = new MemoryStream();
entry.Open().CopyTo(innerCompressed);
// Load the ZIP archive from stream object
using (Archive inner = new Archive(innerCompressed))
{
// Loop over entries of archive
foreach (ArchiveEntry ie in inner.Entries)
{
// Keep the name of entry in list
namesToInsert.Add(ie.Name);
// Extract archive entry into a stream object
MemoryStream content = new MemoryStream();
ie.Open().CopyTo(content);
// Add entry to the list
contentToInsert.Add(content);
}
}
}
}
// Delete the inner ZIP archives
foreach (ArchiveEntry e in entriesToDelete)
outer.DeleteEntry(e);
// Insert the files of inner ZIP archives to parent ZIP archive
for (int i = 0; i < namesToInsert.Count; i++)
outer.CreateEntry(namesToInsert[i], contentToInsert[i]);
outer.Save("Archives/flatten.zip");
}

Get a Free API License

You can get a free temporary license to use Aspose.ZIP for .NET without evaluation limitations.

Conclusion

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.

See Also