From b205e2a84fa81632bcac238923b55c90b16cc161 Mon Sep 17 00:00:00 2001 From: vpenades <5433822+vpenades@users.noreply.github.com> Date: Mon, 13 Jun 2022 11:56:32 +0200 Subject: [PATCH 1/3] Introduced IArchiveFactory --- src/SharpCompress/Archives/ArchiveFactory.cs | 62 ++++++++++++++----- .../Archives/GZip/GZipArchiveFactory.cs | 30 +++++++++ src/SharpCompress/Archives/IArchiveFactory.cs | 44 +++++++++++++ .../Archives/Rar/RarArchiveFactory.cs | 31 ++++++++++ .../SevenZip/SevenZipArchiveFactory.cs | 30 +++++++++ .../Archives/Tar/TarArchiveFactory.cs | 30 +++++++++ .../Archives/Zip/ZipArchiveFactory.cs | 32 ++++++++++ 7 files changed, 244 insertions(+), 15 deletions(-) create mode 100644 src/SharpCompress/Archives/GZip/GZipArchiveFactory.cs create mode 100644 src/SharpCompress/Archives/IArchiveFactory.cs create mode 100644 src/SharpCompress/Archives/Rar/RarArchiveFactory.cs create mode 100644 src/SharpCompress/Archives/SevenZip/SevenZipArchiveFactory.cs create mode 100644 src/SharpCompress/Archives/Tar/TarArchiveFactory.cs create mode 100644 src/SharpCompress/Archives/Zip/ZipArchiveFactory.cs diff --git a/src/SharpCompress/Archives/ArchiveFactory.cs b/src/SharpCompress/Archives/ArchiveFactory.cs index 76c0f642..b697c17c 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.cs @@ -15,6 +15,36 @@ namespace SharpCompress.Archives { public static class ArchiveFactory { + private static readonly HashSet archiveFactories; + + /// + /// Gets the collection of registered archive factories + /// + public static IReadOnlyCollection Factories => archiveFactories; + + static ArchiveFactory() + { + archiveFactories = new HashSet(); + + RegisterFactory(new Tar.TarArchiveFactory()); + RegisterFactory(new Rar.RarArchiveFactory()); + RegisterFactory(new Zip.ZipArchiveFactory()); + RegisterFactory(new GZip.GZipArchiveFactory()); + RegisterFactory(new SevenZip.SevenZipArchiveFactory()); + } + + /// + /// Registers an archive factory. + /// + /// The factory to register. + /// must not be null. + public static void RegisterFactory(IArchiveFactory factory) + { + factory.CheckNotNull(nameof(factory)); + + archiveFactories.Add(factory); + } + /// /// Opens an Archive for random access /// @@ -28,28 +58,30 @@ namespace SharpCompress.Archives { throw new ArgumentException("Stream should be readable and seekable"); } + readerOptions ??= new ReaderOptions(); - ArchiveType? type; - IsArchive(stream, out type); //test and reset stream position + long startPosition = stream.Position; - if (type != null) + foreach(var factory in Factories) { - switch (type.Value) + stream.Seek(startPosition, SeekOrigin.Begin); + + if (factory.IsArchive(stream)) { - case ArchiveType.Zip: - return ZipArchive.Open(stream, readerOptions); - case ArchiveType.SevenZip: - return SevenZipArchive.Open(stream, readerOptions); - case ArchiveType.GZip: - return GZipArchive.Open(stream, readerOptions); - case ArchiveType.Rar: - return RarArchive.Open(stream, readerOptions); - case ArchiveType.Tar: - return TarArchive.Open(stream, readerOptions); + stream.Seek(startPosition, SeekOrigin.Begin); + + return factory.Open(stream, readerOptions); } } - throw new InvalidOperationException("Cannot determine compressed stream type. Supported Archive Formats: Zip, GZip, Tar, Rar, 7Zip, LZip"); + + var extensions = Factories + .SelectMany(item => item.GetSupportedExtensions()) + .Select(item => item.Key) + .Distinct() + .Aggregate((a,b) => a + ", " + b ); + + throw new InvalidOperationException($"Cannot determine compressed stream type. Supported Archive Formats: {extensions}"); } public static IWritableArchive Create(ArchiveType type) diff --git a/src/SharpCompress/Archives/GZip/GZipArchiveFactory.cs b/src/SharpCompress/Archives/GZip/GZipArchiveFactory.cs new file mode 100644 index 00000000..d3f52ce0 --- /dev/null +++ b/src/SharpCompress/Archives/GZip/GZipArchiveFactory.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +using SharpCompress.Readers; + +namespace SharpCompress.Archives.GZip +{ + public class GZipArchiveFactory : IArchiveFactory + { + /// + public IEnumerable> GetSupportedExtensions() + { + yield return new KeyValuePair("GZip", "gz"); + } + + /// + public bool IsArchive(Stream stream) + { + return GZipArchive.IsGZipFile(stream); + } + + /// + public IArchive Open(Stream stream, ReaderOptions? readerOptions = null) + { + return GZipArchive.Open(stream, readerOptions); + } + } +} diff --git a/src/SharpCompress/Archives/IArchiveFactory.cs b/src/SharpCompress/Archives/IArchiveFactory.cs new file mode 100644 index 00000000..a9d55e32 --- /dev/null +++ b/src/SharpCompress/Archives/IArchiveFactory.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.IO; + +using SharpCompress.Common; +using SharpCompress.Readers; + +namespace SharpCompress.Archives +{ + /// + /// Represents a factory used to identify and open archives. + /// + /// + /// Currently implemented by:
+ /// + /// + /// + /// + /// + /// + /// + ///
+ public interface IArchiveFactory + { + /// + /// returns the extensions typically used by this archive type. + /// + /// + IEnumerable> GetSupportedExtensions(); + + /// + /// Returns true if the stream represents an archive of the format defined by this type. + /// + /// + bool IsArchive(Stream stream); + + /// + /// Opens an Archive for random access. + /// + /// An open, readable and seekable stream. + /// reading options. + IArchive Open(Stream stream, ReaderOptions? readerOptions = null); + } +} diff --git a/src/SharpCompress/Archives/Rar/RarArchiveFactory.cs b/src/SharpCompress/Archives/Rar/RarArchiveFactory.cs new file mode 100644 index 00000000..b374a64b --- /dev/null +++ b/src/SharpCompress/Archives/Rar/RarArchiveFactory.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +using SharpCompress.Readers; + +namespace SharpCompress.Archives.Rar +{ + public class RarArchiveFactory : IArchiveFactory + { + /// + public IEnumerable> GetSupportedExtensions() + { + yield return new KeyValuePair("Rar", "rar"); + yield return new KeyValuePair("Rar", "cbr"); + } + + /// + public bool IsArchive(Stream stream) + { + return RarArchive.IsRarFile(stream); + } + + /// + public IArchive Open(Stream stream, ReaderOptions? readerOptions = null) + { + return RarArchive.Open(stream, readerOptions); + } + } +} diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchiveFactory.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchiveFactory.cs new file mode 100644 index 00000000..11fa673f --- /dev/null +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchiveFactory.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +using SharpCompress.Readers; + +namespace SharpCompress.Archives.SevenZip +{ + public class SevenZipArchiveFactory : IArchiveFactory + { + /// + public IEnumerable> GetSupportedExtensions() + { + yield return new KeyValuePair("7Zip", "7z"); + } + + /// + public bool IsArchive(Stream stream) + { + return SevenZipArchive.IsSevenZipFile(stream); + } + + /// + public IArchive Open(Stream stream, ReaderOptions? readerOptions = null) + { + return SevenZipArchive.Open(stream, readerOptions); + } + } +} diff --git a/src/SharpCompress/Archives/Tar/TarArchiveFactory.cs b/src/SharpCompress/Archives/Tar/TarArchiveFactory.cs new file mode 100644 index 00000000..1c7fc599 --- /dev/null +++ b/src/SharpCompress/Archives/Tar/TarArchiveFactory.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +using SharpCompress.Readers; + +namespace SharpCompress.Archives.Tar +{ + public class TarArchiveFactory : IArchiveFactory + { + /// + public IEnumerable> GetSupportedExtensions() + { + yield return new KeyValuePair("Tar", "tar"); + } + + /// + public bool IsArchive(Stream stream) + { + return TarArchive.IsTarFile(stream); + } + + /// + public IArchive Open(Stream stream, ReaderOptions? readerOptions = null) + { + return TarArchive.Open(stream, readerOptions); + } + } +} diff --git a/src/SharpCompress/Archives/Zip/ZipArchiveFactory.cs b/src/SharpCompress/Archives/Zip/ZipArchiveFactory.cs new file mode 100644 index 00000000..d91c6a6a --- /dev/null +++ b/src/SharpCompress/Archives/Zip/ZipArchiveFactory.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +using SharpCompress.Readers; + +namespace SharpCompress.Archives.Zip +{ + public class ZipArchiveFactory : IArchiveFactory + { + /// + public IEnumerable> GetSupportedExtensions() + { + yield return new KeyValuePair("Zip", "zip"); + yield return new KeyValuePair("Zip", "zipx"); + yield return new KeyValuePair("Zip", "cbz"); + } + + /// + public bool IsArchive(Stream stream) + { + return ZipArchive.IsZipFile(stream); + } + + /// + public IArchive Open(Stream stream, ReaderOptions? readerOptions = null) + { + return ZipArchive.Open(stream, readerOptions); + } + } +} From 8ea8dcde19b7879c28e276db9a2dd2829738826a Mon Sep 17 00:00:00 2001 From: vpenades <5433822+vpenades@users.noreply.github.com> Date: Mon, 13 Jun 2022 13:34:21 +0200 Subject: [PATCH 2/3] Added Archive Name --- src/SharpCompress/Archives/ArchiveFactory.cs | 4 +--- src/SharpCompress/Archives/GZip/GZipArchiveFactory.cs | 7 +++++-- src/SharpCompress/Archives/IArchiveFactory.cs | 7 ++++++- src/SharpCompress/Archives/Rar/RarArchiveFactory.cs | 9 ++++++--- .../Archives/SevenZip/SevenZipArchiveFactory.cs | 7 +++++-- src/SharpCompress/Archives/Tar/TarArchiveFactory.cs | 7 +++++-- src/SharpCompress/Archives/Zip/ZipArchiveFactory.cs | 11 +++++++---- 7 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/SharpCompress/Archives/ArchiveFactory.cs b/src/SharpCompress/Archives/ArchiveFactory.cs index b697c17c..f9e304ad 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.cs @@ -76,9 +76,7 @@ namespace SharpCompress.Archives } var extensions = Factories - .SelectMany(item => item.GetSupportedExtensions()) - .Select(item => item.Key) - .Distinct() + .Select(item => item.Name) .Aggregate((a,b) => a + ", " + b ); throw new InvalidOperationException($"Cannot determine compressed stream type. Supported Archive Formats: {extensions}"); diff --git a/src/SharpCompress/Archives/GZip/GZipArchiveFactory.cs b/src/SharpCompress/Archives/GZip/GZipArchiveFactory.cs index d3f52ce0..d4faa639 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchiveFactory.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchiveFactory.cs @@ -10,9 +10,12 @@ namespace SharpCompress.Archives.GZip public class GZipArchiveFactory : IArchiveFactory { /// - public IEnumerable> GetSupportedExtensions() + public string Name => "GZip"; + + /// + public IEnumerable GetSupportedExtensions() { - yield return new KeyValuePair("GZip", "gz"); + yield return "gz"; } /// diff --git a/src/SharpCompress/Archives/IArchiveFactory.cs b/src/SharpCompress/Archives/IArchiveFactory.cs index a9d55e32..ea3f5bb7 100644 --- a/src/SharpCompress/Archives/IArchiveFactory.cs +++ b/src/SharpCompress/Archives/IArchiveFactory.cs @@ -22,11 +22,16 @@ namespace SharpCompress.Archives /// public interface IArchiveFactory { + /// + /// Gets the archive Type name + /// + string Name { get; } + /// /// returns the extensions typically used by this archive type. /// /// - IEnumerable> GetSupportedExtensions(); + IEnumerable GetSupportedExtensions(); /// /// Returns true if the stream represents an archive of the format defined by this type. diff --git a/src/SharpCompress/Archives/Rar/RarArchiveFactory.cs b/src/SharpCompress/Archives/Rar/RarArchiveFactory.cs index b374a64b..bc13bd94 100644 --- a/src/SharpCompress/Archives/Rar/RarArchiveFactory.cs +++ b/src/SharpCompress/Archives/Rar/RarArchiveFactory.cs @@ -10,10 +10,13 @@ namespace SharpCompress.Archives.Rar public class RarArchiveFactory : IArchiveFactory { /// - public IEnumerable> GetSupportedExtensions() + public string Name => "Rar"; + + /// + public IEnumerable GetSupportedExtensions() { - yield return new KeyValuePair("Rar", "rar"); - yield return new KeyValuePair("Rar", "cbr"); + yield return "rar"; + yield return "cbr"; } /// diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchiveFactory.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchiveFactory.cs index 11fa673f..923c7810 100644 --- a/src/SharpCompress/Archives/SevenZip/SevenZipArchiveFactory.cs +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchiveFactory.cs @@ -10,9 +10,12 @@ namespace SharpCompress.Archives.SevenZip public class SevenZipArchiveFactory : IArchiveFactory { /// - public IEnumerable> GetSupportedExtensions() + public string Name => "7Zip"; + + /// + public IEnumerable GetSupportedExtensions() { - yield return new KeyValuePair("7Zip", "7z"); + yield return "7z"; } /// diff --git a/src/SharpCompress/Archives/Tar/TarArchiveFactory.cs b/src/SharpCompress/Archives/Tar/TarArchiveFactory.cs index 1c7fc599..8a67a5f3 100644 --- a/src/SharpCompress/Archives/Tar/TarArchiveFactory.cs +++ b/src/SharpCompress/Archives/Tar/TarArchiveFactory.cs @@ -10,9 +10,12 @@ namespace SharpCompress.Archives.Tar public class TarArchiveFactory : IArchiveFactory { /// - public IEnumerable> GetSupportedExtensions() + public string Name => "Tar"; + + /// + public IEnumerable GetSupportedExtensions() { - yield return new KeyValuePair("Tar", "tar"); + yield return "tar"; } /// diff --git a/src/SharpCompress/Archives/Zip/ZipArchiveFactory.cs b/src/SharpCompress/Archives/Zip/ZipArchiveFactory.cs index d91c6a6a..eade3e5e 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchiveFactory.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchiveFactory.cs @@ -10,11 +10,14 @@ namespace SharpCompress.Archives.Zip public class ZipArchiveFactory : IArchiveFactory { /// - public IEnumerable> GetSupportedExtensions() + public string Name => "Zip"; + + /// + public IEnumerable GetSupportedExtensions() { - yield return new KeyValuePair("Zip", "zip"); - yield return new KeyValuePair("Zip", "zipx"); - yield return new KeyValuePair("Zip", "cbz"); + yield return "zip"; + yield return "zipx"; + yield return "cbz"; } /// From 20142f91fd85632b79154ed2e6d5a91e238c4a95 Mon Sep 17 00:00:00 2001 From: vpenades <5433822+vpenades@users.noreply.github.com> Date: Tue, 14 Jun 2022 11:39:52 +0200 Subject: [PATCH 3/3] Added remaining Open method variants --- src/SharpCompress/Archives/ArchiveFactory.cs | 145 +++++++----------- .../Archives/GZip/GZipArchiveFactory.cs | 20 ++- src/SharpCompress/Archives/IArchiveFactory.cs | 28 +++- .../Archives/Rar/RarArchiveFactory.cs | 20 ++- .../SevenZip/SevenZipArchiveFactory.cs | 20 ++- .../Archives/Tar/TarArchiveFactory.cs | 21 ++- .../Archives/Zip/ZipArchiveFactory.cs | 31 +++- 7 files changed, 183 insertions(+), 102 deletions(-) diff --git a/src/SharpCompress/Archives/ArchiveFactory.cs b/src/SharpCompress/Archives/ArchiveFactory.cs index f9e304ad..8bb8f3f3 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.cs @@ -26,11 +26,11 @@ namespace SharpCompress.Archives { archiveFactories = new HashSet(); - RegisterFactory(new Tar.TarArchiveFactory()); - RegisterFactory(new Rar.RarArchiveFactory()); RegisterFactory(new Zip.ZipArchiveFactory()); - RegisterFactory(new GZip.GZipArchiveFactory()); + RegisterFactory(new Rar.RarArchiveFactory()); RegisterFactory(new SevenZip.SevenZipArchiveFactory()); + RegisterFactory(new GZip.GZipArchiveFactory()); + RegisterFactory(new Tar.TarArchiveFactory()); } /// @@ -61,25 +61,7 @@ namespace SharpCompress.Archives readerOptions ??= new ReaderOptions(); - long startPosition = stream.Position; - - foreach(var factory in Factories) - { - stream.Seek(startPosition, SeekOrigin.Begin); - - if (factory.IsArchive(stream)) - { - stream.Seek(startPosition, SeekOrigin.Begin); - - return factory.Open(stream, readerOptions); - } - } - - var extensions = Factories - .Select(item => item.Name) - .Aggregate((a,b) => a + ", " + b ); - - throw new InvalidOperationException($"Cannot determine compressed stream type. Supported Archive Formats: {extensions}"); + return FindArchiveFactory(stream).Open(stream, readerOptions); } public static IWritableArchive Create(ArchiveType type) @@ -114,29 +96,7 @@ namespace SharpCompress.Archives fileInfo.CheckNotNull(nameof(fileInfo)); options ??= new ReaderOptions { LeaveStreamOpen = false }; - ArchiveType? type; - using (Stream stream = fileInfo.OpenRead()) - { - IsArchive(stream, out type); //test and reset stream position - - if (type != null) - { - switch (type.Value) - { - case ArchiveType.Zip: - return ZipArchive.Open(fileInfo, options); - case ArchiveType.SevenZip: - return SevenZipArchive.Open(fileInfo, options); - case ArchiveType.GZip: - return GZipArchive.Open(fileInfo, options); - case ArchiveType.Rar: - return RarArchive.Open(fileInfo, options); - case ArchiveType.Tar: - return TarArchive.Open(fileInfo, options); - } - } - } - throw new InvalidOperationException("Cannot determine compressed stream type. Supported Archive Formats: Zip, GZip, Tar, Rar, 7Zip"); + return FindArchiveFactory(fileInfo).Open(fileInfo, options); } /// @@ -154,31 +114,10 @@ namespace SharpCompress.Archives if (files.Length == 1) return Open(fileInfo, options); - fileInfo.CheckNotNull(nameof(fileInfo)); options ??= new ReaderOptions { LeaveStreamOpen = false }; - ArchiveType? type; - using (Stream stream = fileInfo.OpenRead()) - IsArchive(stream, out type); //test and reset stream position - - if (type != null) - { - switch (type.Value) - { - case ArchiveType.Zip: - return ZipArchive.Open(files, options); - case ArchiveType.SevenZip: - return SevenZipArchive.Open(files, options); - case ArchiveType.GZip: - return GZipArchive.Open(files, options); - case ArchiveType.Rar: - return RarArchive.Open(files, options); - case ArchiveType.Tar: - return TarArchive.Open(files, options); - } - } - throw new InvalidOperationException("Cannot determine compressed stream type. Supported Archive Formats: Zip, GZip, Tar, Rar, 7Zip"); + return FindArchiveFactory(fileInfo).Open(fileInfos, options); } /// @@ -189,36 +128,18 @@ namespace SharpCompress.Archives public static IArchive Open(IEnumerable streams, ReaderOptions? options = null) { streams.CheckNotNull(nameof(streams)); - if (streams.Count() == 0) + Stream[] streamsArray = streams.ToArray(); + if (streamsArray.Length == 0) throw new InvalidOperationException("No streams"); - if (streams.Count() == 1) - return Open(streams.First(), options); - + Stream firstStream = streamsArray[0]; + if (streamsArray.Length == 1) + return Open(firstStream, options); + firstStream.CheckNotNull(nameof(firstStream)); options ??= new ReaderOptions(); - ArchiveType? type; - using (Stream stream = streams.First()) - IsArchive(stream, out type); //test and reset stream position - - if (type != null) - { - switch (type.Value) - { - case ArchiveType.Zip: - return ZipArchive.Open(streams, options); - case ArchiveType.SevenZip: - return SevenZipArchive.Open(streams, options); - case ArchiveType.GZip: - return GZipArchive.Open(streams, options); - case ArchiveType.Rar: - return RarArchive.Open(streams, options); - case ArchiveType.Tar: - return TarArchive.Open(streams, options); - } - } - throw new InvalidOperationException("Cannot determine compressed stream type. Supported Archive Formats: Zip, GZip, Tar, Rar, 7Zip"); - } + return FindArchiveFactory(firstStream).Open(streamsArray, options); + } /// /// Extract to specific directory, retaining filename @@ -233,6 +154,44 @@ namespace SharpCompress.Archives } } + private static IArchiveFactory FindArchiveFactory(FileInfo finfo) + { + finfo.CheckNotNull(nameof(finfo)); + using (Stream stream = finfo.OpenRead()) + { + return FindArchiveFactory(stream); + } + } + + private static IArchiveFactory FindArchiveFactory(Stream stream) + { + stream.CheckNotNull(nameof(stream)); + if (!stream.CanRead || !stream.CanSeek) + { + throw new ArgumentException("Stream should be readable and seekable"); + } + + long startPosition = stream.Position; + + foreach (var factory in Factories) + { + stream.Seek(startPosition, SeekOrigin.Begin); + + if (factory.IsArchive(stream)) + { + stream.Seek(startPosition, SeekOrigin.Begin); + + return factory; + } + } + + var extensions = Factories + .Select(item => item.Name) + .Aggregate((a, b) => a + ", " + b); + + throw new InvalidOperationException($"Cannot determine compressed stream type. Supported Archive Formats: {extensions}"); + } + public static bool IsArchive(string filePath, out ArchiveType? type) { filePath.CheckNotNullOrEmpty(nameof(filePath)); diff --git a/src/SharpCompress/Archives/GZip/GZipArchiveFactory.cs b/src/SharpCompress/Archives/GZip/GZipArchiveFactory.cs index d4faa639..0d52d6ca 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchiveFactory.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchiveFactory.cs @@ -19,7 +19,7 @@ namespace SharpCompress.Archives.GZip } /// - public bool IsArchive(Stream stream) + public bool IsArchive(Stream stream, string? password = null) { return GZipArchive.IsGZipFile(stream); } @@ -29,5 +29,23 @@ namespace SharpCompress.Archives.GZip { return GZipArchive.Open(stream, readerOptions); } + + /// + public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + { + return GZipArchive.Open(fileInfo, readerOptions); + } + + /// + public IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + { + return GZipArchive.Open(streams, readerOptions); + } + + /// + public IArchive Open(IEnumerable fileInfos, ReaderOptions? readerOptions = null) + { + return GZipArchive.Open(fileInfos, readerOptions); + } } } diff --git a/src/SharpCompress/Archives/IArchiveFactory.cs b/src/SharpCompress/Archives/IArchiveFactory.cs index ea3f5bb7..5674307d 100644 --- a/src/SharpCompress/Archives/IArchiveFactory.cs +++ b/src/SharpCompress/Archives/IArchiveFactory.cs @@ -36,14 +36,36 @@ namespace SharpCompress.Archives /// /// Returns true if the stream represents an archive of the format defined by this type. /// - /// - bool IsArchive(Stream stream); + /// + /// /// optional password + bool IsArchive(Stream stream, string? password = null); /// /// Opens an Archive for random access. /// /// An open, readable and seekable stream. - /// reading options. + /// reading options. IArchive Open(Stream stream, ReaderOptions? readerOptions = null); + + /// + /// Constructor with a FileInfo object to an existing file. + /// + /// the file to open. + /// reading options. + IArchive Open(System.IO.FileInfo fileInfo, ReaderOptions? readerOptions = null); + + /// + /// Constructor with IEnumerable FileInfo objects, multi and split support. + /// + /// + /// reading options. + IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null); + + /// + /// Constructor with IEnumerable Stream objects, multi and split support. + /// + /// + /// reading options. + IArchive Open(IEnumerable fileInfos, ReaderOptions? readerOptions = null); } } diff --git a/src/SharpCompress/Archives/Rar/RarArchiveFactory.cs b/src/SharpCompress/Archives/Rar/RarArchiveFactory.cs index bc13bd94..a049275c 100644 --- a/src/SharpCompress/Archives/Rar/RarArchiveFactory.cs +++ b/src/SharpCompress/Archives/Rar/RarArchiveFactory.cs @@ -20,7 +20,7 @@ namespace SharpCompress.Archives.Rar } /// - public bool IsArchive(Stream stream) + public bool IsArchive(Stream stream, string? password = null) { return RarArchive.IsRarFile(stream); } @@ -30,5 +30,23 @@ namespace SharpCompress.Archives.Rar { return RarArchive.Open(stream, readerOptions); } + + /// + public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + { + return RarArchive.Open(fileInfo, readerOptions); + } + + /// + public IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + { + return RarArchive.Open(streams, readerOptions); + } + + /// + public IArchive Open(IEnumerable fileInfos, ReaderOptions? readerOptions = null) + { + return RarArchive.Open(fileInfos, readerOptions); + } } } diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchiveFactory.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchiveFactory.cs index 923c7810..153fb431 100644 --- a/src/SharpCompress/Archives/SevenZip/SevenZipArchiveFactory.cs +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchiveFactory.cs @@ -19,7 +19,7 @@ namespace SharpCompress.Archives.SevenZip } /// - public bool IsArchive(Stream stream) + public bool IsArchive(Stream stream, string? password = null) { return SevenZipArchive.IsSevenZipFile(stream); } @@ -29,5 +29,23 @@ namespace SharpCompress.Archives.SevenZip { return SevenZipArchive.Open(stream, readerOptions); } + + /// + public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + { + return SevenZipArchive.Open(fileInfo, readerOptions); + } + + /// + public IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + { + return SevenZipArchive.Open(streams, readerOptions); + } + + /// + public IArchive Open(IEnumerable fileInfos, ReaderOptions? readerOptions = null) + { + return SevenZipArchive.Open(fileInfos, readerOptions); + } } } diff --git a/src/SharpCompress/Archives/Tar/TarArchiveFactory.cs b/src/SharpCompress/Archives/Tar/TarArchiveFactory.cs index 8a67a5f3..fb9c170c 100644 --- a/src/SharpCompress/Archives/Tar/TarArchiveFactory.cs +++ b/src/SharpCompress/Archives/Tar/TarArchiveFactory.cs @@ -19,7 +19,7 @@ namespace SharpCompress.Archives.Tar } /// - public bool IsArchive(Stream stream) + public bool IsArchive(Stream stream, string? password = null) { return TarArchive.IsTarFile(stream); } @@ -29,5 +29,24 @@ namespace SharpCompress.Archives.Tar { return TarArchive.Open(stream, readerOptions); } + + /// + public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + { + return TarArchive.Open(fileInfo, readerOptions); + } + + /// + public IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + { + return TarArchive.Open(streams, readerOptions); + } + + /// + public IArchive Open(IEnumerable fileInfos, ReaderOptions? readerOptions = null) + { + return TarArchive.Open(fileInfos, readerOptions); + } + } } diff --git a/src/SharpCompress/Archives/Zip/ZipArchiveFactory.cs b/src/SharpCompress/Archives/Zip/ZipArchiveFactory.cs index eade3e5e..68c3f2b6 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchiveFactory.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchiveFactory.cs @@ -21,9 +21,18 @@ namespace SharpCompress.Archives.Zip } /// - public bool IsArchive(Stream stream) + public bool IsArchive(Stream stream, string? password = null) { - return ZipArchive.IsZipFile(stream); + long startPosition = stream.Position; + + if (ZipArchive.IsZipFile(stream, password)) return true; + + stream.Seek(startPosition, SeekOrigin.Begin); + + //test the zip (last) file of a multipart zip + if (ZipArchive.IsZipMulti(stream)) return true; + + return false; } /// @@ -31,5 +40,23 @@ namespace SharpCompress.Archives.Zip { return ZipArchive.Open(stream, readerOptions); } + + /// + public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + { + return ZipArchive.Open(fileInfo, readerOptions); + } + + /// + public IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + { + return ZipArchive.Open(streams, readerOptions); + } + + /// + public IArchive Open(IEnumerable fileInfos, ReaderOptions? readerOptions = null) + { + return ZipArchive.Open(fileInfos, readerOptions); + } } }