From c4fde80c5e886256028260c80669bb319ececfee Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 27 Sep 2016 10:14:08 +0100 Subject: [PATCH 1/5] Create proper options objects to remove flags from API --- src/SharpCompress/Archives/AbstractArchive.cs | 24 ++--- .../Archives/AbstractWritableArchive.cs | 17 ++-- src/SharpCompress/Archives/ArchiveFactory.cs | 46 +++------ .../Archives/GZip/GZipArchive.cs | 70 ++++--------- .../Archives/IWritableArchive.cs | 4 +- .../Archives/IWritableArchiveExtensions.cs | 33 +------ .../Archives/Rar/FileInfoRarArchiveVolume.cs | 21 ++-- .../Archives/Rar/FileInfoRarFilePart.cs | 6 +- src/SharpCompress/Archives/Rar/RarArchive.cs | 71 ++++++------- .../Archives/Rar/RarArchiveVolumeFactory.cs | 12 +-- .../Archives/Rar/StreamRarArchiveVolume.cs | 8 +- .../Archives/SevenZip/SevenZipArchive.cs | 69 ++++--------- src/SharpCompress/Archives/Tar/TarArchive.cs | 88 ++++++----------- src/SharpCompress/Archives/Zip/ZipArchive.cs | 99 ++++++------------- src/SharpCompress/Common/CompressionInfo.cs | 30 ------ src/SharpCompress/Common/GZip/GZipVolume.cs | 6 +- src/SharpCompress/Common/Options.cs | 23 ----- .../Common/Rar/Headers/RarHeaderFactory.cs | 17 ++-- src/SharpCompress/Common/Rar/RarVolume.cs | 8 +- .../Common/SevenZip/SevenZipVolume.cs | 6 +- src/SharpCompress/Common/Tar/TarVolume.cs | 5 +- src/SharpCompress/Common/Volume.cs | 9 +- src/SharpCompress/Common/Zip/ZipVolume.cs | 5 +- src/SharpCompress/Readers/AbstractReader.cs | 4 +- src/SharpCompress/Readers/GZip/GZipReader.cs | 7 +- .../Readers/Rar/MultiVolumeRarReader.cs | 2 +- src/SharpCompress/Readers/Rar/RarReader.cs | 20 ++-- .../Readers/Rar/RarReaderVolume.cs | 5 +- .../Readers/Rar/SingleVolumeRarReader.cs | 3 +- src/SharpCompress/Readers/ReaderFactory.cs | 13 ++- src/SharpCompress/Readers/ReaderOptions.cs | 18 ++++ src/SharpCompress/Readers/Tar/TarReader.cs | 13 ++- src/SharpCompress/Readers/Zip/ZipReader.cs | 10 +- src/SharpCompress/Writers/Tar/TarWriter.cs | 12 +-- .../Writers/Tar/TarWriterOptions.cs | 22 +++++ src/SharpCompress/Writers/WriterFactory.cs | 18 +--- src/SharpCompress/Writers/WriterOptions.cs | 27 +++++ .../Writers/Zip/ZipCompressionInfo.cs | 47 --------- src/SharpCompress/Writers/Zip/ZipWriter.cs | 94 ++++++++++++------ .../Writers/Zip/ZipWriterEntryOptions.cs | 19 ++++ .../Writers/Zip/ZipWriterOptions.cs | 30 ++++++ .../SharpCompress.Test/Rar/RarArchiveTests.cs | 23 ++++- .../Rar/RarHeaderFactoryTest.cs | 9 +- test/SharpCompress.Test/Rar/RarReaderTests.cs | 25 ++++- .../SharpCompress.Test/Tar/TarArchiveTests.cs | 2 +- test/SharpCompress.Test/TestBase.cs | 4 +- test/SharpCompress.Test/WriterTests.cs | 10 +- .../SharpCompress.Test/Zip/ZipArchiveTests.cs | 11 ++- test/SharpCompress.Test/Zip/ZipReaderTests.cs | 19 +++- 49 files changed, 525 insertions(+), 619 deletions(-) delete mode 100644 src/SharpCompress/Common/CompressionInfo.cs delete mode 100644 src/SharpCompress/Common/Options.cs create mode 100644 src/SharpCompress/Readers/ReaderOptions.cs create mode 100644 src/SharpCompress/Writers/Tar/TarWriterOptions.cs create mode 100644 src/SharpCompress/Writers/WriterOptions.cs delete mode 100644 src/SharpCompress/Writers/Zip/ZipCompressionInfo.cs create mode 100644 src/SharpCompress/Writers/Zip/ZipWriterEntryOptions.cs create mode 100644 src/SharpCompress/Writers/Zip/ZipWriterOptions.cs diff --git a/src/SharpCompress/Archives/AbstractArchive.cs b/src/SharpCompress/Archives/AbstractArchive.cs index 6f588606..45451fc1 100644 --- a/src/SharpCompress/Archives/AbstractArchive.cs +++ b/src/SharpCompress/Archives/AbstractArchive.cs @@ -20,31 +20,33 @@ namespace SharpCompress.Archives public event EventHandler CompressedBytesRead; public event EventHandler FilePartExtractionBegin; - protected string Password { get; private set; } + protected ReaderOptions ReaderOptions { get; } + + private bool disposed; #if !NO_FILE - internal AbstractArchive(ArchiveType type, FileInfo fileInfo, Options options, string password) + internal AbstractArchive(ArchiveType type, FileInfo fileInfo, ReaderOptions readerOptions) { Type = type; - Password = password; if (!fileInfo.Exists) { throw new ArgumentException("File does not exist: " + fileInfo.FullName); } - options = (Options) FlagUtility.SetFlag(options, Options.KeepStreamsOpen, false); - lazyVolumes = new LazyReadOnlyCollection(LoadVolumes(fileInfo, options)); + ReaderOptions = readerOptions; + readerOptions.LeaveOpenStream = false; + lazyVolumes = new LazyReadOnlyCollection(LoadVolumes(fileInfo)); lazyEntries = new LazyReadOnlyCollection(LoadEntries(Volumes)); } - protected abstract IEnumerable LoadVolumes(FileInfo file, Options options); + protected abstract IEnumerable LoadVolumes(FileInfo file); #endif - internal AbstractArchive(ArchiveType type, IEnumerable streams, Options options, string password) + internal AbstractArchive(ArchiveType type, IEnumerable streams, ReaderOptions readerOptions) { Type = type; - Password = password; - lazyVolumes = new LazyReadOnlyCollection(LoadVolumes(streams.Select(CheckStreams), options)); + ReaderOptions = readerOptions; + lazyVolumes = new LazyReadOnlyCollection(LoadVolumes(streams.Select(CheckStreams))); lazyEntries = new LazyReadOnlyCollection(LoadEntries(Volumes)); } @@ -102,15 +104,13 @@ namespace SharpCompress.Archives /// public virtual long TotalUncompressSize { get { return Entries.Aggregate(0L, (total, cf) => total + cf.Size); } } - protected abstract IEnumerable LoadVolumes(IEnumerable streams, Options options); + protected abstract IEnumerable LoadVolumes(IEnumerable streams); protected abstract IEnumerable LoadEntries(IEnumerable volumes); IEnumerable IArchive.Entries { get { return Entries.Cast(); } } IEnumerable IArchive.Volumes { get { return lazyVolumes.Cast(); } } - private bool disposed; - public virtual void Dispose() { if (!disposed) diff --git a/src/SharpCompress/Archives/AbstractWritableArchive.cs b/src/SharpCompress/Archives/AbstractWritableArchive.cs index ae933315..5f38d459 100644 --- a/src/SharpCompress/Archives/AbstractWritableArchive.cs +++ b/src/SharpCompress/Archives/AbstractWritableArchive.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.IO; using System.Linq; using SharpCompress.Common; +using SharpCompress.Readers; +using SharpCompress.Writers; namespace SharpCompress.Archives { @@ -21,14 +23,14 @@ namespace SharpCompress.Archives { } - internal AbstractWritableArchive(ArchiveType type, Stream stream, Options options) - : base(type, stream.AsEnumerable(), options, null) + internal AbstractWritableArchive(ArchiveType type, Stream stream, ReaderOptions readerFactoryOptions) + : base(type, stream.AsEnumerable(), readerFactoryOptions) { } #if !NO_FILE - internal AbstractWritableArchive(ArchiveType type, FileInfo fileInfo, Options options) - : base(type, fileInfo, options, null) + internal AbstractWritableArchive(ArchiveType type, FileInfo fileInfo, ReaderOptions readerFactoryOptions) + : base(type, fileInfo, readerFactoryOptions) { } #endif @@ -112,11 +114,11 @@ namespace SharpCompress.Archives return false; } - public void SaveTo(Stream stream, CompressionInfo compressionType) + public void SaveTo(Stream stream, WriterOptions options) { //reset streams of new entries newEntries.Cast().ForEach(x => x.Stream.Seek(0, SeekOrigin.Begin)); - SaveTo(stream, compressionType, OldEntries, newEntries); + SaveTo(stream, options, OldEntries, newEntries); } protected TEntry CreateEntry(string key, Stream source, long size, DateTime? modified, @@ -132,8 +134,7 @@ namespace SharpCompress.Archives protected abstract TEntry CreateEntryInternal(string key, Stream source, long size, DateTime? modified, bool closeStream); - protected abstract void SaveTo(Stream stream, CompressionInfo compressionType, - IEnumerable oldEntries, IEnumerable newEntries); + protected abstract void SaveTo(Stream stream, WriterOptions options, IEnumerable oldEntries, IEnumerable newEntries); public override void Dispose() { diff --git a/src/SharpCompress/Archives/ArchiveFactory.cs b/src/SharpCompress/Archives/ArchiveFactory.cs index 96268259..fc491e6d 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.cs @@ -6,6 +6,7 @@ using SharpCompress.Archives.SevenZip; using SharpCompress.Archives.Tar; using SharpCompress.Archives.Zip; using SharpCompress.Common; +using SharpCompress.Readers; namespace SharpCompress.Archives { @@ -15,44 +16,44 @@ namespace SharpCompress.Archives /// Opens an Archive for random access /// /// - /// + /// /// - public static IArchive Open(Stream stream, Options options = Options.KeepStreamsOpen) + public static IArchive Open(Stream stream, ReaderOptions readerOptions = null) { stream.CheckNotNull("stream"); if (!stream.CanRead || !stream.CanSeek) { throw new ArgumentException("Stream should be readable and seekable"); } - + readerOptions = readerOptions ?? new ReaderOptions(); if (ZipArchive.IsZipFile(stream, null)) { stream.Seek(0, SeekOrigin.Begin); - return ZipArchive.Open(stream, options, null); + return ZipArchive.Open(stream, readerOptions); } stream.Seek(0, SeekOrigin.Begin); if (SevenZipArchive.IsSevenZipFile(stream)) { stream.Seek(0, SeekOrigin.Begin); - return SevenZipArchive.Open(stream, options); + return SevenZipArchive.Open(stream, readerOptions); } stream.Seek(0, SeekOrigin.Begin); if (GZipArchive.IsGZipFile(stream)) { stream.Seek(0, SeekOrigin.Begin); - return GZipArchive.Open(stream, options); + return GZipArchive.Open(stream, readerOptions); } stream.Seek(0, SeekOrigin.Begin); - if (RarArchive.IsRarFile(stream, options)) + if (RarArchive.IsRarFile(stream, readerOptions)) { stream.Seek(0, SeekOrigin.Begin); - return RarArchive.Open(stream, options); + return RarArchive.Open(stream, readerOptions); } stream.Seek(0, SeekOrigin.Begin); if (TarArchive.IsTarFile(stream)) { stream.Seek(0, SeekOrigin.Begin); - return TarArchive.Open(stream, options); + return TarArchive.Open(stream, readerOptions); } throw new InvalidOperationException("Cannot determine compressed stream type. Supported Archive Formats: Zip, GZip, Tar, Rar, 7Zip"); } @@ -82,33 +83,15 @@ namespace SharpCompress.Archives #if !NO_FILE - /// - /// Constructor expects a filepath to an existing file. - /// - /// - public static IArchive Open(string filePath) - { - return Open(filePath, Options.None); - } - - /// - /// Constructor with a FileInfo object to an existing file. - /// - /// - public static IArchive Open(FileInfo fileInfo) - { - return Open(fileInfo, Options.None); - } - /// /// Constructor expects a filepath to an existing file. /// /// /// - public static IArchive Open(string filePath, Options options) + public static IArchive Open(string filePath, ReaderOptions options = null) { filePath.CheckNotNullOrEmpty("filePath"); - return Open(new FileInfo(filePath), options); + return Open(new FileInfo(filePath), options ?? new ReaderOptions()); } /// @@ -116,15 +99,16 @@ namespace SharpCompress.Archives /// /// /// - public static IArchive Open(FileInfo fileInfo, Options options) + public static IArchive Open(FileInfo fileInfo, ReaderOptions options = null) { fileInfo.CheckNotNull("fileInfo"); + options = options ?? new ReaderOptions(); using (var stream = fileInfo.OpenRead()) { if (ZipArchive.IsZipFile(stream, null)) { stream.Dispose(); - return ZipArchive.Open(fileInfo, options, null); + return ZipArchive.Open(fileInfo, options); } stream.Seek(0, SeekOrigin.Begin); if (SevenZipArchive.IsSevenZipFile(stream)) diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.cs b/src/SharpCompress/Archives/GZip/GZipArchive.cs index 59ab7e48..b896db4f 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.cs @@ -6,6 +6,7 @@ using SharpCompress.Common; using SharpCompress.Common.GZip; using SharpCompress.Readers; using SharpCompress.Readers.GZip; +using SharpCompress.Writers; using SharpCompress.Writers.GZip; namespace SharpCompress.Archives.GZip @@ -13,67 +14,37 @@ namespace SharpCompress.Archives.GZip public class GZipArchive : AbstractWritableArchive { #if !NO_FILE - -/// -/// Constructor expects a filepath to an existing file. -/// -/// - public static GZipArchive Open(string filePath) - { - return Open(filePath, Options.None); - } - - /// - /// Constructor with a FileInfo object to an existing file. - /// - /// - public static GZipArchive Open(FileInfo fileInfo) - { - return Open(fileInfo, Options.None); - } - /// /// Constructor expects a filepath to an existing file. /// /// - /// - public static GZipArchive Open(string filePath, Options options) + /// + public static GZipArchive Open(string filePath, ReaderOptions readerOptions = null) { filePath.CheckNotNullOrEmpty("filePath"); - return Open(new FileInfo(filePath), options); + return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); } /// /// Constructor with a FileInfo object to an existing file. /// /// - /// - public static GZipArchive Open(FileInfo fileInfo, Options options) + /// + public static GZipArchive Open(FileInfo fileInfo, ReaderOptions readerOptions = null) { fileInfo.CheckNotNull("fileInfo"); - return new GZipArchive(fileInfo, options); + return new GZipArchive(fileInfo, readerOptions ?? new ReaderOptions()); } #endif - /// /// Takes a seekable Stream as a source /// /// - public static GZipArchive Open(Stream stream) + /// + public static GZipArchive Open(Stream stream, ReaderOptions readerOptions = null) { stream.CheckNotNull("stream"); - return Open(stream, Options.None); - } - - /// - /// Takes a seekable Stream as a source - /// - /// - /// - public static GZipArchive Open(Stream stream, Options options) - { - stream.CheckNotNull("stream"); - return new GZipArchive(stream, options); + return new GZipArchive(stream, readerOptions ?? new ReaderOptions()); } public static GZipArchive Create() @@ -88,14 +59,14 @@ namespace SharpCompress.Archives.GZip /// /// /// - internal GZipArchive(FileInfo fileInfo, Options options) + internal GZipArchive(FileInfo fileInfo, ReaderOptions options) : base(ArchiveType.GZip, fileInfo, options) { } - protected override IEnumerable LoadVolumes(FileInfo file, Options options) + protected override IEnumerable LoadVolumes(FileInfo file) { - return new GZipVolume(file, options).AsEnumerable(); + return new GZipVolume(file, ReaderOptions).AsEnumerable(); } public static bool IsGZipFile(string filePath) @@ -124,7 +95,7 @@ namespace SharpCompress.Archives.GZip { using (var stream = fileInfo.Open(FileMode.Create, FileAccess.Write)) { - SaveTo(stream); + SaveTo(stream, new WriterOptions(CompressionType.GZip)); } } #endif @@ -159,7 +130,7 @@ namespace SharpCompress.Archives.GZip /// /// /// - internal GZipArchive(Stream stream, Options options) + internal GZipArchive(Stream stream, ReaderOptions options) : base(ArchiveType.GZip, stream, options) { } @@ -169,11 +140,6 @@ namespace SharpCompress.Archives.GZip { } - public void SaveTo(Stream stream) - { - SaveTo(stream, CompressionType.GZip); - } - protected override GZipArchiveEntry CreateEntryInternal(string filePath, Stream source, long size, DateTime? modified, bool closeStream) { @@ -184,7 +150,7 @@ namespace SharpCompress.Archives.GZip return new GZipWritableArchiveEntry(this, source, filePath, size, modified, closeStream); } - protected override void SaveTo(Stream stream, CompressionInfo compressionInfo, + protected override void SaveTo(Stream stream, WriterOptions options, IEnumerable oldEntries, IEnumerable newEntries) { @@ -205,9 +171,9 @@ namespace SharpCompress.Archives.GZip } } - protected override IEnumerable LoadVolumes(IEnumerable streams, Options options) + protected override IEnumerable LoadVolumes(IEnumerable streams) { - return new GZipVolume(streams.First(), options).AsEnumerable(); + return new GZipVolume(streams.First(), ReaderOptions).AsEnumerable(); } protected override IEnumerable LoadEntries(IEnumerable volumes) diff --git a/src/SharpCompress/Archives/IWritableArchive.cs b/src/SharpCompress/Archives/IWritableArchive.cs index e0c0ada0..380d6814 100644 --- a/src/SharpCompress/Archives/IWritableArchive.cs +++ b/src/SharpCompress/Archives/IWritableArchive.cs @@ -1,6 +1,6 @@ using System; using System.IO; -using SharpCompress.Common; +using SharpCompress.Writers; namespace SharpCompress.Archives { @@ -10,6 +10,6 @@ namespace SharpCompress.Archives IArchiveEntry AddEntry(string key, Stream source, bool closeStream, long size = 0, DateTime? modified = null); - void SaveTo(Stream stream, CompressionInfo compressionType); + void SaveTo(Stream stream, WriterOptions options); } } \ No newline at end of file diff --git a/src/SharpCompress/Archives/IWritableArchiveExtensions.cs b/src/SharpCompress/Archives/IWritableArchiveExtensions.cs index 6b38a3c5..bee42a49 100644 --- a/src/SharpCompress/Archives/IWritableArchiveExtensions.cs +++ b/src/SharpCompress/Archives/IWritableArchiveExtensions.cs @@ -2,18 +2,12 @@ using System; #endif using System.IO; -using SharpCompress.Common; +using SharpCompress.Writers; namespace SharpCompress.Archives { public static class IWritableArchiveExtensions { - public static void SaveTo(this IWritableArchive writableArchive, - Stream stream, CompressionType compressionType) - { - writableArchive.SaveTo(stream, new CompressionInfo {Type = compressionType}); - } - #if !NO_FILE public static void AddEntry(this IWritableArchive writableArchive, @@ -28,33 +22,16 @@ namespace SharpCompress.Archives fileInfo.LastWriteTime); } - public static void SaveTo(this IWritableArchive writableArchive, - string filePath, CompressionType compressionType) + public static void SaveTo(this IWritableArchive writableArchive, string filePath, WriterOptions options) { - writableArchive.SaveTo(new FileInfo(filePath), new CompressionInfo {Type = compressionType}); + writableArchive.SaveTo(new FileInfo(filePath), options); } - public static void SaveTo(this IWritableArchive writableArchive, - FileInfo fileInfo, CompressionType compressionType) + public static void SaveTo(this IWritableArchive writableArchive, FileInfo fileInfo, WriterOptions options) { using (var stream = fileInfo.Open(FileMode.Create, FileAccess.Write)) { - writableArchive.SaveTo(stream, new CompressionInfo {Type = compressionType}); - } - } - - public static void SaveTo(this IWritableArchive writableArchive, - string filePath, CompressionInfo compressionInfo) - { - writableArchive.SaveTo(new FileInfo(filePath), compressionInfo); - } - - public static void SaveTo(this IWritableArchive writableArchive, - FileInfo fileInfo, CompressionInfo compressionInfo) - { - using (var stream = fileInfo.Open(FileMode.Create, FileAccess.Write)) - { - writableArchive.SaveTo(stream, compressionInfo); + writableArchive.SaveTo(stream, options); } } diff --git a/src/SharpCompress/Archives/Rar/FileInfoRarArchiveVolume.cs b/src/SharpCompress/Archives/Rar/FileInfoRarArchiveVolume.cs index 85165dd6..99e84e6d 100644 --- a/src/SharpCompress/Archives/Rar/FileInfoRarArchiveVolume.cs +++ b/src/SharpCompress/Archives/Rar/FileInfoRarArchiveVolume.cs @@ -2,10 +2,10 @@ #if !NO_FILE using System.Collections.Generic; using System.IO; -using SharpCompress.Common; using SharpCompress.Common.Rar; using SharpCompress.Common.Rar.Headers; using SharpCompress.IO; +using SharpCompress.Readers; namespace SharpCompress.Archives.Rar { @@ -14,30 +14,27 @@ namespace SharpCompress.Archives.Rar /// internal class FileInfoRarArchiveVolume : RarVolume { - internal FileInfoRarArchiveVolume(FileInfo fileInfo, string password, Options options) - : base(StreamingMode.Seekable, fileInfo.OpenRead(), password, FixOptions(options)) + internal FileInfoRarArchiveVolume(FileInfo fileInfo, ReaderOptions options) + : base(StreamingMode.Seekable, fileInfo.OpenRead(), FixOptions(options)) { FileInfo = fileInfo; - FileParts = base.GetVolumeFileParts().ToReadOnly(); + FileParts = GetVolumeFileParts().ToReadOnly(); } - private static Options FixOptions(Options options) + private static ReaderOptions FixOptions(ReaderOptions options) { //make sure we're closing streams with fileinfo - if (options.HasFlag(Options.KeepStreamsOpen)) - { - options = (Options) FlagUtility.SetFlag(options, Options.KeepStreamsOpen, false); - } + options.LeaveOpenStream = false; return options; } - internal ReadOnlyCollection FileParts { get; private set; } + internal ReadOnlyCollection FileParts { get; } - internal FileInfo FileInfo { get; private set; } + internal FileInfo FileInfo { get; } internal override RarFilePart CreateFilePart(FileHeader fileHeader, MarkHeader markHeader) { - return new FileInfoRarFilePart(this, markHeader, fileHeader, FileInfo); + return new FileInfoRarFilePart(this, ReaderOptions.Password, markHeader, fileHeader, FileInfo); } internal override IEnumerable ReadFileParts() diff --git a/src/SharpCompress/Archives/Rar/FileInfoRarFilePart.cs b/src/SharpCompress/Archives/Rar/FileInfoRarFilePart.cs index d4e3e4c4..4b31a774 100644 --- a/src/SharpCompress/Archives/Rar/FileInfoRarFilePart.cs +++ b/src/SharpCompress/Archives/Rar/FileInfoRarFilePart.cs @@ -7,13 +7,13 @@ namespace SharpCompress.Archives.Rar { internal class FileInfoRarFilePart : SeekableFilePart { - internal FileInfoRarFilePart(FileInfoRarArchiveVolume volume, MarkHeader mh, FileHeader fh, FileInfo fi) - : base(mh, fh, volume.Stream, volume.Password) + internal FileInfoRarFilePart(FileInfoRarArchiveVolume volume, string password, MarkHeader mh, FileHeader fh, FileInfo fi) + : base(mh, fh, volume.Stream, password) { FileInfo = fi; } - internal FileInfo FileInfo { get; private set; } + internal FileInfo FileInfo { get; } internal override string FilePartName { diff --git a/src/SharpCompress/Archives/Rar/RarArchive.cs b/src/SharpCompress/Archives/Rar/RarArchive.cs index 6d57e453..b3f48a26 100644 --- a/src/SharpCompress/Archives/Rar/RarArchive.cs +++ b/src/SharpCompress/Archives/Rar/RarArchive.cs @@ -17,20 +17,19 @@ namespace SharpCompress.Archives.Rar #if !NO_FILE -/// -/// Constructor with a FileInfo object to an existing file. -/// -/// -/// -/// - internal RarArchive(FileInfo fileInfo, Options options, string password) - : base(ArchiveType.Rar, fileInfo, options, password) + /// + /// Constructor with a FileInfo object to an existing file. + /// + /// + /// + internal RarArchive(FileInfo fileInfo, ReaderOptions options) + : base(ArchiveType.Rar, fileInfo, options) { } - protected override IEnumerable LoadVolumes(FileInfo file, Options options) + protected override IEnumerable LoadVolumes(FileInfo file) { - return RarArchiveVolumeFactory.GetParts(file, Password, options); + return RarArchiveVolumeFactory.GetParts(file, ReaderOptions); } #endif @@ -39,9 +38,8 @@ namespace SharpCompress.Archives.Rar /// /// /// - /// - internal RarArchive(IEnumerable streams, Options options, string password) - : base(ArchiveType.Rar, streams, options, password) + internal RarArchive(IEnumerable streams, ReaderOptions options) + : base(ArchiveType.Rar, streams, options) { } @@ -50,16 +48,16 @@ namespace SharpCompress.Archives.Rar return RarArchiveEntryFactory.GetEntries(this, volumes); } - protected override IEnumerable LoadVolumes(IEnumerable streams, Options options) + protected override IEnumerable LoadVolumes(IEnumerable streams) { - return RarArchiveVolumeFactory.GetParts(streams, Password, options); + return RarArchiveVolumeFactory.GetParts(streams, ReaderOptions); } protected override IReader CreateReaderForSolidExtraction() { var stream = Volumes.First().Stream; stream.Position = 0; - return RarReader.Open(stream, Password); + return RarReader.Open(stream, ReaderOptions); } public override bool IsSolid { get { return Volumes.First().IsSolidArchive; } } @@ -68,16 +66,15 @@ namespace SharpCompress.Archives.Rar #if !NO_FILE -/// -/// Constructor expects a filepath to an existing file. -/// -/// -/// -/// - public static RarArchive Open(string filePath, Options options = Options.None, string password = null) + /// + /// Constructor with a FileInfo object to an existing file. + /// + /// + /// + public static RarArchive Open(string filePath, ReaderOptions options = null) { filePath.CheckNotNullOrEmpty("filePath"); - return Open(new FileInfo(filePath), options, password); + return new RarArchive(new FileInfo(filePath), options ?? new ReaderOptions()); } /// @@ -85,11 +82,10 @@ namespace SharpCompress.Archives.Rar /// /// /// - /// - public static RarArchive Open(FileInfo fileInfo, Options options = Options.None, string password = null) + public static RarArchive Open(FileInfo fileInfo, ReaderOptions options = null) { fileInfo.CheckNotNull("fileInfo"); - return new RarArchive(fileInfo, options, password); + return new RarArchive(fileInfo, options ?? new ReaderOptions()); } #endif @@ -98,11 +94,10 @@ namespace SharpCompress.Archives.Rar /// /// /// - /// - public static RarArchive Open(Stream stream, Options options = Options.KeepStreamsOpen, string password = null) + public static RarArchive Open(Stream stream, ReaderOptions options = null) { stream.CheckNotNull("stream"); - return Open(stream.AsEnumerable(), options, password); + return Open(stream.AsEnumerable(), options ?? new ReaderOptions()); } /// @@ -110,11 +105,10 @@ namespace SharpCompress.Archives.Rar /// /// /// - /// - public static RarArchive Open(IEnumerable streams, Options options = Options.KeepStreamsOpen, string password = null) + public static RarArchive Open(IEnumerable streams, ReaderOptions options = null) { streams.CheckNotNull("streams"); - return new RarArchive(streams, options, password); + return new RarArchive(streams, options ?? new ReaderOptions()); } #if !NO_FILE @@ -135,17 +129,12 @@ namespace SharpCompress.Archives.Rar } } #endif - - public static bool IsRarFile(Stream stream) - { - return IsRarFile(stream, Options.None); - } - - public static bool IsRarFile(Stream stream, Options options) + + public static bool IsRarFile(Stream stream, ReaderOptions options = null) { try { - var headerFactory = new RarHeaderFactory(StreamingMode.Seekable, options); + var headerFactory = new RarHeaderFactory(StreamingMode.Seekable, options ?? new ReaderOptions()); var markHeader = headerFactory.ReadHeaders(stream).FirstOrDefault() as MarkHeader; return markHeader != null && markHeader.IsValid(); } diff --git a/src/SharpCompress/Archives/Rar/RarArchiveVolumeFactory.cs b/src/SharpCompress/Archives/Rar/RarArchiveVolumeFactory.cs index 0aea2c50..4bf39967 100644 --- a/src/SharpCompress/Archives/Rar/RarArchiveVolumeFactory.cs +++ b/src/SharpCompress/Archives/Rar/RarArchiveVolumeFactory.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.IO; using SharpCompress.Common; using SharpCompress.Common.Rar; - +using SharpCompress.Readers; #if !NO_FILE using System.Linq; using System.Text; @@ -14,7 +14,7 @@ namespace SharpCompress.Archives.Rar { internal static class RarArchiveVolumeFactory { - internal static IEnumerable GetParts(IEnumerable streams, string password, Options options) + internal static IEnumerable GetParts(IEnumerable streams, ReaderOptions options) { foreach (Stream s in streams) { @@ -22,15 +22,15 @@ namespace SharpCompress.Archives.Rar { throw new ArgumentException("Stream is not readable and seekable"); } - StreamRarArchiveVolume part = new StreamRarArchiveVolume(s, password, options); + StreamRarArchiveVolume part = new StreamRarArchiveVolume(s, options); yield return part; } } #if !NO_FILE - internal static IEnumerable GetParts(FileInfo fileInfo, string password, Options options) + internal static IEnumerable GetParts(FileInfo fileInfo, ReaderOptions options) { - FileInfoRarArchiveVolume part = new FileInfoRarArchiveVolume(fileInfo, password, options); + FileInfoRarArchiveVolume part = new FileInfoRarArchiveVolume(fileInfo, options); yield return part; if (!part.ArchiveHeader.ArchiveHeaderFlags.HasFlag(ArchiveFlags.VOLUME)) @@ -42,7 +42,7 @@ namespace SharpCompress.Archives.Rar //we use fileinfo because rar is dumb and looks at file names rather than archive info for another volume while (fileInfo != null && fileInfo.Exists) { - part = new FileInfoRarArchiveVolume(fileInfo, password, options); + part = new FileInfoRarArchiveVolume(fileInfo, options); fileInfo = GetNextFileInfo(ah, part.FileParts.FirstOrDefault() as FileInfoRarFilePart); yield return part; diff --git a/src/SharpCompress/Archives/Rar/StreamRarArchiveVolume.cs b/src/SharpCompress/Archives/Rar/StreamRarArchiveVolume.cs index 6eedfa84..3da1a531 100644 --- a/src/SharpCompress/Archives/Rar/StreamRarArchiveVolume.cs +++ b/src/SharpCompress/Archives/Rar/StreamRarArchiveVolume.cs @@ -1,16 +1,16 @@ using System.Collections.Generic; using System.IO; -using SharpCompress.Common; using SharpCompress.Common.Rar; using SharpCompress.Common.Rar.Headers; using SharpCompress.IO; +using SharpCompress.Readers; namespace SharpCompress.Archives.Rar { internal class StreamRarArchiveVolume : RarVolume { - internal StreamRarArchiveVolume(Stream stream, string password, Options options) - : base(StreamingMode.Seekable, stream, password, options) + internal StreamRarArchiveVolume(Stream stream, ReaderOptions options) + : base(StreamingMode.Seekable, stream, options) { } @@ -21,7 +21,7 @@ namespace SharpCompress.Archives.Rar internal override RarFilePart CreateFilePart(FileHeader fileHeader, MarkHeader markHeader) { - return new SeekableFilePart(markHeader, fileHeader, Stream, Password); + return new SeekableFilePart(markHeader, fileHeader, Stream, ReaderOptions.Password); } } } \ No newline at end of file diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs index ae4b0ce2..84115144 100644 --- a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs @@ -14,81 +14,48 @@ namespace SharpCompress.Archives.SevenZip private ArchiveDatabase database; #if !NO_FILE -/// -/// Constructor expects a filepath to an existing file. -/// -/// - public static SevenZipArchive Open(string filePath) - { - return Open(filePath, Options.None); - } - - /// - /// Constructor with a FileInfo object to an existing file. - /// - /// - public static SevenZipArchive Open(FileInfo fileInfo) - { - return Open(fileInfo, Options.None); - } - /// /// Constructor expects a filepath to an existing file. /// /// - /// - public static SevenZipArchive Open(string filePath, Options options) + /// + public static SevenZipArchive Open(string filePath, ReaderOptions readerOptions = null) { filePath.CheckNotNullOrEmpty("filePath"); - return Open(new FileInfo(filePath), options); + return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); } /// /// Constructor with a FileInfo object to an existing file. /// /// - /// - public static SevenZipArchive Open(FileInfo fileInfo, Options options) + /// + public static SevenZipArchive Open(FileInfo fileInfo, ReaderOptions readerOptions) { fileInfo.CheckNotNull("fileInfo"); - return new SevenZipArchive(fileInfo, options); + return new SevenZipArchive(fileInfo, readerOptions ?? new ReaderOptions()); } #endif - /// /// Takes a seekable Stream as a source /// /// - public static SevenZipArchive Open(Stream stream) + /// + public static SevenZipArchive Open(Stream stream, ReaderOptions readerOptions = null) { stream.CheckNotNull("stream"); - return Open(stream, Options.None); - } - - /// - /// Takes a seekable Stream as a source - /// - /// - /// - public static SevenZipArchive Open(Stream stream, Options options) - { - stream.CheckNotNull("stream"); - return new SevenZipArchive(stream, options); + return new SevenZipArchive(stream, readerOptions); } #if !NO_FILE - internal SevenZipArchive(FileInfo fileInfo, Options options) - : base(ArchiveType.SevenZip, fileInfo, options, null) + internal SevenZipArchive(FileInfo fileInfo, ReaderOptions readerOptions) + : base(ArchiveType.SevenZip, fileInfo, readerOptions) { } - protected override IEnumerable LoadVolumes(FileInfo file, Options options) + protected override IEnumerable LoadVolumes(FileInfo file) { - if (FlagUtility.HasFlag(options, Options.KeepStreamsOpen)) - { - options = (Options)FlagUtility.SetFlag(options, Options.KeepStreamsOpen, false); - } - return new SevenZipVolume(file.OpenRead(), options).AsEnumerable(); + return new SevenZipVolume(file.OpenRead(), ReaderOptions).AsEnumerable(); } public static bool IsSevenZipFile(string filePath) @@ -109,8 +76,8 @@ namespace SharpCompress.Archives.SevenZip } #endif - internal SevenZipArchive(Stream stream, Options options) - : base(ArchiveType.SevenZip, stream.AsEnumerable(), options, null) + internal SevenZipArchive(Stream stream, ReaderOptions readerOptions) + : base(ArchiveType.SevenZip, stream.AsEnumerable(), readerOptions) { } @@ -119,7 +86,7 @@ namespace SharpCompress.Archives.SevenZip { } - protected override IEnumerable LoadVolumes(IEnumerable streams, Options options) + protected override IEnumerable LoadVolumes(IEnumerable streams) { foreach (Stream s in streams) { @@ -127,7 +94,7 @@ namespace SharpCompress.Archives.SevenZip { throw new ArgumentException("Stream is not readable and seekable"); } - SevenZipVolume volume = new SevenZipVolume(s, options); + SevenZipVolume volume = new SevenZipVolume(s, ReaderOptions); yield return volume; } } @@ -202,7 +169,7 @@ namespace SharpCompress.Archives.SevenZip private CFileItem currentItem; internal SevenZipReader(SevenZipArchive archive) - : base(Options.KeepStreamsOpen, ArchiveType.SevenZip) + : base(new ReaderOptions(), ArchiveType.SevenZip) { this.archive = archive; } diff --git a/src/SharpCompress/Archives/Tar/TarArchive.cs b/src/SharpCompress/Archives/Tar/TarArchive.cs index 5695bd49..b5d8a16a 100644 --- a/src/SharpCompress/Archives/Tar/TarArchive.cs +++ b/src/SharpCompress/Archives/Tar/TarArchive.cs @@ -8,6 +8,7 @@ using SharpCompress.Common.Tar.Headers; using SharpCompress.IO; using SharpCompress.Readers; using SharpCompress.Readers.Tar; +using SharpCompress.Writers; using SharpCompress.Writers.Tar; namespace SharpCompress.Archives.Tar @@ -15,67 +16,39 @@ namespace SharpCompress.Archives.Tar public class TarArchive : AbstractWritableArchive { #if !NO_FILE - -/// -/// Constructor expects a filepath to an existing file. -/// -/// - public static TarArchive Open(string filePath) - { - return Open(filePath, Options.None); - } - - /// - /// Constructor with a FileInfo object to an existing file. - /// - /// - public static TarArchive Open(FileInfo fileInfo) - { - return Open(fileInfo, Options.None); - } - + /// /// Constructor expects a filepath to an existing file. /// /// - /// - public static TarArchive Open(string filePath, Options options) + /// + public static TarArchive Open(string filePath, ReaderOptions readerOptions = null) { filePath.CheckNotNullOrEmpty("filePath"); - return Open(new FileInfo(filePath), options); + return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); } /// /// Constructor with a FileInfo object to an existing file. /// /// - /// - public static TarArchive Open(FileInfo fileInfo, Options options) + /// + public static TarArchive Open(FileInfo fileInfo, ReaderOptions readerOptions = null) { fileInfo.CheckNotNull("fileInfo"); - return new TarArchive(fileInfo, options); + return new TarArchive(fileInfo, readerOptions ?? new ReaderOptions()); } #endif - + /// /// Takes a seekable Stream as a source /// /// - public static TarArchive Open(Stream stream) + /// + public static TarArchive Open(Stream stream, ReaderOptions readerOptions = null) { stream.CheckNotNull("stream"); - return Open(stream, Options.None); - } - - /// - /// Takes a seekable Stream as a source - /// - /// - /// - public static TarArchive Open(Stream stream, Options options) - { - stream.CheckNotNull("stream"); - return new TarArchive(stream, options); + return new TarArchive(stream, readerOptions ?? new ReaderOptions()); } #if !NO_FILE @@ -113,23 +86,20 @@ namespace SharpCompress.Archives.Tar #if !NO_FILE -/// -/// Constructor with a FileInfo object to an existing file. -/// -/// -/// - internal TarArchive(FileInfo fileInfo, Options options) - : base(ArchiveType.Tar, fileInfo, options) + /// + /// Constructor with a FileInfo object to an existing file. + /// + /// + /// + internal TarArchive(FileInfo fileInfo, ReaderOptions readerOptions) + : base(ArchiveType.Tar, fileInfo, readerOptions) { } - protected override IEnumerable LoadVolumes(FileInfo file, Options options) + protected override IEnumerable LoadVolumes(FileInfo file) { - if (FlagUtility.HasFlag(options, Options.KeepStreamsOpen)) - { - options = (Options)FlagUtility.SetFlag(options, Options.KeepStreamsOpen, false); - } - return new TarVolume(file.OpenRead(), options).AsEnumerable(); + + return new TarVolume(file.OpenRead(), ReaderOptions).AsEnumerable(); } #endif @@ -137,9 +107,9 @@ namespace SharpCompress.Archives.Tar /// Takes multiple seekable Streams for a multi-part archive /// /// - /// - internal TarArchive(Stream stream, Options options) - : base(ArchiveType.Tar, stream, options) + /// + internal TarArchive(Stream stream, ReaderOptions readerOptions) + : base(ArchiveType.Tar, stream, readerOptions) { } @@ -148,9 +118,9 @@ namespace SharpCompress.Archives.Tar { } - protected override IEnumerable LoadVolumes(IEnumerable streams, Options options) + protected override IEnumerable LoadVolumes(IEnumerable streams) { - return new TarVolume(streams.First(), options).AsEnumerable(); + return new TarVolume(streams.First(), ReaderOptions).AsEnumerable(); } protected override IEnumerable LoadEntries(IEnumerable volumes) @@ -208,11 +178,11 @@ namespace SharpCompress.Archives.Tar closeStream); } - protected override void SaveTo(Stream stream, CompressionInfo compressionInfo, + protected override void SaveTo(Stream stream, WriterOptions options, IEnumerable oldEntries, IEnumerable newEntries) { - using (var writer = new TarWriter(stream, compressionInfo)) + using (var writer = new TarWriter(stream, new TarWriterOptions(options))) { foreach (var entry in oldEntries.Concat(newEntries) .Where(x => !x.IsDirectory)) diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.cs b/src/SharpCompress/Archives/Zip/ZipArchive.cs index a0c23730..f3a71b34 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.cs @@ -8,6 +8,7 @@ using SharpCompress.Common.Zip.Headers; using SharpCompress.Compressors.Deflate; using SharpCompress.Readers; using SharpCompress.Readers.Zip; +using SharpCompress.Writers; using SharpCompress.Writers.Zip; namespace SharpCompress.Archives.Zip @@ -23,73 +24,37 @@ namespace SharpCompress.Archives.Zip public CompressionLevel DeflateCompressionLevel { get; set; } #if !NO_FILE - /// /// Constructor expects a filepath to an existing file. /// /// - /// - public static ZipArchive Open(string filePath, string password = null) - { - return Open(filePath, Options.None, password); - } - - /// - /// Constructor with a FileInfo object to an existing file. - /// - /// - /// - public static ZipArchive Open(FileInfo fileInfo, string password = null) - { - return Open(fileInfo, Options.None, password); - } - - /// - /// Constructor expects a filepath to an existing file. - /// - /// - /// - /// - public static ZipArchive Open(string filePath, Options options, string password = null) + /// + public static ZipArchive Open(string filePath, ReaderOptions readerOptions = null) { filePath.CheckNotNullOrEmpty("filePath"); - return Open(new FileInfo(filePath), options, password); + return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); } /// /// Constructor with a FileInfo object to an existing file. /// /// - /// - /// - public static ZipArchive Open(FileInfo fileInfo, Options options, string password = null) + /// + public static ZipArchive Open(FileInfo fileInfo, ReaderOptions readerOptions = null) { fileInfo.CheckNotNull("fileInfo"); - return new ZipArchive(fileInfo, options, password); + return new ZipArchive(fileInfo, readerOptions ?? new ReaderOptions()); } #endif - /// /// Takes a seekable Stream as a source /// /// - /// - public static ZipArchive Open(Stream stream, string password = null) + /// + public static ZipArchive Open(Stream stream, ReaderOptions readerOptions = null) { stream.CheckNotNull("stream"); - return Open(stream, Options.None, password); - } - - /// - /// Takes a seekable Stream as a source - /// - /// - /// - /// - public static ZipArchive Open(Stream stream, Options options, string password = null) - { - stream.CheckNotNull("stream"); - return new ZipArchive(stream, options, password); + return new ZipArchive(stream, readerOptions ?? new ReaderOptions()); } #if !NO_FILE @@ -136,25 +101,20 @@ namespace SharpCompress.Archives.Zip #if !NO_FILE -/// -/// Constructor with a FileInfo object to an existing file. -/// -/// -/// -/// - internal ZipArchive(FileInfo fileInfo, Options options, string password = null) - : base(ArchiveType.Zip, fileInfo, options) + /// + /// Constructor with a FileInfo object to an existing file. + /// + /// + /// + internal ZipArchive(FileInfo fileInfo, ReaderOptions readerOptions) + : base(ArchiveType.Zip, fileInfo, readerOptions) { - headerFactory = new SeekableZipHeaderFactory(password); + headerFactory = new SeekableZipHeaderFactory(readerOptions.Password); } - protected override IEnumerable LoadVolumes(FileInfo file, Options options) + protected override IEnumerable LoadVolumes(FileInfo file) { - if (FlagUtility.HasFlag(options, Options.KeepStreamsOpen)) - { - options = (Options)FlagUtility.SetFlag(options, Options.KeepStreamsOpen, false); - } - return new ZipVolume(file.OpenRead(), options).AsEnumerable(); + return new ZipVolume(file.OpenRead(), ReaderOptions).AsEnumerable(); } #endif @@ -167,17 +127,16 @@ namespace SharpCompress.Archives.Zip /// Takes multiple seekable Streams for a multi-part archive /// /// - /// - /// - internal ZipArchive(Stream stream, Options options, string password = null) - : base(ArchiveType.Zip, stream, options) + /// + internal ZipArchive(Stream stream, ReaderOptions readerOptions) + : base(ArchiveType.Zip, stream, readerOptions) { - headerFactory = new SeekableZipHeaderFactory(password); + headerFactory = new SeekableZipHeaderFactory(readerOptions.Password); } - protected override IEnumerable LoadVolumes(IEnumerable streams, Options options) + protected override IEnumerable LoadVolumes(IEnumerable streams) { - return new ZipVolume(streams.First(), options).AsEnumerable(); + return new ZipVolume(streams.First(), ReaderOptions).AsEnumerable(); } protected override IEnumerable LoadEntries(IEnumerable volumes) @@ -209,18 +168,18 @@ namespace SharpCompress.Archives.Zip } } - protected override void SaveTo(Stream stream, CompressionInfo compressionInfo, + protected override void SaveTo(Stream stream, WriterOptions options, IEnumerable oldEntries, IEnumerable newEntries) { - using (var writer = new ZipWriter(stream, compressionInfo, string.Empty)) + using (var writer = new ZipWriter(stream, new ZipWriterOptions(options))) { foreach (var entry in oldEntries.Concat(newEntries) .Where(x => !x.IsDirectory)) { using (var entryStream = entry.OpenEntryStream()) { - writer.Write(entry.Key, entryStream, entry.LastModifiedTime, string.Empty); + writer.Write(entry.Key, entryStream, entry.LastModifiedTime); } } } diff --git a/src/SharpCompress/Common/CompressionInfo.cs b/src/SharpCompress/Common/CompressionInfo.cs deleted file mode 100644 index 13d69df9..00000000 --- a/src/SharpCompress/Common/CompressionInfo.cs +++ /dev/null @@ -1,30 +0,0 @@ -using SharpCompress.Compressors.Deflate; - -namespace SharpCompress.Common -{ - /// - /// Detailed compression properties when saving. - /// - public class CompressionInfo - { - public CompressionInfo() - { - DeflateCompressionLevel = CompressionLevel.Default; - } - - /// - /// The algorthm to use. Must be valid for the format type. - /// - public CompressionType Type { get; set; } - - /// - /// When CompressionType.Deflate is used, this property is referenced. Defaults to CompressionLevel.Default. - /// - public CompressionLevel DeflateCompressionLevel { get; set; } - - public static implicit operator CompressionInfo(CompressionType compressionType) - { - return new CompressionInfo {Type = compressionType}; - } - } -} \ No newline at end of file diff --git a/src/SharpCompress/Common/GZip/GZipVolume.cs b/src/SharpCompress/Common/GZip/GZipVolume.cs index 1965838f..d37c3649 100644 --- a/src/SharpCompress/Common/GZip/GZipVolume.cs +++ b/src/SharpCompress/Common/GZip/GZipVolume.cs @@ -1,18 +1,20 @@ using System.IO; +using SharpCompress.Readers; namespace SharpCompress.Common.GZip { public class GZipVolume : Volume { - public GZipVolume(Stream stream, Options options) + public GZipVolume(Stream stream, ReaderOptions options) : base(stream, options) { } #if !NO_FILE - public GZipVolume(FileInfo fileInfo, Options options) + public GZipVolume(FileInfo fileInfo, ReaderOptions options) : base(fileInfo.OpenRead(), options) { + options.LeaveOpenStream = false; } #endif diff --git a/src/SharpCompress/Common/Options.cs b/src/SharpCompress/Common/Options.cs deleted file mode 100644 index 25286013..00000000 --- a/src/SharpCompress/Common/Options.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; - -namespace SharpCompress.Common -{ - [Flags] - public enum Options - { - /// - /// No options specified - /// - None = 0, - - /// - /// SharpCompress will keep the supplied streams open - /// - KeepStreamsOpen = 1, - - /// - /// Look for RarArchive (Check for self-extracting archives or cases where RarArchive isn't at the start of the file) - /// - LookForHeader = 2 - } -} \ No newline at end of file diff --git a/src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs b/src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs index 0a9e5fbc..0407cac1 100644 --- a/src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs +++ b/src/SharpCompress/Common/Rar/Headers/RarHeaderFactory.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; using SharpCompress.IO; +using SharpCompress.Readers; namespace SharpCompress.Common.Rar.Headers { @@ -9,21 +10,19 @@ namespace SharpCompress.Common.Rar.Headers { private const int MAX_SFX_SIZE = 0x80000 - 16; //archive.cpp line 136 - internal RarHeaderFactory(StreamingMode mode, Options options, string password = null) + internal RarHeaderFactory(StreamingMode mode, ReaderOptions options) { StreamingMode = mode; Options = options; - Password = password; } - private Options Options { get; } - public string Password { get; private set; } + private ReaderOptions Options { get; } internal StreamingMode StreamingMode { get; } internal bool IsEncrypted { get; private set; } internal IEnumerable ReadHeaders(Stream stream) { - if (Options.HasFlag(Options.LookForHeader)) + if (Options.LookForHeader) { stream = CheckSFX(stream); } @@ -91,7 +90,7 @@ namespace SharpCompress.Common.Rar.Headers } catch (Exception e) { - if (!Options.HasFlag(Options.KeepStreamsOpen)) + if (!Options.LeaveOpenStream) { #if NET35 reader.Close(); @@ -117,11 +116,11 @@ namespace SharpCompress.Common.Rar.Headers private RarHeader ReadNextHeader(Stream stream) { #if !NO_CRYPTO - var reader = new RarCryptoBinaryReader(stream, Password); + var reader = new RarCryptoBinaryReader(stream, Options.Password); if (IsEncrypted) { - if (Password == null) + if (Options.Password == null) { throw new CryptographicException("Encrypted Rar archive has no password specified."); } @@ -223,7 +222,7 @@ namespace SharpCompress.Common.Rar.Headers else { #if !NO_CRYPTO - fh.PackedStream = new RarCryptoWrapper(ms, Password, fh.Salt); + fh.PackedStream = new RarCryptoWrapper(ms, Options.Password, fh.Salt); #else throw new NotSupportedException("RarCrypto not supported"); #endif diff --git a/src/SharpCompress/Common/Rar/RarVolume.cs b/src/SharpCompress/Common/Rar/RarVolume.cs index e2a4abd1..c7e9729f 100644 --- a/src/SharpCompress/Common/Rar/RarVolume.cs +++ b/src/SharpCompress/Common/Rar/RarVolume.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using SharpCompress.Common.Rar.Headers; using SharpCompress.IO; +using SharpCompress.Readers; namespace SharpCompress.Common.Rar { @@ -14,15 +15,12 @@ namespace SharpCompress.Common.Rar { private readonly RarHeaderFactory headerFactory; - internal RarVolume(StreamingMode mode, Stream stream, string password, Options options) + internal RarVolume(StreamingMode mode, Stream stream, ReaderOptions options) : base(stream, options) { - headerFactory = new RarHeaderFactory(mode, options, password); - Password = password; + headerFactory = new RarHeaderFactory(mode, options); } - internal string Password { get; private set; } - internal StreamingMode Mode { get { return headerFactory.StreamingMode; } } internal abstract IEnumerable ReadFileParts(); diff --git a/src/SharpCompress/Common/SevenZip/SevenZipVolume.cs b/src/SharpCompress/Common/SevenZip/SevenZipVolume.cs index 9b2a849a..32b50cc7 100644 --- a/src/SharpCompress/Common/SevenZip/SevenZipVolume.cs +++ b/src/SharpCompress/Common/SevenZip/SevenZipVolume.cs @@ -1,11 +1,13 @@ using System.IO; +using SharpCompress.Archives; +using SharpCompress.Readers; namespace SharpCompress.Common.SevenZip { public class SevenZipVolume : Volume { - public SevenZipVolume(Stream stream, Options options) - : base(stream, options) + public SevenZipVolume(Stream stream, ReaderOptions readerFactoryOptions) + : base(stream, readerFactoryOptions) { } } diff --git a/src/SharpCompress/Common/Tar/TarVolume.cs b/src/SharpCompress/Common/Tar/TarVolume.cs index 5e445683..1ddae4d7 100644 --- a/src/SharpCompress/Common/Tar/TarVolume.cs +++ b/src/SharpCompress/Common/Tar/TarVolume.cs @@ -1,11 +1,12 @@ using System.IO; +using SharpCompress.Readers; namespace SharpCompress.Common.Tar { public class TarVolume : Volume { - public TarVolume(Stream stream, Options options) - : base(stream, options) + public TarVolume(Stream stream, ReaderOptions readerOptions) + : base(stream, readerOptions) { } } diff --git a/src/SharpCompress/Common/Volume.cs b/src/SharpCompress/Common/Volume.cs index 1d1f8ac6..1f10f823 100644 --- a/src/SharpCompress/Common/Volume.cs +++ b/src/SharpCompress/Common/Volume.cs @@ -1,5 +1,6 @@ using System.IO; using SharpCompress.IO; +using SharpCompress.Readers; namespace SharpCompress.Common { @@ -7,15 +8,15 @@ namespace SharpCompress.Common { private readonly Stream actualStream; - internal Volume(Stream stream, Options options) + internal Volume(Stream stream, ReaderOptions readerFactoryOptions) { actualStream = stream; - Options = options; + ReaderOptions = readerFactoryOptions; } internal Stream Stream { get { return new NonDisposingStream(actualStream); } } - internal Options Options { get; } + protected ReaderOptions ReaderOptions { get; } /// /// RarArchive is the first volume of a multi-part archive. @@ -32,7 +33,7 @@ namespace SharpCompress.Common public void Dispose() { - if (!Options.HasFlag(Options.KeepStreamsOpen) && !disposed) + if (!ReaderOptions.LeaveOpenStream && !disposed) { actualStream.Dispose(); disposed = true; diff --git a/src/SharpCompress/Common/Zip/ZipVolume.cs b/src/SharpCompress/Common/Zip/ZipVolume.cs index 92ee8910..4f6d52c8 100644 --- a/src/SharpCompress/Common/Zip/ZipVolume.cs +++ b/src/SharpCompress/Common/Zip/ZipVolume.cs @@ -1,11 +1,12 @@ using System.IO; +using SharpCompress.Readers; namespace SharpCompress.Common.Zip { public class ZipVolume : Volume { - public ZipVolume(Stream stream, Options options) - : base(stream, options) + public ZipVolume(Stream stream, ReaderOptions readerOptions) + : base(stream, readerOptions) { } diff --git a/src/SharpCompress/Readers/AbstractReader.cs b/src/SharpCompress/Readers/AbstractReader.cs index fd4c3e5c..810d7c5e 100644 --- a/src/SharpCompress/Readers/AbstractReader.cs +++ b/src/SharpCompress/Readers/AbstractReader.cs @@ -23,13 +23,13 @@ namespace SharpCompress.Readers public event EventHandler CompressedBytesRead; public event EventHandler FilePartExtractionBegin; - internal AbstractReader(Options options, ArchiveType archiveType) + internal AbstractReader(ReaderOptions options, ArchiveType archiveType) { ArchiveType = archiveType; Options = options; } - internal Options Options { get; private set; } + internal ReaderOptions Options { get; private set; } public ArchiveType ArchiveType { get; } diff --git a/src/SharpCompress/Readers/GZip/GZipReader.cs b/src/SharpCompress/Readers/GZip/GZipReader.cs index 1f8d6c3c..4150428a 100644 --- a/src/SharpCompress/Readers/GZip/GZipReader.cs +++ b/src/SharpCompress/Readers/GZip/GZipReader.cs @@ -7,7 +7,7 @@ namespace SharpCompress.Readers.GZip { public class GZipReader : AbstractReader { - internal GZipReader(Stream stream, Options options) + internal GZipReader(Stream stream, ReaderOptions options) : base(options, ArchiveType.GZip) { Volume = new GZipVolume(stream, options); @@ -23,11 +23,10 @@ namespace SharpCompress.Readers.GZip /// /// /// - public static GZipReader Open(Stream stream, - Options options = Options.KeepStreamsOpen) + public static GZipReader Open(Stream stream, ReaderOptions options = null) { stream.CheckNotNull("stream"); - return new GZipReader(stream, options); + return new GZipReader(stream, options ?? new ReaderOptions()); } #endregion diff --git a/src/SharpCompress/Readers/Rar/MultiVolumeRarReader.cs b/src/SharpCompress/Readers/Rar/MultiVolumeRarReader.cs index 9384d44a..b3bf672f 100644 --- a/src/SharpCompress/Readers/Rar/MultiVolumeRarReader.cs +++ b/src/SharpCompress/Readers/Rar/MultiVolumeRarReader.cs @@ -12,7 +12,7 @@ namespace SharpCompress.Readers.Rar private readonly IEnumerator streams; private Stream tempStream; - internal MultiVolumeRarReader(IEnumerable streams, Options options) + internal MultiVolumeRarReader(IEnumerable streams, ReaderOptions options) : base(options) { this.streams = streams.GetEnumerator(); diff --git a/src/SharpCompress/Readers/Rar/RarReader.cs b/src/SharpCompress/Readers/Rar/RarReader.cs index 98a90a7e..5c1f59d0 100644 --- a/src/SharpCompress/Readers/Rar/RarReader.cs +++ b/src/SharpCompress/Readers/Rar/RarReader.cs @@ -12,11 +12,10 @@ namespace SharpCompress.Readers.Rar /// public abstract class RarReader : AbstractReader { - public string Password { get; set; } private RarVolume volume; private readonly Unpack pack = new Unpack(); - internal RarReader(Options options) + internal RarReader(ReaderOptions options) : base(options, ArchiveType.Rar) { } @@ -33,9 +32,10 @@ namespace SharpCompress.Readers.Rar /// /// /// - public static RarReader Open(Stream stream, Options options = Options.KeepStreamsOpen) + public static RarReader Open(Stream stream, ReaderOptions options = null) { - return Open(stream, null, options); + stream.CheckNotNull("stream"); + return new SingleVolumeRarReader(stream, options ?? new ReaderOptions()); } /// @@ -44,17 +44,17 @@ namespace SharpCompress.Readers.Rar /// /// /// - public static RarReader Open(IEnumerable streams, Options options = Options.KeepStreamsOpen) + public static RarReader Open(IEnumerable streams, ReaderOptions options = null) { streams.CheckNotNull("streams"); - return new MultiVolumeRarReader(streams, options); + return new MultiVolumeRarReader(streams, options ?? new ReaderOptions()); } #endregion internal override IEnumerable GetEntries(Stream stream) { - volume = new RarReaderVolume(stream, Password, Options); + volume = new RarReaderVolume(stream, Options); foreach (RarFilePart fp in volume.ReadFileParts()) { ValidateArchive(volume); @@ -73,11 +73,5 @@ namespace SharpCompress.Readers.Rar new MultiVolumeReadOnlyStream( CreateFilePartEnumerableForCurrentEntry().Cast(), this))); } - - public static RarReader Open(Stream stream, string password, Options options = Options.KeepStreamsOpen) - { - stream.CheckNotNull("stream"); - return new SingleVolumeRarReader(stream, password, options); - } } } \ No newline at end of file diff --git a/src/SharpCompress/Readers/Rar/RarReaderVolume.cs b/src/SharpCompress/Readers/Rar/RarReaderVolume.cs index 01b88f53..bf68043f 100644 --- a/src/SharpCompress/Readers/Rar/RarReaderVolume.cs +++ b/src/SharpCompress/Readers/Rar/RarReaderVolume.cs @@ -1,6 +1,5 @@ using System.Collections.Generic; using System.IO; -using SharpCompress.Common; using SharpCompress.Common.Rar; using SharpCompress.Common.Rar.Headers; using SharpCompress.IO; @@ -9,8 +8,8 @@ namespace SharpCompress.Readers.Rar { public class RarReaderVolume : RarVolume { - internal RarReaderVolume(Stream stream, string password, Options options) - : base(StreamingMode.Streaming, stream, password, options) + internal RarReaderVolume(Stream stream, ReaderOptions options) + : base(StreamingMode.Streaming, stream, options) { } diff --git a/src/SharpCompress/Readers/Rar/SingleVolumeRarReader.cs b/src/SharpCompress/Readers/Rar/SingleVolumeRarReader.cs index 4bb2a1bf..3ffede69 100644 --- a/src/SharpCompress/Readers/Rar/SingleVolumeRarReader.cs +++ b/src/SharpCompress/Readers/Rar/SingleVolumeRarReader.cs @@ -8,10 +8,9 @@ namespace SharpCompress.Readers.Rar { private readonly Stream stream; - internal SingleVolumeRarReader(Stream stream, string password, Options options) + internal SingleVolumeRarReader(Stream stream, ReaderOptions options) : base(options) { - Password = password; this.stream = stream; } diff --git a/src/SharpCompress/Readers/ReaderFactory.cs b/src/SharpCompress/Readers/ReaderFactory.cs index 8c2ea4af..464a60f5 100644 --- a/src/SharpCompress/Readers/ReaderFactory.cs +++ b/src/SharpCompress/Readers/ReaderFactory.cs @@ -24,16 +24,19 @@ namespace SharpCompress.Readers /// /// /// - public static IReader Open(Stream stream, Options options = Options.KeepStreamsOpen) + public static IReader Open(Stream stream, ReaderOptions options = null) { stream.CheckNotNull("stream"); - + options = options ?? new ReaderOptions() + { + LeaveOpenStream = false + }; RewindableStream rewindableStream = new RewindableStream(stream); rewindableStream.StartRecording(); if (ZipArchive.IsZipFile(rewindableStream, null)) { rewindableStream.Rewind(true); - return ZipReader.Open(rewindableStream, null, options); + return ZipReader.Open(rewindableStream, options); } rewindableStream.Rewind(false); if (GZipArchive.IsGZipFile(rewindableStream)) @@ -43,7 +46,7 @@ namespace SharpCompress.Readers if (TarArchive.IsTarFile(testStream)) { rewindableStream.Rewind(true); - return new TarReader(rewindableStream, CompressionType.GZip, options); + return new TarReader(rewindableStream, options, CompressionType.GZip); } rewindableStream.Rewind(true); return GZipReader.Open(rewindableStream, options); @@ -57,7 +60,7 @@ namespace SharpCompress.Readers if (TarArchive.IsTarFile(testStream)) { rewindableStream.Rewind(true); - return new TarReader(rewindableStream, CompressionType.BZip2, options); + return new TarReader(rewindableStream, options, CompressionType.BZip2); } } diff --git a/src/SharpCompress/Readers/ReaderOptions.cs b/src/SharpCompress/Readers/ReaderOptions.cs new file mode 100644 index 00000000..f7617c65 --- /dev/null +++ b/src/SharpCompress/Readers/ReaderOptions.cs @@ -0,0 +1,18 @@ +using SharpCompress.Common; + +namespace SharpCompress.Readers +{ + public class ReaderOptions + { + /// + /// SharpCompress will keep the supplied streams open. Default is true. + /// + public virtual bool LeaveOpenStream { get; set; } = true; + + /// + /// Look for RarArchive (Check for self-extracting archives or cases where RarArchive isn't at the start of the file) + /// + public bool LookForHeader { get; set; } + public string Password { get; set; } + } +} \ No newline at end of file diff --git a/src/SharpCompress/Readers/Tar/TarReader.cs b/src/SharpCompress/Readers/Tar/TarReader.cs index 04847140..bc8a5642 100644 --- a/src/SharpCompress/Readers/Tar/TarReader.cs +++ b/src/SharpCompress/Readers/Tar/TarReader.cs @@ -16,8 +16,7 @@ namespace SharpCompress.Readers.Tar { private readonly CompressionType compressionType; - internal TarReader(Stream stream, CompressionType compressionType, - Options options) + internal TarReader(Stream stream, ReaderOptions options, CompressionType compressionType) : base(options, ArchiveType.Tar) { this.compressionType = compressionType; @@ -58,10 +57,10 @@ namespace SharpCompress.Readers.Tar /// /// /// - public static TarReader Open(Stream stream, Options options = Options.KeepStreamsOpen) + public static TarReader Open(Stream stream, ReaderOptions options = null) { stream.CheckNotNull("stream"); - + options = options ?? new ReaderOptions(); RewindableStream rewindableStream = new RewindableStream(stream); rewindableStream.StartRecording(); if (GZipArchive.IsGZipFile(rewindableStream)) @@ -71,7 +70,7 @@ namespace SharpCompress.Readers.Tar if (TarArchive.IsTarFile(testStream)) { rewindableStream.Rewind(true); - return new TarReader(rewindableStream, CompressionType.GZip, options); + return new TarReader(rewindableStream, options, CompressionType.GZip); } throw new InvalidFormatException("Not a tar file."); } @@ -84,12 +83,12 @@ namespace SharpCompress.Readers.Tar if (TarArchive.IsTarFile(testStream)) { rewindableStream.Rewind(true); - return new TarReader(rewindableStream, CompressionType.BZip2, options); + return new TarReader(rewindableStream, options, CompressionType.BZip2); } throw new InvalidFormatException("Not a tar file."); } rewindableStream.Rewind(true); - return new TarReader(rewindableStream, CompressionType.None, options); + return new TarReader(rewindableStream, options, CompressionType.None); } #endregion diff --git a/src/SharpCompress/Readers/Zip/ZipReader.cs b/src/SharpCompress/Readers/Zip/ZipReader.cs index f50f63b2..7500509b 100644 --- a/src/SharpCompress/Readers/Zip/ZipReader.cs +++ b/src/SharpCompress/Readers/Zip/ZipReader.cs @@ -10,11 +10,11 @@ namespace SharpCompress.Readers.Zip { private readonly StreamingZipHeaderFactory headerFactory; - internal ZipReader(Stream stream, Options options, string password) + internal ZipReader(Stream stream, ReaderOptions options) : base(options, ArchiveType.Zip) { Volume = new ZipVolume(stream, options); - headerFactory = new StreamingZipHeaderFactory(password); + headerFactory = new StreamingZipHeaderFactory(options.Password); } public override ZipVolume Volume { get; } @@ -26,13 +26,11 @@ namespace SharpCompress.Readers.Zip /// /// /// - /// /// - public static ZipReader Open(Stream stream, string password = null, - Options options = Options.KeepStreamsOpen) + public static ZipReader Open(Stream stream, ReaderOptions options = null) { stream.CheckNotNull("stream"); - return new ZipReader(stream, options, password); + return new ZipReader(stream, options ?? new ReaderOptions()); } #endregion diff --git a/src/SharpCompress/Writers/Tar/TarWriter.cs b/src/SharpCompress/Writers/Tar/TarWriter.cs index 0e5f484b..59344813 100644 --- a/src/SharpCompress/Writers/Tar/TarWriter.cs +++ b/src/SharpCompress/Writers/Tar/TarWriter.cs @@ -10,33 +10,33 @@ namespace SharpCompress.Writers.Tar { public class TarWriter : AbstractWriter { - public TarWriter(Stream destination, CompressionInfo compressionInfo, bool leaveOpen = false) + public TarWriter(Stream destination, TarWriterOptions options) : base(ArchiveType.Tar) { if (!destination.CanWrite) { throw new ArgumentException("Tars require writable streams."); } - switch (compressionInfo.Type) + switch (options.CompressionType) { case CompressionType.None: break; case CompressionType.BZip2: { - destination = new BZip2Stream(destination, CompressionMode.Compress, leaveOpen); + destination = new BZip2Stream(destination, CompressionMode.Compress, options.LeaveOpenStream); } break; case CompressionType.GZip: { - destination = new GZipStream(destination, CompressionMode.Compress, leaveOpen); + destination = new GZipStream(destination, CompressionMode.Compress, options.LeaveOpenStream); } break; default: { - throw new InvalidFormatException("Tar does not support compression: " + compressionInfo.Type); + throw new InvalidFormatException("Tar does not support compression: " + options.CompressionType); } } - InitalizeStream(destination, !leaveOpen); + InitalizeStream(destination, !options.LeaveOpenStream); } public override void Write(string filename, Stream source, DateTime? modificationTime) diff --git a/src/SharpCompress/Writers/Tar/TarWriterOptions.cs b/src/SharpCompress/Writers/Tar/TarWriterOptions.cs new file mode 100644 index 00000000..1ad1d077 --- /dev/null +++ b/src/SharpCompress/Writers/Tar/TarWriterOptions.cs @@ -0,0 +1,22 @@ +using SharpCompress.Common; + +namespace SharpCompress.Writers.Tar +{ + public class TarWriterOptions + { + public TarWriterOptions() + { + + } + + internal TarWriterOptions(WriterOptions readerOptions) + { + LeaveOpenStream = readerOptions.LeaveOpenStream; + CompressionType = readerOptions.CompressionType; + } + + public bool LeaveOpenStream { get; set; } + + public CompressionType CompressionType { get; set; } = CompressionType.Unknown; + } +} \ No newline at end of file diff --git a/src/SharpCompress/Writers/WriterFactory.cs b/src/SharpCompress/Writers/WriterFactory.cs index 60c727bd..c558d16e 100644 --- a/src/SharpCompress/Writers/WriterFactory.cs +++ b/src/SharpCompress/Writers/WriterFactory.cs @@ -9,33 +9,25 @@ namespace SharpCompress.Writers { public static class WriterFactory { - public static IWriter Open(Stream stream, ArchiveType archiveType, CompressionType compressionType, bool leaveOpen = false) - { - return Open(stream, archiveType, new CompressionInfo - { - Type = compressionType - }, leaveOpen); - } - - public static IWriter Open(Stream stream, ArchiveType archiveType, CompressionInfo compressionInfo, bool leaveOpen = false) + public static IWriter Open(Stream stream, ArchiveType archiveType, WriterOptions writerOptions) { switch (archiveType) { case ArchiveType.GZip: { - if (compressionInfo.Type != CompressionType.GZip) + if (writerOptions.CompressionType != CompressionType.GZip) { throw new InvalidFormatException("GZip archives only support GZip compression type."); } - return new GZipWriter(stream, leaveOpen); + return new GZipWriter(stream, writerOptions.LeaveOpenStream); } case ArchiveType.Zip: { - return new ZipWriter(stream, compressionInfo, null, leaveOpen); + return new ZipWriter(stream, new ZipWriterOptions(writerOptions)); } case ArchiveType.Tar: { - return new TarWriter(stream, compressionInfo, leaveOpen); + return new TarWriter(stream, new TarWriterOptions(writerOptions)); } default: { diff --git a/src/SharpCompress/Writers/WriterOptions.cs b/src/SharpCompress/Writers/WriterOptions.cs new file mode 100644 index 00000000..dcf8f338 --- /dev/null +++ b/src/SharpCompress/Writers/WriterOptions.cs @@ -0,0 +1,27 @@ +using SharpCompress.Common; +using SharpCompress.Compressors.Deflate; + +namespace SharpCompress.Writers +{ + public class WriterOptions + { + public WriterOptions(CompressionType compressionType) + { + CompressionType = compressionType; + } + public CompressionType CompressionType { get; set; } = CompressionType.Unknown; + + public bool LeaveOpenStream { get; set; } + + + /// + /// When CompressionType.Deflate is used, this property is referenced. Defaults to CompressionLevel.Default. + /// + public CompressionLevel DeflateCompressionLevel { get; set; } = CompressionLevel.Default; + + public static implicit operator WriterOptions(CompressionType compressionType) + { + return new WriterOptions(compressionType); + } + } +} \ No newline at end of file diff --git a/src/SharpCompress/Writers/Zip/ZipCompressionInfo.cs b/src/SharpCompress/Writers/Zip/ZipCompressionInfo.cs deleted file mode 100644 index d4e20183..00000000 --- a/src/SharpCompress/Writers/Zip/ZipCompressionInfo.cs +++ /dev/null @@ -1,47 +0,0 @@ -using SharpCompress.Common; -using SharpCompress.Common.Zip; -using SharpCompress.Compressors.Deflate; - -namespace SharpCompress.Writers.Zip -{ - internal class ZipCompressionInfo - { - internal CompressionLevel DeflateCompressionLevel { get; private set; } - internal ZipCompressionMethod Compression { get; private set; } - - public ZipCompressionInfo(CompressionInfo compressionInfo) - { - switch (compressionInfo.Type) - { - case CompressionType.None: - { - Compression = ZipCompressionMethod.None; - } - break; - case CompressionType.Deflate: - { - DeflateCompressionLevel = compressionInfo.DeflateCompressionLevel; - Compression = ZipCompressionMethod.Deflate; - } - break; - case CompressionType.BZip2: - { - Compression = ZipCompressionMethod.BZip2; - } - break; - case CompressionType.LZMA: - { - Compression = ZipCompressionMethod.LZMA; - } - break; - case CompressionType.PPMd: - { - Compression = ZipCompressionMethod.PPMd; - } - break; - default: - throw new InvalidFormatException("Invalid compression method: " + compressionInfo.Type); - } - } - } -} \ No newline at end of file diff --git a/src/SharpCompress/Writers/Zip/ZipWriter.cs b/src/SharpCompress/Writers/Zip/ZipWriter.cs index 7abefef3..bca7a751 100644 --- a/src/SharpCompress/Writers/Zip/ZipWriter.cs +++ b/src/SharpCompress/Writers/Zip/ZipWriter.cs @@ -17,19 +17,21 @@ namespace SharpCompress.Writers.Zip { public class ZipWriter : AbstractWriter { - private readonly ZipCompressionInfo zipCompressionInfo; + private readonly CompressionType compressionType; + private readonly CompressionLevel compressionLevel; private readonly PpmdProperties ppmdProperties = new PpmdProperties(); // Caching properties to speed up PPMd private readonly List entries = new List(); private readonly string zipComment; private long streamPosition; - public ZipWriter(Stream destination, CompressionInfo compressionInfo, string zipComment, bool leaveOpen = false) + public ZipWriter(Stream destination, ZipWriterOptions zipWriterOptions) : base(ArchiveType.Zip) { - this.zipComment = zipComment ?? string.Empty; + zipComment = zipWriterOptions.ArchiveComment ?? string.Empty; - zipCompressionInfo = new ZipCompressionInfo(compressionInfo); - InitalizeStream(destination, !leaveOpen); + compressionType = zipWriterOptions.CompressionType; + compressionLevel = zipWriterOptions.DeflateCompressionLevel; + InitalizeStream(destination, !zipWriterOptions.LeaveOpenStream); } protected override void Dispose(bool isDisposing) @@ -39,42 +41,75 @@ namespace SharpCompress.Writers.Zip uint size = 0; foreach (ZipCentralDirectoryEntry entry in entries) { - size += entry.Write(OutputStream, zipCompressionInfo.Compression); + size += entry.Write(OutputStream, ToZipCompressionMethod(compressionType)); } WriteEndRecord(size); } base.Dispose(isDisposing); } + private static ZipCompressionMethod ToZipCompressionMethod(CompressionType compressionType) + { + switch (compressionType) + { + case CompressionType.None: + { + return ZipCompressionMethod.None; + } + case CompressionType.Deflate: + { + return ZipCompressionMethod.Deflate; + } + case CompressionType.BZip2: + { + return ZipCompressionMethod.BZip2; + } + case CompressionType.LZMA: + { + return ZipCompressionMethod.LZMA; + } + case CompressionType.PPMd: + { + return ZipCompressionMethod.PPMd; + } + default: + throw new InvalidFormatException("Invalid compression method: " + compressionType); + } + } public override void Write(string entryPath, Stream source, DateTime? modificationTime) { - Write(entryPath, source, modificationTime, null); + Write(entryPath, source, new ZipWriterEntryOptions() + { + ModificationDateTime = modificationTime + }); } - public void Write(string entryPath, Stream source, DateTime? modificationTime, string comment, CompressionInfo compressionInfo = null) + public void Write(string entryPath, Stream source, ZipWriterEntryOptions zipWriterEntryOptions) { - using (Stream output = WriteToStream(entryPath, modificationTime, comment, compressionInfo)) + using (Stream output = WriteToStream(entryPath, zipWriterEntryOptions)) { source.TransferTo(output); } } - public Stream WriteToStream(string entryPath, DateTime? modificationTime, string comment, CompressionInfo compressionInfo = null) + public Stream WriteToStream(string entryPath, ZipWriterEntryOptions options) { entryPath = NormalizeFilename(entryPath); - modificationTime = modificationTime ?? DateTime.Now; - comment = comment ?? ""; + options.ModificationDateTime = options.ModificationDateTime ?? DateTime.Now; + options.EntryComment = options.EntryComment ?? string.Empty; var entry = new ZipCentralDirectoryEntry { - Comment = comment, + Comment = options.EntryComment, FileName = entryPath, - ModificationTime = modificationTime, + ModificationTime = options.ModificationDateTime, HeaderOffset = (uint)streamPosition }; - var headersize = (uint)WriteHeader(entryPath, modificationTime, compressionInfo); + var headersize = (uint)WriteHeader(entryPath, options); streamPosition += headersize; - return new ZipWritingStream(this, OutputStream, entry, compressionInfo); + return new ZipWritingStream(this, OutputStream, entry, + ToZipCompressionMethod(options.CompressionType ?? compressionType), + options.DeflateCompressionLevel ?? compressionLevel); } private string NormalizeFilename(string filename) @@ -90,13 +125,13 @@ namespace SharpCompress.Writers.Zip return filename.Trim('/'); } - private int WriteHeader(string filename, DateTime? modificationTime, CompressionInfo compressionInfo = null) + private int WriteHeader(string filename, ZipWriterEntryOptions zipWriterEntryOptions) { - var explicitZipCompressionInfo = compressionInfo != null ? new ZipCompressionInfo(compressionInfo) : zipCompressionInfo; + var explicitZipCompressionInfo = ToZipCompressionMethod(zipWriterEntryOptions.CompressionType ?? compressionType); byte[] encodedFilename = ArchiveEncoding.Default.GetBytes(filename); OutputStream.Write(DataConverter.LittleEndian.GetBytes(ZipHeaderFactory.ENTRY_HEADER_BYTES), 0, 4); - if (explicitZipCompressionInfo.Compression == ZipCompressionMethod.Deflate) + if (explicitZipCompressionInfo == ZipCompressionMethod.Deflate) { OutputStream.Write(new byte[] {20, 0}, 0, 2); //older version which is more compatible } @@ -108,14 +143,14 @@ namespace SharpCompress.Writers.Zip if (!OutputStream.CanSeek) { flags |= HeaderFlags.UsePostDataDescriptor; - if (explicitZipCompressionInfo.Compression == ZipCompressionMethod.LZMA) + if (explicitZipCompressionInfo == ZipCompressionMethod.LZMA) { flags |= HeaderFlags.Bit1; // eos marker } } OutputStream.Write(DataConverter.LittleEndian.GetBytes((ushort)flags), 0, 2); - OutputStream.Write(DataConverter.LittleEndian.GetBytes((ushort)explicitZipCompressionInfo.Compression), 0, 2); // zipping method - OutputStream.Write(DataConverter.LittleEndian.GetBytes(modificationTime.DateTimeToDosTime()), 0, 4); + OutputStream.Write(DataConverter.LittleEndian.GetBytes((ushort)explicitZipCompressionInfo), 0, 2); // zipping method + OutputStream.Write(DataConverter.LittleEndian.GetBytes(zipWriterEntryOptions.ModificationDateTime.DateTimeToDosTime()), 0, 4); // zipping date and time OutputStream.Write(new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 12); @@ -157,17 +192,20 @@ namespace SharpCompress.Writers.Zip private readonly Stream originalStream; private readonly Stream writeStream; private readonly ZipWriter writer; - private readonly ZipCompressionInfo compressionInfo; + private readonly ZipCompressionMethod zipCompressionMethod; + private readonly CompressionLevel compressionLevel; private CountingWritableSubStream counting; private uint decompressed; - internal ZipWritingStream(ZipWriter writer, Stream originalStream, ZipCentralDirectoryEntry entry, CompressionInfo compressionInfo) + internal ZipWritingStream(ZipWriter writer, Stream originalStream, ZipCentralDirectoryEntry entry, + ZipCompressionMethod zipCompressionMethod, CompressionLevel compressionLevel) { this.writer = writer; this.originalStream = originalStream; this.writer = writer; this.entry = entry; - this.compressionInfo = compressionInfo == null ? writer.zipCompressionInfo : new ZipCompressionInfo(compressionInfo); + this.zipCompressionMethod = zipCompressionMethod; + this.compressionLevel = compressionLevel; writeStream = GetWriteStream(originalStream); } @@ -185,7 +223,7 @@ namespace SharpCompress.Writers.Zip { counting = new CountingWritableSubStream(writeStream); Stream output = counting; - switch (compressionInfo.Compression) + switch (zipCompressionMethod) { case ZipCompressionMethod.None: { @@ -193,7 +231,7 @@ namespace SharpCompress.Writers.Zip } case ZipCompressionMethod.Deflate: { - return new DeflateStream(counting, CompressionMode.Compress, compressionInfo.DeflateCompressionLevel, + return new DeflateStream(counting, CompressionMode.Compress, compressionLevel, true); } case ZipCompressionMethod.BZip2: @@ -219,7 +257,7 @@ namespace SharpCompress.Writers.Zip } default: { - throw new NotSupportedException("CompressionMethod: " + compressionInfo.Compression); + throw new NotSupportedException("CompressionMethod: " + zipCompressionMethod); } } } diff --git a/src/SharpCompress/Writers/Zip/ZipWriterEntryOptions.cs b/src/SharpCompress/Writers/Zip/ZipWriterEntryOptions.cs new file mode 100644 index 00000000..81f1d1c0 --- /dev/null +++ b/src/SharpCompress/Writers/Zip/ZipWriterEntryOptions.cs @@ -0,0 +1,19 @@ +using System; +using SharpCompress.Common; +using SharpCompress.Compressors.Deflate; + +namespace SharpCompress.Writers.Zip +{ + public class ZipWriterEntryOptions + { + public CompressionType? CompressionType { get; set; } + /// + /// When CompressionType.Deflate is used, this property is referenced. Defaults to CompressionLevel.Default. + /// + public CompressionLevel? DeflateCompressionLevel { get; set; } + + public string EntryComment { get; set; } + + public DateTime? ModificationDateTime { get; set; } + } +} \ No newline at end of file diff --git a/src/SharpCompress/Writers/Zip/ZipWriterOptions.cs b/src/SharpCompress/Writers/Zip/ZipWriterOptions.cs new file mode 100644 index 00000000..fa5d26d0 --- /dev/null +++ b/src/SharpCompress/Writers/Zip/ZipWriterOptions.cs @@ -0,0 +1,30 @@ +using SharpCompress.Archives; +using SharpCompress.Common; +using SharpCompress.Compressors.Deflate; + +namespace SharpCompress.Writers.Zip +{ + public class ZipWriterOptions + { + public ZipWriterOptions() + { + + } + + internal ZipWriterOptions(WriterOptions options) + { + LeaveOpenStream = options.LeaveOpenStream; + CompressionType = options.CompressionType; + } + + public bool LeaveOpenStream { get; set; } + + public CompressionType CompressionType { get; set; } = CompressionType.Unknown; + /// + /// When CompressionType.Deflate is used, this property is referenced. Defaults to CompressionLevel.Default. + /// + public CompressionLevel DeflateCompressionLevel { get; set; } = CompressionLevel.Default; + + public string ArchiveComment { get; set; } + } +} \ No newline at end of file diff --git a/test/SharpCompress.Test/Rar/RarArchiveTests.cs b/test/SharpCompress.Test/Rar/RarArchiveTests.cs index c9f18fc1..ce03a2b4 100644 --- a/test/SharpCompress.Test/Rar/RarArchiveTests.cs +++ b/test/SharpCompress.Test/Rar/RarArchiveTests.cs @@ -3,6 +3,7 @@ using System.Linq; using SharpCompress.Archives; using SharpCompress.Archives.Rar; using SharpCompress.Common; +using SharpCompress.Readers; using Xunit; namespace SharpCompress.Test @@ -33,7 +34,11 @@ namespace SharpCompress.Test { ResetScratch(); using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, testArchive))) - using (var archive = RarArchive.Open(stream, Options.KeepStreamsOpen, password)) + using (var archive = RarArchive.Open(stream, new ReaderOptions() + { + Password = password, + LeaveOpenStream = true + })) { foreach (var entry in archive.Entries) { @@ -56,7 +61,11 @@ namespace SharpCompress.Test protected void ArchiveFileReadPassword(string archiveName, string password) { ResetScratch(); - using (var archive = RarArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, archiveName), Options.None, password)) + using (var archive = RarArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, archiveName), new ReaderOptions() + { + Password = password, + LeaveOpenStream = true + })) { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -101,7 +110,10 @@ namespace SharpCompress.Test ResetScratch(); using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "RarJpeg.jpg"))) { - using (var archive = RarArchive.Open(stream, Options.LookForHeader)) + using (var archive = RarArchive.Open(stream, new ReaderOptions() + { + LookForHeader = true + })) { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -197,7 +209,10 @@ namespace SharpCompress.Test public void Rar_Jpg_ArchiveFileRead() { ResetScratch(); - using (var archive = RarArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, "RarJpeg.jpg"), Options.LookForHeader)) + using (var archive = RarArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, "RarJpeg.jpg"), new ReaderOptions() + { + LookForHeader = true + })) { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { diff --git a/test/SharpCompress.Test/Rar/RarHeaderFactoryTest.cs b/test/SharpCompress.Test/Rar/RarHeaderFactoryTest.cs index be8239f4..f49c4d68 100644 --- a/test/SharpCompress.Test/Rar/RarHeaderFactoryTest.cs +++ b/test/SharpCompress.Test/Rar/RarHeaderFactoryTest.cs @@ -1,7 +1,7 @@ using System.IO; -using SharpCompress.Common; using SharpCompress.Common.Rar.Headers; using SharpCompress.IO; +using SharpCompress.Readers; using Xunit; namespace SharpCompress.Test.Rar @@ -16,7 +16,10 @@ namespace SharpCompress.Test.Rar public RarHeaderFactoryTest() { ResetScratch(); - rarHeaderFactory = new RarHeaderFactory(StreamingMode.Seekable, Options.KeepStreamsOpen); + rarHeaderFactory = new RarHeaderFactory(StreamingMode.Seekable, new ReaderOptions() + { + LeaveOpenStream = true + }); } @@ -33,6 +36,7 @@ namespace SharpCompress.Test.Rar private void ReadEncryptedFlag(string testArchive, bool isEncrypted) { using (var stream = GetReaderStream(testArchive)) + { foreach (var header in rarHeaderFactory.ReadHeaders(stream)) { if (header.HeaderType == HeaderType.ArchiveHeader) @@ -41,6 +45,7 @@ namespace SharpCompress.Test.Rar break; } } + } } [Fact] diff --git a/test/SharpCompress.Test/Rar/RarReaderTests.cs b/test/SharpCompress.Test/Rar/RarReaderTests.cs index 6ca55a76..4f354205 100644 --- a/test/SharpCompress.Test/Rar/RarReaderTests.cs +++ b/test/SharpCompress.Test/Rar/RarReaderTests.cs @@ -128,7 +128,10 @@ namespace SharpCompress.Test { ResetScratch(); using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, testArchive))) - using (var reader = RarReader.Open(stream, password)) + using (var reader = RarReader.Open(stream, new ReaderOptions() + { + Password = password + })) { while (reader.MoveToNextEntry()) { @@ -181,7 +184,10 @@ namespace SharpCompress.Test { ResetScratch(); using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Audio_program.rar"))) - using (var reader = RarReader.Open(stream, Options.LookForHeader)) + using (var reader = RarReader.Open(stream, new ReaderOptions() + { + LookForHeader = true + })) { while (reader.MoveToNextEntry()) { @@ -198,7 +204,10 @@ namespace SharpCompress.Test { ResetScratch(); using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "RarJpeg.jpg"))) - using (var reader = RarReader.Open(stream, Options.LookForHeader)) + using (var reader = RarReader.Open(stream, new ReaderOptions() + { + LookForHeader = true + })) { while (reader.MoveToNextEntry()) { @@ -220,7 +229,10 @@ namespace SharpCompress.Test { ResetScratch(); using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.solid.rar"))) - using (var reader = RarReader.Open(stream, Options.LookForHeader)) + using (var reader = RarReader.Open(stream, new ReaderOptions() + { + LookForHeader = true + })) { while (reader.MoveToNextEntry()) { @@ -238,7 +250,10 @@ namespace SharpCompress.Test { ResetScratch(); using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.rar"))) - using (var reader = RarReader.Open(stream, Options.LookForHeader)) + using (var reader = RarReader.Open(stream, new ReaderOptions() + { + LookForHeader = true + })) { while (reader.MoveToNextEntry()) { diff --git a/test/SharpCompress.Test/Tar/TarArchiveTests.cs b/test/SharpCompress.Test/Tar/TarArchiveTests.cs index a7175ce6..0a23fc80 100644 --- a/test/SharpCompress.Test/Tar/TarArchiveTests.cs +++ b/test/SharpCompress.Test/Tar/TarArchiveTests.cs @@ -139,7 +139,7 @@ namespace SharpCompress.Test base.ResetScratch(); using (var archive = TarArchive.Open(unmodified)) { - var entry = archive.Entries.Where(x => x.Key.EndsWith("jpg")).Single(); + var entry = archive.Entries.Single(x => x.Key.EndsWith("jpg")); archive.RemoveEntry(entry); archive.SaveTo(scratchPath, CompressionType.None); } diff --git a/test/SharpCompress.Test/TestBase.cs b/test/SharpCompress.Test/TestBase.cs index 592e6e22..b1c3a098 100644 --- a/test/SharpCompress.Test/TestBase.cs +++ b/test/SharpCompress.Test/TestBase.cs @@ -210,8 +210,8 @@ namespace SharpCompress.Test protected void CompareArchivesByPath(string file1, string file2) { - using (var archive1 = ReaderFactory.Open(File.OpenRead(file1), Options.None)) - using (var archive2 = ReaderFactory.Open(File.OpenRead(file2), Options.None)) + using (var archive1 = ReaderFactory.Open(File.OpenRead(file1))) + using (var archive2 = ReaderFactory.Open(File.OpenRead(file2))) { while (archive1.MoveToNextEntry()) { diff --git a/test/SharpCompress.Test/WriterTests.cs b/test/SharpCompress.Test/WriterTests.cs index a22c44f3..37856243 100644 --- a/test/SharpCompress.Test/WriterTests.cs +++ b/test/SharpCompress.Test/WriterTests.cs @@ -1,6 +1,7 @@ using System; using System.IO; using SharpCompress.Common; +using SharpCompress.IO; using SharpCompress.Readers; using SharpCompress.Writers; @@ -20,7 +21,10 @@ namespace SharpCompress.Test ResetScratch(); using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive))) { - using (var writer = WriterFactory.Open(stream, type, compressionType, true)) + using (var writer = WriterFactory.Open(new NonDisposingStream(stream), type, new WriterOptions(compressionType) + { + LeaveOpenStream = true + })) { writer.WriteAll(ORIGINAL_FILES_PATH, "*", SearchOption.AllDirectories); } @@ -33,9 +37,9 @@ namespace SharpCompress.Test Path.Combine(TEST_ARCHIVES_PATH, archiveToVerifyAgainst)); using (Stream stream = File.OpenRead(Path.Combine(SCRATCH2_FILES_PATH, archive))) - using (var reader = ReaderFactory.Open(stream)) + using (var reader = ReaderFactory.Open(new NonDisposingStream(stream))) { - reader.WriteAllToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath); + reader.WriteAllToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath); } VerifyFiles(); } diff --git a/test/SharpCompress.Test/Zip/ZipArchiveTests.cs b/test/SharpCompress.Test/Zip/ZipArchiveTests.cs index 1f0b5762..f2836e0c 100644 --- a/test/SharpCompress.Test/Zip/ZipArchiveTests.cs +++ b/test/SharpCompress.Test/Zip/ZipArchiveTests.cs @@ -6,6 +6,7 @@ using System.Text; using SharpCompress.Archives; using SharpCompress.Archives.Zip; using SharpCompress.Common; +using SharpCompress.Readers; using SharpCompress.Writers; using Xunit; @@ -295,7 +296,10 @@ namespace SharpCompress.Test public void Zip_Deflate_WinzipAES_Read() { ResetScratch(); - using (var reader = ZipArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.WinzipAES.zip"), "test")) + using (var reader = ZipArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.WinzipAES.zip"), new ReaderOptions() + { + Password = "test" + })) { foreach (var entry in reader.Entries.Where(x => !x.IsDirectory)) { @@ -310,7 +314,10 @@ namespace SharpCompress.Test public void Zip_BZip2_Pkware_Read() { ResetScratch(); - using (var reader = ZipArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.pkware.zip"), "test")) + using (var reader = ZipArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.pkware.zip"), new ReaderOptions() + { + Password = "test" + })) { foreach (var entry in reader.Entries.Where(x => !x.IsDirectory)) { diff --git a/test/SharpCompress.Test/Zip/ZipReaderTests.cs b/test/SharpCompress.Test/Zip/ZipReaderTests.cs index 18b3b452..36d36377 100644 --- a/test/SharpCompress.Test/Zip/ZipReaderTests.cs +++ b/test/SharpCompress.Test/Zip/ZipReaderTests.cs @@ -84,7 +84,10 @@ namespace SharpCompress.Test { ResetScratch(); using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.pkware.zip"))) - using (var reader = ZipReader.Open(stream, "test")) + using (var reader = ZipReader.Open(stream, new ReaderOptions() + { + Password = "test" + })) { while (reader.MoveToNextEntry()) { @@ -104,7 +107,7 @@ namespace SharpCompress.Test ResetScratch(); using (TestStream stream = new TestStream(File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip")))) { - using (var reader = ReaderFactory.Open(stream, Options.None)) + using (var reader = ReaderFactory.Open(stream)) { while (reader.MoveToNextEntry()) { @@ -125,7 +128,7 @@ namespace SharpCompress.Test ResetScratch(); using (TestStream stream = new TestStream(File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip")))) { - var reader = ReaderFactory.Open(stream, Options.None); + var reader = ReaderFactory.Open(stream); while (reader.MoveToNextEntry()) { if (!reader.Entry.IsDirectory) @@ -148,7 +151,10 @@ namespace SharpCompress.Test Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.lzma.winzipaes.zip"))) - using (var reader = ZipReader.Open(stream, "test")) + using (var reader = ZipReader.Open(stream, new ReaderOptions() + { + Password = "test" + })) { while (reader.MoveToNextEntry()) { @@ -171,7 +177,10 @@ namespace SharpCompress.Test { ResetScratch(); using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.WinzipAES.zip"))) - using (var reader = ZipReader.Open(stream, "test")) + using (var reader = ZipReader.Open(stream, new ReaderOptions() + { + Password = "test" + })) { while (reader.MoveToNextEntry()) { From 5dafcb02d462daa8a9eb5a89be08fd38b40e1d3d Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 27 Sep 2016 10:23:35 +0100 Subject: [PATCH 2/5] Redo options classes --- src/SharpCompress/Archives/Tar/TarArchive.cs | 2 +- src/SharpCompress/Common/OptionsBase.cs | 10 +++++++++ src/SharpCompress/Readers/ReaderOptions.cs | 7 +----- src/SharpCompress/Writers/Tar/TarWriter.cs | 2 +- .../Writers/Tar/TarWriterOptions.cs | 22 ------------------- src/SharpCompress/Writers/WriterFactory.cs | 2 +- src/SharpCompress/Writers/WriterOptions.cs | 12 ++-------- .../Writers/Zip/ZipWriterOptions.cs | 14 +++++------- 8 files changed, 21 insertions(+), 50 deletions(-) create mode 100644 src/SharpCompress/Common/OptionsBase.cs delete mode 100644 src/SharpCompress/Writers/Tar/TarWriterOptions.cs diff --git a/src/SharpCompress/Archives/Tar/TarArchive.cs b/src/SharpCompress/Archives/Tar/TarArchive.cs index b5d8a16a..8103938f 100644 --- a/src/SharpCompress/Archives/Tar/TarArchive.cs +++ b/src/SharpCompress/Archives/Tar/TarArchive.cs @@ -182,7 +182,7 @@ namespace SharpCompress.Archives.Tar IEnumerable oldEntries, IEnumerable newEntries) { - using (var writer = new TarWriter(stream, new TarWriterOptions(options))) + using (var writer = new TarWriter(stream, options)) { foreach (var entry in oldEntries.Concat(newEntries) .Where(x => !x.IsDirectory)) diff --git a/src/SharpCompress/Common/OptionsBase.cs b/src/SharpCompress/Common/OptionsBase.cs new file mode 100644 index 00000000..c46ba1bf --- /dev/null +++ b/src/SharpCompress/Common/OptionsBase.cs @@ -0,0 +1,10 @@ +namespace SharpCompress.Common +{ + public class OptionsBase + { + /// + /// SharpCompress will keep the supplied streams open. Default is true. + /// + public bool LeaveOpenStream { get; set; } = true; + } +} \ No newline at end of file diff --git a/src/SharpCompress/Readers/ReaderOptions.cs b/src/SharpCompress/Readers/ReaderOptions.cs index f7617c65..f7a4df30 100644 --- a/src/SharpCompress/Readers/ReaderOptions.cs +++ b/src/SharpCompress/Readers/ReaderOptions.cs @@ -2,13 +2,8 @@ namespace SharpCompress.Readers { - public class ReaderOptions + public class ReaderOptions : OptionsBase { - /// - /// SharpCompress will keep the supplied streams open. Default is true. - /// - public virtual bool LeaveOpenStream { get; set; } = true; - /// /// Look for RarArchive (Check for self-extracting archives or cases where RarArchive isn't at the start of the file) /// diff --git a/src/SharpCompress/Writers/Tar/TarWriter.cs b/src/SharpCompress/Writers/Tar/TarWriter.cs index 59344813..cf674521 100644 --- a/src/SharpCompress/Writers/Tar/TarWriter.cs +++ b/src/SharpCompress/Writers/Tar/TarWriter.cs @@ -10,7 +10,7 @@ namespace SharpCompress.Writers.Tar { public class TarWriter : AbstractWriter { - public TarWriter(Stream destination, TarWriterOptions options) + public TarWriter(Stream destination, WriterOptions options) : base(ArchiveType.Tar) { if (!destination.CanWrite) diff --git a/src/SharpCompress/Writers/Tar/TarWriterOptions.cs b/src/SharpCompress/Writers/Tar/TarWriterOptions.cs deleted file mode 100644 index 1ad1d077..00000000 --- a/src/SharpCompress/Writers/Tar/TarWriterOptions.cs +++ /dev/null @@ -1,22 +0,0 @@ -using SharpCompress.Common; - -namespace SharpCompress.Writers.Tar -{ - public class TarWriterOptions - { - public TarWriterOptions() - { - - } - - internal TarWriterOptions(WriterOptions readerOptions) - { - LeaveOpenStream = readerOptions.LeaveOpenStream; - CompressionType = readerOptions.CompressionType; - } - - public bool LeaveOpenStream { get; set; } - - public CompressionType CompressionType { get; set; } = CompressionType.Unknown; - } -} \ No newline at end of file diff --git a/src/SharpCompress/Writers/WriterFactory.cs b/src/SharpCompress/Writers/WriterFactory.cs index c558d16e..e80df526 100644 --- a/src/SharpCompress/Writers/WriterFactory.cs +++ b/src/SharpCompress/Writers/WriterFactory.cs @@ -27,7 +27,7 @@ namespace SharpCompress.Writers } case ArchiveType.Tar: { - return new TarWriter(stream, new TarWriterOptions(writerOptions)); + return new TarWriter(stream, writerOptions); } default: { diff --git a/src/SharpCompress/Writers/WriterOptions.cs b/src/SharpCompress/Writers/WriterOptions.cs index dcf8f338..f1ac39b7 100644 --- a/src/SharpCompress/Writers/WriterOptions.cs +++ b/src/SharpCompress/Writers/WriterOptions.cs @@ -3,21 +3,13 @@ using SharpCompress.Compressors.Deflate; namespace SharpCompress.Writers { - public class WriterOptions + public class WriterOptions : OptionsBase { public WriterOptions(CompressionType compressionType) { CompressionType = compressionType; } - public CompressionType CompressionType { get; set; } = CompressionType.Unknown; - - public bool LeaveOpenStream { get; set; } - - - /// - /// When CompressionType.Deflate is used, this property is referenced. Defaults to CompressionLevel.Default. - /// - public CompressionLevel DeflateCompressionLevel { get; set; } = CompressionLevel.Default; + public CompressionType CompressionType { get; set; } public static implicit operator WriterOptions(CompressionType compressionType) { diff --git a/src/SharpCompress/Writers/Zip/ZipWriterOptions.cs b/src/SharpCompress/Writers/Zip/ZipWriterOptions.cs index fa5d26d0..67d574ff 100644 --- a/src/SharpCompress/Writers/Zip/ZipWriterOptions.cs +++ b/src/SharpCompress/Writers/Zip/ZipWriterOptions.cs @@ -4,22 +4,18 @@ using SharpCompress.Compressors.Deflate; namespace SharpCompress.Writers.Zip { - public class ZipWriterOptions + public class ZipWriterOptions : WriterOptions { - public ZipWriterOptions() + public ZipWriterOptions(CompressionType compressionType) + : base(compressionType) { - } - + internal ZipWriterOptions(WriterOptions options) + : base(options.CompressionType) { LeaveOpenStream = options.LeaveOpenStream; - CompressionType = options.CompressionType; } - - public bool LeaveOpenStream { get; set; } - - public CompressionType CompressionType { get; set; } = CompressionType.Unknown; /// /// When CompressionType.Deflate is used, this property is referenced. Defaults to CompressionLevel.Default. /// From 177fc2a12cb84e75e7cf071eab8fc725dc7ddfc1 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 27 Sep 2016 10:50:36 +0100 Subject: [PATCH 3/5] Flags were a better idea when I was younger. It's not clear though. --- src/SharpCompress/Archives/ArchiveFactory.cs | 2 +- .../Archives/IArchiveEntryExtensions.cs | 19 ++++++-- .../Archives/IArchiveExtensions.cs | 5 +- src/SharpCompress/Common/IEntry.Extensions.cs | 9 ++-- .../ExtractionOptions.cs} | 17 +++---- .../Readers/IReaderExtensions.cs | 21 +++++--- src/SharpCompress/Writers/WriterOptions.cs | 1 - test/SharpCompress.Test/ArchiveTests.cs | 27 +++++++++-- .../SharpCompress.Test/Rar/RarArchiveTests.cs | 42 +++++++++++++--- test/SharpCompress.Test/Rar/RarReaderTests.cs | 48 +++++++++++++++---- test/SharpCompress.Test/ReaderTests.cs | 6 ++- test/SharpCompress.Test/WriterTests.cs | 5 +- .../SharpCompress.Test/Zip/ZipArchiveTests.cs | 12 ++++- test/SharpCompress.Test/Zip/ZipReaderTests.cs | 31 +++++++++--- 14 files changed, 186 insertions(+), 59 deletions(-) rename src/SharpCompress/{Common/ExtractOptions.cs => Readers/ExtractionOptions.cs} (57%) diff --git a/src/SharpCompress/Archives/ArchiveFactory.cs b/src/SharpCompress/Archives/ArchiveFactory.cs index fc491e6d..3e7f25c1 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.cs @@ -142,7 +142,7 @@ namespace SharpCompress.Archives /// Extract to specific directory, retaining filename /// public static void WriteToDirectory(string sourceArchive, string destinationDirectory, - ExtractOptions options = ExtractOptions.Overwrite) + ExtractionOptions options = null) { using (IArchive archive = Open(sourceArchive)) { diff --git a/src/SharpCompress/Archives/IArchiveEntryExtensions.cs b/src/SharpCompress/Archives/IArchiveEntryExtensions.cs index df1fb57b..f4f8cb6b 100644 --- a/src/SharpCompress/Archives/IArchiveEntryExtensions.cs +++ b/src/SharpCompress/Archives/IArchiveEntryExtensions.cs @@ -1,6 +1,7 @@ using System.IO; using SharpCompress.Common; using SharpCompress.IO; +using SharpCompress.Readers; namespace SharpCompress.Archives { @@ -43,13 +44,18 @@ namespace SharpCompress.Archives /// Extract to specific directory, retaining filename /// public static void WriteToDirectory(this IArchiveEntry entry, string destinationDirectory, - ExtractOptions options = ExtractOptions.Overwrite) + ExtractionOptions options = null) { string destinationFileName; string file = Path.GetFileName(entry.Key); + options = options ?? new ExtractionOptions() + { + Overwrite = true + }; - if (options.HasFlag(ExtractOptions.ExtractFullPath)) + + if (options.ExtractFullPath) { string folder = Path.GetDirectoryName(entry.Key); string destdir = Path.Combine(destinationDirectory, folder); @@ -73,11 +79,16 @@ namespace SharpCompress.Archives /// Extract to specific file /// public static void WriteToFile(this IArchiveEntry entry, string destinationFileName, - ExtractOptions options = ExtractOptions.Overwrite) + ExtractionOptions options = null) { FileMode fm = FileMode.Create; + options = options ?? new ExtractionOptions() + { + Overwrite = true + }; - if (!options.HasFlag(ExtractOptions.Overwrite)) + + if (!options.Overwrite) { fm = FileMode.CreateNew; } diff --git a/src/SharpCompress/Archives/IArchiveExtensions.cs b/src/SharpCompress/Archives/IArchiveExtensions.cs index 00ab5068..5f9d9e54 100644 --- a/src/SharpCompress/Archives/IArchiveExtensions.cs +++ b/src/SharpCompress/Archives/IArchiveExtensions.cs @@ -1,6 +1,7 @@ #if !NO_FILE using System.Linq; -using SharpCompress.Common; +using SharpCompress.Readers; + #endif namespace SharpCompress.Archives @@ -13,7 +14,7 @@ namespace SharpCompress.Archives /// Extract to specific directory, retaining filename /// public static void WriteToDirectory(this IArchive archive, string destinationDirectory, - ExtractOptions options = ExtractOptions.Overwrite) + ExtractionOptions options = null) { foreach (IArchiveEntry entry in archive.Entries.Where(x => !x.IsDirectory)) { diff --git a/src/SharpCompress/Common/IEntry.Extensions.cs b/src/SharpCompress/Common/IEntry.Extensions.cs index 12a97213..70a634ac 100644 --- a/src/SharpCompress/Common/IEntry.Extensions.cs +++ b/src/SharpCompress/Common/IEntry.Extensions.cs @@ -1,15 +1,16 @@  #if !NO_FILE using System.IO; +using SharpCompress.Readers; namespace SharpCompress.Common { internal static class IEntryExtensions { internal static void PreserveExtractionOptions(this IEntry entry, string destinationFileName, - ExtractOptions options) + ExtractionOptions options) { - if (options.HasFlag(ExtractOptions.PreserveFileTime) || options.HasFlag(ExtractOptions.PreserveAttributes)) + if (options.PreserveFileTime || options.PreserveAttributes) { FileInfo nf = new FileInfo(destinationFileName); if (!nf.Exists) @@ -18,7 +19,7 @@ namespace SharpCompress.Common } // update file time to original packed time - if (options.HasFlag(ExtractOptions.PreserveFileTime)) + if (options.PreserveFileTime) { if (entry.CreatedTime.HasValue) { @@ -36,7 +37,7 @@ namespace SharpCompress.Common } } - if (options.HasFlag(ExtractOptions.PreserveAttributes)) + if (options.PreserveAttributes) { if (entry.Attrib.HasValue) { diff --git a/src/SharpCompress/Common/ExtractOptions.cs b/src/SharpCompress/Readers/ExtractionOptions.cs similarity index 57% rename from src/SharpCompress/Common/ExtractOptions.cs rename to src/SharpCompress/Readers/ExtractionOptions.cs index 5fa347a2..ef06b1ff 100644 --- a/src/SharpCompress/Common/ExtractOptions.cs +++ b/src/SharpCompress/Readers/ExtractionOptions.cs @@ -1,30 +1,25 @@ -using System; - -namespace SharpCompress.Common +namespace SharpCompress.Readers { - [Flags] - public enum ExtractOptions + public class ExtractionOptions { - None = 0, - /// /// overwrite target if it exists /// - Overwrite = 1 << 0, + public bool Overwrite {get; set; } /// /// extract with internal directory structure /// - ExtractFullPath = 1 << 1, + public bool ExtractFullPath { get; set; } /// /// preserve file time /// - PreserveFileTime = 1 << 2, + public bool PreserveFileTime { get; set; } /// /// preserve windows file attributes /// - PreserveAttributes = 1 << 3 + public bool PreserveAttributes { get; set; } } } \ No newline at end of file diff --git a/src/SharpCompress/Readers/IReaderExtensions.cs b/src/SharpCompress/Readers/IReaderExtensions.cs index 8982d68a..4100a607 100644 --- a/src/SharpCompress/Readers/IReaderExtensions.cs +++ b/src/SharpCompress/Readers/IReaderExtensions.cs @@ -28,7 +28,7 @@ namespace SharpCompress.Readers /// Extract all remaining unread entries to specific directory, retaining filename /// public static void WriteAllToDirectory(this IReader reader, string destinationDirectory, - ExtractOptions options = ExtractOptions.Overwrite) + ExtractionOptions options = null) { while (reader.MoveToNextEntry()) { @@ -40,13 +40,16 @@ namespace SharpCompress.Readers /// Extract to specific directory, retaining filename /// public static void WriteEntryToDirectory(this IReader reader, string destinationDirectory, - ExtractOptions options = ExtractOptions.Overwrite) + ExtractionOptions options = null) { string destinationFileName = string.Empty; string file = Path.GetFileName(reader.Entry.Key); + options = options ?? new ExtractionOptions() + { + Overwrite = true + }; - - if (options.HasFlag(ExtractOptions.ExtractFullPath)) + if (options.ExtractFullPath) { string folder = Path.GetDirectoryName(reader.Entry.Key); string destdir = Path.Combine(destinationDirectory, folder); @@ -65,7 +68,7 @@ namespace SharpCompress.Readers { reader.WriteEntryToFile(destinationFileName, options); } - else if (options.HasFlag(ExtractOptions.ExtractFullPath) && !Directory.Exists(destinationFileName)) + else if (options.ExtractFullPath && !Directory.Exists(destinationFileName)) { Directory.CreateDirectory(destinationFileName); } @@ -75,11 +78,15 @@ namespace SharpCompress.Readers /// Extract to specific file /// public static void WriteEntryToFile(this IReader reader, string destinationFileName, - ExtractOptions options = ExtractOptions.Overwrite) + ExtractionOptions options = null) { FileMode fm = FileMode.Create; + options = options ?? new ExtractionOptions() + { + Overwrite = true + }; - if (!options.HasFlag(ExtractOptions.Overwrite)) + if (!options.Overwrite) { fm = FileMode.CreateNew; } diff --git a/src/SharpCompress/Writers/WriterOptions.cs b/src/SharpCompress/Writers/WriterOptions.cs index f1ac39b7..db1e2ffa 100644 --- a/src/SharpCompress/Writers/WriterOptions.cs +++ b/src/SharpCompress/Writers/WriterOptions.cs @@ -1,5 +1,4 @@ using SharpCompress.Common; -using SharpCompress.Compressors.Deflate; namespace SharpCompress.Writers { diff --git a/test/SharpCompress.Test/ArchiveTests.cs b/test/SharpCompress.Test/ArchiveTests.cs index 09c1642a..8962cf13 100644 --- a/test/SharpCompress.Test/ArchiveTests.cs +++ b/test/SharpCompress.Test/ArchiveTests.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using SharpCompress.Archives; using SharpCompress.Common; +using SharpCompress.Readers; using Xunit; namespace SharpCompress.Test @@ -39,7 +40,11 @@ namespace SharpCompress.Test foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { entry.WriteToDirectory(SCRATCH_FILES_PATH, - ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } VerifyFiles(); @@ -67,7 +72,11 @@ namespace SharpCompress.Test { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { - entry.WriteToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } VerifyFiles(); @@ -93,7 +102,11 @@ namespace SharpCompress.Test foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { entry.WriteToDirectory(SCRATCH_FILES_PATH, - ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } VerifyFiles(); @@ -151,7 +164,13 @@ namespace SharpCompress.Test foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { entry.WriteToDirectory(SCRATCH_FILES_PATH, - ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite | ExtractOptions.PreserveFileTime | ExtractOptions.PreserveAttributes); + new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true, + PreserveAttributes = true, + PreserveFileTime = true + }); } } VerifyFilesEx(); diff --git a/test/SharpCompress.Test/Rar/RarArchiveTests.cs b/test/SharpCompress.Test/Rar/RarArchiveTests.cs index ce03a2b4..8f5d6145 100644 --- a/test/SharpCompress.Test/Rar/RarArchiveTests.cs +++ b/test/SharpCompress.Test/Rar/RarArchiveTests.cs @@ -45,7 +45,11 @@ namespace SharpCompress.Test if (!entry.IsDirectory) { Assert.Equal(entry.CompressionType, CompressionType.Rar); - entry.WriteToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } } @@ -70,7 +74,11 @@ namespace SharpCompress.Test foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { entry.WriteToDirectory(SCRATCH_FILES_PATH, - ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } VerifyFiles(); @@ -98,7 +106,11 @@ namespace SharpCompress.Test { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { - entry.WriteToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } } @@ -118,7 +130,11 @@ namespace SharpCompress.Test foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { entry.WriteToDirectory(SCRATCH_FILES_PATH, - ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } VerifyFiles(); @@ -137,7 +153,11 @@ namespace SharpCompress.Test foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { entry.WriteToDirectory(SCRATCH_FILES_PATH, - ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } } @@ -173,7 +193,11 @@ namespace SharpCompress.Test { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { - entry.WriteToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } VerifyFiles(); @@ -216,7 +240,11 @@ namespace SharpCompress.Test { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { - entry.WriteToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } VerifyFiles(); diff --git a/test/SharpCompress.Test/Rar/RarReaderTests.cs b/test/SharpCompress.Test/Rar/RarReaderTests.cs index 4f354205..306309ac 100644 --- a/test/SharpCompress.Test/Rar/RarReaderTests.cs +++ b/test/SharpCompress.Test/Rar/RarReaderTests.cs @@ -26,7 +26,11 @@ namespace SharpCompress.Test { while (reader.MoveToNextEntry()) { - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } VerifyFiles(); @@ -49,7 +53,11 @@ namespace SharpCompress.Test { while (reader.MoveToNextEntry()) { - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } VerifyFiles(); @@ -77,7 +85,11 @@ namespace SharpCompress.Test { while (reader.MoveToNextEntry()) { - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } foreach (var stream in streams) @@ -138,7 +150,11 @@ namespace SharpCompress.Test if (!reader.Entry.IsDirectory) { Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar); - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } } @@ -192,7 +208,11 @@ namespace SharpCompress.Test while (reader.MoveToNextEntry()) { Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar); - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } CompareFilesByPath(Path.Combine(SCRATCH_FILES_PATH, "test.dat"), @@ -212,7 +232,11 @@ namespace SharpCompress.Test while (reader.MoveToNextEntry()) { Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar); - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } VerifyFiles(); @@ -239,7 +263,11 @@ namespace SharpCompress.Test if (reader.Entry.Key.Contains("jpg")) { Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar); - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } } @@ -260,7 +288,11 @@ namespace SharpCompress.Test if (reader.Entry.Key.Contains("jpg")) { Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar); - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } } diff --git a/test/SharpCompress.Test/ReaderTests.cs b/test/SharpCompress.Test/ReaderTests.cs index 9108b581..7e8a3ae3 100644 --- a/test/SharpCompress.Test/ReaderTests.cs +++ b/test/SharpCompress.Test/ReaderTests.cs @@ -34,7 +34,11 @@ namespace SharpCompress.Test if (!reader.Entry.IsDirectory) { Assert.Equal(reader.Entry.CompressionType, expectedCompression); - reader.WriteEntryToDirectory(test.SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + reader.WriteEntryToDirectory(test.SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } test.VerifyFiles(); diff --git a/test/SharpCompress.Test/WriterTests.cs b/test/SharpCompress.Test/WriterTests.cs index 37856243..f9257dfc 100644 --- a/test/SharpCompress.Test/WriterTests.cs +++ b/test/SharpCompress.Test/WriterTests.cs @@ -39,7 +39,10 @@ namespace SharpCompress.Test using (Stream stream = File.OpenRead(Path.Combine(SCRATCH2_FILES_PATH, archive))) using (var reader = ReaderFactory.Open(new NonDisposingStream(stream))) { - reader.WriteAllToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath); + reader.WriteAllToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true + }); } VerifyFiles(); } diff --git a/test/SharpCompress.Test/Zip/ZipArchiveTests.cs b/test/SharpCompress.Test/Zip/ZipArchiveTests.cs index f2836e0c..fbd6b350 100644 --- a/test/SharpCompress.Test/Zip/ZipArchiveTests.cs +++ b/test/SharpCompress.Test/Zip/ZipArchiveTests.cs @@ -303,7 +303,11 @@ namespace SharpCompress.Test { foreach (var entry in reader.Entries.Where(x => !x.IsDirectory)) { - entry.WriteToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } VerifyFiles(); @@ -321,7 +325,11 @@ namespace SharpCompress.Test { foreach (var entry in reader.Entries.Where(x => !x.IsDirectory)) { - entry.WriteToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } VerifyFiles(); diff --git a/test/SharpCompress.Test/Zip/ZipReaderTests.cs b/test/SharpCompress.Test/Zip/ZipReaderTests.cs index 36d36377..004496ff 100644 --- a/test/SharpCompress.Test/Zip/ZipReaderTests.cs +++ b/test/SharpCompress.Test/Zip/ZipReaderTests.cs @@ -94,7 +94,11 @@ namespace SharpCompress.Test if (!reader.Entry.IsDirectory) { Assert.Equal(reader.Entry.CompressionType, CompressionType.BZip2); - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } } @@ -114,7 +118,11 @@ namespace SharpCompress.Test if (!reader.Entry.IsDirectory) { reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, - ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } } @@ -134,7 +142,11 @@ namespace SharpCompress.Test if (!reader.Entry.IsDirectory) { reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, - ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } Assert.False(stream.IsDisposed); @@ -163,8 +175,11 @@ namespace SharpCompress.Test Assert.Equal(reader.Entry.CompressionType, CompressionType.Unknown); reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, - ExtractOptions.ExtractFullPath - | ExtractOptions.Overwrite); + new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } } @@ -188,7 +203,11 @@ namespace SharpCompress.Test { Assert.Equal(reader.Entry.CompressionType, CompressionType.Unknown); reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, - ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); + new ExtractOptions() + { + ExtractFullPath = true, + Overwrite = true + }); } } } From d71520808d7e3105e6cacd4b3a9c78856bf29255 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 27 Sep 2016 11:08:54 +0100 Subject: [PATCH 4/5] Helps if I rename everything --- test/SharpCompress.Test/ArchiveTests.cs | 10 +++++----- test/SharpCompress.Test/Rar/RarArchiveTests.cs | 14 +++++++------- test/SharpCompress.Test/Rar/RarReaderTests.cs | 16 ++++++++-------- test/SharpCompress.Test/ReaderTests.cs | 2 +- test/SharpCompress.Test/WriterTests.cs | 2 +- test/SharpCompress.Test/Zip/ZipArchiveTests.cs | 4 ++-- test/SharpCompress.Test/Zip/ZipReaderTests.cs | 10 +++++----- 7 files changed, 29 insertions(+), 29 deletions(-) diff --git a/test/SharpCompress.Test/ArchiveTests.cs b/test/SharpCompress.Test/ArchiveTests.cs index 8962cf13..f5ddf543 100644 --- a/test/SharpCompress.Test/ArchiveTests.cs +++ b/test/SharpCompress.Test/ArchiveTests.cs @@ -40,7 +40,7 @@ namespace SharpCompress.Test foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { entry.WriteToDirectory(SCRATCH_FILES_PATH, - new ExtractOptions() + new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -72,7 +72,7 @@ namespace SharpCompress.Test { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { - entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -102,7 +102,7 @@ namespace SharpCompress.Test foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { entry.WriteToDirectory(SCRATCH_FILES_PATH, - new ExtractOptions() + new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -147,7 +147,7 @@ namespace SharpCompress.Test } /// - /// Demonstrate the TotalUncompressSize property, and the ExtractOptions.PreserveFileTime and ExtractOptions.PreserveAttributes extract options + /// Demonstrate the TotalUncompressSize property, and the ExtractionOptions.PreserveFileTime and ExtractionOptions.PreserveAttributes extract options /// protected void ArchiveFileReadEx(IEnumerable testArchives) { @@ -164,7 +164,7 @@ namespace SharpCompress.Test foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { entry.WriteToDirectory(SCRATCH_FILES_PATH, - new ExtractOptions() + new ExtractionOptions() { ExtractFullPath = true, Overwrite = true, diff --git a/test/SharpCompress.Test/Rar/RarArchiveTests.cs b/test/SharpCompress.Test/Rar/RarArchiveTests.cs index 8f5d6145..6016f201 100644 --- a/test/SharpCompress.Test/Rar/RarArchiveTests.cs +++ b/test/SharpCompress.Test/Rar/RarArchiveTests.cs @@ -45,7 +45,7 @@ namespace SharpCompress.Test if (!entry.IsDirectory) { Assert.Equal(entry.CompressionType, CompressionType.Rar); - entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -74,7 +74,7 @@ namespace SharpCompress.Test foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { entry.WriteToDirectory(SCRATCH_FILES_PATH, - new ExtractOptions() + new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -106,7 +106,7 @@ namespace SharpCompress.Test { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { - entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -130,7 +130,7 @@ namespace SharpCompress.Test foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { entry.WriteToDirectory(SCRATCH_FILES_PATH, - new ExtractOptions() + new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -153,7 +153,7 @@ namespace SharpCompress.Test foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { entry.WriteToDirectory(SCRATCH_FILES_PATH, - new ExtractOptions() + new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -193,7 +193,7 @@ namespace SharpCompress.Test { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { - entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -240,7 +240,7 @@ namespace SharpCompress.Test { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { - entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true diff --git a/test/SharpCompress.Test/Rar/RarReaderTests.cs b/test/SharpCompress.Test/Rar/RarReaderTests.cs index 306309ac..912692ec 100644 --- a/test/SharpCompress.Test/Rar/RarReaderTests.cs +++ b/test/SharpCompress.Test/Rar/RarReaderTests.cs @@ -26,7 +26,7 @@ namespace SharpCompress.Test { while (reader.MoveToNextEntry()) { - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -53,7 +53,7 @@ namespace SharpCompress.Test { while (reader.MoveToNextEntry()) { - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -85,7 +85,7 @@ namespace SharpCompress.Test { while (reader.MoveToNextEntry()) { - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -150,7 +150,7 @@ namespace SharpCompress.Test if (!reader.Entry.IsDirectory) { Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar); - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -208,7 +208,7 @@ namespace SharpCompress.Test while (reader.MoveToNextEntry()) { Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar); - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -232,7 +232,7 @@ namespace SharpCompress.Test while (reader.MoveToNextEntry()) { Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar); - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -263,7 +263,7 @@ namespace SharpCompress.Test if (reader.Entry.Key.Contains("jpg")) { Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar); - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -288,7 +288,7 @@ namespace SharpCompress.Test if (reader.Entry.Key.Contains("jpg")) { Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar); - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true diff --git a/test/SharpCompress.Test/ReaderTests.cs b/test/SharpCompress.Test/ReaderTests.cs index 7e8a3ae3..1de62c59 100644 --- a/test/SharpCompress.Test/ReaderTests.cs +++ b/test/SharpCompress.Test/ReaderTests.cs @@ -34,7 +34,7 @@ namespace SharpCompress.Test if (!reader.Entry.IsDirectory) { Assert.Equal(reader.Entry.CompressionType, expectedCompression); - reader.WriteEntryToDirectory(test.SCRATCH_FILES_PATH, new ExtractOptions() + reader.WriteEntryToDirectory(test.SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true diff --git a/test/SharpCompress.Test/WriterTests.cs b/test/SharpCompress.Test/WriterTests.cs index f9257dfc..3563f440 100644 --- a/test/SharpCompress.Test/WriterTests.cs +++ b/test/SharpCompress.Test/WriterTests.cs @@ -39,7 +39,7 @@ namespace SharpCompress.Test using (Stream stream = File.OpenRead(Path.Combine(SCRATCH2_FILES_PATH, archive))) using (var reader = ReaderFactory.Open(new NonDisposingStream(stream))) { - reader.WriteAllToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + reader.WriteAllToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true }); diff --git a/test/SharpCompress.Test/Zip/ZipArchiveTests.cs b/test/SharpCompress.Test/Zip/ZipArchiveTests.cs index fbd6b350..f38e0918 100644 --- a/test/SharpCompress.Test/Zip/ZipArchiveTests.cs +++ b/test/SharpCompress.Test/Zip/ZipArchiveTests.cs @@ -303,7 +303,7 @@ namespace SharpCompress.Test { foreach (var entry in reader.Entries.Where(x => !x.IsDirectory)) { - entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -325,7 +325,7 @@ namespace SharpCompress.Test { foreach (var entry in reader.Entries.Where(x => !x.IsDirectory)) { - entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true diff --git a/test/SharpCompress.Test/Zip/ZipReaderTests.cs b/test/SharpCompress.Test/Zip/ZipReaderTests.cs index 004496ff..0927a31b 100644 --- a/test/SharpCompress.Test/Zip/ZipReaderTests.cs +++ b/test/SharpCompress.Test/Zip/ZipReaderTests.cs @@ -94,7 +94,7 @@ namespace SharpCompress.Test if (!reader.Entry.IsDirectory) { Assert.Equal(reader.Entry.CompressionType, CompressionType.BZip2); - reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions() + reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -118,7 +118,7 @@ namespace SharpCompress.Test if (!reader.Entry.IsDirectory) { reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, - new ExtractOptions() + new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -142,7 +142,7 @@ namespace SharpCompress.Test if (!reader.Entry.IsDirectory) { reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, - new ExtractOptions() + new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -175,7 +175,7 @@ namespace SharpCompress.Test Assert.Equal(reader.Entry.CompressionType, CompressionType.Unknown); reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, - new ExtractOptions() + new ExtractionOptions() { ExtractFullPath = true, Overwrite = true @@ -203,7 +203,7 @@ namespace SharpCompress.Test { Assert.Equal(reader.Entry.CompressionType, CompressionType.Unknown); reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, - new ExtractOptions() + new ExtractionOptions() { ExtractFullPath = true, Overwrite = true From 260c0ee776eef2f758eece3ac49ca5abb73b5fe0 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 27 Sep 2016 11:19:52 +0100 Subject: [PATCH 5/5] Add SaveTo overload for zip archives --- src/SharpCompress/Archives/Zip/ZipArchive.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.cs b/src/SharpCompress/Archives/Zip/ZipArchive.cs index f3a71b34..e4c132c0 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.cs @@ -168,6 +168,11 @@ namespace SharpCompress.Archives.Zip } } + public void SaveTo(Stream stream) + { + SaveTo(stream, new WriterOptions(CompressionType.Deflate)); + } + protected override void SaveTo(Stream stream, WriterOptions options, IEnumerable oldEntries, IEnumerable newEntries)