Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
735 changes: 275 additions & 460 deletions .editorconfig

Large diffs are not rendered by default.

37 changes: 30 additions & 7 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,37 @@
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<AnalysisMode>Recommended</AnalysisMode>
<WarningsAsErrors>true</WarningsAsErrors>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<RunAnalyzersDuringLiveAnalysis>False</RunAnalyzersDuringLiveAnalysis>
<RunAnalyzersDuringBuild>False</RunAnalyzersDuringBuild>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>
<PropertyGroup Label="Analyers">
<EnableNetAnalyzers>true</EnableNetAnalyzers>
<AnalysisLevel>latest-AllEnabledByDefault</AnalysisLevel>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors>true</WarningsAsErrors>
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
<EnforceCodeStyleInBuild>false</EnforceCodeStyleInBuild>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>
<!--Disabled by design-->
CA5399;CA1812;
<!--XML comment-->
CS1591;CS1573;
<!-- Globalization rules -->
CA1303;CA1304;CA1305;CA1307;CA1308;CA1309;CA1310;CA1311;
<!-- Logging -->
CA1848;CA1727;
<!-- Others we don't want -->
CA1815;CA1725;
<!-- Naming things is hard enough -->
CA1710;CA1711;CA1720;CA1724;
<!-- Aspirational, slowly add -->
CA1502;CA1716;NETSDK1206;IDE0017;IDE0032;IDE0040;IDE0044;IDE0051;IDE0052;IDE0060;IDE0251;IDE1006;IDE1007;IDE1033;IDE1036;IDE1040;IDE1041;IDE1045;IDE1050;IDE1051;IDE1055;IDE1060;IDE1063;
IDE1070;IDE1071;IDE1075;IDE1080;IDE1090;CA1024;CA1027;CA1031;CA1032;CA1033;CA1028;CA1034;CA1051;CA1063;CA1069;CA1505;CA1802;CA1814;CA1819;CA1805;CA2214;CA2225;CA2237;
$(NoWarn)
</NoWarn
>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion build/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ IEnumerable<string> GetFiles(string d)

Target("default", [Publish], () => Console.WriteLine("Done!"));

await RunTargetsAndExitAsync(args);
await RunTargetsAndExitAsync(args).ConfigureAwait(false);
7 changes: 3 additions & 4 deletions src/SharpCompress/Algorithms/Adler32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
#if SUPPORTS_RUNTIME_INTRINSICS
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
#endif
Expand Down Expand Up @@ -108,8 +108,7 @@ public static int EvenReduceSum(Vector256<int> accumulator)

// The C# compiler emits this as a compile-time constant embedded in the PE file.
private static ReadOnlySpan<byte> Tap1Tap2 =>
new byte[]
{
[
32,
31,
30,
Expand Down Expand Up @@ -142,7 +141,7 @@ public static int EvenReduceSum(Vector256<int> accumulator)
3,
2,
1, // tap2
};
];
#endif

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/SharpCompress/Archives/AbstractArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
{
Type = type;
ReaderOptions = new();
_lazyVolumes = new LazyReadOnlyCollection<TVolume>(Enumerable.Empty<TVolume>());
_lazyEntries = new LazyReadOnlyCollection<TEntry>(Enumerable.Empty<TEntry>());
_lazyVolumes = new LazyReadOnlyCollection<TVolume>([]);
_lazyEntries = new LazyReadOnlyCollection<TEntry>([]);
}

public ArchiveType Type { get; }
Expand Down Expand Up @@ -87,7 +87,7 @@

IEnumerable<IVolume> IArchive.Volumes => _lazyVolumes.Cast<IVolume>();

public virtual void Dispose()

Check warning on line 90 in src/SharpCompress/Archives/AbstractArchive.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Change AbstractArchive<TEntry, TVolume>.Dispose() to call GC.SuppressFinalize(object). This will prevent derived types that introduce a finalizer from needing to re-implement 'IDisposable' to call it. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1816)
{
if (!_disposed)
{
Expand Down
6 changes: 3 additions & 3 deletions src/SharpCompress/Archives/AbstractWritableArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public void Dispose()
}
}

private readonly List<TEntry> newEntries = new();
private readonly List<TEntry> removedEntries = new();
private readonly List<TEntry> newEntries = [];
private readonly List<TEntry> removedEntries = [];

private readonly List<TEntry> modifiedEntries = new();
private readonly List<TEntry> modifiedEntries = [];
private bool hasModifications;
private bool pauseRebuilding;

Expand Down
5 changes: 5 additions & 0 deletions src/SharpCompress/Archives/ArchiveVolumeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal abstract class ArchiveVolumeFactory
//split 001, 002 ...
var m = Regex.Match(part1.Name, @"^(.*\.)([0-9]+)$", RegexOptions.IgnoreCase);
if (m.Success)
{
item = new FileInfo(
Path.Combine(
part1.DirectoryName!,
Expand All @@ -22,9 +23,13 @@ internal abstract class ArchiveVolumeFactory
)
)
);
}

if (item != null && item.Exists)
{
return item;
}

return null;
}
}
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/AutoArchiveFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace SharpCompress.Archives;

class AutoArchiveFactory : IArchiveFactory
internal class AutoArchiveFactory : IArchiveFactory
{
public string Name => nameof(AutoArchiveFactory);

Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/GZip/GZipArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected override GZipArchiveEntry CreateEntryInternal(
bool closeStream
)
{
if (Entries.Any())
if (Entries.Count != 0)
{
throw new InvalidFormatException("Only one entry is allowed in a GZip Archive");
}
Expand Down
5 changes: 4 additions & 1 deletion src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ namespace SharpCompress.Archives.GZip;
public class GZipArchiveEntry : GZipEntry, IArchiveEntry
{
internal GZipArchiveEntry(GZipArchive archive, GZipFilePart? part)
: base(part) => Archive = archive;
: base(part)
{
Archive = archive;
}

public virtual Stream OpenEntryStream()
{
Expand Down
1 change: 0 additions & 1 deletion src/SharpCompress/Archives/IArchiveExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using SharpCompress.Common;
using SharpCompress.Readers;
Expand Down
14 changes: 0 additions & 14 deletions src/SharpCompress/Archives/IWriteableArchiveFactory.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
namespace SharpCompress.Archives;

/// <summary>
/// Decorator for <see cref="Factories.Factory"/> used to declare an archive format as able to create writeable archives
/// </summary>
/// <remarks>
/// Implemented by:<br/>
/// <list type="table">
/// <item><see cref="Factories.TarFactory"/></item>
/// <item><see cref="Factories.ZipFactory"/></item>
/// <item><see cref="Factories.GZipFactory"/></item>
/// </list>
public interface IWriteableArchiveFactory : Factories.IFactory
{
/// <summary>
/// Creates a new, empty archive, ready to be written.
/// </summary>
/// <returns></returns>
IWritableArchive CreateWriteableArchive();
}
5 changes: 4 additions & 1 deletion src/SharpCompress/Archives/Rar/FileInfoRarFilePart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ internal FileInfoRarFilePart(
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; }

Expand Down
4 changes: 2 additions & 2 deletions src/SharpCompress/Archives/Rar/RarArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using SharpCompress.Common.Rar;
using SharpCompress.Common.Rar.Headers;
using SharpCompress.Compressors.Rar;
using SharpCompress.Compressors.Rar.UnpackV2017;
using SharpCompress.IO;
using SharpCompress.Readers;
using SharpCompress.Readers.Rar;
Expand All @@ -15,8 +16,7 @@ namespace SharpCompress.Archives.Rar;
public class RarArchive : AbstractArchive<RarArchiveEntry, RarVolume>
{
private bool _disposed;
internal Lazy<IRarUnpack> UnpackV2017 { get; } =
new(() => new Compressors.Rar.UnpackV2017.Unpack());
internal Lazy<IRarUnpack> UnpackV2017 { get; } = new(() => new Unpack());
internal Lazy<IRarUnpack> UnpackV1 { get; } = new(() => new Compressors.Rar.UnpackV1.Unpack());

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion src/SharpCompress/Archives/Rar/RarArchiveEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ReaderOptions readerOptions

public IArchive Archive => archive;

internal override IEnumerable<FilePart> Parts => parts.Cast<FilePart>();
internal override IEnumerable<FilePart> Parts => parts;

internal override FileHeader FileHeader => parts.First().FileHeader;

Expand Down Expand Up @@ -88,8 +88,10 @@ public bool IsComplete
{
get
{
#pragma warning disable CA1851
var headers = parts.Select(x => x.FileHeader);
return !headers.First().IsSplitBefore && !headers.Last().IsSplitAfter;
#pragma warning restore CA1851
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/Rar/RarArchiveEntryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ IEnumerable<RarVolume> parts
if (!fp.FileHeader.IsSplitAfter)
{
yield return groupedParts;
groupedParts = new List<RarFilePart>();
groupedParts = [];
}
}
if (groupedParts.Count > 0)
Expand Down
8 changes: 8 additions & 0 deletions src/SharpCompress/Archives/Rar/RarArchiveVolumeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//new style rar - ..part1 | /part01 | part001 ....
var m = Regex.Match(part1.Name, @"^(.*\.part)([0-9]+)(\.rar)$", RegexOptions.IgnoreCase);
if (m.Success)
{
item = new FileInfo(
Path.Combine(
part1.DirectoryName!,
Expand All @@ -23,11 +24,13 @@
)
)
);
}
else
{
//old style - ...rar, .r00, .r01 ...
m = Regex.Match(part1.Name, @"^(.*\.)([r-z{])(ar|[0-9]+)$", RegexOptions.IgnoreCase);
if (m.Success)
{
item = new FileInfo(
Path.Combine(
part1.DirectoryName!,
Expand All @@ -40,12 +43,17 @@
)
)
);
}
else //split .001, .002 ....
{
return ArchiveVolumeFactory.GetFilePart(index, part1);
}
}

if (item != null && item.Exists)

Check warning on line 53 in src/SharpCompress/Archives/Rar/RarArchiveVolumeFactory.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

'item != null' is always 'true'. Remove or refactor the condition(s) to avoid dead code. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1508)
{
return item;
}

return null; //no more items
}
Expand Down
17 changes: 11 additions & 6 deletions src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
protected override IEnumerable<SevenZipVolume> LoadVolumes(SourceStream sourceStream)
{
sourceStream.NotNull("SourceStream is null").LoadAllParts(); //request all streams
return new SevenZipVolume(sourceStream, ReaderOptions, 0).AsEnumerable(); //simple single volume or split, multivolume not supported
return new SevenZipVolume(sourceStream, ReaderOptions).AsEnumerable(); //simple single volume or split, multivolume not supported
}

public static bool IsSevenZipFile(string filePath) => IsSevenZipFile(new FileInfo(filePath));
Expand All @@ -139,7 +139,7 @@
LoadFactory(stream);
if (_database is null)
{
return Enumerable.Empty<SevenZipArchiveEntry>();
return [];
}
var entries = new SevenZipArchiveEntry[_database._files.Count];
for (var i = 0; i < _database._files.Count; i++)
Expand Down Expand Up @@ -186,12 +186,11 @@
}
}

private static ReadOnlySpan<byte> Signature =>
new byte[] { (byte)'7', (byte)'z', 0xBC, 0xAF, 0x27, 0x1C };
private static ReadOnlySpan<byte> Signature => [(byte)'7', (byte)'z', 0xBC, 0xAF, 0x27, 0x1C];

private static bool SignatureMatch(Stream stream)
{
var reader = new BinaryReader(stream);

Check warning on line 193 in src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Call System.IDisposable.Dispose on object created by 'new BinaryReader(stream)' before all references to it are out of scope (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2000)
ReadOnlySpan<byte> signatureBytes = reader.ReadBytes(6);
return signatureBytes.SequenceEqual(Signature);
}
Expand All @@ -216,7 +215,10 @@
private CFileItem? _currentItem;

internal SevenZipReader(ReaderOptions readerOptions, SevenZipArchive archive)
: base(readerOptions, ArchiveType.SevenZip) => this._archive = archive;
: base(readerOptions, ArchiveType.SevenZip)
{
_archive = archive;
}

public override SevenZipVolume Volume => _archive.Volumes.Single();

Expand Down Expand Up @@ -266,7 +268,10 @@
{
private readonly string? _password;

public PasswordProvider(string? password) => _password = password;
public PasswordProvider(string? password)
{
_password = password;
}

public string? CryptoGetTextPassword() => _password;
}
Expand Down
5 changes: 4 additions & 1 deletion src/SharpCompress/Archives/SevenZip/SevenZipArchiveEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ namespace SharpCompress.Archives.SevenZip;
public class SevenZipArchiveEntry : SevenZipEntry, IArchiveEntry
{
internal SevenZipArchiveEntry(SevenZipArchive archive, SevenZipFilePart part)
: base(part) => Archive = archive;
: base(part)
{
Archive = archive;
}

public Stream OpenEntryStream() => FilePart.GetCompressedStream();

Expand Down
5 changes: 4 additions & 1 deletion src/SharpCompress/Archives/Tar/TarArchiveEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ namespace SharpCompress.Archives.Tar;
public class TarArchiveEntry : TarEntry, IArchiveEntry
{
internal TarArchiveEntry(TarArchive archive, TarFilePart? part, CompressionType compressionType)
: base(part, compressionType) => Archive = archive;
: base(part, compressionType)
{
Archive = archive;
}

public virtual Stream OpenEntryStream() => Parts.Single().GetCompressedStream().NotNull();

Expand Down
9 changes: 5 additions & 4 deletions src/SharpCompress/Archives/Zip/ZipArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ public class ZipArchive : AbstractWritableArchive<ZipArchiveEntry, ZipVolume>
/// Constructor with a SourceStream able to handle FileInfo and Streams.
/// </summary>
/// <param name="sourceStream"></param>
/// <param name="options"></param>
internal ZipArchive(SourceStream sourceStream)
: base(ArchiveType.Zip, sourceStream) =>
: base(ArchiveType.Zip, sourceStream)
{
headerFactory = new SeekableZipHeaderFactory(
sourceStream.ReaderOptions.Password,
sourceStream.ReaderOptions.ArchiveEncoding
);
}

/// <summary>
/// Constructor expects a filepath to an existing file.
Expand Down Expand Up @@ -224,7 +225,7 @@ protected override IEnumerable<ZipVolume> LoadVolumes(SourceStream stream)

var streams = stream.Streams.ToList();
var idx = 0;
if (streams.Count() > 1) //test part 2 - true = multipart not split
if (streams.Count > 1) //test part 2 - true = multipart not split
{
streams[1].Position += 4; //skip the POST_DATA_DESCRIPTOR to prevent an exception
var isZip = IsZipFile(streams[1], ReaderOptions.Password, ReaderOptions.BufferSize);
Expand Down Expand Up @@ -287,7 +288,7 @@ protected override IEnumerable<ZipArchiveEntry> LoadEntries(IEnumerable<ZipVolum
break;
case ZipHeaderType.DirectoryEnd:
{
var bytes = ((DirectoryEndHeader)h).Comment ?? Array.Empty<byte>();
var bytes = ((DirectoryEndHeader)h).Comment ?? [];
vols.Last().Comment = ReaderOptions.ArchiveEncoding.Decode(bytes);
yield break;
}
Expand Down
Loading
Loading