mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-07-09 02:26:47 +00:00
tar can write async
This commit is contained in:
@@ -404,19 +404,95 @@ public class TarFactory
|
||||
nameof(writerOptions)
|
||||
),
|
||||
};
|
||||
|
||||
if (!stream.CanWrite)
|
||||
{
|
||||
throw new ArgumentException("Tars require writable streams.");
|
||||
}
|
||||
if (writerOptions.LeaveStreamOpen)
|
||||
{
|
||||
stream = SharpCompressStream.CreateNonDisposing(stream);
|
||||
}
|
||||
|
||||
var providers = writerOptions.Providers;
|
||||
|
||||
stream = writerOptions.CompressionType switch
|
||||
{
|
||||
CompressionType.None => stream,
|
||||
CompressionType.BZip2 => providers.CreateCompressStream(
|
||||
CompressionType.BZip2,
|
||||
stream,
|
||||
writerOptions.CompressionLevel
|
||||
),
|
||||
CompressionType.GZip => providers.CreateCompressStream(
|
||||
CompressionType.GZip,
|
||||
stream,
|
||||
writerOptions.CompressionLevel
|
||||
),
|
||||
CompressionType.LZip => providers.CreateCompressStream(
|
||||
CompressionType.LZip,
|
||||
stream,
|
||||
writerOptions.CompressionLevel
|
||||
),
|
||||
_ => throw new InvalidFormatException(
|
||||
"Tar does not support compression: " + writerOptions.CompressionType
|
||||
),
|
||||
};
|
||||
return new TarWriter(stream, tarOptions);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ValueTask<IAsyncWriter> OpenAsyncWriter(
|
||||
public async ValueTask<IAsyncWriter> OpenAsyncWriter(
|
||||
Stream stream,
|
||||
IWriterOptions writerOptions,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var writer = OpenWriter(stream, writerOptions);
|
||||
return new((IAsyncWriter)writer);
|
||||
TarWriterOptions tarOptions = writerOptions switch
|
||||
{
|
||||
TarWriterOptions two => two,
|
||||
WriterOptions wo => new TarWriterOptions(wo),
|
||||
_ => throw new ArgumentException(
|
||||
$"Expected WriterOptions or TarWriterOptions, got {writerOptions.GetType().Name}",
|
||||
nameof(writerOptions)
|
||||
),
|
||||
};
|
||||
|
||||
if (!stream.CanWrite)
|
||||
{
|
||||
throw new ArgumentException("Tars require writable streams.");
|
||||
}
|
||||
if (writerOptions.LeaveStreamOpen)
|
||||
{
|
||||
stream = SharpCompressStream.CreateNonDisposing(stream);
|
||||
}
|
||||
|
||||
var providers = writerOptions.Providers;
|
||||
|
||||
stream = writerOptions.CompressionType switch
|
||||
{
|
||||
CompressionType.None => stream,
|
||||
CompressionType.BZip2 => await providers.CreateCompressStreamAsync(
|
||||
CompressionType.BZip2,
|
||||
stream,
|
||||
writerOptions.CompressionLevel,
|
||||
cancellationToken).ConfigureAwait(false),
|
||||
CompressionType.GZip => await providers.CreateCompressStreamAsync(
|
||||
CompressionType.GZip,
|
||||
stream,
|
||||
writerOptions.CompressionLevel,
|
||||
cancellationToken).ConfigureAwait(false),
|
||||
CompressionType.LZip => await providers.CreateCompressStreamAsync(
|
||||
CompressionType.LZip,
|
||||
stream,
|
||||
writerOptions.CompressionLevel,
|
||||
cancellationToken).ConfigureAwait(false),
|
||||
_ => throw new InvalidFormatException(
|
||||
"Tar does not support compression: " + writerOptions.CompressionType
|
||||
),
|
||||
};
|
||||
return new TarWriter(stream, tarOptions);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -21,39 +21,6 @@ public partial class TarWriter : AbstractWriter
|
||||
finalizeArchiveOnClose = options.FinalizeArchiveOnClose;
|
||||
headerFormat = options.HeaderFormat;
|
||||
|
||||
if (!destination.CanWrite)
|
||||
{
|
||||
throw new ArgumentException("Tars require writable streams.");
|
||||
}
|
||||
if (WriterOptions.LeaveStreamOpen)
|
||||
{
|
||||
destination = SharpCompressStream.CreateNonDisposing(destination);
|
||||
}
|
||||
|
||||
var providers = options.Providers;
|
||||
|
||||
destination = options.CompressionType switch
|
||||
{
|
||||
CompressionType.None => destination,
|
||||
CompressionType.BZip2 => providers.CreateCompressStream(
|
||||
CompressionType.BZip2,
|
||||
destination,
|
||||
options.CompressionLevel
|
||||
),
|
||||
CompressionType.GZip => providers.CreateCompressStream(
|
||||
CompressionType.GZip,
|
||||
destination,
|
||||
options.CompressionLevel
|
||||
),
|
||||
CompressionType.LZip => providers.CreateCompressStream(
|
||||
CompressionType.LZip,
|
||||
destination,
|
||||
options.CompressionLevel
|
||||
),
|
||||
_ => throw new InvalidFormatException(
|
||||
"Tar does not support compression: " + options.CompressionType
|
||||
),
|
||||
};
|
||||
|
||||
InitializeStream(destination);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace SharpCompress.Test.Mocks;
|
||||
|
||||
public class AsyncOnlyStream(Stream stream) : Stream
|
||||
public class AsyncOnlyStream(Stream stream, bool disposeStream = true) : Stream
|
||||
{
|
||||
private readonly Stream _stream = stream ?? throw new ArgumentNullException(nameof(stream));
|
||||
|
||||
@@ -71,7 +71,7 @@ public class AsyncOnlyStream(Stream stream) : Stream
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
if (disposing && disposeStream)
|
||||
{
|
||||
_stream.Dispose();
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ public class TarWriterAsyncTests : WriterTests
|
||||
using Stream content = File.OpenRead(Path.Combine(ORIGINAL_FILES_PATH, "jpg", "test.jpg"));
|
||||
await using (
|
||||
var writer = new TarWriter(
|
||||
new AsyncOnlyStream(stream),
|
||||
new AsyncOnlyStream(stream, false),
|
||||
new TarWriterOptions(CompressionType.None, finalizeArchive)
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user