Edit, Flatten Nested Files in ZIP Archive without Extracting to Disk

We have a project requirement, in which we need to edit the zip file on the fly without extracting it on the disk. For example, we have nested zip files and we need to extract the nested zip file’s contents into the parent zip file. Can anyone please provide me an example if it is possible with Aspose.zip?

@meenal,

Thank you for your interest in Aspose.ZIP for .NET API. We have logged your requirement in our issue tracking system. Your ticket number is ZIPNET-393. We will further look into the details of this requirement and will keep you updated on the status of the linked issue.

@meenal,

Please check the following C# code and let us know if this helps you to flatten nested files in ZIP archive?

C# Code to Flatten ZIP Files

using Aspose.Zip;
using System;
using System.Collections.Generic;
using System.IO;

namespace Flatten_Nested_Files_In_ZIP
{
    class Program
    {
        static void Main(string[] args)
        {
            License license = new License();
            license.SetLicense("Aspose.ZIP.License.lic");

            using (Archive outer = new Archive("outer.zip"))
            {
                List<ArchiveEntry> entriesToDelete = new List<ArchiveEntry>();
                List<string> namesToInsert = new List<string>();
                List<MemoryStream> contentToInsert = new List<MemoryStream>();

                foreach (ArchiveEntry entry in outer.Entries)
                {
                    // Find an entry which is an archive itself
                    if (entry.Name.EndsWith(".zip",
                                    StringComparison.InvariantCultureIgnoreCase) /* or another condition */ )
                    {
                        // Keep reference to the entry in order to remove it from the archive later
                        entriesToDelete.Add(entry);

                        MemoryStream innerCompressed = new MemoryStream();

                        //This extracts the entry to a memory stream
                        entry.Open().CopyTo(innerCompressed);

                        // We know that content of the entry is an zip archive so we may extract
                        using (Archive inner = new Archive(innerCompressed))
                        {
                            // Loop over entries of inner archive
                            foreach (ArchiveEntry ie in inner.Entries)
                            {
                                // Keep the name of inner entry.
                                namesToInsert.Add(ie.Name);
                                MemoryStream content = new MemoryStream();
                                ie.Open().CopyTo(content);

                                // Keep the content of inner entry.
                                contentToInsert.Add(content);
                            }
                        }
                    }
                }

                // Delete all the entries which are archives itself
                foreach (ArchiveEntry e in entriesToDelete) { outer.DeleteEntry(e); }

                for (int i = 0; i < namesToInsert.Count; i++)
                {
                    // Adds entries which were entries of inner archives
                    outer.CreateEntry(namesToInsert[i], contentToInsert[i]);
                }

                outer.Save("flatten.zip");
            }
        }
    }
}

So, to flatten an archive we extract inner archives into a memory and remove them later. We keep content of inner entries within memory streams too, so memory consumption may be higher. It is also possible to save archive to the same path i.e. outer.Save("outer.zip"); but it is better to save to another, because saving to the same involves copying to temporary file.