|
| 1 | +using SharpCompress.Readers; |
| 2 | +using SharpCompress.Readers.Ace; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | + |
| 7 | +namespace Microsoft.CST.RecursiveExtractor.Extractors |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// The ACE Archive extractor implementation |
| 11 | + /// </summary> |
| 12 | + public class AceExtractor : AsyncExtractorInterface |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// The constructor takes the Extractor context for recursion. |
| 16 | + /// </summary> |
| 17 | + /// <param name="context">The Extractor context.</param> |
| 18 | + public AceExtractor(Extractor context) |
| 19 | + { |
| 20 | + Context = context; |
| 21 | + } |
| 22 | + private readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); |
| 23 | + |
| 24 | + internal Extractor Context { get; } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Extracts an ACE archive |
| 28 | + /// </summary> |
| 29 | + ///<inheritdoc /> |
| 30 | + public async IAsyncEnumerable<FileEntry> ExtractAsync(FileEntry fileEntry, ExtractorOptions options, ResourceGovernor governor, bool topLevel = true) |
| 31 | + { |
| 32 | + AceReader? aceReader = null; |
| 33 | + try |
| 34 | + { |
| 35 | + aceReader = AceReader.Open(fileEntry.Content, new ReaderOptions() |
| 36 | + { |
| 37 | + LeaveStreamOpen = true |
| 38 | + }); |
| 39 | + } |
| 40 | + catch (Exception e) |
| 41 | + { |
| 42 | + Logger.Debug(Extractor.FAILED_PARSING_ERROR_MESSAGE_STRING, ArchiveFileType.ACE, fileEntry.FullPath, string.Empty, e.GetType()); |
| 43 | + } |
| 44 | + |
| 45 | + if (aceReader != null) |
| 46 | + { |
| 47 | + using (aceReader) |
| 48 | + { |
| 49 | + while (aceReader.MoveToNextEntry()) |
| 50 | + { |
| 51 | + var entry = aceReader.Entry; |
| 52 | + if (entry.IsDirectory) |
| 53 | + { |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + var name = entry.Key?.Replace('/', Path.DirectorySeparatorChar); |
| 58 | + if (string.IsNullOrEmpty(name)) |
| 59 | + { |
| 60 | + Logger.Debug(Extractor.ENTRY_MISSING_NAME_ERROR_MESSAGE_STRING, ArchiveFileType.ACE, fileEntry.FullPath); |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + var newFileEntry = await FileEntry.FromStreamAsync(name, aceReader.OpenEntryStream(), fileEntry, entry.CreatedTime, entry.LastModifiedTime, entry.LastAccessedTime, memoryStreamCutoff: options.MemoryStreamCutoff).ConfigureAwait(false); |
| 65 | + if (newFileEntry != null) |
| 66 | + { |
| 67 | + governor.CheckResourceGovernor(newFileEntry.Content.Length); |
| 68 | + |
| 69 | + if (options.Recurse || topLevel) |
| 70 | + { |
| 71 | + await foreach (var innerEntry in Context.ExtractAsync(newFileEntry, options, governor, false)) |
| 72 | + { |
| 73 | + yield return innerEntry; |
| 74 | + } |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + yield return newFileEntry; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + if (options.ExtractSelfOnFail) |
| 87 | + { |
| 88 | + fileEntry.EntryStatus = FileEntryStatus.FailedArchive; |
| 89 | + yield return fileEntry; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Extracts an ACE archive |
| 96 | + /// </summary> |
| 97 | + ///<inheritdoc /> |
| 98 | + public IEnumerable<FileEntry> Extract(FileEntry fileEntry, ExtractorOptions options, ResourceGovernor governor, bool topLevel = true) |
| 99 | + { |
| 100 | + AceReader? aceReader = null; |
| 101 | + try |
| 102 | + { |
| 103 | + aceReader = AceReader.Open(fileEntry.Content, new ReaderOptions() |
| 104 | + { |
| 105 | + LeaveStreamOpen = true |
| 106 | + }); |
| 107 | + } |
| 108 | + catch (Exception e) |
| 109 | + { |
| 110 | + Logger.Debug(Extractor.FAILED_PARSING_ERROR_MESSAGE_STRING, ArchiveFileType.ACE, fileEntry.FullPath, string.Empty, e.GetType()); |
| 111 | + } |
| 112 | + |
| 113 | + if (aceReader != null) |
| 114 | + { |
| 115 | + using (aceReader) |
| 116 | + { |
| 117 | + while (aceReader.MoveToNextEntry()) |
| 118 | + { |
| 119 | + var entry = aceReader.Entry; |
| 120 | + if (entry.IsDirectory) |
| 121 | + { |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + FileEntry? newFileEntry = null; |
| 126 | + try |
| 127 | + { |
| 128 | + var stream = aceReader.OpenEntryStream(); |
| 129 | + var name = entry.Key?.Replace('/', Path.DirectorySeparatorChar); |
| 130 | + if (string.IsNullOrEmpty(name)) |
| 131 | + { |
| 132 | + Logger.Debug(Extractor.ENTRY_MISSING_NAME_ERROR_MESSAGE_STRING, ArchiveFileType.ACE, fileEntry.FullPath); |
| 133 | + continue; |
| 134 | + } |
| 135 | + newFileEntry = new FileEntry(name, stream, fileEntry, false, entry.CreatedTime, entry.LastModifiedTime, entry.LastAccessedTime, memoryStreamCutoff: options.MemoryStreamCutoff); |
| 136 | + } |
| 137 | + catch (Exception e) |
| 138 | + { |
| 139 | + Logger.Debug(Extractor.FAILED_PARSING_ERROR_MESSAGE_STRING, ArchiveFileType.ACE, fileEntry.FullPath, entry.Key, e.GetType()); |
| 140 | + } |
| 141 | + if (newFileEntry != null) |
| 142 | + { |
| 143 | + governor.CheckResourceGovernor(newFileEntry.Content.Length); |
| 144 | + |
| 145 | + if (options.Recurse || topLevel) |
| 146 | + { |
| 147 | + foreach (var innerEntry in Context.Extract(newFileEntry, options, governor, false)) |
| 148 | + { |
| 149 | + yield return innerEntry; |
| 150 | + } |
| 151 | + } |
| 152 | + else |
| 153 | + { |
| 154 | + yield return newFileEntry; |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + else |
| 161 | + { |
| 162 | + if (options.ExtractSelfOnFail) |
| 163 | + { |
| 164 | + fileEntry.EntryStatus = FileEntryStatus.FailedArchive; |
| 165 | + yield return fileEntry; |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments