EGG extraction

Overview

Aspose.Zip for .NET lets you read and extract EGG archives with the Aspose.Zip.Egg namespace. Use EggArchive to open an existing archive, inspect its comment and entries, or unpack its contents. You can extract the whole archive to a directory or work with individual files.

Extract the Whole Archive

Call ExtractToDirectory to unpack the archive into a destination directory. The method creates the directory if it does not exist.

1    using Aspose.Zip.Egg;
2
3    using (EggArchive archive = new EggArchive("archive.egg"))
4    {
5        archive.ExtractToDirectory(@"C:\extracted");
6    }

Inspect Archive Metadata

The archive exposes a Comment property and an Entries collection. Each EggEntry provides its name, compressed and uncompressed sizes, modification time, and an IsDirectory flag.

 1    using Aspose.Zip.Egg;
 2    using System;
 3
 4    using (EggArchive archive = new EggArchive("archive.egg"))
 5    {
 6        Console.WriteLine(archive.Comment);
 7
 8        foreach (EggEntry entry in archive.Entries)
 9        {
10            Console.WriteLine($"{entry.Name}: {entry.CompressedSize} compressed bytes, " +
11                $"{entry.UncompressedSize} uncompressed bytes");
12        }
13    }

Extract an Individual File

Use Extract with a destination path to save a selected file. An existing file at that path is overwritten. This example selects the first entry that is not a directory and checks whether a file was found before extracting it.

 1    using Aspose.Zip.Egg;
 2    using System.IO;
 3    using System.Linq;
 4
 5    using (EggArchive archive = new EggArchive("archive.egg"))
 6    {
 7        EggEntry entry = archive.Entries.FirstOrDefault(item => !item.IsDirectory);
 8
 9        if (entry != null)
10        {
11            Directory.CreateDirectory(@"C:\extracted");
12            entry.Extract(@"C:\extracted\first.bin");
13        }
14    }

Extract to a Stream

The Extract(Stream) overload writes an entry’s contents to a writable stream. Use a MemoryStream when another component needs the decompressed data in memory. Reset its position before reading the extracted content.

 1    using Aspose.Zip.Egg;
 2    using System.IO;
 3    using System.Linq;
 4
 5    using (EggArchive archive = new EggArchive("archive.egg"))
 6    {
 7        EggEntry entry = archive.Entries.FirstOrDefault(item => !item.IsDirectory);
 8
 9        if (entry != null)
10        {
11            using (MemoryStream output = new MemoryStream())
12            {
13                entry.Extract(output);
14                output.Position = 0;
15                // Read the extracted data from output here.
16            }
17        }
18    }

Alternatively, call Open to obtain a stream from which you can read the decompressed entry content. Keep the archive open while consuming this stream, and dispose of the stream when finished.

Open an Archive from a Stream

The stream constructor accepts a source that supports both reading and seeking. The following example opens an EGG file as a FileStream and extracts its contents.

1    using Aspose.Zip.Egg;
2    using System.IO;
3
4    using (FileStream source = File.OpenRead("archive.egg"))
5    using (EggArchive archive = new EggArchive(source))
6    {
7        archive.ExtractToDirectory(@"C:\extracted");
8    }

Cancel Extraction

Pass an EggArchiveLoadOptions instance with a CancellationToken to the archive constructor to cancel a long-running extraction. Handle OperationCanceledException when cancellation is requested. The cancellation option is available for .NET Framework 4.0 and later.

 1    using Aspose.Zip.Egg;
 2    using System;
 3    using System.Threading;
 4
 5    using (CancellationTokenSource cts = new CancellationTokenSource())
 6    {
 7        EggArchiveLoadOptions options = new EggArchiveLoadOptions()
 8        {
 9            CancellationToken = cts.Token
10        };
11
12        cts.CancelAfter(TimeSpan.FromSeconds(60));
13
14        try
15        {
16            using (EggArchive archive = new EggArchive("large.egg", options))
17            {
18                archive.ExtractToDirectory(@"C:\extracted");
19            }
20        }
21        catch (OperationCanceledException)
22        {
23            Console.WriteLine("EGG extraction was canceled.");
24        }
25    }