diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json index d1ac7eef..f92b715a 100644 --- a/.config/dotnet-tools.json +++ b/.config/dotnet-tools.json @@ -3,7 +3,7 @@ "isRoot": true, "tools": { "csharpier": { - "version": "0.21.0", + "version": "0.23.0", "commands": [ "dotnet-csharpier" ] diff --git a/.github/workflows/dotnetcore.yml b/.github/workflows/dotnetcore.yml index 530ec766..45abba29 100644 --- a/.github/workflows/dotnetcore.yml +++ b/.github/workflows/dotnetcore.yml @@ -18,6 +18,13 @@ jobs: - uses: actions/setup-dotnet@v3 with: dotnet-version: 7.0.x + - name: NuGet Caching + uses: actions/cache@v3 + with: + path: ~/.nuget/packages + key: ${{ runner.os }}-nuget-${{ hashFiles('packages.lock.json', '*/packages.lock.json') }} + restore-keys: | + ${{ runner.os }}-nuget- - run: dotnet run --project build/build.csproj - uses: actions/upload-artifact@v3 with: diff --git a/README.md b/README.md index f60272b8..777b383d 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ SharpCompress is a compression library in pure C# for .NET Standard 2.0, 2.1, .N The major feature is support for non-seekable streams so large files can be processed on the fly (i.e. download stream). GitHub Actions Build - -[![GitHubActions](https://github.com/adamhathcock/sharpcompress/workflows/SharpCompress/badge.svg)](https://circleci.com/gh/adamhathcock/sharpcompress) +[![SharpCompress](https://github.com/adamhathcock/sharpcompress/actions/workflows/dotnetcore.yml/badge.svg)](https://github.com/adamhathcock/sharpcompress/actions/workflows/dotnetcore.yml) ## Need Help? diff --git a/SharpCompress.sln b/SharpCompress.sln index 5b033547..71ec294e 100644 --- a/SharpCompress.sln +++ b/SharpCompress.sln @@ -3,8 +3,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.26430.6 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F18F1765-4A02-42FD-9BEF-F0E2FCBD9D17}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{3C5BE746-03E5-4895-9988-0B57F162F86C}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{0F0901FF-E8D9-426A-B5A2-17C7F47C1529}" @@ -15,6 +13,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpCompress.Test", "tests EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "build", "build\build.csproj", "{D4D613CB-5E94-47FB-85BE-B8423D20C545}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Config", "Config", "{CDB42573-7D22-4490-BA12-1B7FB99CE7FB}" + ProjectSection(SolutionItems) = preProject + Directory.Build.props = Directory.Build.props + global.json = global.json + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/USAGE.md b/USAGE.md index a7a7f0d6..c96f9c1b 100644 --- a/USAGE.md +++ b/USAGE.md @@ -33,6 +33,18 @@ If using Compression Stream classes directly and you don't want the wrapped stre Also, look over the tests for more thorough [examples](https://github.com/adamhathcock/sharpcompress/tree/master/tests/SharpCompress.Test) +### Create Zip Archive from multiple files +```C# +using(var archive = ZipArchive.Create()) +{ + archive.AddEntry("file01.txt", "C:\\file01.txt"); + archive.AddEntry("file02.txt", "C:\\file02.txt"); + ... + + archive.SaveTo("C:\\temp.zip", CompressionType.Deflate); +} +``` + ### Create Zip Archive from all files in a directory to a file ```C# @@ -143,4 +155,4 @@ foreach(var entry in tr.Entries) { Console.WriteLine($"{entry.Key}"); } -``` \ No newline at end of file +``` diff --git a/build/Program.cs b/build/Program.cs index 000051dd..9475a66b 100644 --- a/build/Program.cs +++ b/build/Program.cs @@ -7,8 +7,10 @@ using static Bullseye.Targets; using static SimpleExec.Command; const string Clean = "clean"; +const string Restore = "restore"; const string Build = "build"; const string Test = "test"; +const string Format = "format"; const string Publish = "publish"; Target( @@ -38,10 +40,21 @@ Target( ); Target( - Build, + Format, () => { - Run("dotnet", "build src/SharpCompress/SharpCompress.csproj -c Release"); + Run("dotnet", "tool restore"); + Run("dotnet", "csharpier --check ."); + } +); +Target(Restore, DependsOn(Format), () => Run("dotnet", "restore")); + +Target( + Build, + DependsOn(Restore), + () => + { + Run("dotnet", "build src/SharpCompress/SharpCompress.csproj -c Release --no-restore"); } ); @@ -63,7 +76,7 @@ Target( foreach (var file in GetFiles("**/*.Test.csproj")) { - Run("dotnet", $"test {file} -c Release -f {framework}"); + Run("dotnet", $"test {file} -c Release -f {framework} --no-restore --verbosity=normal"); } } ); diff --git a/build/build.csproj b/build/build.csproj index 66d55099..7b478cf3 100644 --- a/build/build.csproj +++ b/build/build.csproj @@ -6,7 +6,7 @@ - + diff --git a/src/SharpCompress/Archives/AbstractWritableArchive.cs b/src/SharpCompress/Archives/AbstractWritableArchive.cs index cf97aff8..30083ec7 100644 --- a/src/SharpCompress/Archives/AbstractWritableArchive.cs +++ b/src/SharpCompress/Archives/AbstractWritableArchive.cs @@ -38,7 +38,8 @@ public abstract class AbstractWritableArchive private bool hasModifications; private bool pauseRebuilding; - internal AbstractWritableArchive(ArchiveType type) : base(type) { } + internal AbstractWritableArchive(ArchiveType type) + : base(type) { } internal AbstractWritableArchive(ArchiveType type, SourceStream srcStream) : base(type, srcStream) { } diff --git a/src/SharpCompress/Archives/ArchiveFactory.cs b/src/SharpCompress/Archives/ArchiveFactory.cs index 6b489fa1..cd0952f1 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.cs @@ -128,14 +128,16 @@ public static class ArchiveFactory } } - private static T FindFactory(FileInfo finfo) where T : IFactory + private static T FindFactory(FileInfo finfo) + where T : IFactory { finfo.CheckNotNull(nameof(finfo)); using Stream stream = finfo.OpenRead(); return FindFactory(stream); } - private static T FindFactory(Stream stream) where T : IFactory + private static T FindFactory(Stream stream) + where T : IFactory { stream.CheckNotNull(nameof(stream)); if (!stream.CanRead || !stream.CanSeek) @@ -173,7 +175,7 @@ public static class ArchiveFactory return IsArchive(s, out type); } - private static bool IsArchive(Stream stream, out ArchiveType? type) + public static bool IsArchive(Stream stream, out ArchiveType? type) { type = null; stream.CheckNotNull(nameof(stream)); diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.cs b/src/SharpCompress/Archives/GZip/GZipArchive.cs index eea56db0..fb2f7691 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.cs @@ -101,7 +101,8 @@ public class GZipArchive : AbstractWritableArchive /// /// /// - internal GZipArchive(SourceStream srcStream) : base(ArchiveType.Tar, srcStream) { } + internal GZipArchive(SourceStream srcStream) + : base(ArchiveType.Tar, srcStream) { } protected override IEnumerable LoadVolumes(SourceStream srcStream) { @@ -150,7 +151,8 @@ public class GZipArchive : AbstractWritableArchive return true; } - internal GZipArchive() : base(ArchiveType.GZip) { } + internal GZipArchive() + : base(ArchiveType.GZip) { } protected override GZipArchiveEntry CreateEntryInternal( string filePath, diff --git a/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs b/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs index 0109ad98..be872e80 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs @@ -6,8 +6,8 @@ namespace SharpCompress.Archives.GZip; public class GZipArchiveEntry : GZipEntry, IArchiveEntry { - internal GZipArchiveEntry(GZipArchive archive, GZipFilePart part) : base(part) => - Archive = archive; + internal GZipArchiveEntry(GZipArchive archive, GZipFilePart part) + : base(part) => Archive = archive; public virtual Stream OpenEntryStream() { diff --git a/src/SharpCompress/Archives/GZip/GZipWritableArchiveEntry.cs b/src/SharpCompress/Archives/GZip/GZipWritableArchiveEntry.cs index 7685ceb1..27dfc2bf 100644 --- a/src/SharpCompress/Archives/GZip/GZipWritableArchiveEntry.cs +++ b/src/SharpCompress/Archives/GZip/GZipWritableArchiveEntry.cs @@ -20,7 +20,8 @@ internal sealed class GZipWritableArchiveEntry : GZipArchiveEntry, IWritableArch long size, DateTime? lastModified, bool closeStream - ) : base(archive, null) + ) + : base(archive, null) { this.stream = stream; Key = path; diff --git a/src/SharpCompress/Archives/Rar/FileInfoRarFilePart.cs b/src/SharpCompress/Archives/Rar/FileInfoRarFilePart.cs index b5416f9e..02d2f769 100644 --- a/src/SharpCompress/Archives/Rar/FileInfoRarFilePart.cs +++ b/src/SharpCompress/Archives/Rar/FileInfoRarFilePart.cs @@ -11,7 +11,8 @@ internal sealed class FileInfoRarFilePart : SeekableFilePart MarkHeader mh, FileHeader fh, FileInfo fi - ) : base(mh, fh, volume.Index, volume.Stream, password) => FileInfo = fi; + ) + : base(mh, fh, volume.Index, volume.Stream, password) => FileInfo = fi; internal FileInfo FileInfo { get; } diff --git a/src/SharpCompress/Archives/Rar/RarArchive.cs b/src/SharpCompress/Archives/Rar/RarArchive.cs index 48f4ef45..29d733f6 100644 --- a/src/SharpCompress/Archives/Rar/RarArchive.cs +++ b/src/SharpCompress/Archives/Rar/RarArchive.cs @@ -23,7 +23,8 @@ public class RarArchive : AbstractArchive /// /// /// - internal RarArchive(SourceStream srcStream) : base(ArchiveType.Rar, srcStream) { } + internal RarArchive(SourceStream srcStream) + : base(ArchiveType.Rar, srcStream) { } protected override IEnumerable LoadEntries(IEnumerable volumes) => RarArchiveEntryFactory.GetEntries(this, volumes, ReaderOptions); diff --git a/src/SharpCompress/Archives/Rar/RarArchiveVolumeFactory.cs b/src/SharpCompress/Archives/Rar/RarArchiveVolumeFactory.cs index 6903bc5f..72ccf60f 100644 --- a/src/SharpCompress/Archives/Rar/RarArchiveVolumeFactory.cs +++ b/src/SharpCompress/Archives/Rar/RarArchiveVolumeFactory.cs @@ -26,7 +26,7 @@ internal static class RarArchiveVolumeFactory else { //old style - ...rar, .r00, .r01 ... - m = Regex.Match(part1.Name, @"^(.*\.r)(ar|[0-9]+)$", RegexOptions.IgnoreCase); + m = Regex.Match(part1.Name, @"^(.*\.)([r-z{])(ar|[0-9]+)$", RegexOptions.IgnoreCase); if (m.Success) item = new FileInfo( Path.Combine( @@ -34,8 +34,9 @@ internal static class RarArchiveVolumeFactory String.Concat( m.Groups[1].Value, index == 0 - ? "ar" - : (index - 1).ToString().PadLeft(m.Groups[2].Value.Length, '0') + ? m.Groups[2].Value + m.Groups[3].Value + : (char)(m.Groups[2].Value[0] + ((index - 1) / 100)) + + (index - 1).ToString("D4").Substring(2) ) ) ); diff --git a/src/SharpCompress/Archives/Rar/SeekableFilePart.cs b/src/SharpCompress/Archives/Rar/SeekableFilePart.cs index e7247eae..08aceb13 100644 --- a/src/SharpCompress/Archives/Rar/SeekableFilePart.cs +++ b/src/SharpCompress/Archives/Rar/SeekableFilePart.cs @@ -15,7 +15,8 @@ internal class SeekableFilePart : RarFilePart int index, Stream stream, string? password - ) : base(mh, fh, index) + ) + : base(mh, fh, index) { this.stream = stream; this.password = password; diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs index 8bb782f0..7577b43d 100644 --- a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs @@ -104,7 +104,8 @@ public class SevenZipArchive : AbstractArchive /// /// - internal SevenZipArchive(SourceStream srcStream) : base(ArchiveType.SevenZip, srcStream) { } + internal SevenZipArchive(SourceStream srcStream) + : base(ArchiveType.SevenZip, srcStream) { } protected override IEnumerable LoadVolumes(SourceStream srcStream) { @@ -125,7 +126,8 @@ public class SevenZipArchive : AbstractArchive LoadEntries( IEnumerable volumes diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchiveEntry.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchiveEntry.cs index 4e30f18e..9824ca47 100644 --- a/src/SharpCompress/Archives/SevenZip/SevenZipArchiveEntry.cs +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchiveEntry.cs @@ -5,8 +5,8 @@ namespace SharpCompress.Archives.SevenZip; public class SevenZipArchiveEntry : SevenZipEntry, IArchiveEntry { - internal SevenZipArchiveEntry(SevenZipArchive archive, SevenZipFilePart part) : base(part) => - Archive = archive; + internal SevenZipArchiveEntry(SevenZipArchive archive, SevenZipFilePart part) + : base(part) => Archive = archive; public Stream OpenEntryStream() => FilePart.GetCompressedStream(); diff --git a/src/SharpCompress/Archives/Tar/TarArchive.cs b/src/SharpCompress/Archives/Tar/TarArchive.cs index 218eda42..43184b66 100644 --- a/src/SharpCompress/Archives/Tar/TarArchive.cs +++ b/src/SharpCompress/Archives/Tar/TarArchive.cs @@ -135,9 +135,11 @@ public class TarArchive : AbstractWritableArchive /// /// /// - internal TarArchive(SourceStream srcStream) : base(ArchiveType.Tar, srcStream) { } + internal TarArchive(SourceStream srcStream) + : base(ArchiveType.Tar, srcStream) { } - internal TarArchive() : base(ArchiveType.Tar) { } + internal TarArchive() + : base(ArchiveType.Tar) { } protected override IEnumerable LoadEntries(IEnumerable volumes) { diff --git a/src/SharpCompress/Archives/Tar/TarWritableArchiveEntry.cs b/src/SharpCompress/Archives/Tar/TarWritableArchiveEntry.cs index e110987b..0bbea709 100644 --- a/src/SharpCompress/Archives/Tar/TarWritableArchiveEntry.cs +++ b/src/SharpCompress/Archives/Tar/TarWritableArchiveEntry.cs @@ -21,7 +21,8 @@ internal sealed class TarWritableArchiveEntry : TarArchiveEntry, IWritableArchiv long size, DateTime? lastModified, bool closeStream - ) : base(archive, null, compressionType) + ) + : base(archive, null, compressionType) { this.stream = stream; Key = path; diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.cs b/src/SharpCompress/Archives/Zip/ZipArchive.cs index 99a66161..14e4a93e 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.cs @@ -32,7 +32,8 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - internal ZipArchive(SourceStream srcStream) : base(ArchiveType.Zip, srcStream) => + internal ZipArchive(SourceStream srcStream) + : base(ArchiveType.Zip, srcStream) => headerFactory = new SeekableZipHeaderFactory( srcStream.ReaderOptions.Password, srcStream.ReaderOptions.ArchiveEncoding @@ -133,7 +134,7 @@ public class ZipArchive : AbstractWritableArchive public static bool IsZipFile(Stream stream, string? password = null) { - var headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding()); + var headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding(), null); try { var header = headerFactory @@ -157,7 +158,7 @@ public class ZipArchive : AbstractWritableArchive public static bool IsZipMulti(Stream stream, string? password = null) { - var headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding()); + var headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding(), null); try { var header = headerFactory @@ -217,7 +218,8 @@ public class ZipArchive : AbstractWritableArchive return new ZipVolume(SrcStream, ReaderOptions, idx++).AsEnumerable(); } - internal ZipArchive() : base(ArchiveType.Zip) { } + internal ZipArchive() + : base(ArchiveType.Zip) { } protected override IEnumerable LoadEntries(IEnumerable volumes) { @@ -298,6 +300,6 @@ public class ZipArchive : AbstractWritableArchive { var stream = Volumes.Single().Stream; stream.Position = 0; - return ZipReader.Open(stream, ReaderOptions); + return ZipReader.Open(stream, ReaderOptions, Entries); } } diff --git a/src/SharpCompress/Archives/Zip/ZipArchiveEntry.cs b/src/SharpCompress/Archives/Zip/ZipArchiveEntry.cs index 59e30fd8..a94ed2c6 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchiveEntry.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchiveEntry.cs @@ -6,8 +6,8 @@ namespace SharpCompress.Archives.Zip; public class ZipArchiveEntry : ZipEntry, IArchiveEntry { - internal ZipArchiveEntry(ZipArchive archive, SeekableZipFilePart? part) : base(part) => - Archive = archive; + internal ZipArchiveEntry(ZipArchive archive, SeekableZipFilePart? part) + : base(part) => Archive = archive; public virtual Stream OpenEntryStream() => Parts.Single().GetCompressedStream(); diff --git a/src/SharpCompress/Archives/Zip/ZipWritableArchiveEntry.cs b/src/SharpCompress/Archives/Zip/ZipWritableArchiveEntry.cs index a13a3dbb..628e505f 100644 --- a/src/SharpCompress/Archives/Zip/ZipWritableArchiveEntry.cs +++ b/src/SharpCompress/Archives/Zip/ZipWritableArchiveEntry.cs @@ -19,7 +19,8 @@ internal class ZipWritableArchiveEntry : ZipArchiveEntry, IWritableArchiveEntry long size, DateTime? lastModified, bool closeStream - ) : base(archive, null) + ) + : base(archive, null) { this.stream = stream; Key = path; diff --git a/src/SharpCompress/Common/ArchiveEncoding.cs b/src/SharpCompress/Common/ArchiveEncoding.cs index 53848ef7..f66044d3 100644 --- a/src/SharpCompress/Common/ArchiveEncoding.cs +++ b/src/SharpCompress/Common/ArchiveEncoding.cs @@ -26,7 +26,8 @@ public class ArchiveEncoding /// string Func(bytes, index, length) public Func? CustomDecoder { get; set; } - public ArchiveEncoding() : this(Encoding.Default, Encoding.Default) { } + public ArchiveEncoding() + : this(Encoding.Default, Encoding.Default) { } public ArchiveEncoding(Encoding def, Encoding password) { diff --git a/src/SharpCompress/Common/ArchiveException.cs b/src/SharpCompress/Common/ArchiveException.cs index 0fc98baa..507d5fd8 100644 --- a/src/SharpCompress/Common/ArchiveException.cs +++ b/src/SharpCompress/Common/ArchiveException.cs @@ -4,5 +4,6 @@ namespace SharpCompress.Common; public class ArchiveException : Exception { - public ArchiveException(string message) : base(message) { } + public ArchiveException(string message) + : base(message) { } } diff --git a/src/SharpCompress/Common/CryptographicException.cs b/src/SharpCompress/Common/CryptographicException.cs index c20775a8..6127524a 100644 --- a/src/SharpCompress/Common/CryptographicException.cs +++ b/src/SharpCompress/Common/CryptographicException.cs @@ -4,5 +4,6 @@ namespace SharpCompress.Common; public class CryptographicException : Exception { - public CryptographicException(string message) : base(message) { } + public CryptographicException(string message) + : base(message) { } } diff --git a/src/SharpCompress/Common/ExtractionException.cs b/src/SharpCompress/Common/ExtractionException.cs index 44316b73..4bc4f00c 100644 --- a/src/SharpCompress/Common/ExtractionException.cs +++ b/src/SharpCompress/Common/ExtractionException.cs @@ -4,7 +4,9 @@ namespace SharpCompress.Common; public class ExtractionException : Exception { - public ExtractionException(string message) : base(message) { } + public ExtractionException(string message) + : base(message) { } - public ExtractionException(string message, Exception inner) : base(message, inner) { } + public ExtractionException(string message, Exception inner) + : base(message, inner) { } } diff --git a/src/SharpCompress/Common/FlagUtility.cs b/src/SharpCompress/Common/FlagUtility.cs index 73215817..8f272b9d 100644 --- a/src/SharpCompress/Common/FlagUtility.cs +++ b/src/SharpCompress/Common/FlagUtility.cs @@ -12,8 +12,8 @@ internal static class FlagUtility /// Flagged variable /// Flag to test /// - public static bool HasFlag(long bitField, T flag) where T : struct => - HasFlag(bitField, flag); + public static bool HasFlag(long bitField, T flag) + where T : struct => HasFlag(bitField, flag); /// /// Returns true if the flag is set on the specified bit field. @@ -23,8 +23,8 @@ internal static class FlagUtility /// Flagged variable /// Flag to test /// - public static bool HasFlag(ulong bitField, T flag) where T : struct => - HasFlag(bitField, flag); + public static bool HasFlag(ulong bitField, T flag) + where T : struct => HasFlag(bitField, flag); /// /// Returns true if the flag is set on the specified bit field. @@ -45,8 +45,8 @@ internal static class FlagUtility /// Flagged variable /// Flag to test /// - public static bool HasFlag(T bitField, T flag) where T : struct => - HasFlag(Convert.ToInt64(bitField), Convert.ToInt64(flag)); + public static bool HasFlag(T bitField, T flag) + where T : struct => HasFlag(Convert.ToInt64(bitField), Convert.ToInt64(flag)); /// /// Returns true if the flag is set on the specified bit field. @@ -81,6 +81,6 @@ internal static class FlagUtility /// Flag to change /// bool /// The flagged variable with the flag changed - public static long SetFlag(T bitField, T flag, bool on) where T : struct => - SetFlag(Convert.ToInt64(bitField), Convert.ToInt64(flag), on); + public static long SetFlag(T bitField, T flag, bool on) + where T : struct => SetFlag(Convert.ToInt64(bitField), Convert.ToInt64(flag), on); } diff --git a/src/SharpCompress/Common/GZip/GZipFilePart.cs b/src/SharpCompress/Common/GZip/GZipFilePart.cs index 0dd9a2d1..fbf4ee45 100644 --- a/src/SharpCompress/Common/GZip/GZipFilePart.cs +++ b/src/SharpCompress/Common/GZip/GZipFilePart.cs @@ -13,7 +13,8 @@ internal sealed class GZipFilePart : FilePart private string? _name; private readonly Stream _stream; - internal GZipFilePart(Stream stream, ArchiveEncoding archiveEncoding) : base(archiveEncoding) + internal GZipFilePart(Stream stream, ArchiveEncoding archiveEncoding) + : base(archiveEncoding) { _stream = stream; ReadAndValidateGzipHeader(); diff --git a/src/SharpCompress/Common/IncompleteArchiveException.cs b/src/SharpCompress/Common/IncompleteArchiveException.cs index f1ce3641..a033001a 100644 --- a/src/SharpCompress/Common/IncompleteArchiveException.cs +++ b/src/SharpCompress/Common/IncompleteArchiveException.cs @@ -2,5 +2,6 @@ namespace SharpCompress.Common; public class IncompleteArchiveException : ArchiveException { - public IncompleteArchiveException(string message) : base(message) { } + public IncompleteArchiveException(string message) + : base(message) { } } diff --git a/src/SharpCompress/Common/InvalidFormatException.cs b/src/SharpCompress/Common/InvalidFormatException.cs index b9501369..8f14df14 100644 --- a/src/SharpCompress/Common/InvalidFormatException.cs +++ b/src/SharpCompress/Common/InvalidFormatException.cs @@ -4,7 +4,9 @@ namespace SharpCompress.Common; public class InvalidFormatException : ExtractionException { - public InvalidFormatException(string message) : base(message) { } + public InvalidFormatException(string message) + : base(message) { } - public InvalidFormatException(string message, Exception inner) : base(message, inner) { } + public InvalidFormatException(string message, Exception inner) + : base(message, inner) { } } diff --git a/src/SharpCompress/Common/MultiVolumeExtractionException.cs b/src/SharpCompress/Common/MultiVolumeExtractionException.cs index ee2f95ad..764ac808 100644 --- a/src/SharpCompress/Common/MultiVolumeExtractionException.cs +++ b/src/SharpCompress/Common/MultiVolumeExtractionException.cs @@ -4,8 +4,9 @@ namespace SharpCompress.Common; public class MultiVolumeExtractionException : ExtractionException { - public MultiVolumeExtractionException(string message) : base(message) { } + public MultiVolumeExtractionException(string message) + : base(message) { } - public MultiVolumeExtractionException(string message, Exception inner) : base(message, inner) - { } + public MultiVolumeExtractionException(string message, Exception inner) + : base(message, inner) { } } diff --git a/src/SharpCompress/Common/MultipartStreamRequiredException.cs b/src/SharpCompress/Common/MultipartStreamRequiredException.cs index bbe3ae93..33a842d9 100644 --- a/src/SharpCompress/Common/MultipartStreamRequiredException.cs +++ b/src/SharpCompress/Common/MultipartStreamRequiredException.cs @@ -2,5 +2,6 @@ namespace SharpCompress.Common; public class MultipartStreamRequiredException : ExtractionException { - public MultipartStreamRequiredException(string message) : base(message) { } + public MultipartStreamRequiredException(string message) + : base(message) { } } diff --git a/src/SharpCompress/Common/PasswordProtectedException.cs b/src/SharpCompress/Common/PasswordProtectedException.cs index 7b559625..9ebe3d65 100644 --- a/src/SharpCompress/Common/PasswordProtectedException.cs +++ b/src/SharpCompress/Common/PasswordProtectedException.cs @@ -4,7 +4,9 @@ namespace SharpCompress.Common; public class PasswordProtectedException : ExtractionException { - public PasswordProtectedException(string message) : base(message) { } + public PasswordProtectedException(string message) + : base(message) { } - public PasswordProtectedException(string message, Exception inner) : base(message, inner) { } + public PasswordProtectedException(string message, Exception inner) + : base(message, inner) { } } diff --git a/src/SharpCompress/Common/Rar/RarCrcBinaryReader.cs b/src/SharpCompress/Common/Rar/RarCrcBinaryReader.cs index 97a8aaa9..b4c2d0c9 100644 --- a/src/SharpCompress/Common/Rar/RarCrcBinaryReader.cs +++ b/src/SharpCompress/Common/Rar/RarCrcBinaryReader.cs @@ -8,7 +8,8 @@ internal class RarCrcBinaryReader : MarkingBinaryReader { private uint _currentCrc; - public RarCrcBinaryReader(Stream stream) : base(stream) { } + public RarCrcBinaryReader(Stream stream) + : base(stream) { } public uint GetCrc32() => ~_currentCrc; diff --git a/src/SharpCompress/Common/Rar/RarCryptoBinaryReader.cs b/src/SharpCompress/Common/Rar/RarCryptoBinaryReader.cs index 71902fdf..0be6e74e 100644 --- a/src/SharpCompress/Common/Rar/RarCryptoBinaryReader.cs +++ b/src/SharpCompress/Common/Rar/RarCryptoBinaryReader.cs @@ -11,7 +11,8 @@ internal sealed class RarCryptoBinaryReader : RarCrcBinaryReader private readonly Queue _data = new Queue(); private long _readCount; - public RarCryptoBinaryReader(Stream stream, string password) : base(stream) + public RarCryptoBinaryReader(Stream stream, string password) + : base(stream) { _password = password; diff --git a/src/SharpCompress/Common/Rar/RarCryptoWrapper.cs b/src/SharpCompress/Common/Rar/RarCryptoWrapper.cs index e92056b4..a1cee046 100644 --- a/src/SharpCompress/Common/Rar/RarCryptoWrapper.cs +++ b/src/SharpCompress/Common/Rar/RarCryptoWrapper.cs @@ -15,7 +15,7 @@ internal sealed class RarCryptoWrapper : Stream { _actualStream = actualStream; _salt = salt; - _rijndael = RarRijndael.InitializeFrom(password, salt); + _rijndael = RarRijndael.InitializeFrom(password ?? "", salt); } public override void Flush() => throw new NotSupportedException(); diff --git a/src/SharpCompress/Common/Rar/RarEntry.cs b/src/SharpCompress/Common/Rar/RarEntry.cs index f3614a40..7f280e4a 100644 --- a/src/SharpCompress/Common/Rar/RarEntry.cs +++ b/src/SharpCompress/Common/Rar/RarEntry.cs @@ -1,4 +1,4 @@ -using System; +using System; using SharpCompress.Common.Rar.Headers; namespace SharpCompress.Common.Rar; @@ -11,7 +11,8 @@ public abstract class RarEntry : Entry /// As the V2017 port isn't complete, add this check to use the legacy Rar code. /// internal bool IsRarV3 => - FileHeader.CompressionAlgorithm == 20 + FileHeader.CompressionAlgorithm == 15 + || FileHeader.CompressionAlgorithm == 20 || FileHeader.CompressionAlgorithm == 26 || FileHeader.CompressionAlgorithm == 29 || FileHeader.CompressionAlgorithm == 36; //Nanook - Added 20+26 as Test arc from WinRar2.8 (algo 20) was failing with 2017 code diff --git a/src/SharpCompress/Common/Rar/RarFilePart.cs b/src/SharpCompress/Common/Rar/RarFilePart.cs index 7608d0ac..f87d214d 100644 --- a/src/SharpCompress/Common/Rar/RarFilePart.cs +++ b/src/SharpCompress/Common/Rar/RarFilePart.cs @@ -8,7 +8,8 @@ namespace SharpCompress.Common.Rar; /// internal abstract class RarFilePart : FilePart { - internal RarFilePart(MarkHeader mh, FileHeader fh, int index) : base(fh.ArchiveEncoding) + internal RarFilePart(MarkHeader mh, FileHeader fh, int index) + : base(fh.ArchiveEncoding) { MarkHeader = mh; FileHeader = fh; diff --git a/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs b/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs index 7aac0ade..ac1ad24d 100644 --- a/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs +++ b/src/SharpCompress/Common/SevenZip/SevenZipFilePart.cs @@ -17,7 +17,8 @@ internal class SevenZipFilePart : FilePart int index, CFileItem fileEntry, ArchiveEncoding archiveEncoding - ) : base(archiveEncoding) + ) + : base(archiveEncoding) { _stream = stream; _database = database; diff --git a/src/SharpCompress/Common/Tar/TarEntry.cs b/src/SharpCompress/Common/Tar/TarEntry.cs index 7b5e4a05..59743070 100644 --- a/src/SharpCompress/Common/Tar/TarEntry.cs +++ b/src/SharpCompress/Common/Tar/TarEntry.cs @@ -72,6 +72,10 @@ public class TarEntry : Entry yield return new TarEntry(new TarFilePart(h, null), compressionType); } } + else + { + throw new IncompleteArchiveException("Unexpected EOF reading tar file"); + } } } } diff --git a/src/SharpCompress/Common/Tar/TarFilePart.cs b/src/SharpCompress/Common/Tar/TarFilePart.cs index c81f87e4..bfd2fe29 100644 --- a/src/SharpCompress/Common/Tar/TarFilePart.cs +++ b/src/SharpCompress/Common/Tar/TarFilePart.cs @@ -7,7 +7,8 @@ internal sealed class TarFilePart : FilePart { private readonly Stream _seekableStream; - internal TarFilePart(TarHeader header, Stream seekableStream) : base(header.ArchiveEncoding) + internal TarFilePart(TarHeader header, Stream seekableStream) + : base(header.ArchiveEncoding) { _seekableStream = seekableStream; Header = header; diff --git a/src/SharpCompress/Common/Zip/Headers/DirectoryEndHeader.cs b/src/SharpCompress/Common/Zip/Headers/DirectoryEndHeader.cs index 943ae47c..2e54a6dd 100644 --- a/src/SharpCompress/Common/Zip/Headers/DirectoryEndHeader.cs +++ b/src/SharpCompress/Common/Zip/Headers/DirectoryEndHeader.cs @@ -4,7 +4,8 @@ namespace SharpCompress.Common.Zip.Headers; internal class DirectoryEndHeader : ZipHeader { - public DirectoryEndHeader() : base(ZipHeaderType.DirectoryEnd) { } + public DirectoryEndHeader() + : base(ZipHeaderType.DirectoryEnd) { } internal override void Read(BinaryReader reader) { diff --git a/src/SharpCompress/Common/Zip/Headers/IgnoreHeader.cs b/src/SharpCompress/Common/Zip/Headers/IgnoreHeader.cs index 4852228c..5a587a7b 100644 --- a/src/SharpCompress/Common/Zip/Headers/IgnoreHeader.cs +++ b/src/SharpCompress/Common/Zip/Headers/IgnoreHeader.cs @@ -4,7 +4,8 @@ namespace SharpCompress.Common.Zip.Headers; internal class IgnoreHeader : ZipHeader { - public IgnoreHeader(ZipHeaderType type) : base(type) { } + public IgnoreHeader(ZipHeaderType type) + : base(type) { } internal override void Read(BinaryReader reader) { } } diff --git a/src/SharpCompress/Common/Zip/Headers/SplitHeader.cs b/src/SharpCompress/Common/Zip/Headers/SplitHeader.cs index e151d4e5..4151a6cb 100644 --- a/src/SharpCompress/Common/Zip/Headers/SplitHeader.cs +++ b/src/SharpCompress/Common/Zip/Headers/SplitHeader.cs @@ -5,7 +5,8 @@ namespace SharpCompress.Common.Zip.Headers; internal class SplitHeader : ZipHeader { - public SplitHeader() : base(ZipHeaderType.Split) { } + public SplitHeader() + : base(ZipHeaderType.Split) { } internal override void Read(BinaryReader reader) => throw new NotImplementedException(); } diff --git a/src/SharpCompress/Common/Zip/Headers/Zip64DirectoryEndHeader.cs b/src/SharpCompress/Common/Zip/Headers/Zip64DirectoryEndHeader.cs index 0356c48c..a74b4d1f 100644 --- a/src/SharpCompress/Common/Zip/Headers/Zip64DirectoryEndHeader.cs +++ b/src/SharpCompress/Common/Zip/Headers/Zip64DirectoryEndHeader.cs @@ -4,7 +4,8 @@ namespace SharpCompress.Common.Zip.Headers; internal class Zip64DirectoryEndHeader : ZipHeader { - public Zip64DirectoryEndHeader() : base(ZipHeaderType.Zip64DirectoryEnd) { } + public Zip64DirectoryEndHeader() + : base(ZipHeaderType.Zip64DirectoryEnd) { } internal override void Read(BinaryReader reader) { diff --git a/src/SharpCompress/Common/Zip/Headers/Zip64DirectoryEndLocatorHeader.cs b/src/SharpCompress/Common/Zip/Headers/Zip64DirectoryEndLocatorHeader.cs index 9bbf2670..3020d377 100644 --- a/src/SharpCompress/Common/Zip/Headers/Zip64DirectoryEndLocatorHeader.cs +++ b/src/SharpCompress/Common/Zip/Headers/Zip64DirectoryEndLocatorHeader.cs @@ -4,7 +4,8 @@ namespace SharpCompress.Common.Zip.Headers; internal class Zip64DirectoryEndLocatorHeader : ZipHeader { - public Zip64DirectoryEndLocatorHeader() : base(ZipHeaderType.Zip64DirectoryEndLocator) { } + public Zip64DirectoryEndLocatorHeader() + : base(ZipHeaderType.Zip64DirectoryEndLocator) { } internal override void Read(BinaryReader reader) { diff --git a/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.cs b/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.cs index 79347014..98d230f5 100644 --- a/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.cs +++ b/src/SharpCompress/Common/Zip/Headers/ZipFileEntry.cs @@ -9,7 +9,8 @@ namespace SharpCompress.Common.Zip.Headers; internal abstract class ZipFileEntry : ZipHeader { - protected ZipFileEntry(ZipHeaderType type, ArchiveEncoding archiveEncoding) : base(type) + protected ZipFileEntry(ZipHeaderType type, ArchiveEncoding archiveEncoding) + : base(type) { Extra = new List(); ArchiveEncoding = archiveEncoding; diff --git a/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs b/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs index dc7e4783..63fdc933 100644 --- a/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs @@ -14,7 +14,8 @@ internal class SeekableZipFilePart : ZipFilePart SeekableZipHeaderFactory headerFactory, DirectoryEntryHeader header, Stream stream - ) : base(header, stream) + ) + : base(header, stream) { _headerFactory = headerFactory; _directoryEntryHeader = header; diff --git a/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs b/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs index c5ed221a..1cd1f61f 100644 --- a/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/StreamingZipFilePart.cs @@ -1,4 +1,5 @@ using System.IO; +using System.Net.Sockets; using SharpCompress.Common.Zip.Headers; using SharpCompress.Compressors.Deflate; using SharpCompress.IO; @@ -9,7 +10,8 @@ internal sealed class StreamingZipFilePart : ZipFilePart { private Stream? _decompressionStream; - internal StreamingZipFilePart(ZipFileEntry header, Stream stream) : base(header, stream) { } + internal StreamingZipFilePart(ZipFileEntry header, Stream stream) + : base(header, stream) { } protected override Stream CreateBaseStream() => Header.PackedStream; @@ -36,57 +38,19 @@ internal sealed class StreamingZipFilePart : ZipFilePart { return new BinaryReader(rewindableStream); } + if (Header.HasData && !Skipped) { _decompressionStream ??= GetCompressedStream(); - if (Header.CompressionMethod != ZipCompressionMethod.None) + _decompressionStream.Skip(); + + // If we had TotalIn / TotalOut we could have used them + Header.CompressedSize = _decompressionStream.Position; + + if (_decompressionStream is DeflateStream deflateStream) { - _decompressionStream.Skip(); - - // If we had TotalIn / TotalOut we could have used them - Header.CompressedSize = _decompressionStream.Position; - - if (_decompressionStream is DeflateStream deflateStream) - { - rewindableStream.Rewind(deflateStream.InputBuffer); - } - } - else - { - // We would need to search for the magic word - rewindableStream.Position -= 4; - var pos = rewindableStream.Position; - while (Utility.Find(rewindableStream, new byte[] { 0x50, 0x4b, 0x07, 0x08 })) - { - // We should probably check CRC32 for positive matching as well - var size = rewindableStream.Position - pos; - var br = new BinaryReader(rewindableStream); - br.ReadUInt32(); - br.ReadUInt32(); // CRC32 - var compressed_size = br.ReadUInt32(); - var uncompressed_size = br.ReadUInt32(); - var uncompressed_64bit = br.ReadInt64(); - - var test_64bit = ((long)uncompressed_size << 32) | compressed_size; - - if (test_64bit == size && test_64bit == uncompressed_64bit) - { - Header.CompressedSize = test_64bit; - Header.UncompressedSize = uncompressed_64bit; - rewindableStream.Position -= 24; - break; - } - - if (compressed_size == size && compressed_size == uncompressed_size) - { - Header.CompressedSize = compressed_size; - Header.UncompressedSize = uncompressed_size; - rewindableStream.Position -= 24; - break; - } - rewindableStream.Position -= 20; - } + rewindableStream.Rewind(deflateStream.InputBuffer); } Skipped = true; diff --git a/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.cs b/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.cs index 3e5cd808..e76a2856 100644 --- a/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.cs +++ b/src/SharpCompress/Common/Zip/StreamingZipHeaderFactory.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.IO; +using System.Linq; using SharpCompress.Common.Zip.Headers; using SharpCompress.IO; @@ -7,8 +8,17 @@ namespace SharpCompress.Common.Zip; internal class StreamingZipHeaderFactory : ZipHeaderFactory { - internal StreamingZipHeaderFactory(string? password, ArchiveEncoding archiveEncoding) - : base(StreamingMode.Streaming, password, archiveEncoding) { } + private IEnumerable? _entries; + + internal StreamingZipHeaderFactory( + string? password, + ArchiveEncoding archiveEncoding, + IEnumerable? entries + ) + : base(StreamingMode.Streaming, password, archiveEncoding) + { + _entries = entries; + } internal IEnumerable ReadStreamHeader(Stream stream) { @@ -87,6 +97,21 @@ internal class StreamingZipHeaderFactory : ZipHeaderFactory if (header.ZipHeaderType == ZipHeaderType.LocalEntry) { var local_header = ((LocalEntryHeader)header); + var dir_header = _entries?.FirstOrDefault( + entry => + entry.Key == local_header.Name + && local_header.CompressedSize == 0 + && local_header.UncompressedSize == 0 + && local_header.Crc == 0 + && local_header.IsDirectory == false + ); + + if (dir_header != null) + { + local_header.UncompressedSize = dir_header.Size; + local_header.CompressedSize = dir_header.CompressedSize; + local_header.Crc = (uint)dir_header.Crc; + } // If we have CompressedSize, there is data to be read if (local_header.CompressedSize > 0) diff --git a/src/SharpCompress/Common/Zip/ZipFilePart.cs b/src/SharpCompress/Common/Zip/ZipFilePart.cs index 6f5a73b9..faefdf15 100644 --- a/src/SharpCompress/Common/Zip/ZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/ZipFilePart.cs @@ -17,7 +17,8 @@ namespace SharpCompress.Common.Zip; internal abstract class ZipFilePart : FilePart { - internal ZipFilePart(ZipFileEntry header, Stream stream) : base(header.ArchiveEncoding) + internal ZipFilePart(ZipFileEntry header, Stream stream) + : base(header.ArchiveEncoding) { Header = header; header.Part = this; @@ -66,7 +67,17 @@ internal abstract class ZipFilePart : FilePart { case ZipCompressionMethod.None: { - return stream; + if (stream is ReadOnlySubStream) + { + return stream; + } + + if (Header.CompressedSize > 0) + { + return new ReadOnlySubStream(stream, Header.CompressedSize); + } + + return new DataDescriptorStream(stream); } case ZipCompressionMethod.Deflate: { diff --git a/src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs b/src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs index 4f8a240b..a975ffbd 100644 --- a/src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs +++ b/src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs @@ -319,7 +319,8 @@ internal sealed class CBZip2OutputStream : Stream private int currentChar = -1; private int runLength; - public CBZip2OutputStream(Stream inStream) : this(inStream, 9) { } + public CBZip2OutputStream(Stream inStream) + : this(inStream, 9) { } public CBZip2OutputStream(Stream inStream, int inBlockSize) { diff --git a/src/SharpCompress/Compressors/Deflate/Zlib.cs b/src/SharpCompress/Compressors/Deflate/Zlib.cs index 1377b116..6fbeb4d9 100644 --- a/src/SharpCompress/Compressors/Deflate/Zlib.cs +++ b/src/SharpCompress/Compressors/Deflate/Zlib.cs @@ -189,7 +189,8 @@ public class ZlibException : Exception /// This ctor collects a message attached to the exception. /// /// - public ZlibException(string s) : base(s) { } + public ZlibException(string s) + : base(s) { } } internal class SharedUtils diff --git a/src/SharpCompress/Compressors/Filters/BCJFilter.cs b/src/SharpCompress/Compressors/Filters/BCJFilter.cs index ca8e8c7e..83a93397 100644 --- a/src/SharpCompress/Compressors/Filters/BCJFilter.cs +++ b/src/SharpCompress/Compressors/Filters/BCJFilter.cs @@ -21,8 +21,8 @@ internal class BCJFilter : Filter private int _pos; private int _prevMask; - public BCJFilter(bool isEncoder, Stream baseStream) : base(isEncoder, baseStream, 5) => - _pos = 5; + public BCJFilter(bool isEncoder, Stream baseStream) + : base(isEncoder, baseStream, 5) => _pos = 5; private static bool Test86MsByte(byte b) => b == 0x00 || b == 0xFF; diff --git a/src/SharpCompress/Compressors/Filters/BranchExecFilter.cs b/src/SharpCompress/Compressors/Filters/BranchExecFilter.cs index 091ceda8..9fc439d4 100644 --- a/src/SharpCompress/Compressors/Filters/BranchExecFilter.cs +++ b/src/SharpCompress/Compressors/Filters/BranchExecFilter.cs @@ -34,7 +34,17 @@ public sealed class BranchExecFilter //The previous version of the code would corrupt 2 bytes in the Test.exe at 0x6CF9 (3D6D - should be 4000) - Test zip: WinZip27.Xz.zipx public static void X86Converter(byte[] buf, uint ip, ref uint state) { - bool[] mask_to_allowed_status = new[] { true, true, true, false, true, false, false, false }; + bool[] mask_to_allowed_status = new[] + { + true, + true, + true, + false, + true, + false, + false, + false + }; byte[] mask_to_bit_num = new byte[] { 0, 1, 2, 2, 3, 3, 3, 3 }; @@ -69,8 +79,7 @@ public sealed class BranchExecFilter if (prev_mask != 0) { b = buf[i + 4 - mask_to_bit_num[prev_mask]]; - if (!mask_to_allowed_status[prev_mask] - || X86TestByte(b)) + if (!mask_to_allowed_status[prev_mask] || X86TestByte(b)) { prev_pos = i; prev_mask = (prev_mask << 1) | 1; @@ -83,7 +92,8 @@ public sealed class BranchExecFilter if (X86TestByte(buf[i + 4])) { - src = ((uint)buf[i + 4] << 24) + src = + ((uint)buf[i + 4] << 24) | ((uint)buf[i + 3] << 16) | ((uint)buf[i + 2] << 8) | buf[i + 1]; diff --git a/src/SharpCompress/Compressors/Filters/DeltaFilter.cs b/src/SharpCompress/Compressors/Filters/DeltaFilter.cs new file mode 100644 index 00000000..c5fbeb41 --- /dev/null +++ b/src/SharpCompress/Compressors/Filters/DeltaFilter.cs @@ -0,0 +1,37 @@ +using System; +using System.IO; + +namespace SharpCompress.Compressors.Filters +{ + internal class DeltaFilter : Filter + { + private const int DISTANCE_MIN = 1; + private const int DISTANCE_MAX = 256; + private const int DISTANCE_MASK = DISTANCE_MAX - 1; + + private int _distance; + private byte[] _history; + private int _position; + + public DeltaFilter(bool isEncoder, Stream baseStream, byte[] info) + : base(isEncoder, baseStream, 1) + { + _distance = info[0]; + _history = new byte[DISTANCE_MAX]; + _position = 0; + } + + protected override int Transform(byte[] buffer, int offset, int count) + { + int end = offset + count; + + for (int i = offset; i < end; i++) + { + buffer[i] += _history[(_distance + _position--) & DISTANCE_MASK]; + _history[_position & DISTANCE_MASK] = buffer[i]; + } + + return count; + } + } +} diff --git a/src/SharpCompress/Compressors/LZMA/BitVector.cs b/src/SharpCompress/Compressors/LZMA/BitVector.cs index 5b40536d..9e998430 100644 --- a/src/SharpCompress/Compressors/LZMA/BitVector.cs +++ b/src/SharpCompress/Compressors/LZMA/BitVector.cs @@ -28,7 +28,8 @@ internal class BitVector } } - public BitVector(List bits) : this(bits.Count) + public BitVector(List bits) + : this(bits.Count) { for (var i = 0; i < bits.Count; i++) { diff --git a/src/SharpCompress/Compressors/LZMA/ICoder.cs b/src/SharpCompress/Compressors/LZMA/ICoder.cs index 71265d70..dcddf929 100644 --- a/src/SharpCompress/Compressors/LZMA/ICoder.cs +++ b/src/SharpCompress/Compressors/LZMA/ICoder.cs @@ -8,7 +8,8 @@ namespace SharpCompress.Compressors.LZMA; /// internal class DataErrorException : Exception { - public DataErrorException() : base("Data Error") { } + public DataErrorException() + : base("Data Error") { } } /// @@ -16,7 +17,8 @@ internal class DataErrorException : Exception /// internal class InvalidParamException : Exception { - public InvalidParamException() : base("Invalid Parameter") { } + public InvalidParamException() + : base("Invalid Parameter") { } } public interface ICodeProgress diff --git a/src/SharpCompress/Compressors/LZMA/LzmaEncoder.cs b/src/SharpCompress/Compressors/LZMA/LzmaEncoder.cs index 1dfb43b1..558fe37b 100644 --- a/src/SharpCompress/Compressors/LZMA/LzmaEncoder.cs +++ b/src/SharpCompress/Compressors/LZMA/LzmaEncoder.cs @@ -1725,7 +1725,10 @@ internal class Encoder : ICoder, ISetCoderProperties, IWriteCoderProperties return -1; } - public void SetCoderProperties(ReadOnlySpan propIDs, ReadOnlySpan properties) + public void SetCoderProperties( + ReadOnlySpan propIDs, + ReadOnlySpan properties + ) { for (int i = 0; i < properties.Length; i++) { diff --git a/src/SharpCompress/Compressors/LZMA/LzmaEncoderProperties.cs b/src/SharpCompress/Compressors/LZMA/LzmaEncoderProperties.cs index 9a34e219..274e757b 100644 --- a/src/SharpCompress/Compressors/LZMA/LzmaEncoderProperties.cs +++ b/src/SharpCompress/Compressors/LZMA/LzmaEncoderProperties.cs @@ -12,11 +12,14 @@ public class LzmaEncoderProperties internal ReadOnlySpan Properties => _properties; private readonly object[] _properties; - public LzmaEncoderProperties() : this(false) { } + public LzmaEncoderProperties() + : this(false) { } - public LzmaEncoderProperties(bool eos) : this(eos, 1 << 20) { } + public LzmaEncoderProperties(bool eos) + : this(eos, 1 << 20) { } - public LzmaEncoderProperties(bool eos, int dictionary) : this(eos, dictionary, 32) { } + public LzmaEncoderProperties(bool eos, int dictionary) + : this(eos, dictionary, 32) { } public LzmaEncoderProperties(bool eos, int dictionary, int numFastBytes) { diff --git a/src/SharpCompress/Compressors/LZMA/Registry.cs b/src/SharpCompress/Compressors/LZMA/Registry.cs index 3c25ec46..1fb20cf1 100644 --- a/src/SharpCompress/Compressors/LZMA/Registry.cs +++ b/src/SharpCompress/Compressors/LZMA/Registry.cs @@ -14,6 +14,7 @@ namespace SharpCompress.Compressors.LZMA; internal static class DecoderRegistry { private const uint K_COPY = 0x0; + private const uint K_DELTA = 0x3; private const uint K_LZMA2 = 0x21; private const uint K_LZMA = 0x030101; private const uint K_PPMD = 0x030401; @@ -39,6 +40,8 @@ internal static class DecoderRegistry throw new NotSupportedException(); } return inStreams.Single(); + case K_DELTA: + return new DeltaFilter(false, inStreams.Single(), info); case K_LZMA: case K_LZMA2: return new LzmaStream(info, inStreams.Single(), -1, limit); diff --git a/src/SharpCompress/Compressors/PPMd/H/FreqData.cs b/src/SharpCompress/Compressors/PPMd/H/FreqData.cs index 5811b59b..5d3d41f4 100644 --- a/src/SharpCompress/Compressors/PPMd/H/FreqData.cs +++ b/src/SharpCompress/Compressors/PPMd/H/FreqData.cs @@ -14,7 +14,8 @@ internal class FreqData : Pointer // STATE _PACK_ATTR * Stats; // }; - internal FreqData(byte[] memory) : base(memory) { } + internal FreqData(byte[] memory) + : base(memory) { } internal int SummFreq { diff --git a/src/SharpCompress/Compressors/PPMd/H/PPMContext.cs b/src/SharpCompress/Compressors/PPMd/H/PPMContext.cs index a573aa8d..ab00615d 100644 --- a/src/SharpCompress/Compressors/PPMd/H/PPMContext.cs +++ b/src/SharpCompress/Compressors/PPMd/H/PPMContext.cs @@ -82,7 +82,8 @@ internal class PpmContext : Pointer //UPGRADE_NOTE: Final was removed from the declaration of 'ps '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'" internal int[] _ps = new int[256]; - public PpmContext(byte[] memory) : base(memory) + public PpmContext(byte[] memory) + : base(memory) { _oneState = new State(memory); _freqData = new FreqData(memory); diff --git a/src/SharpCompress/Compressors/PPMd/H/Pointer.cs b/src/SharpCompress/Compressors/PPMd/H/Pointer.cs index b304b359..96cf4c3e 100644 --- a/src/SharpCompress/Compressors/PPMd/H/Pointer.cs +++ b/src/SharpCompress/Compressors/PPMd/H/Pointer.cs @@ -13,7 +13,8 @@ internal abstract class Pointer internal virtual int Address { get; set; } - protected T Initialize(byte[] mem) where T : Pointer + protected T Initialize(byte[] mem) + where T : Pointer { Memory = mem; Address = 0; diff --git a/src/SharpCompress/Compressors/PPMd/H/RarMemBlock.cs b/src/SharpCompress/Compressors/PPMd/H/RarMemBlock.cs index 9fe3958a..ba57cb31 100644 --- a/src/SharpCompress/Compressors/PPMd/H/RarMemBlock.cs +++ b/src/SharpCompress/Compressors/PPMd/H/RarMemBlock.cs @@ -13,7 +13,8 @@ internal class RarMemBlock : Pointer private int _next, _prev; // Pointer RarMemBlock - public RarMemBlock(byte[] memory) : base(memory) { } + public RarMemBlock(byte[] memory) + : base(memory) { } internal int Stamp { diff --git a/src/SharpCompress/Compressors/PPMd/H/RarNode.cs b/src/SharpCompress/Compressors/PPMd/H/RarNode.cs index 614e62cb..c7bde7d6 100644 --- a/src/SharpCompress/Compressors/PPMd/H/RarNode.cs +++ b/src/SharpCompress/Compressors/PPMd/H/RarNode.cs @@ -10,7 +10,8 @@ internal class RarNode : Pointer public const int SIZE = 4; - public RarNode(byte[] memory) : base(memory) { } + public RarNode(byte[] memory) + : base(memory) { } internal int GetNext() { diff --git a/src/SharpCompress/Compressors/PPMd/H/State.cs b/src/SharpCompress/Compressors/PPMd/H/State.cs index dc901c2e..a175f633 100644 --- a/src/SharpCompress/Compressors/PPMd/H/State.cs +++ b/src/SharpCompress/Compressors/PPMd/H/State.cs @@ -8,7 +8,8 @@ internal sealed class State : Pointer { internal const int SIZE = 6; - internal State(byte[]? memory) : base(memory) { } + internal State(byte[]? memory) + : base(memory) { } internal int Symbol { diff --git a/src/SharpCompress/Compressors/PPMd/PpmdProperties.cs b/src/SharpCompress/Compressors/PPMd/PpmdProperties.cs index e75d88b8..e4d52c17 100644 --- a/src/SharpCompress/Compressors/PPMd/PpmdProperties.cs +++ b/src/SharpCompress/Compressors/PPMd/PpmdProperties.cs @@ -9,7 +9,8 @@ public class PpmdProperties private int _allocatorSize; internal Allocator? _allocator; - public PpmdProperties() : this(16 << 20, 6) { } + public PpmdProperties() + : this(16 << 20, 6) { } public PpmdProperties(int allocatorSize, int modelOrder) : this(allocatorSize, modelOrder, ModelRestorationMethod.Restart) { } @@ -29,7 +30,8 @@ public class PpmdProperties public PpmdVersion Version { get; } = PpmdVersion.I1; internal ModelRestorationMethod RestorationMethod { get; } - public PpmdProperties(byte[] properties) : this(properties.AsSpan()) { } + public PpmdProperties(byte[] properties) + : this(properties.AsSpan()) { } public PpmdProperties(ReadOnlySpan properties) { diff --git a/src/SharpCompress/Compressors/Rar/RarCrcStream.cs b/src/SharpCompress/Compressors/Rar/RarCrcStream.cs index e4450c92..98d50b00 100644 --- a/src/SharpCompress/Compressors/Rar/RarCrcStream.cs +++ b/src/SharpCompress/Compressors/Rar/RarCrcStream.cs @@ -12,7 +12,8 @@ internal class RarCrcStream : RarStream IRarUnpack unpack, FileHeader fileHeader, MultiVolumeReadOnlyStream readStream - ) : base(unpack, fileHeader, readStream) + ) + : base(unpack, fileHeader, readStream) { this.readStream = readStream; ResetCrc(); diff --git a/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/BitDecode.cs b/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/BitDecode.cs index d61ce17a..f7f29af3 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/BitDecode.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/BitDecode.cs @@ -2,5 +2,6 @@ namespace SharpCompress.Compressors.Rar.UnpackV1.Decode; internal class BitDecode : Decode { - internal BitDecode() : base(new int[PackDef.BC]) { } + internal BitDecode() + : base(new int[PackDef.BC]) { } } diff --git a/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/Decode.cs b/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/Decode.cs index 44ce0333..2e06c372 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/Decode.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/Decode.cs @@ -2,7 +2,8 @@ namespace SharpCompress.Compressors.Rar.UnpackV1.Decode; internal class Decode { - internal Decode() : this(new int[2]) { } + internal Decode() + : this(new int[2]) { } protected Decode(int[] customDecodeNum) { diff --git a/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/DistDecode.cs b/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/DistDecode.cs index 7b3c29b5..c2231131 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/DistDecode.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/DistDecode.cs @@ -2,5 +2,6 @@ namespace SharpCompress.Compressors.Rar.UnpackV1.Decode; internal class DistDecode : Decode { - internal DistDecode() : base(new int[PackDef.DC]) { } + internal DistDecode() + : base(new int[PackDef.DC]) { } } diff --git a/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/LitDecode.cs b/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/LitDecode.cs index b45eaf8d..f501d3ae 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/LitDecode.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/LitDecode.cs @@ -2,5 +2,6 @@ namespace SharpCompress.Compressors.Rar.UnpackV1.Decode; internal class LitDecode : Decode { - internal LitDecode() : base(new int[PackDef.NC]) { } + internal LitDecode() + : base(new int[PackDef.NC]) { } } diff --git a/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/LowDistDecode.cs b/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/LowDistDecode.cs index c438a3af..9c40c088 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/LowDistDecode.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/LowDistDecode.cs @@ -2,5 +2,6 @@ namespace SharpCompress.Compressors.Rar.UnpackV1.Decode; internal class LowDistDecode : Decode { - internal LowDistDecode() : base(new int[PackDef.LDC]) { } + internal LowDistDecode() + : base(new int[PackDef.LDC]) { } } diff --git a/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/MultDecode.cs b/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/MultDecode.cs index 6ef48ceb..bb8ec59e 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/MultDecode.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/MultDecode.cs @@ -2,5 +2,6 @@ namespace SharpCompress.Compressors.Rar.UnpackV1.Decode; internal class MultDecode : Decode { - internal MultDecode() : base(new int[PackDef.MC20]) { } + internal MultDecode() + : base(new int[PackDef.MC20]) { } } diff --git a/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/RepDecode.cs b/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/RepDecode.cs index d13914b3..f0d60634 100644 --- a/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/RepDecode.cs +++ b/src/SharpCompress/Compressors/Rar/UnpackV1/Decode/RepDecode.cs @@ -2,5 +2,6 @@ namespace SharpCompress.Compressors.Rar.UnpackV1.Decode; internal class RepDecode : Decode { - internal RepDecode() : base(new int[PackDef.RC]) { } + internal RepDecode() + : base(new int[PackDef.RC]) { } } diff --git a/src/SharpCompress/Compressors/Xz/Filters/BlockFilter.cs b/src/SharpCompress/Compressors/Xz/Filters/BlockFilter.cs index baca8403..936a3a0d 100644 --- a/src/SharpCompress/Compressors/Xz/Filters/BlockFilter.cs +++ b/src/SharpCompress/Compressors/Xz/Filters/BlockFilter.cs @@ -6,32 +6,29 @@ namespace SharpCompress.Compressors.Xz.Filters; public abstract class BlockFilter : ReadOnlyStream { - [CLSCompliant(false)] - public enum FilterTypes : ulong + private enum FilterTypes : ulong { - DELTA = 0x03, - ARCH_x86_FILTER = 0x04, - ARCH_PowerPC_FILTER = 0x05, - ARCH_IA64_FILTER = 0x06, - ARCH_ARM_FILTER = 0x07, - ARCH_ARMTHUMB_FILTER = 0x08, - ARCH_SPARC_FILTER = 0x09, - LZMA2 = 0x21 + //Delta = 0x03, + ArchX86Filter = 0x04, + ArchPowerPcFilter = 0x05, + ArchIa64Filter = 0x06, + ArchArmFilter = 0x07, + ArchArmThumbFilter = 0x08, + ArchSparcFilter = 0x09, + Lzma2 = 0x21 } - private static readonly Dictionary> FilterMap = new Dictionary< - FilterTypes, - Func - > - { - { FilterTypes.ARCH_x86_FILTER, () => new X86Filter() }, - { FilterTypes.ARCH_PowerPC_FILTER, () => new PowerPCFilter() }, - { FilterTypes.ARCH_IA64_FILTER, () => new IA64Filter() }, - { FilterTypes.ARCH_ARM_FILTER, () => new ArmFilter() }, - { FilterTypes.ARCH_ARMTHUMB_FILTER, () => new ArmThumbFilter() }, - { FilterTypes.ARCH_SPARC_FILTER, () => new SparcFilter() }, - { FilterTypes.LZMA2, () => new Lzma2Filter() } - }; + private static readonly Dictionary> FILTER_MAP = + new() + { + { FilterTypes.ArchX86Filter, () => new X86Filter() }, + { FilterTypes.ArchPowerPcFilter, () => new PowerPCFilter() }, + { FilterTypes.ArchIa64Filter, () => new IA64Filter() }, + { FilterTypes.ArchArmFilter, () => new ArmFilter() }, + { FilterTypes.ArchArmThumbFilter, () => new ArmThumbFilter() }, + { FilterTypes.ArchSparcFilter, () => new SparcFilter() }, + { FilterTypes.Lzma2, () => new Lzma2Filter() } + }; public abstract bool AllowAsLast { get; } public abstract bool AllowAsNonLast { get; } @@ -43,12 +40,12 @@ public abstract class BlockFilter : ReadOnlyStream public static BlockFilter Read(BinaryReader reader) { var filterType = (FilterTypes)reader.ReadXZInteger(); - if (!FilterMap.ContainsKey(filterType)) + if (!FILTER_MAP.ContainsKey(filterType)) { throw new NotImplementedException($"Filter {filterType} has not yet been implemented"); } - var filter = FilterMap[filterType]()!; + var filter = FILTER_MAP[filterType](); var sizeOfProperties = reader.ReadXZInteger(); if (sizeOfProperties > int.MaxValue) diff --git a/src/SharpCompress/Compressors/Xz/XZBlock.cs b/src/SharpCompress/Compressors/Xz/XZBlock.cs index 5b948500..ea907609 100644 --- a/src/SharpCompress/Compressors/Xz/XZBlock.cs +++ b/src/SharpCompress/Compressors/Xz/XZBlock.cs @@ -27,7 +27,8 @@ public sealed class XZBlock : XZReadOnlyStream private bool _crcChecked; private ulong _bytesRead; - public XZBlock(Stream stream, CheckType checkType, int checkSize) : base(stream) + public XZBlock(Stream stream, CheckType checkType, int checkSize) + : base(stream) { _checkType = checkType; _checkSize = checkSize; diff --git a/src/SharpCompress/Compressors/Xz/XZStream.cs b/src/SharpCompress/Compressors/Xz/XZStream.cs index 78e97a10..26d3dcb2 100644 --- a/src/SharpCompress/Compressors/Xz/XZStream.cs +++ b/src/SharpCompress/Compressors/Xz/XZStream.cs @@ -45,7 +45,8 @@ public sealed class XZStream : XZReadOnlyStream private bool _endOfStream; - public XZStream(Stream stream) : base(stream) { } + public XZStream(Stream stream) + : base(stream) { } public override int Read(byte[] buffer, int offset, int count) { diff --git a/src/SharpCompress/Crypto/Crc32Stream.cs b/src/SharpCompress/Crypto/Crc32Stream.cs index 1360e1d5..7f0af6ca 100644 --- a/src/SharpCompress/Crypto/Crc32Stream.cs +++ b/src/SharpCompress/Crypto/Crc32Stream.cs @@ -18,7 +18,8 @@ public sealed class Crc32Stream : Stream private readonly Stream stream; - public Crc32Stream(Stream stream) : this(stream, DefaultPolynomial, DefaultSeed) { } + public Crc32Stream(Stream stream) + : this(stream, DefaultPolynomial, DefaultSeed) { } public Crc32Stream(Stream stream, uint polynomial, uint seed) { diff --git a/src/SharpCompress/Crypto/CryptoException.cs b/src/SharpCompress/Crypto/CryptoException.cs index 575c34d9..234fd5e5 100644 --- a/src/SharpCompress/Crypto/CryptoException.cs +++ b/src/SharpCompress/Crypto/CryptoException.cs @@ -6,7 +6,9 @@ public class CryptoException : Exception { public CryptoException() { } - public CryptoException(string message) : base(message) { } + public CryptoException(string message) + : base(message) { } - public CryptoException(string message, Exception exception) : base(message, exception) { } + public CryptoException(string message, Exception exception) + : base(message, exception) { } } diff --git a/src/SharpCompress/Crypto/DataLengthException.cs b/src/SharpCompress/Crypto/DataLengthException.cs index bc6cd183..828c1a8f 100644 --- a/src/SharpCompress/Crypto/DataLengthException.cs +++ b/src/SharpCompress/Crypto/DataLengthException.cs @@ -16,7 +16,9 @@ public class DataLengthException : CryptoException * @param message the message to be carried with the exception. */ - public DataLengthException(string message) : base(message) { } + public DataLengthException(string message) + : base(message) { } - public DataLengthException(string message, Exception exception) : base(message, exception) { } + public DataLengthException(string message, Exception exception) + : base(message, exception) { } } diff --git a/src/SharpCompress/Crypto/RijndaelEngine.cs b/src/SharpCompress/Crypto/RijndaelEngine.cs index 7635d7a0..98dd5bc1 100644 --- a/src/SharpCompress/Crypto/RijndaelEngine.cs +++ b/src/SharpCompress/Crypto/RijndaelEngine.cs @@ -1697,7 +1697,8 @@ public sealed class RijndaelEngine : IBlockCipher * default constructor - 128 bit block size. */ - public RijndaelEngine() : this(128) { } + public RijndaelEngine() + : this(128) { } /** * basic constructor - set the cipher up for a given blocksize diff --git a/src/SharpCompress/IO/CountingWritableSubStream.cs b/src/SharpCompress/IO/CountingWritableSubStream.cs index 206eae7c..ce9ace71 100644 --- a/src/SharpCompress/IO/CountingWritableSubStream.cs +++ b/src/SharpCompress/IO/CountingWritableSubStream.cs @@ -5,7 +5,8 @@ namespace SharpCompress.IO; internal class CountingWritableSubStream : NonDisposingStream { - internal CountingWritableSubStream(Stream stream) : base(stream, throwOnDispose: false) { } + internal CountingWritableSubStream(Stream stream) + : base(stream, throwOnDispose: false) { } public ulong Count { get; private set; } diff --git a/src/SharpCompress/IO/DataDescriptorStream.cs b/src/SharpCompress/IO/DataDescriptorStream.cs new file mode 100644 index 00000000..8ba9eb0f --- /dev/null +++ b/src/SharpCompress/IO/DataDescriptorStream.cs @@ -0,0 +1,154 @@ +using System; +using System.IO; +using System.Runtime.CompilerServices; + +namespace SharpCompress.IO; + +public class DataDescriptorStream : Stream +{ + private readonly Stream _stream; + private long _start; + private int _search_position; + private bool _isDisposed; + private bool _done; + + private static byte[] DataDescriptorMarker = new byte[] { 0x50, 0x4b, 0x07, 0x08 }; + private static long DataDescriptorSize = 24; + + public DataDescriptorStream(Stream stream) + { + _stream = stream; + _start = _stream.Position; + _search_position = 0; + _done = false; + } + + internal bool IsRecording { get; private set; } + + protected override void Dispose(bool disposing) + { + if (_isDisposed) + { + return; + } + _isDisposed = true; + base.Dispose(disposing); + if (disposing) + { + _stream.Dispose(); + } + } + + public override bool CanRead => true; + + public override bool CanSeek => _stream.CanSeek; + + public override bool CanWrite => false; + + public override void Flush() => throw new NotSupportedException(); + + public override long Length => _stream.Length; + + public override long Position + { + get => _stream.Position; + set => _stream.Position = value; + } + + private bool validate_data_descriptor(Stream stream, long size) + { + var br = new BinaryReader(stream); + br.ReadUInt32(); + br.ReadUInt32(); // CRC32 can be checked if we calculate it + var compressed_size = br.ReadUInt32(); + var uncompressed_size = br.ReadUInt32(); + var uncompressed_64bit = br.ReadInt64(); + + stream.Position -= DataDescriptorSize; + + var test_64bit = ((long)uncompressed_size << 32) | compressed_size; + + if (test_64bit == size && test_64bit == uncompressed_64bit) + { + return true; + } + + if (compressed_size == size && compressed_size == uncompressed_size) + { + return true; + } + + return false; + } + + public override int Read(byte[] buffer, int offset, int count) + { + if (count == 0 || _done) + { + return 0; + } + + int read = _stream.Read(buffer, offset, count); + + for (int i = 0; i < read; i++) + { + if (buffer[offset + i] == DataDescriptorMarker[_search_position]) + { + _search_position++; + + if (_search_position == 4) + { + _search_position = 0; + + if (read - i > DataDescriptorSize) + { + var check = new MemoryStream( + buffer, + offset + i - 3, + (int)DataDescriptorSize + ); + _done = validate_data_descriptor( + check, + _stream.Position - read + i - 3 - _start + ); + + if (_done) + { + _stream.Position = _stream.Position - read + i - 3; + + return i - 3; + } + } + else + { + _stream.Position = _stream.Position - read + i - 3; + + _done = validate_data_descriptor(_stream, _stream.Position - _start); + + return i - 3; + } + } + } + else + { + _search_position = 0; + } + } + + if (_search_position > 0) + { + read -= _search_position; + _stream.Position -= _search_position; + _search_position = 0; + } + + return read; + } + + public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); + + public override void SetLength(long value) => throw new NotSupportedException(); + + public override void Write(byte[] buffer, int offset, int count) => + throw new NotSupportedException(); +} diff --git a/src/SharpCompress/IO/MarkingBinaryReader.cs b/src/SharpCompress/IO/MarkingBinaryReader.cs index 833effc5..424b9e08 100644 --- a/src/SharpCompress/IO/MarkingBinaryReader.cs +++ b/src/SharpCompress/IO/MarkingBinaryReader.cs @@ -6,7 +6,8 @@ namespace SharpCompress.IO; internal class MarkingBinaryReader : BinaryReader { - public MarkingBinaryReader(Stream stream) : base(stream) { } + public MarkingBinaryReader(Stream stream) + : base(stream) { } public virtual long CurrentReadByteCount { get; protected set; } diff --git a/src/SharpCompress/IO/ReadOnlySubStream.cs b/src/SharpCompress/IO/ReadOnlySubStream.cs index c734df0f..c0b39d54 100644 --- a/src/SharpCompress/IO/ReadOnlySubStream.cs +++ b/src/SharpCompress/IO/ReadOnlySubStream.cs @@ -2,11 +2,13 @@ using System; using System.IO; namespace SharpCompress.IO; + internal class ReadOnlySubStream : NonDisposingStream { private long _position; - public ReadOnlySubStream(Stream stream, long bytesToRead) : this(stream, null, bytesToRead) { } + public ReadOnlySubStream(Stream stream, long bytesToRead) + : this(stream, null, bytesToRead) { } public ReadOnlySubStream(Stream stream, long? origin, long bytesToRead) : base(stream, throwOnDispose: false) @@ -75,6 +77,7 @@ internal class ReadOnlySubStream : NonDisposingStream if (read > 0) { BytesLeftToRead -= read; + _position += read; } return read; } diff --git a/src/SharpCompress/IO/SourceStream.cs b/src/SharpCompress/IO/SourceStream.cs index 1cd0b3ed..26ce0af1 100644 --- a/src/SharpCompress/IO/SourceStream.cs +++ b/src/SharpCompress/IO/SourceStream.cs @@ -9,10 +9,10 @@ namespace SharpCompress.IO; public class SourceStream : Stream { private long _prevSize; - private List _files; - private List _streams; - private Func _getFilePart; - private Func _getStreamPart; + private readonly List _files; + private readonly List _streams; + private readonly Func _getFilePart; + private readonly Func _getStreamPart; private int _stream; public SourceStream(FileInfo file, Func getPart, ReaderOptions options) @@ -39,10 +39,10 @@ public class SourceStream : Stream { _streams.Add(stream!); _getStreamPart = getStreamPart!; - _getFilePart = new Func(a => null!); - if (stream is FileStream) + _getFilePart = _ => null!; + if (stream is FileStream fileStream) { - _files.Add(new FileInfo(((FileStream)stream!).Name)); + _files.Add(new FileInfo(fileStream.Name)); } } else @@ -50,7 +50,7 @@ public class SourceStream : Stream _files.Add(file!); _streams.Add(_files[0].OpenRead()); _getFilePart = getFilePart!; - _getStreamPart = new Func(a => null!); + _getStreamPart = _ => null!; } _stream = 0; _prevSize = 0; @@ -98,9 +98,9 @@ public class SourceStream : Stream } //throw new Exception($"Stream part {idx} not available."); _streams.Add(s); - if (s is FileStream) + if (s is FileStream stream) { - _files.Add(new FileInfo(((FileStream)s).Name)); + _files.Add(new FileInfo(stream.Name)); } } } @@ -123,11 +123,11 @@ public class SourceStream : Stream public override bool CanWrite => false; - public override long Length => (!IsVolumes ? _streams.Sum(a => a.Length) : Current.Length); + public override long Length => !IsVolumes ? _streams.Sum(a => a.Length) : Current.Length; public override long Position { - get => _prevSize + Current.Position; //_prevSize is 0 for multivolume + get => _prevSize + Current.Position; //_prevSize is 0 for multi-volume set => Seek(value, SeekOrigin.Begin); } @@ -167,7 +167,7 @@ public class SourceStream : Stream // Add length of previous stream _prevSize += length; Current.Seek(0, SeekOrigin.Begin); - r = -1; //BugFix: reset to allow loop if count is still not 0 - was breaking split zipx (lzma xz etc) + r = -1; //BugFix: reset to allow loop if count is still not 0 - was breaking split zipx (lzma xz etc) } } @@ -222,9 +222,12 @@ public class SourceStream : Stream { try { - stream?.Dispose(); + stream.Dispose(); + } + catch + { + // ignored } - catch { } } _streams.Clear(); _files.Clear(); diff --git a/src/SharpCompress/Readers/AbstractReader.cs b/src/SharpCompress/Readers/AbstractReader.cs index 86150744..7927937f 100644 --- a/src/SharpCompress/Readers/AbstractReader.cs +++ b/src/SharpCompress/Readers/AbstractReader.cs @@ -129,7 +129,7 @@ public abstract class AbstractReader : IReader, IReaderExtracti { var part = Entry.Parts.First(); - if (ArchiveType != ArchiveType.Rar && !Entry.IsSolid && Entry.CompressedSize > 0) + if (!Entry.IsSolid && Entry.CompressedSize > 0) { //not solid and has a known compressed size then we can skip raw bytes. var rawStream = part.GetRawStream(); diff --git a/src/SharpCompress/Readers/GZip/GZipReader.cs b/src/SharpCompress/Readers/GZip/GZipReader.cs index 84129d20..27394cc7 100644 --- a/src/SharpCompress/Readers/GZip/GZipReader.cs +++ b/src/SharpCompress/Readers/GZip/GZipReader.cs @@ -7,8 +7,8 @@ namespace SharpCompress.Readers.GZip; public class GZipReader : AbstractReader { - internal GZipReader(Stream stream, ReaderOptions options) : base(options, ArchiveType.GZip) => - Volume = new GZipVolume(stream, options); + internal GZipReader(Stream stream, ReaderOptions options) + : base(options, ArchiveType.GZip) => Volume = new GZipVolume(stream, options); public override GZipVolume Volume { get; } diff --git a/src/SharpCompress/Readers/Rar/NonSeekableStreamFilePart.cs b/src/SharpCompress/Readers/Rar/NonSeekableStreamFilePart.cs index 09753b95..3a43e60c 100644 --- a/src/SharpCompress/Readers/Rar/NonSeekableStreamFilePart.cs +++ b/src/SharpCompress/Readers/Rar/NonSeekableStreamFilePart.cs @@ -11,5 +11,7 @@ internal class NonSeekableStreamFilePart : RarFilePart internal override Stream GetCompressedStream() => FileHeader.PackedStream; + internal override Stream? GetRawStream() => FileHeader.PackedStream; + internal override string FilePartName => "Unknown Stream - File Entry: " + FileHeader.FileName; } diff --git a/src/SharpCompress/Readers/Rar/RarReader.cs b/src/SharpCompress/Readers/Rar/RarReader.cs index 71d671a3..da8c132b 100644 --- a/src/SharpCompress/Readers/Rar/RarReader.cs +++ b/src/SharpCompress/Readers/Rar/RarReader.cs @@ -18,7 +18,8 @@ public abstract class RarReader : AbstractReader internal Lazy UnpackV1 { get; } = new Lazy(() => new Compressors.Rar.UnpackV1.Unpack()); - internal RarReader(ReaderOptions options) : base(options, ArchiveType.Rar) { } + internal RarReader(ReaderOptions options) + : base(options, ArchiveType.Rar) { } internal abstract void ValidateArchive(RarVolume archive); diff --git a/src/SharpCompress/Readers/Rar/SingleVolumeRarReader.cs b/src/SharpCompress/Readers/Rar/SingleVolumeRarReader.cs index f3ab98b4..768aa115 100644 --- a/src/SharpCompress/Readers/Rar/SingleVolumeRarReader.cs +++ b/src/SharpCompress/Readers/Rar/SingleVolumeRarReader.cs @@ -8,8 +8,8 @@ internal class SingleVolumeRarReader : RarReader { private readonly Stream stream; - internal SingleVolumeRarReader(Stream stream, ReaderOptions options) : base(options) => - this.stream = stream; + internal SingleVolumeRarReader(Stream stream, ReaderOptions options) + : base(options) => this.stream = stream; internal override void ValidateArchive(RarVolume archive) { diff --git a/src/SharpCompress/Readers/Zip/ZipReader.cs b/src/SharpCompress/Readers/Zip/ZipReader.cs index 616e8057..b00690f0 100644 --- a/src/SharpCompress/Readers/Zip/ZipReader.cs +++ b/src/SharpCompress/Readers/Zip/ZipReader.cs @@ -10,10 +10,26 @@ public class ZipReader : AbstractReader { private readonly StreamingZipHeaderFactory _headerFactory; - private ZipReader(Stream stream, ReaderOptions options) : base(options, ArchiveType.Zip) + private ZipReader(Stream stream, ReaderOptions options) + : base(options, ArchiveType.Zip) { Volume = new ZipVolume(stream, options); - _headerFactory = new StreamingZipHeaderFactory(options.Password, options.ArchiveEncoding); + _headerFactory = new StreamingZipHeaderFactory( + options.Password, + options.ArchiveEncoding, + null + ); + } + + private ZipReader(Stream stream, ReaderOptions options, IEnumerable entries) + : base(options, ArchiveType.Zip) + { + Volume = new ZipVolume(stream, options); + _headerFactory = new StreamingZipHeaderFactory( + options.Password, + options.ArchiveEncoding, + entries + ); } public override ZipVolume Volume { get; } @@ -32,6 +48,16 @@ public class ZipReader : AbstractReader return new ZipReader(stream, options ?? new ReaderOptions()); } + public static ZipReader Open( + Stream stream, + ReaderOptions? options, + IEnumerable entries + ) + { + stream.CheckNotNull(nameof(stream)); + return new ZipReader(stream, options ?? new ReaderOptions(), entries); + } + #endregion Open protected override IEnumerable GetEntries(Stream stream) diff --git a/src/SharpCompress/Writers/GZip/GZipWriterOptions.cs b/src/SharpCompress/Writers/GZip/GZipWriterOptions.cs index faff1cc0..35e19faf 100644 --- a/src/SharpCompress/Writers/GZip/GZipWriterOptions.cs +++ b/src/SharpCompress/Writers/GZip/GZipWriterOptions.cs @@ -5,9 +5,11 @@ namespace SharpCompress.Writers.GZip; public class GZipWriterOptions : WriterOptions { - public GZipWriterOptions() : base(CompressionType.GZip) { } + public GZipWriterOptions() + : base(CompressionType.GZip) { } - internal GZipWriterOptions(WriterOptions options) : base(options.CompressionType) + internal GZipWriterOptions(WriterOptions options) + : base(options.CompressionType) { LeaveStreamOpen = options.LeaveStreamOpen; ArchiveEncoding = options.ArchiveEncoding; diff --git a/src/SharpCompress/Writers/Tar/TarWriter.cs b/src/SharpCompress/Writers/Tar/TarWriter.cs index 068aeee1..2427db37 100644 --- a/src/SharpCompress/Writers/Tar/TarWriter.cs +++ b/src/SharpCompress/Writers/Tar/TarWriter.cs @@ -14,7 +14,8 @@ public class TarWriter : AbstractWriter { private readonly bool finalizeArchiveOnClose; - public TarWriter(Stream destination, TarWriterOptions options) : base(ArchiveType.Tar, options) + public TarWriter(Stream destination, TarWriterOptions options) + : base(ArchiveType.Tar, options) { finalizeArchiveOnClose = options.FinalizeArchiveOnClose; diff --git a/src/SharpCompress/Writers/Tar/TarWriterOptions.cs b/src/SharpCompress/Writers/Tar/TarWriterOptions.cs index ca51ebb6..8f2e866d 100755 --- a/src/SharpCompress/Writers/Tar/TarWriterOptions.cs +++ b/src/SharpCompress/Writers/Tar/TarWriterOptions.cs @@ -12,6 +12,6 @@ public class TarWriterOptions : WriterOptions public TarWriterOptions(CompressionType compressionType, bool finalizeArchiveOnClose) : base(compressionType) => FinalizeArchiveOnClose = finalizeArchiveOnClose; - internal TarWriterOptions(WriterOptions options) : this(options.CompressionType, true) => - ArchiveEncoding = options.ArchiveEncoding; + internal TarWriterOptions(WriterOptions options) + : this(options.CompressionType, true) => ArchiveEncoding = options.ArchiveEncoding; } diff --git a/src/SharpCompress/Writers/Zip/ZipWriterOptions.cs b/src/SharpCompress/Writers/Zip/ZipWriterOptions.cs index fc11ffbf..fcbc9db3 100644 --- a/src/SharpCompress/Writers/Zip/ZipWriterOptions.cs +++ b/src/SharpCompress/Writers/Zip/ZipWriterOptions.cs @@ -5,9 +5,11 @@ namespace SharpCompress.Writers.Zip; public class ZipWriterOptions : WriterOptions { - public ZipWriterOptions(CompressionType compressionType) : base(compressionType) { } + public ZipWriterOptions(CompressionType compressionType) + : base(compressionType) { } - internal ZipWriterOptions(WriterOptions options) : base(options.CompressionType) + internal ZipWriterOptions(WriterOptions options) + : base(options.CompressionType) { LeaveStreamOpen = options.LeaveStreamOpen; ArchiveEncoding = options.ArchiveEncoding; diff --git a/tests/SharpCompress.Test/ArchiveTests.cs b/tests/SharpCompress.Test/ArchiveTests.cs index c44271e1..08377a0e 100644 --- a/tests/SharpCompress.Test/ArchiveTests.cs +++ b/tests/SharpCompress.Test/ArchiveTests.cs @@ -282,4 +282,29 @@ public class ArchiveTests : ReaderTests } VerifyFilesEx(); } + + protected void ArchiveDeltaDistanceRead(string testArchive) + { + testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive); + using (var archive = Archives.ArchiveFactory.Open(testArchive, null)) + using (var reader = archive.ExtractAllEntries()) + while (reader.MoveToNextEntry()) + { + if (!reader.Entry.IsDirectory) + { + var memory = new MemoryStream(); + reader.WriteEntryTo(memory); + + memory.Position = 0; + + for (int y = 0; y < 9; y++) + for (int x = 0; x < 256; x++) + { + Assert.Equal(x, memory.ReadByte()); + } + + Assert.Equal((int)-1, memory.ReadByte()); + } + } + } } diff --git a/tests/SharpCompress.Test/GZip/GZipWriterTests.cs b/tests/SharpCompress.Test/GZip/GZipWriterTests.cs index 7fbf0b5b..f4ca13d3 100644 --- a/tests/SharpCompress.Test/GZip/GZipWriterTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipWriterTests.cs @@ -8,7 +8,8 @@ namespace SharpCompress.Test.GZip; public class GZipWriterTests : WriterTests { - public GZipWriterTests() : base(ArchiveType.GZip) => UseExtensionInsteadOfNameToVerify = true; + public GZipWriterTests() + : base(ArchiveType.GZip) => UseExtensionInsteadOfNameToVerify = true; [Fact] public void GZip_Writer_Generic() diff --git a/tests/SharpCompress.Test/Mocks/TestStream.cs b/tests/SharpCompress.Test/Mocks/TestStream.cs index f79c5b9d..66923df3 100644 --- a/tests/SharpCompress.Test/Mocks/TestStream.cs +++ b/tests/SharpCompress.Test/Mocks/TestStream.cs @@ -6,8 +6,8 @@ public class TestStream : Stream { private readonly Stream stream; - public TestStream(Stream stream) : this(stream, stream.CanRead, stream.CanWrite, stream.CanSeek) - { } + public TestStream(Stream stream) + : this(stream, stream.CanRead, stream.CanWrite, stream.CanSeek) { } public bool IsDisposed { get; private set; } diff --git a/tests/SharpCompress.Test/Rar/RarArchiveTests.cs b/tests/SharpCompress.Test/Rar/RarArchiveTests.cs index 0936a8ed..6496e15e 100644 --- a/tests/SharpCompress.Test/Rar/RarArchiveTests.cs +++ b/tests/SharpCompress.Test/Rar/RarArchiveTests.cs @@ -355,6 +355,24 @@ public class RarArchiveTests : ArchiveTests [Fact] public void Rar2_ArchiveFileRead() => ArchiveFileRead("Rar2.rar"); + [Fact] + public void Rar15_ArchiveFileRead() + { + UseExtensionInsteadOfNameToVerify = true; + UseCaseInsensitiveToVerify = true; + ArchiveFileRead("Rar15.rar"); + } + + [Fact] + public void Rar15_ArchiveVersionTest() + { + var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "Rar15.rar"); + + using var archive = RarArchive.Open(testArchive); + Assert.Equal(1, archive.MinVersion); + Assert.Equal(1, archive.MaxVersion); + } + [Fact] public void Rar2_ArchiveVersionTest() { diff --git a/tests/SharpCompress.Test/Rar/RarReaderTests.cs b/tests/SharpCompress.Test/Rar/RarReaderTests.cs index 45744270..097b0fc8 100644 --- a/tests/SharpCompress.Test/Rar/RarReaderTests.cs +++ b/tests/SharpCompress.Test/Rar/RarReaderTests.cs @@ -332,4 +332,49 @@ public class RarReaderTests : ReaderTests } } } + + [Fact] + public void Rar_NullReference() + { + { + var archives = new[] + { + "Rar.EncryptedParts.part01.rar", + "Rar.EncryptedParts.part02.rar", + "Rar.EncryptedParts.part03.rar", + "Rar.EncryptedParts.part04.rar", + "Rar.EncryptedParts.part05.rar", + "Rar.EncryptedParts.part06.rar" + }; + + using ( + var reader = RarReader.Open( + archives + .Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)) + .Select(p => File.OpenRead(p)), + new ReaderOptions() { Password = "test" } + ) + ) + { + while (reader.MoveToNextEntry()) + { + // + } + } + } + + { + using var stream = File.OpenRead( + Path.Combine(TEST_ARCHIVES_PATH, "Rar.encrypted_filesOnly.rar") + ); + using var reader = ReaderFactory.Open( + stream, + new ReaderOptions() { LookForHeader = true } + ); + while (reader.MoveToNextEntry()) + { + // + } + } + } } diff --git a/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs index 1b5848cd..64ea96b6 100644 --- a/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs +++ b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using SharpCompress.Common; using SharpCompress.Readers; using Xunit; @@ -132,4 +133,9 @@ public class SevenZipArchiveTests : ArchiveTests ); + public void SevenZipArchive_Delta_FileRead() => ArchiveFileRead("7Zip.delta.7z"); + + [Fact] + public void SevenZipArchive_Delta_Distance() => + ArchiveDeltaDistanceRead("7Zip.delta.distance.7z"); } diff --git a/tests/SharpCompress.Test/SharpCompress.Test.csproj b/tests/SharpCompress.Test/SharpCompress.Test.csproj index cbc76254..c20664a7 100644 --- a/tests/SharpCompress.Test/SharpCompress.Test.csproj +++ b/tests/SharpCompress.Test/SharpCompress.Test.csproj @@ -8,8 +8,8 @@ - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs b/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs index 8c252186..2f336329 100644 --- a/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs +++ b/tests/SharpCompress.Test/Streams/LzmaStreamTests.cs @@ -539,7 +539,11 @@ public class LzmaStreamTests { using MemoryStream inputStream = new MemoryStream(lzmaResultData); using MemoryStream outputStream = new(); - using LzmaStream lzmaStream = new LzmaStream(LzmaEncoderProperties.Default, false, outputStream); + using LzmaStream lzmaStream = new LzmaStream( + LzmaEncoderProperties.Default, + false, + outputStream + ); inputStream.CopyTo(lzmaStream); lzmaStream.Close(); Assert.NotEqual(0, outputStream.Length); @@ -550,20 +554,43 @@ public class LzmaStreamTests { var input = new MemoryStream(lzmaResultData); var compressed = new MemoryStream(); - LzmaStream lzmaEncodingStream = new LzmaStream(LzmaEncoderProperties.Default, false, compressed); + LzmaStream lzmaEncodingStream = new LzmaStream( + LzmaEncoderProperties.Default, + false, + compressed + ); input.CopyTo(lzmaEncodingStream); lzmaEncodingStream.Close(); compressed.Position = 0; var output = new MemoryStream(); - DecompressLzmaStream(lzmaEncodingStream.Properties, compressed, compressed.Length, output, lzmaResultData.LongLength); + DecompressLzmaStream( + lzmaEncodingStream.Properties, + compressed, + compressed.Length, + output, + lzmaResultData.LongLength + ); Assert.Equal(output.ToArray(), lzmaResultData); } - private static void DecompressLzmaStream(byte[] properties, Stream compressedStream, long compressedSize, Stream decompressedStream, long decompressedSize) + private static void DecompressLzmaStream( + byte[] properties, + Stream compressedStream, + long compressedSize, + Stream decompressedStream, + long decompressedSize + ) { - LzmaStream lzmaStream = new LzmaStream(properties, compressedStream, compressedSize, -1, null, false); + LzmaStream lzmaStream = new LzmaStream( + properties, + compressedStream, + compressedSize, + -1, + null, + false + ); byte[] buffer = ArrayPool.Shared.Rent(1024); long totalRead = 0; diff --git a/tests/SharpCompress.Test/Tar/TarArchiveTests.cs b/tests/SharpCompress.Test/Tar/TarArchiveTests.cs index fd05d15e..dddb32a6 100644 --- a/tests/SharpCompress.Test/Tar/TarArchiveTests.cs +++ b/tests/SharpCompress.Test/Tar/TarArchiveTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using System.Linq; using SharpCompress.Archives; diff --git a/tests/SharpCompress.Test/Tar/TarReaderTests.cs b/tests/SharpCompress.Test/Tar/TarReaderTests.cs index a92a3c0a..76629363 100644 --- a/tests/SharpCompress.Test/Tar/TarReaderTests.cs +++ b/tests/SharpCompress.Test/Tar/TarReaderTests.cs @@ -183,6 +183,25 @@ public class TarReaderTests : ReaderTests } } + [Fact] + public void Tar_Broken_Stream() + { + string archiveFullPath = Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar"); + using (Stream stream = File.OpenRead(archiveFullPath)) + using (IReader reader = ReaderFactory.Open(stream)) + { + var memoryStream = new MemoryStream(); + + Action action = () => reader.MoveToNextEntry(); + var exception = Record.Exception(action); + Assert.Null(exception); + reader.MoveToNextEntry(); + reader.WriteEntryTo(memoryStream); + stream.Close(); + Assert.Throws(action); + } + } + #if !NETFRAMEWORK [Fact] public void Tar_GZip_With_Symlink_Entries() diff --git a/tests/SharpCompress.Test/Tar/TarWriterTests.cs b/tests/SharpCompress.Test/Tar/TarWriterTests.cs index 4be9f9d8..4f4464c1 100644 --- a/tests/SharpCompress.Test/Tar/TarWriterTests.cs +++ b/tests/SharpCompress.Test/Tar/TarWriterTests.cs @@ -8,7 +8,8 @@ namespace SharpCompress.Test.Tar; public class TarWriterTests : WriterTests { - public TarWriterTests() : base(ArchiveType.Tar) => UseExtensionInsteadOfNameToVerify = true; + public TarWriterTests() + : base(ArchiveType.Tar) => UseExtensionInsteadOfNameToVerify = true; [Fact] public void Tar_Writer() => diff --git a/tests/SharpCompress.Test/TestBase.cs b/tests/SharpCompress.Test/TestBase.cs index ddfd0096..3ad1fe79 100644 --- a/tests/SharpCompress.Test/TestBase.cs +++ b/tests/SharpCompress.Test/TestBase.cs @@ -138,12 +138,23 @@ public class TestBase : IDisposable } protected bool UseExtensionInsteadOfNameToVerify { get; set; } + protected bool UseCaseInsensitiveToVerify { get; set; } protected void VerifyFilesByExtension() { - var extracted = Directory - .EnumerateFiles(SCRATCH_FILES_PATH, "*.*", SearchOption.AllDirectories) - .ToLookup(path => Path.GetExtension(path)); + ILookup extracted; + if (UseCaseInsensitiveToVerify) + { + extracted = Directory + .EnumerateFiles(SCRATCH_FILES_PATH, "*.*", SearchOption.AllDirectories) + .ToLookup(path => Path.GetExtension(path).ToLowerInvariant()); + } + else + { + extracted = Directory + .EnumerateFiles(SCRATCH_FILES_PATH, "*.*", SearchOption.AllDirectories) + .ToLookup(path => Path.GetExtension(path)); + } var original = Directory .EnumerateFiles(ORIGINAL_FILES_PATH, "*.*", SearchOption.AllDirectories) .ToLookup(path => Path.GetExtension(path)); diff --git a/tests/SharpCompress.Test/Zip/Zip64Tests.cs b/tests/SharpCompress.Test/Zip/Zip64Tests.cs index 7e20c3d4..739867f7 100644 --- a/tests/SharpCompress.Test/Zip/Zip64Tests.cs +++ b/tests/SharpCompress.Test/Zip/Zip64Tests.cs @@ -14,7 +14,8 @@ namespace SharpCompress.Test.Zip; public class Zip64Tests : WriterTests { - public Zip64Tests() : base(ArchiveType.Zip) { } + public Zip64Tests() + : base(ArchiveType.Zip) { } // 4GiB + 1 private const long FOUR_GB_LIMIT = ((long)uint.MaxValue) + 1; diff --git a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs index f2b8ab1c..fde87dc6 100644 --- a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs @@ -715,6 +715,29 @@ public class ZipArchiveTests : ArchiveTests } } + [Fact] + public void Zip_Uncompressed_Read_All() + { + string zipPath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.uncompressed.zip"); + using (var stream = File.Open(zipPath, FileMode.Open, FileAccess.Read)) + { + IArchive archive = ArchiveFactory.Open(stream); + IReader reader = archive.ExtractAllEntries(); + int entries = 0; + while (reader.MoveToNextEntry()) + { + using (var entryStream = reader.OpenEntryStream()) + using (var target = new MemoryStream()) + { + entryStream.CopyTo(target); + } + + entries++; + } + Assert.Equal(4, entries); + } + } + [Fact] public void Zip_Uncompressed_Skip_All() { diff --git a/tests/SharpCompress.Test/Zip/ZipReaderTests.cs b/tests/SharpCompress.Test/Zip/ZipReaderTests.cs index 95d9b93d..e5254c19 100644 --- a/tests/SharpCompress.Test/Zip/ZipReaderTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipReaderTests.cs @@ -338,7 +338,20 @@ public class ZipReaderTests : ReaderTests } [Fact] - public void Zip_Uncompressed_Skip_All() + public void Zip_ReaderFactory_Uncompressed_Read_All() + { + var zipPath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.uncompressed.zip"); + using var stream = File.Open(zipPath, FileMode.Open, FileAccess.Read); + using var reader = ReaderFactory.Open(stream); + while (reader.MoveToNextEntry()) + { + var target = new MemoryStream(); + reader.OpenEntryStream().CopyTo(target); + } + } + + [Fact] + public void Zip_ReaderFactory_Uncompressed_Skip_All() { var zipPath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.uncompressed.zip"); using var stream = File.Open(zipPath, FileMode.Open, FileAccess.Read); diff --git a/tests/SharpCompress.Test/Zip/ZipWriterTests.cs b/tests/SharpCompress.Test/Zip/ZipWriterTests.cs index d2224000..530397bd 100644 --- a/tests/SharpCompress.Test/Zip/ZipWriterTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipWriterTests.cs @@ -7,7 +7,8 @@ namespace SharpCompress.Test.Zip; public class ZipWriterTests : WriterTests { - public ZipWriterTests() : base(ArchiveType.Zip) { } + public ZipWriterTests() + : base(ArchiveType.Zip) { } [Fact] public void Zip_Deflate_Write() => diff --git a/tests/TestArchives/Archives/7Zip.delta.7z b/tests/TestArchives/Archives/7Zip.delta.7z new file mode 100644 index 00000000..35244ee3 Binary files /dev/null and b/tests/TestArchives/Archives/7Zip.delta.7z differ diff --git a/tests/TestArchives/Archives/7Zip.delta.distance.7z b/tests/TestArchives/Archives/7Zip.delta.distance.7z new file mode 100644 index 00000000..0a0369b7 Binary files /dev/null and b/tests/TestArchives/Archives/7Zip.delta.distance.7z differ diff --git a/tests/TestArchives/Archives/Rar15.rar b/tests/TestArchives/Archives/Rar15.rar new file mode 100644 index 00000000..dfd42bf0 Binary files /dev/null and b/tests/TestArchives/Archives/Rar15.rar differ