diff --git a/SabreTools.FileTypes/FileTypeTool.cs b/SabreTools.FileTypes/FileTypeTool.cs index ea2589bf..1d5387f3 100644 --- a/SabreTools.FileTypes/FileTypeTool.cs +++ b/SabreTools.FileTypes/FileTypeTool.cs @@ -49,25 +49,7 @@ namespace SabreTools.FileTypes // Get input information var fileType = GetFileType(input); - Stream inputStream = File.OpenRead(input); - - // Try to match the supplied header skipper - if (header != null) - { - SkipperMatch.Init(); - var rule = SkipperMatch.GetMatchingRule(input, Path.GetFileNameWithoutExtension(header)); - - // If there's a match, transform the stream before getting info - if (rule.Tests != null && rule.Tests.Length != 0) - { - // Create the output stream - MemoryStream outputStream = new(); - - // Transform the stream and get the information from it - rule.TransformStream(inputStream, outputStream, keepReadOpen: false, keepWriteOpen: true); - inputStream = outputStream; - } - } + Stream inputStream = GetInfoStream(input, header); // Get the info in the proper manner BaseFile? baseFile; @@ -174,6 +156,33 @@ namespace SabreTools.FileTypes return baseFile; } + /// + /// Get the required stream for info hashing + /// + /// Filename to get information from + /// Populated string representing the name of the skipper to use, a blank string to use the first available checker, null otherwise + /// Open stream representing the file + private static Stream GetInfoStream(string input, string? header) + { + // Open the file directly + Stream inputStream = File.Open(input, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + if (header == null) + return inputStream; + + // Try to match the supplied header skipper + SkipperMatch.Init(); + var rule = SkipperMatch.GetMatchingRule(input, Path.GetFileNameWithoutExtension(header)); + + // If there's no match, return the original stream + if (rule.Tests == null || rule.Tests.Length == 0) + return inputStream; + + // Transform the stream and get the information from it + var outputStream = new MemoryStream(); + rule.TransformStream(inputStream, outputStream, keepReadOpen: false, keepWriteOpen: true); + return outputStream; + } + #endregion #region File Type