-
Notifications
You must be signed in to change notification settings - Fork 35
Add ARJ and ARC decompression support #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
7
commits into
main
Choose a base branch
from
copilot/add-arj-decompression-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+375
−2
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
aede26f
Initial plan
Copilot 70714c4
Add ARJ and ARC decompression support with SharpCompress 0.44.3
Copilot 3ca4f2c
Simplify redundant ARC magic byte check
Copilot 5693355
Revert nuget.config to original Microsoft feed
Copilot d8b877d
Add CopyToOutputDirectory for TestData.arj and TestData.arc test files
Copilot 6b9d02d
Track extracted bytes after decompression in ArcExtractor for overflo…
Copilot b86ac56
Improve ARC Detection fidelityl
gfs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| using SharpCompress.Readers.Arc; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
|
|
||
| namespace Microsoft.CST.RecursiveExtractor.Extractors | ||
| { | ||
| /// <summary> | ||
| /// The ARC Archive extractor implementation | ||
| /// </summary> | ||
| public class ArcExtractor : AsyncExtractorInterface | ||
| { | ||
| /// <summary> | ||
| /// The constructor takes the Extractor context for recursion. | ||
| /// </summary> | ||
| /// <param name="context">The Extractor context.</param> | ||
| public ArcExtractor(Extractor context) | ||
| { | ||
| Context = context; | ||
| } | ||
| private readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); | ||
|
|
||
| internal Extractor Context { get; } | ||
|
|
||
| /// <summary> | ||
| /// Extracts an ARC archive | ||
| /// </summary> | ||
| ///<inheritdoc /> | ||
| public async IAsyncEnumerable<FileEntry> ExtractAsync(FileEntry fileEntry, ExtractorOptions options, ResourceGovernor governor, bool topLevel = true) | ||
| { | ||
| ArcReader? arcReader = null; | ||
| try | ||
| { | ||
| arcReader = ArcReader.Open(fileEntry.Content, new SharpCompress.Readers.ReaderOptions() | ||
| { | ||
| LeaveStreamOpen = true | ||
| }); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Logger.Debug(Extractor.FAILED_PARSING_ERROR_MESSAGE_STRING, ArchiveFileType.ARC, fileEntry.FullPath, string.Empty, e.GetType()); | ||
| } | ||
|
|
||
| if (arcReader != null) | ||
| { | ||
| using (arcReader) | ||
| { | ||
| while (arcReader.MoveToNextEntry()) | ||
| { | ||
| var entry = arcReader.Entry; | ||
| if (entry.IsDirectory) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var name = entry.Key?.Replace('/', Path.DirectorySeparatorChar); | ||
| if (string.IsNullOrEmpty(name)) | ||
| { | ||
| Logger.Debug(Extractor.ENTRY_MISSING_NAME_ERROR_MESSAGE_STRING, ArchiveFileType.ARC, fileEntry.FullPath); | ||
| continue; | ||
| } | ||
|
|
||
| var newFileEntry = await FileEntry.FromStreamAsync(name, arcReader.OpenEntryStream(), fileEntry, entry.CreatedTime, entry.LastModifiedTime, entry.LastAccessedTime, memoryStreamCutoff: options.MemoryStreamCutoff).ConfigureAwait(false); | ||
| if (newFileEntry != null) | ||
| { | ||
| // SharpCompress ARC does not expose entry sizes, so we check the resource governor | ||
| // after extraction using the actual decompressed content length. | ||
| governor.CheckResourceGovernor(newFileEntry.Content.Length); | ||
|
|
||
| if (options.Recurse || topLevel) | ||
| { | ||
| await foreach (var innerEntry in Context.ExtractAsync(newFileEntry, options, governor, false)) | ||
| { | ||
| yield return innerEntry; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| yield return newFileEntry; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| if (options.ExtractSelfOnFail) | ||
| { | ||
| fileEntry.EntryStatus = FileEntryStatus.FailedArchive; | ||
| yield return fileEntry; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Extracts an ARC archive | ||
| /// </summary> | ||
| ///<inheritdoc /> | ||
| public IEnumerable<FileEntry> Extract(FileEntry fileEntry, ExtractorOptions options, ResourceGovernor governor, bool topLevel = true) | ||
| { | ||
| ArcReader? arcReader = null; | ||
| try | ||
| { | ||
| arcReader = ArcReader.Open(fileEntry.Content, new SharpCompress.Readers.ReaderOptions() | ||
| { | ||
| LeaveStreamOpen = true | ||
| }); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Logger.Debug(Extractor.FAILED_PARSING_ERROR_MESSAGE_STRING, ArchiveFileType.ARC, fileEntry.FullPath, string.Empty, e.GetType()); | ||
| } | ||
|
|
||
| if (arcReader != null) | ||
| { | ||
| using (arcReader) | ||
| { | ||
| while (arcReader.MoveToNextEntry()) | ||
| { | ||
| var entry = arcReader.Entry; | ||
| if (entry.IsDirectory) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| FileEntry? newFileEntry = null; | ||
| try | ||
| { | ||
| var stream = arcReader.OpenEntryStream(); | ||
| var name = entry.Key?.Replace('/', Path.DirectorySeparatorChar); | ||
| if (string.IsNullOrEmpty(name)) | ||
| { | ||
| Logger.Debug(Extractor.ENTRY_MISSING_NAME_ERROR_MESSAGE_STRING, ArchiveFileType.ARC, fileEntry.FullPath); | ||
| continue; | ||
| } | ||
| newFileEntry = new FileEntry(name, stream, fileEntry, false, entry.CreatedTime, entry.LastModifiedTime, entry.LastAccessedTime, memoryStreamCutoff: options.MemoryStreamCutoff); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Logger.Debug(Extractor.FAILED_PARSING_ERROR_MESSAGE_STRING, ArchiveFileType.ARC, fileEntry.FullPath, entry.Key, e.GetType()); | ||
| } | ||
| if (newFileEntry != null) | ||
| { | ||
| // SharpCompress ARC does not expose entry sizes, so we check the resource governor | ||
| // after extraction using the actual decompressed content length. | ||
| governor.CheckResourceGovernor(newFileEntry.Content.Length); | ||
gfs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (options.Recurse || topLevel) | ||
| { | ||
| foreach (var innerEntry in Context.Extract(newFileEntry, options, governor, false)) | ||
| { | ||
| yield return innerEntry; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| yield return newFileEntry; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| if (options.ExtractSelfOnFail) | ||
| { | ||
| fileEntry.EntryStatus = FileEntryStatus.FailedArchive; | ||
| yield return fileEntry; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| using SharpCompress.Readers.Arj; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
|
|
||
| namespace Microsoft.CST.RecursiveExtractor.Extractors | ||
| { | ||
| /// <summary> | ||
| /// The ARJ Archive extractor implementation | ||
| /// </summary> | ||
| public class ArjExtractor : AsyncExtractorInterface | ||
| { | ||
| /// <summary> | ||
| /// The constructor takes the Extractor context for recursion. | ||
| /// </summary> | ||
| /// <param name="context">The Extractor context.</param> | ||
| public ArjExtractor(Extractor context) | ||
| { | ||
| Context = context; | ||
| } | ||
| private readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); | ||
|
|
||
| internal Extractor Context { get; } | ||
|
|
||
| /// <summary> | ||
| /// Extracts an ARJ archive | ||
| /// </summary> | ||
| ///<inheritdoc /> | ||
| public async IAsyncEnumerable<FileEntry> ExtractAsync(FileEntry fileEntry, ExtractorOptions options, ResourceGovernor governor, bool topLevel = true) | ||
| { | ||
| ArjReader? arjReader = null; | ||
| try | ||
| { | ||
| arjReader = ArjReader.Open(fileEntry.Content, new SharpCompress.Readers.ReaderOptions() | ||
| { | ||
| LeaveStreamOpen = true | ||
| }); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Logger.Debug(Extractor.FAILED_PARSING_ERROR_MESSAGE_STRING, ArchiveFileType.ARJ, fileEntry.FullPath, string.Empty, e.GetType()); | ||
| } | ||
|
|
||
| if (arjReader != null) | ||
| { | ||
| using (arjReader) | ||
| { | ||
| while (arjReader.MoveToNextEntry()) | ||
| { | ||
| var entry = arjReader.Entry; | ||
| if (entry.IsDirectory) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| governor.CheckResourceGovernor(entry.Size); | ||
| var name = entry.Key?.Replace('/', Path.DirectorySeparatorChar); | ||
| if (string.IsNullOrEmpty(name)) | ||
| { | ||
| Logger.Debug(Extractor.ENTRY_MISSING_NAME_ERROR_MESSAGE_STRING, ArchiveFileType.ARJ, fileEntry.FullPath); | ||
| continue; | ||
| } | ||
|
|
||
| var newFileEntry = await FileEntry.FromStreamAsync(name, arjReader.OpenEntryStream(), fileEntry, entry.CreatedTime, entry.LastModifiedTime, entry.LastAccessedTime, memoryStreamCutoff: options.MemoryStreamCutoff).ConfigureAwait(false); | ||
| if (newFileEntry != null) | ||
| { | ||
| if (options.Recurse || topLevel) | ||
| { | ||
| await foreach (var innerEntry in Context.ExtractAsync(newFileEntry, options, governor, false)) | ||
| { | ||
| yield return innerEntry; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| yield return newFileEntry; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| if (options.ExtractSelfOnFail) | ||
| { | ||
| fileEntry.EntryStatus = FileEntryStatus.FailedArchive; | ||
| yield return fileEntry; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Extracts an ARJ archive | ||
| /// </summary> | ||
| ///<inheritdoc /> | ||
| public IEnumerable<FileEntry> Extract(FileEntry fileEntry, ExtractorOptions options, ResourceGovernor governor, bool topLevel = true) | ||
| { | ||
| ArjReader? arjReader = null; | ||
| try | ||
| { | ||
| arjReader = ArjReader.Open(fileEntry.Content, new SharpCompress.Readers.ReaderOptions() | ||
| { | ||
| LeaveStreamOpen = true | ||
| }); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Logger.Debug(Extractor.FAILED_PARSING_ERROR_MESSAGE_STRING, ArchiveFileType.ARJ, fileEntry.FullPath, string.Empty, e.GetType()); | ||
| } | ||
|
|
||
| if (arjReader != null) | ||
| { | ||
| using (arjReader) | ||
| { | ||
| while (arjReader.MoveToNextEntry()) | ||
| { | ||
| var entry = arjReader.Entry; | ||
| if (entry.IsDirectory) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| governor.CheckResourceGovernor(entry.Size); | ||
| FileEntry? newFileEntry = null; | ||
| try | ||
| { | ||
| var stream = arjReader.OpenEntryStream(); | ||
| var name = entry.Key?.Replace('/', Path.DirectorySeparatorChar); | ||
| if (string.IsNullOrEmpty(name)) | ||
| { | ||
| Logger.Debug(Extractor.ENTRY_MISSING_NAME_ERROR_MESSAGE_STRING, ArchiveFileType.ARJ, fileEntry.FullPath); | ||
| continue; | ||
| } | ||
| newFileEntry = new FileEntry(name, stream, fileEntry, false, entry.CreatedTime, entry.LastModifiedTime, entry.LastAccessedTime, memoryStreamCutoff: options.MemoryStreamCutoff); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Logger.Debug(Extractor.FAILED_PARSING_ERROR_MESSAGE_STRING, ArchiveFileType.ARJ, fileEntry.FullPath, entry.Key, e.GetType()); | ||
| } | ||
| if (newFileEntry != null) | ||
| { | ||
| if (options.Recurse || topLevel) | ||
| { | ||
| foreach (var innerEntry in Context.Extract(newFileEntry, options, governor, false)) | ||
| { | ||
| yield return innerEntry; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| yield return newFileEntry; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| if (options.ExtractSelfOnFail) | ||
| { | ||
| fileEntry.EntryStatus = FileEntryStatus.FailedArchive; | ||
| yield return fileEntry; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.