Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/AbstractArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal AbstractArchive(ArchiveType type, SourceStream sourceStream)
internal AbstractArchive(ArchiveType type)
{
Type = type;
ReaderOptions = new();
ReaderOptions = ReaderOptions.Default;
_lazyVolumes = new LazyReadOnlyCollection<TVolume>(Enumerable.Empty<TVolume>());
_lazyEntries = new LazyReadOnlyCollection<TEntry>(Enumerable.Empty<TEntry>());
_lazyVolumesAsync = new LazyAsyncReadOnlyCollection<TVolume>(
Expand Down
6 changes: 5 additions & 1 deletion src/SharpCompress/Archives/ArchiveFactory.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public static ValueTask<IAsyncArchive> OpenAsyncArchive(
)
{
filePath.NotNullOrEmpty(nameof(filePath));
return OpenAsyncArchive(new FileInfo(filePath), options, cancellationToken);
return OpenAsyncArchive(
new FileInfo(filePath),
options ?? ReaderOptions.ForFilePath,
cancellationToken
);
}

public static async ValueTask<IAsyncArchive> OpenAsyncArchive(
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/ArchiveFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static IWritableArchive<TOptions> CreateArchive<TOptions>()
public static IArchive OpenArchive(string filePath, ReaderOptions? options = null)
{
filePath.NotNullOrEmpty(nameof(filePath));
return OpenArchive(new FileInfo(filePath), options);
return OpenArchive(new FileInfo(filePath), options ?? ReaderOptions.ForFilePath);
}

public static IArchive OpenArchive(FileInfo fileInfo, ReaderOptions? options = null)
Expand Down
13 changes: 6 additions & 7 deletions src/SharpCompress/Archives/Rar/FileInfoRarArchiveVolume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ namespace SharpCompress.Archives.Rar;
internal class FileInfoRarArchiveVolume : RarVolume
{
internal FileInfoRarArchiveVolume(FileInfo fileInfo, ReaderOptions options, int index)
: base(StreamingMode.Seekable, fileInfo.OpenRead(), FixOptions(options), index)
: base(
StreamingMode.Seekable,
fileInfo.OpenRead(),
options.WithLeaveStreamOpen(false),
index
)
{
FileInfo = fileInfo;
FileParts = GetVolumeFileParts().ToArray().ToReadOnly();
}

private static ReaderOptions FixOptions(ReaderOptions options)
{
//make sure we're closing streams with fileinfo
return options with { LeaveStreamOpen = false };
}

internal ReadOnlyCollection<RarFilePart> FileParts { get; }

internal FileInfo FileInfo { get; }
Expand Down
4 changes: 2 additions & 2 deletions src/SharpCompress/Archives/Rar/RarArchive.Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static IRarArchive OpenArchive(FileInfo fileInfo, ReaderOptions? readerOp
new SourceStream(
fileInfo,
i => RarArchiveVolumeFactory.GetFilePart(i, fileInfo),
readerOptions ?? new ReaderOptions()
readerOptions ?? ReaderOptions.ForFilePath
)
);
}
Expand All @@ -66,7 +66,7 @@ public static IRarArchive OpenArchive(Stream stream, ReaderOptions? readerOption
}

return new RarArchive(
new SourceStream(stream, _ => null, readerOptions ?? new ReaderOptions())
new SourceStream(stream, _ => null, readerOptions ?? ReaderOptions.ForExternalStream)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/Zip/ZipArchive.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var h in headerFactory.NotNull().ReadSeekableHeaderAsync(volsArray.Last().Stream
s = new SourceStream(
v[0].Stream,
i => i < v.Length ? v[i].Stream : null,
new ReaderOptions() { LeaveStreamOpen = true }
ReaderOptions.ForExternalStream
);
}
else
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Archives/Zip/ZipArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected override IEnumerable<ZipArchiveEntry> LoadEntries(IEnumerable<ZipVolum
s = new SourceStream(
v[0].Stream,
i => i < v.Length ? v[i].Stream : null,
new ReaderOptions() { LeaveStreamOpen = true }
ReaderOptions.ForExternalStream
);
}
else
Expand Down
10 changes: 1 addition & 9 deletions src/SharpCompress/Common/ExtractionMethods.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,7 @@ public static async ValueTask WriteEntryToFileAsync(
options ??= new ExtractionOptions();
if (entry.LinkTarget != null)
{
if (options.SymbolicLinkHandler is not null)
{
options.SymbolicLinkHandler(destinationFileName, entry.LinkTarget);
}
else
{
ExtractionOptions.DefaultSymbolicLinkHandler(destinationFileName, entry.LinkTarget);
}
return;
options.SymbolicLinkHandler?.Invoke(destinationFileName, entry.LinkTarget);
}
else
{
Expand Down
10 changes: 1 addition & 9 deletions src/SharpCompress/Common/ExtractionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,7 @@ Action<string, FileMode> openAndWrite
options ??= new ExtractionOptions();
if (entry.LinkTarget != null)
{
if (options.SymbolicLinkHandler is not null)
{
options.SymbolicLinkHandler(destinationFileName, entry.LinkTarget);
}
else
{
ExtractionOptions.DefaultSymbolicLinkHandler(destinationFileName, entry.LinkTarget);
}
return;
options.SymbolicLinkHandler?.Invoke(destinationFileName, entry.LinkTarget);
}
else
{
Expand Down
17 changes: 2 additions & 15 deletions src/SharpCompress/Common/ExtractionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public sealed record ExtractionOptions : IExtractionOptions
/// </summary>
/// <remarks>
/// <b>Breaking change:</b> Changed from field to init-only property in version 0.40.0.
/// The default handler logs a warning message.
/// If no handler is provided, symbolic links are silently skipped during extraction.
/// </remarks>
public Action<string, string>? SymbolicLinkHandler { get; init; }

Expand All @@ -58,10 +58,7 @@ public ExtractionOptions() { }
/// Creates a new ExtractionOptions instance with the specified overwrite behavior.
/// </summary>
/// <param name="overwrite">Whether to overwrite existing files.</param>
public ExtractionOptions(bool overwrite)
{
Overwrite = overwrite;
}
public ExtractionOptions(bool overwrite) => Overwrite = overwrite;

/// <summary>
/// Creates a new ExtractionOptions instance with the specified extraction path and overwrite behavior.
Expand Down Expand Up @@ -102,14 +99,4 @@ public ExtractionOptions(bool extractFullPath, bool overwrite, bool preserveFile
/// </summary>
public static ExtractionOptions PreserveMetadata =>
new() { PreserveFileTime = true, PreserveAttributes = true };
Comment thread
adamhathcock marked this conversation as resolved.

/// <summary>
/// Default symbolic link handler that logs a warning message.
/// </summary>
public static void DefaultSymbolicLinkHandler(string sourcePath, string targetPath)
{
Console.WriteLine(
$"Could not write symlink {sourcePath} -> {targetPath}, for more information please see https://github.com/dotnet/runtime/issues/24271"
);
}
}
4 changes: 2 additions & 2 deletions src/SharpCompress/Common/GZip/GZipVolume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace SharpCompress.Common.GZip;

public class GZipVolume : Volume
{
public GZipVolume(Stream stream, ReaderOptions? options, int index)
public GZipVolume(Stream stream, ReaderOptions options, int index)
: base(stream, options, index) { }
Comment thread
adamhathcock marked this conversation as resolved.

public GZipVolume(FileInfo fileInfo, ReaderOptions options)
: base(fileInfo.OpenRead(), options with { LeaveStreamOpen = false }) { }
: base(fileInfo.OpenRead(), options.WithLeaveStreamOpen(false)) { }

public override bool IsFirstVolume => true;

Expand Down
4 changes: 2 additions & 2 deletions src/SharpCompress/Common/Lzw/LzwVolume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace SharpCompress.Common.Lzw;

public class LzwVolume : Volume
{
public LzwVolume(Stream stream, ReaderOptions? options, int index)
public LzwVolume(Stream stream, ReaderOptions options, int index)
: base(stream, options, index) { }
Comment thread
adamhathcock marked this conversation as resolved.

public LzwVolume(FileInfo fileInfo, ReaderOptions options)
: base(fileInfo.OpenRead(), options with { LeaveStreamOpen = false }) { }
: base(fileInfo.OpenRead(), options.WithLeaveStreamOpen(false)) { }

public override bool IsFirstVolume => true;

Expand Down
4 changes: 2 additions & 2 deletions src/SharpCompress/Common/Volume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public abstract partial class Volume : IVolume, IAsyncDisposable
private readonly Stream _baseStream;
private readonly Stream _actualStream;

internal Volume(Stream stream, ReaderOptions? readerOptions, int index = 0)
internal Volume(Stream stream, ReaderOptions readerOptions, int index = 0)
{
Index = index;
ReaderOptions = readerOptions ?? new ReaderOptions();
ReaderOptions = readerOptions;
_baseStream = stream;

// Only rewind if it's a buffered SharpCompressStream (not passthrough)
Expand Down
4 changes: 2 additions & 2 deletions src/SharpCompress/Factories/TarFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public async ValueTask<IAsyncArchive> OpenAsyncArchive(
/// <inheritdoc/>
public IReader OpenReader(Stream stream, ReaderOptions? options)
{
options ??= new ReaderOptions();
options ??= ReaderOptions.ForExternalStream;
var sharpCompressStream = new SharpCompressStream(stream);
sharpCompressStream.StartRecording(TarWrapper.MaximumRewindBufferSize);
foreach (var wrapper in TarWrapper.Wrappers)
Expand Down Expand Up @@ -350,7 +350,7 @@ public async ValueTask<IAsyncReader> OpenAsyncReader(
)
{
cancellationToken.ThrowIfCancellationRequested();
options ??= new ReaderOptions();
options ??= ReaderOptions.ForExternalStream;
var sharpCompressStream = new SharpCompressStream(stream);
sharpCompressStream.StartRecording(TarWrapper.MaximumRewindBufferSize);
foreach (var wrapper in TarWrapper.Wrappers)
Expand Down
6 changes: 3 additions & 3 deletions src/SharpCompress/Readers/Ace/AceReader.Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public partial class AceReader
public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = null)
{
stream.NotNull(nameof(stream));
return new SingleVolumeAceReader(stream, readerOptions ?? new ReaderOptions());
return new SingleVolumeAceReader(stream, readerOptions ?? ReaderOptions.ForExternalStream);
}

/// <summary>
Expand All @@ -32,7 +32,7 @@ public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = n
public static IReader OpenReader(IEnumerable<Stream> streams, ReaderOptions? options = null)
{
streams.NotNull(nameof(streams));
return new MultiVolumeAceReader(streams, options ?? new ReaderOptions());
return new MultiVolumeAceReader(streams, options ?? ReaderOptions.ForExternalStream);
}

public static ValueTask<IAsyncReader> OpenAsyncReader(
Expand Down Expand Up @@ -62,7 +62,7 @@ public static IAsyncReader OpenAsyncReader(
)
{
streams.NotNull(nameof(streams));
return new MultiVolumeAceReader(streams, options ?? new ReaderOptions());
return new MultiVolumeAceReader(streams, options ?? ReaderOptions.ForExternalStream);
}

public static ValueTask<IAsyncReader> OpenAsyncReader(
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Readers/Arc/ArcReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private ArcReader(Stream stream, ReaderOptions options)
public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = null)
{
stream.NotNull(nameof(stream));
return new ArcReader(stream, readerOptions ?? new ReaderOptions());
return new ArcReader(stream, readerOptions ?? ReaderOptions.ForExternalStream);
}

protected override IEnumerable<ArcEntry> GetEntries(Stream stream)
Expand Down
4 changes: 2 additions & 2 deletions src/SharpCompress/Readers/Arj/ArjReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal ArjReader(ReaderOptions options)
public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = null)
{
stream.NotNull(nameof(stream));
return new SingleVolumeArjReader(stream, readerOptions ?? new ReaderOptions());
return new SingleVolumeArjReader(stream, readerOptions ?? ReaderOptions.ForExternalStream);
}

/// <summary>
Expand All @@ -44,7 +44,7 @@ public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = n
public static IReader OpenReader(IEnumerable<Stream> streams, ReaderOptions? options = null)
{
streams.NotNull(nameof(streams));
return new MultiVolumeArjReader(streams, options ?? new ReaderOptions());
return new MultiVolumeArjReader(streams, options ?? ReaderOptions.ForExternalStream);
}

protected abstract void ValidateArchive(ArjVolume archive);
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Readers/GZip/GZipReader.Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public static IReader OpenReader(FileInfo fileInfo, ReaderOptions? readerOptions
public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = null)
{
stream.NotNull(nameof(stream));
return new GZipReader(stream, readerOptions ?? new ReaderOptions());
return new GZipReader(stream, readerOptions ?? ReaderOptions.ForExternalStream);
}
}
2 changes: 1 addition & 1 deletion src/SharpCompress/Readers/Lzw/LzwReader.Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public static IReader OpenReader(FileInfo fileInfo, ReaderOptions? readerOptions
public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = null)
{
stream.NotNull(nameof(stream));
return new LzwReader(stream, readerOptions ?? new ReaderOptions());
return new LzwReader(stream, readerOptions ?? ReaderOptions.ForExternalStream);
}
}
4 changes: 2 additions & 2 deletions src/SharpCompress/Readers/Rar/RarReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static IReader OpenReader(IEnumerable<FileInfo> fileInfos, ReaderOptions?
public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = null)
{
stream.NotNull(nameof(stream));
return new SingleVolumeRarReader(stream, readerOptions ?? new ReaderOptions());
return new SingleVolumeRarReader(stream, readerOptions ?? ReaderOptions.ForExternalStream);
}

/// <summary>
Expand All @@ -84,7 +84,7 @@ public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = n
public static IReader OpenReader(IEnumerable<Stream> streams, ReaderOptions? options = null)
{
streams.NotNull(nameof(streams));
return new MultiVolumeRarReader(streams, options ?? new ReaderOptions());
return new MultiVolumeRarReader(streams, options ?? ReaderOptions.ForExternalStream);
}

protected override IEnumerable<RarReaderEntry> GetEntries(Stream stream)
Expand Down
6 changes: 5 additions & 1 deletion src/SharpCompress/Readers/ReaderFactory.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ public static ValueTask<IAsyncReader> OpenAsyncReader(
)
{
filePath.NotNullOrEmpty(nameof(filePath));
return OpenAsyncReader(new FileInfo(filePath), options, cancellationToken);
return OpenAsyncReader(
new FileInfo(filePath),
options ?? ReaderOptions.ForFilePath,
cancellationToken
);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/SharpCompress/Readers/ReaderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static partial class ReaderFactory
public static IReader OpenReader(string filePath, ReaderOptions? options = null)
{
filePath.NotNullOrEmpty(nameof(filePath));
return OpenReader(new FileInfo(filePath), options);
return OpenReader(new FileInfo(filePath), options ?? ReaderOptions.ForFilePath);
}

public static IReader OpenReader(FileInfo fileInfo, ReaderOptions? options = null)
Expand Down
15 changes: 7 additions & 8 deletions src/SharpCompress/Readers/ReaderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,35 +151,34 @@ public ReaderOptions() { }
/// <summary>
/// Gets ReaderOptions configured for caller-provided streams.
/// </summary>
public static ReaderOptions ForExternalStream => new() { LeaveStreamOpen = true };
internal static ReaderOptions Default => new();

public static ReaderOptions ForExternalStream => Default.WithLeaveStreamOpen(true);

/// <summary>
/// Gets ReaderOptions configured for file-based overloads that open their own stream.
/// </summary>
public static ReaderOptions ForFilePath => new() { LeaveStreamOpen = false };
public static ReaderOptions ForFilePath => Default;

/// <summary>
/// Creates ReaderOptions for reading encrypted archives.
/// </summary>
/// <param name="password">The password for encrypted archives.</param>
public static ReaderOptions ForEncryptedArchive(string? password = null) =>
new ReaderOptions().WithPassword(password);
Default.WithPassword(password);

/// <summary>
/// Creates ReaderOptions for archives with custom character encoding.
/// </summary>
/// <param name="encoding">The encoding for archive entry names.</param>
public static ReaderOptions ForEncoding(IArchiveEncoding encoding) =>
new ReaderOptions().WithArchiveEncoding(encoding);
Default.WithArchiveEncoding(encoding);

/// <summary>
/// Creates ReaderOptions for self-extracting archives that require header search.
/// </summary>
public static ReaderOptions ForSelfExtractingArchive(string? password = null) =>
new ReaderOptions()
.WithLookForHeader(true)
.WithPassword(password)
.WithRewindableBufferSize(1_048_576); // 1MB for SFX archives
Default.WithLookForHeader(true).WithPassword(password).WithRewindableBufferSize(1_048_576); // 1MB for SFX archives

// Note: Parameterized constructors have been removed.
// Use fluent With*() helpers or object initializers instead:
Expand Down
4 changes: 2 additions & 2 deletions src/SharpCompress/Readers/Tar/TarReader.Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static async ValueTask<IAsyncReader> OpenAsyncReader(
{
cancellationToken.ThrowIfCancellationRequested();
stream.NotNull(nameof(stream));
readerOptions ??= new ReaderOptions();
readerOptions ??= ReaderOptions.ForExternalStream;
var sharpCompressStream = SharpCompressStream.Create(
stream,
bufferSize: Math.Max(
Expand Down Expand Up @@ -171,7 +171,7 @@ public static IReader OpenReader(FileInfo fileInfo, ReaderOptions? readerOptions
public static IReader OpenReader(Stream stream, ReaderOptions? readerOptions = null)
{
stream.NotNull(nameof(stream));
readerOptions ??= new ReaderOptions();
readerOptions ??= ReaderOptions.ForExternalStream;
var sharpCompressStream = SharpCompressStream.Create(
stream,
bufferSize: Math.Max(
Expand Down
Loading
Loading