Create a Flat ZIP Archive in C#

Introduction

ZIP files often contain nested archives, making them difficult to manage. This guide explains how to flatten ZIP structures by extracting all inner ZIP archives into a single archive using Aspose.ZIP for .NET.

Why Create a Flat ZIP?

  • Simplifies ZIP file management.
  • Extracts all nested ZIP contents into one archive.
  • Reduces redundant folder structures.

Table of Contents

  1. Setting Up ZIP Archive Processing in C#
  2. Steps to Create a Flat ZIP Archive
  3. Saving the Flattened ZIP File
  4. Batch Processing Multiple ZIP Archives
  5. Getting a Free API License
  6. Conclusion and Additional Resources

1. Setting Up ZIP Archive Processing in C#

To process nested ZIP archives, we use Aspose.ZIP for .NET. This library provides:

  • Automated ZIP extraction and re-compression.
  • Support for multiple archive formats (ZIP, TAR, GZip, etc.).
  • Efficient processing of large archives.

Installation

Install via NuGet:

PM> Install-Package Aspose.Zip

Alternatively, download the DLL from the Aspose Downloads Page.


2. Steps to Create a Flat ZIP Archive

Consider this nested ZIP structure:

parent.zip
 ├ first.txt
 ├ inner.zip
 │ ├ file1.exe
 │ └ data.bin
 └ picture.gif

After flattening, all inner ZIP contents are moved to the parent ZIP:

flattened.zip
 ├ first.txt
 ├ picture.gif
 ├ file1.exe
 └ data.bin

Code Example

// Load the parent ZIP file
using (Archive parentZip = new Archive("parent.zip"))
{
    List<ArchiveEntry> toDelete = new List<ArchiveEntry>();
    List<string> newEntryNames = new List<string>();
    List<MemoryStream> newStreams = new List<MemoryStream>();

    foreach (ArchiveEntry entry in parentZip.Entries)
    {
        if (entry.Name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
        {
            toDelete.Add(entry);
            using (MemoryStream zipStream = new MemoryStream())
            {
                entry.Open().CopyTo(zipStream);
                zipStream.Position = 0;
                using (Archive innerZip = new Archive(zipStream))
                {
                    foreach (ArchiveEntry innerEntry in innerZip.Entries)
                    {
                        using (MemoryStream entryStream = new MemoryStream())
                        {
                            innerEntry.Open().CopyTo(entryStream);
                            newEntryNames.Add(innerEntry.Name);
                            newStreams.Add(entryStream);
                        }
                    }
                }
            }
        }
    }

    foreach (var entry in toDelete)
        parentZip.DeleteEntry(entry);

    for (int i = 0; i < newEntryNames.Count; i++)
        parentZip.CreateEntry(newEntryNames[i], newStreams[i]);

    parentZip.Save("flattened.zip");
}

This method automates nested ZIP file extraction.


3. Saving the Flattened ZIP File

Once the nested ZIP contents are extracted, save the flattened ZIP:

parentZip.Save("flattened.zip");

This ensures a single ZIP archive without nested folders.


4. Batch Processing Multiple ZIP Archives

To flatten multiple ZIP archives in a folder:

string[] files = Directory.GetFiles("zip_folder", "*.zip");
foreach (string file in files)
{
    using (Archive archive = new Archive(file))
    {
        archive.ExtractToDirectory("output_folder");
    }
}

This method automates bulk ZIP processing.


5. Getting a Free API License

To unlock full Aspose.ZIP features, request a free temporary license.

For documentation, visit the Aspose.ZIP Guide or ask queries on the Aspose forum.


6. Conclusion and Additional Resources

Summary

This guide covered:

How to flatten ZIP files in C#
Extracting nested ZIP archives
Saving the final ZIP archive
Processing multiple ZIP files at once

With Aspose.ZIP for .NET, you can efficiently extract and manage ZIP archives in your applications. Start optimizing ZIP processing workflows today!