AAR extraction
Overview
Apple Archive (.aar) is an archive format used on Apple platforms. Aspose.ZIP for .NET can extract Apple Archive files with the Aspose.Zip.Apple API, using methods similar to those for other archive formats.
Aspose.ZIP supports extraction of uncompressed Store entries and entries compressed with LZMA, Zlib, LZ4, or LZFSE.
Extract whole archive
To unpack the complete contents of an Apple Archive, call ExtractToDirectory:
1 using (AppleArchive archive = new AppleArchive("archive.aar"))
2 {
3 archive.ExtractToDirectory("extracted");
4 }Extract a selected entry
If you only need one file from the archive, open that entry as a readable stream and copy it to the destination:
1 using (AppleArchive archive = new AppleArchive("archive.aar"))
2 {
3 AppleArchiveEntry entry = archive.Entries.First(e => !e.IsDirectory);
4 string outputPath = Path.Combine("extracted", entry.Name);
5 var outputDirectory = Path.GetDirectoryName(outputPath);
6
7 if (!string.IsNullOrEmpty(outputDirectory))
8 {
9 Directory.CreateDirectory(outputDirectory);
10 }
11
12 using (Stream source = entry.Open())
13 using (FileStream destination = File.Create(outputPath))
14 {
15 source.CopyTo(destination);
16 }
17 }Individual entry extraction is not available when archive.IsSolid is true. Use ExtractToDirectory for solid Apple Archives.
Cancel extraction
Long-running Apple Archive extractions can be stopped by passing a
CancellationToken through AppleArchiveLoadOptions:
1 using (CancellationTokenSource cts = new CancellationTokenSource())
2 {
3 cts.CancelAfter(TimeSpan.FromSeconds(60));
4
5 try
6 {
7 AppleArchiveLoadOptions options = new AppleArchiveLoadOptions() { CancellationToken = cts.Token };
8 using (AppleArchive archive = new AppleArchive("big.aar", options))
9 {
10 archive.ExtractToDirectory("extracted");
11 }
12 }
13 catch (OperationCanceledException)
14 {
15 Console.WriteLine("Extraction was canceled.");
16 }
17 }Cancellation can leave only part of the archive extracted. If your application needs all-or-nothing behavior, extract to a temporary directory first and move the result after extraction completes.