mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-02-16 21:23:13 +00:00
Compare commits
6 Commits
adam/write
...
0.46.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1fddd102dc | ||
|
|
13fdb4d7ca | ||
|
|
cdb13da054 | ||
|
|
fb70f06fd4 | ||
|
|
31c6eb3b5c | ||
|
|
eb7e55ac7d |
@@ -77,9 +77,7 @@ public static partial class ArchiveFactory
|
||||
|
||||
var factory = await FindFactoryAsync<IMultiArchiveFactory>(fileInfo, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
return await factory
|
||||
.OpenAsyncArchive(filesArray, options, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
return factory.OpenAsyncArchive(filesArray, options);
|
||||
}
|
||||
|
||||
public static async ValueTask<IAsyncArchive> OpenAsyncArchive(
|
||||
@@ -108,9 +106,7 @@ public static partial class ArchiveFactory
|
||||
|
||||
var factory = await FindFactoryAsync<IMultiArchiveFactory>(firstStream, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
return await factory
|
||||
.OpenAsyncArchive(streamsArray, options, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
return factory.OpenAsyncArchive(streamsArray, options);
|
||||
}
|
||||
|
||||
public static ValueTask<T> FindFactoryAsync<T>(
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Factories;
|
||||
using SharpCompress.Readers;
|
||||
|
||||
@@ -34,12 +33,9 @@ public interface IMultiArchiveFactory : IFactory
|
||||
/// </summary>
|
||||
/// <param name="streams"></param>
|
||||
/// <param name="readerOptions">reading options.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <returns>A <see cref="ValueTask{TResult}"/> containing the opened async archive.</returns>
|
||||
ValueTask<IAsyncArchive> OpenAsyncArchive(
|
||||
IAsyncArchive OpenAsyncArchive(
|
||||
IReadOnlyList<Stream> streams,
|
||||
ReaderOptions? readerOptions = null,
|
||||
CancellationToken cancellationToken = default
|
||||
ReaderOptions? readerOptions = null
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
@@ -54,11 +50,8 @@ public interface IMultiArchiveFactory : IFactory
|
||||
/// </summary>
|
||||
/// <param name="fileInfos"></param>
|
||||
/// <param name="readerOptions">reading options.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <returns>A <see cref="ValueTask{TResult}"/> containing the opened async archive.</returns>
|
||||
ValueTask<IAsyncArchive> OpenAsyncArchive(
|
||||
IAsyncArchive OpenAsyncArchive(
|
||||
IReadOnlyList<FileInfo> fileInfos,
|
||||
ReaderOptions? readerOptions = null,
|
||||
CancellationToken cancellationToken = default
|
||||
ReaderOptions? readerOptions = null
|
||||
);
|
||||
}
|
||||
|
||||
@@ -164,6 +164,12 @@ public partial class SevenZipArchive : AbstractArchive<SevenZipArchiveEntry, Sev
|
||||
|
||||
var folder = entry.FilePart.Folder;
|
||||
|
||||
// If folder is null (empty stream entry), return empty stream
|
||||
if (folder is null)
|
||||
{
|
||||
return CreateEntryStream(Stream.Null);
|
||||
}
|
||||
|
||||
// Check if we're starting a new folder - dispose old folder stream if needed
|
||||
if (folder != _currentFolder)
|
||||
{
|
||||
|
||||
@@ -95,17 +95,10 @@ public class GZipFactory
|
||||
) => GZipArchive.OpenArchive(streams, readerOptions);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async ValueTask<IAsyncArchive> OpenAsyncArchive(
|
||||
public IAsyncArchive OpenAsyncArchive(
|
||||
IReadOnlyList<Stream> streams,
|
||||
ReaderOptions? readerOptions = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return await GZipArchive
|
||||
.OpenAsyncArchive(streams, readerOptions, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
ReaderOptions? readerOptions = null
|
||||
) => (IAsyncArchive)OpenArchive(streams, readerOptions);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IArchive OpenArchive(
|
||||
@@ -114,17 +107,10 @@ public class GZipFactory
|
||||
) => GZipArchive.OpenArchive(fileInfos, readerOptions);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async ValueTask<IAsyncArchive> OpenAsyncArchive(
|
||||
public IAsyncArchive OpenAsyncArchive(
|
||||
IReadOnlyList<FileInfo> fileInfos,
|
||||
ReaderOptions? readerOptions = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return await GZipArchive
|
||||
.OpenAsyncArchive(fileInfos, readerOptions, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
ReaderOptions? readerOptions = null
|
||||
) => (IAsyncArchive)OpenArchive(fileInfos, readerOptions);
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -201,15 +187,14 @@ public class GZipFactory
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ValueTask<IAsyncWriter> OpenAsyncWriter(
|
||||
public IAsyncWriter OpenAsyncWriter(
|
||||
Stream stream,
|
||||
IWriterOptions writerOptions,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var writer = OpenWriter(stream, writerOptions);
|
||||
return new((IAsyncWriter)writer);
|
||||
return (IAsyncWriter)OpenWriter(stream, writerOptions);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -90,17 +90,10 @@ public class RarFactory : Factory, IArchiveFactory, IMultiArchiveFactory, IReade
|
||||
) => RarArchive.OpenArchive(streams, readerOptions);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async ValueTask<IAsyncArchive> OpenAsyncArchive(
|
||||
public IAsyncArchive OpenAsyncArchive(
|
||||
IReadOnlyList<Stream> streams,
|
||||
ReaderOptions? readerOptions = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return await RarArchive
|
||||
.OpenAsyncArchive(streams, readerOptions, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
ReaderOptions? readerOptions = null
|
||||
) => (IAsyncArchive)OpenArchive(streams, readerOptions);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IArchive OpenArchive(
|
||||
@@ -109,16 +102,12 @@ public class RarFactory : Factory, IArchiveFactory, IMultiArchiveFactory, IReade
|
||||
) => RarArchive.OpenArchive(fileInfos, readerOptions);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async ValueTask<IAsyncArchive> OpenAsyncArchive(
|
||||
public IAsyncArchive OpenAsyncArchive(
|
||||
IReadOnlyList<FileInfo> fileInfos,
|
||||
ReaderOptions? readerOptions = null,
|
||||
CancellationToken cancellationToken = default
|
||||
ReaderOptions? readerOptions = null
|
||||
)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return await RarArchive
|
||||
.OpenAsyncArchive(fileInfos, readerOptions, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
return (IAsyncArchive)OpenArchive(fileInfos, readerOptions);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -85,17 +85,10 @@ public class SevenZipFactory : Factory, IArchiveFactory, IMultiArchiveFactory
|
||||
) => SevenZipArchive.OpenArchive(streams, readerOptions);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async ValueTask<IAsyncArchive> OpenAsyncArchive(
|
||||
public IAsyncArchive OpenAsyncArchive(
|
||||
IReadOnlyList<Stream> streams,
|
||||
ReaderOptions? readerOptions = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return await SevenZipArchive
|
||||
.OpenAsyncArchive(streams, readerOptions, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
ReaderOptions? readerOptions = null
|
||||
) => (IAsyncArchive)OpenArchive(streams, readerOptions);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IArchive OpenArchive(
|
||||
@@ -104,17 +97,10 @@ public class SevenZipFactory : Factory, IArchiveFactory, IMultiArchiveFactory
|
||||
) => SevenZipArchive.OpenArchive(fileInfos, readerOptions);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async ValueTask<IAsyncArchive> OpenAsyncArchive(
|
||||
public IAsyncArchive OpenAsyncArchive(
|
||||
IReadOnlyList<FileInfo> fileInfos,
|
||||
ReaderOptions? readerOptions = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return await SevenZipArchive
|
||||
.OpenAsyncArchive(fileInfos, readerOptions, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
ReaderOptions? readerOptions = null
|
||||
) => (IAsyncArchive)OpenArchive(fileInfos, readerOptions);
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -195,17 +195,10 @@ public class TarFactory
|
||||
) => TarArchive.OpenArchive(streams, readerOptions);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async ValueTask<IAsyncArchive> OpenAsyncArchive(
|
||||
public IAsyncArchive OpenAsyncArchive(
|
||||
IReadOnlyList<Stream> streams,
|
||||
ReaderOptions? readerOptions = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return await TarArchive
|
||||
.OpenAsyncArchive(streams, readerOptions, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
ReaderOptions? readerOptions = null
|
||||
) => (IAsyncArchive)OpenArchive(streams, readerOptions);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IArchive OpenArchive(
|
||||
@@ -214,17 +207,10 @@ public class TarFactory
|
||||
) => TarArchive.OpenArchive(fileInfos, readerOptions);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async ValueTask<IAsyncArchive> OpenAsyncArchive(
|
||||
public IAsyncArchive OpenAsyncArchive(
|
||||
IReadOnlyList<FileInfo> fileInfos,
|
||||
ReaderOptions? readerOptions = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return await TarArchive
|
||||
.OpenAsyncArchive(fileInfos, readerOptions, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
ReaderOptions? readerOptions = null
|
||||
) => (IAsyncArchive)OpenArchive(fileInfos, readerOptions);
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -312,15 +298,14 @@ public class TarFactory
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ValueTask<IAsyncWriter> OpenAsyncWriter(
|
||||
public IAsyncWriter OpenAsyncWriter(
|
||||
Stream stream,
|
||||
IWriterOptions writerOptions,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var writer = OpenWriter(stream, writerOptions);
|
||||
return new((IAsyncWriter)writer);
|
||||
return (IAsyncWriter)OpenWriter(stream, writerOptions);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -163,17 +163,10 @@ public class ZipFactory
|
||||
) => ZipArchive.OpenArchive(streams, readerOptions);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async ValueTask<IAsyncArchive> OpenAsyncArchive(
|
||||
public IAsyncArchive OpenAsyncArchive(
|
||||
IReadOnlyList<Stream> streams,
|
||||
ReaderOptions? readerOptions = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return await ZipArchive
|
||||
.OpenAsyncArchive(streams, readerOptions, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
ReaderOptions? readerOptions = null
|
||||
) => (IAsyncArchive)OpenArchive(streams, readerOptions);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IArchive OpenArchive(
|
||||
@@ -182,17 +175,10 @@ public class ZipFactory
|
||||
) => ZipArchive.OpenArchive(fileInfos, readerOptions);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async ValueTask<IAsyncArchive> OpenAsyncArchive(
|
||||
public IAsyncArchive OpenAsyncArchive(
|
||||
IReadOnlyList<FileInfo> fileInfos,
|
||||
ReaderOptions? readerOptions = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return await ZipArchive
|
||||
.OpenAsyncArchive(fileInfos, readerOptions, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
ReaderOptions? readerOptions = null
|
||||
) => (IAsyncArchive)OpenArchive(fileInfos, readerOptions);
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -233,15 +219,14 @@ public class ZipFactory
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ValueTask<IAsyncWriter> OpenAsyncWriter(
|
||||
public IAsyncWriter OpenAsyncWriter(
|
||||
Stream stream,
|
||||
IWriterOptions writerOptions,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var writer = OpenWriter(stream, writerOptions);
|
||||
return new((IAsyncWriter)writer);
|
||||
return (IAsyncWriter)OpenWriter(stream, writerOptions);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -206,8 +206,22 @@ internal partial class SharpCompressStream : Stream, IStreamStack
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public override long Length =>
|
||||
_isPassthrough ? stream.Length : throw new NotSupportedException();
|
||||
public override long Length
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_isPassthrough)
|
||||
{
|
||||
return stream.Length;
|
||||
}
|
||||
|
||||
if (_ringBuffer is not null)
|
||||
{
|
||||
return _ringBuffer.Length;
|
||||
}
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
|
||||
public override long Position
|
||||
{
|
||||
|
||||
@@ -35,15 +35,14 @@ public static partial class ReaderFactory
|
||||
/// <param name="options"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public static async ValueTask<IAsyncReader> OpenAsyncReader(
|
||||
public static ValueTask<IAsyncReader> OpenAsyncReader(
|
||||
FileInfo fileInfo,
|
||||
ReaderOptions? options = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
options ??= ReaderOptions.ForOwnedFile;
|
||||
var stream = fileInfo.OpenAsyncReadStream(cancellationToken);
|
||||
return await OpenAsyncReader(stream, options, cancellationToken);
|
||||
return OpenAsyncReader(fileInfo.OpenRead(), options, cancellationToken);
|
||||
}
|
||||
|
||||
public static async ValueTask<IAsyncReader> OpenAsyncReader(
|
||||
|
||||
@@ -121,15 +121,14 @@ public partial class TarReader
|
||||
return new TarReader(sharpCompressStream, options, CompressionType.None);
|
||||
}
|
||||
|
||||
public static async ValueTask<IAsyncReader> OpenAsyncReader(
|
||||
public static ValueTask<IAsyncReader> OpenAsyncReader(
|
||||
FileInfo fileInfo,
|
||||
ReaderOptions? readerOptions = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
readerOptions ??= new ReaderOptions() { LeaveStreamOpen = false };
|
||||
var stream = fileInfo.OpenAsyncReadStream(cancellationToken);
|
||||
return await OpenAsyncReader(stream, readerOptions, cancellationToken);
|
||||
return OpenAsyncReader(fileInfo.OpenRead(), readerOptions, cancellationToken);
|
||||
}
|
||||
|
||||
public static IReader OpenReader(string filePath, ReaderOptions? readerOptions = null)
|
||||
|
||||
@@ -119,108 +119,4 @@ internal static partial class Utility
|
||||
return (total >= count);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a file stream for asynchronous writing.
|
||||
/// Uses File.OpenHandle with FileOptions.Asynchronous on .NET 8.0+ for optimal performance.
|
||||
/// Falls back to FileStream constructor with async options on legacy frameworks.
|
||||
/// </summary>
|
||||
/// <param name="path">The file path to open.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <returns>A FileStream configured for asynchronous operations.</returns>
|
||||
public static Stream OpenAsyncWriteStream(string path, CancellationToken cancellationToken)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
#if !LEGACY_DOTNET
|
||||
// Use File.OpenHandle with async options for .NET 8.0+
|
||||
var handle = File.OpenHandle(
|
||||
path,
|
||||
FileMode.Create,
|
||||
FileAccess.Write,
|
||||
FileShare.None,
|
||||
FileOptions.Asynchronous
|
||||
);
|
||||
return new FileStream(handle, FileAccess.Write);
|
||||
#else
|
||||
// For legacy .NET, use FileStream constructor with async options
|
||||
return new FileStream(
|
||||
path,
|
||||
FileMode.Create,
|
||||
FileAccess.Write,
|
||||
FileShare.None,
|
||||
bufferSize: 4096, //default
|
||||
FileOptions.Asynchronous
|
||||
);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a file stream for asynchronous writing from a FileInfo.
|
||||
/// Uses File.OpenHandle with FileOptions.Asynchronous on .NET 8.0+ for optimal performance.
|
||||
/// Falls back to FileStream constructor with async options on legacy frameworks.
|
||||
/// </summary>
|
||||
/// <param name="fileInfo">The FileInfo to open.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <returns>A FileStream configured for asynchronous operations.</returns>
|
||||
public static Stream OpenAsyncWriteStream(
|
||||
this FileInfo fileInfo,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
fileInfo.NotNull(nameof(fileInfo));
|
||||
return OpenAsyncWriteStream(fileInfo.FullName, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a file stream for asynchronous reading.
|
||||
/// Uses File.OpenHandle with FileOptions.Asynchronous on .NET 8.0+ for optimal performance.
|
||||
/// Falls back to FileStream constructor with async options on legacy frameworks.
|
||||
/// </summary>
|
||||
/// <param name="path">The file path to open.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <returns>A FileStream configured for asynchronous operations.</returns>
|
||||
public static Stream OpenAsyncReadStream(string path, CancellationToken cancellationToken)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
#if !LEGACY_DOTNET
|
||||
// Use File.OpenHandle with async options for .NET 8.0+
|
||||
var handle = File.OpenHandle(
|
||||
path,
|
||||
FileMode.Open,
|
||||
FileAccess.Read,
|
||||
FileShare.Read,
|
||||
FileOptions.Asynchronous
|
||||
);
|
||||
return new FileStream(handle, FileAccess.Read);
|
||||
#else
|
||||
// For legacy .NET, use FileStream constructor with async options
|
||||
return new FileStream(
|
||||
path,
|
||||
FileMode.Open,
|
||||
FileAccess.Read,
|
||||
FileShare.Read,
|
||||
bufferSize: 4096,
|
||||
FileOptions.Asynchronous
|
||||
);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a file stream for asynchronous reading from a FileInfo.
|
||||
/// Uses File.OpenHandle with FileOptions.Asynchronous on .NET 8.0+ for optimal performance.
|
||||
/// Falls back to FileStream constructor with async options on legacy frameworks.
|
||||
/// </summary>
|
||||
/// <param name="fileInfo">The FileInfo to open.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <returns>A FileStream configured for asynchronous operations.</returns>
|
||||
public static Stream OpenAsyncReadStream(
|
||||
this FileInfo fileInfo,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
fileInfo.NotNull(nameof(fileInfo));
|
||||
return OpenAsyncReadStream(fileInfo.FullName, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Common.Options;
|
||||
using SharpCompress.Factories;
|
||||
|
||||
@@ -10,7 +9,7 @@ public interface IWriterFactory : IFactory
|
||||
{
|
||||
IWriter OpenWriter(Stream stream, IWriterOptions writerOptions);
|
||||
|
||||
ValueTask<IAsyncWriter> OpenAsyncWriter(
|
||||
IAsyncWriter OpenAsyncWriter(
|
||||
Stream stream,
|
||||
IWriterOptions writerOptions,
|
||||
CancellationToken cancellationToken = default
|
||||
|
||||
@@ -2,7 +2,6 @@ using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Common.Options;
|
||||
|
||||
@@ -27,14 +26,10 @@ public static class WriterFactory
|
||||
)
|
||||
{
|
||||
fileInfo.NotNull(nameof(fileInfo));
|
||||
return OpenWriter(
|
||||
fileInfo.OpenWrite(),
|
||||
archiveType,
|
||||
writerOptions.WithLeaveStreamOpen(false)
|
||||
);
|
||||
return OpenWriter(fileInfo.OpenWrite(), archiveType, writerOptions);
|
||||
}
|
||||
|
||||
public static async ValueTask<IAsyncWriter> OpenAsyncWriter(
|
||||
public static IAsyncWriter OpenAsyncWriter(
|
||||
string filePath,
|
||||
ArchiveType archiveType,
|
||||
IWriterOptions writerOptions,
|
||||
@@ -42,7 +37,7 @@ public static class WriterFactory
|
||||
)
|
||||
{
|
||||
filePath.NotNullOrEmpty(nameof(filePath));
|
||||
return await OpenAsyncWriter(
|
||||
return OpenAsyncWriter(
|
||||
new FileInfo(filePath),
|
||||
archiveType,
|
||||
writerOptions,
|
||||
@@ -50,7 +45,7 @@ public static class WriterFactory
|
||||
);
|
||||
}
|
||||
|
||||
public static async ValueTask<IAsyncWriter> OpenAsyncWriter(
|
||||
public static IAsyncWriter OpenAsyncWriter(
|
||||
FileInfo fileInfo,
|
||||
ArchiveType archiveType,
|
||||
IWriterOptions writerOptions,
|
||||
@@ -58,11 +53,10 @@ public static class WriterFactory
|
||||
)
|
||||
{
|
||||
fileInfo.NotNull(nameof(fileInfo));
|
||||
var stream = fileInfo.OpenAsyncWriteStream(cancellationToken);
|
||||
return await OpenAsyncWriter(
|
||||
stream,
|
||||
return OpenAsyncWriter(
|
||||
fileInfo.Open(FileMode.Create, FileAccess.Write),
|
||||
archiveType,
|
||||
writerOptions.WithLeaveStreamOpen(false),
|
||||
writerOptions,
|
||||
cancellationToken
|
||||
);
|
||||
}
|
||||
@@ -92,8 +86,8 @@ public static class WriterFactory
|
||||
/// <param name="archiveType">The archive type.</param>
|
||||
/// <param name="writerOptions">Writer options.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <returns>A <see cref="ValueTask{TResult}"/> containing the async writer.</returns>
|
||||
public static async ValueTask<IAsyncWriter> OpenAsyncWriter(
|
||||
/// <returns>A task that returns an IWriter.</returns>
|
||||
public static IAsyncWriter OpenAsyncWriter(
|
||||
Stream stream,
|
||||
ArchiveType archiveType,
|
||||
IWriterOptions writerOptions,
|
||||
@@ -106,7 +100,7 @@ public static class WriterFactory
|
||||
|
||||
if (factory != null)
|
||||
{
|
||||
return await factory.OpenAsyncWriter(stream, writerOptions, cancellationToken);
|
||||
return factory.OpenAsyncWriter(stream, writerOptions, cancellationToken);
|
||||
}
|
||||
|
||||
throw new NotSupportedException("Archive Type does not have a Writer: " + archiveType);
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
using System;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Common.Options;
|
||||
using SharpCompress.Writers.GZip;
|
||||
using SharpCompress.Writers.Tar;
|
||||
using SharpCompress.Writers.Zip;
|
||||
|
||||
namespace SharpCompress.Writers;
|
||||
|
||||
@@ -23,29 +20,6 @@ public static class WriterOptionsExtensions
|
||||
bool leaveStreamOpen
|
||||
) => options with { LeaveStreamOpen = leaveStreamOpen };
|
||||
|
||||
/// <summary>
|
||||
/// Creates a copy with the specified LeaveStreamOpen value.
|
||||
/// Works with any IWriterOptions implementation.
|
||||
/// </summary>
|
||||
/// <param name="options">The source options.</param>
|
||||
/// <param name="leaveStreamOpen">Whether to leave the stream open.</param>
|
||||
/// <returns>A new options instance with the specified LeaveStreamOpen value.</returns>
|
||||
public static IWriterOptions WithLeaveStreamOpen(
|
||||
this IWriterOptions options,
|
||||
bool leaveStreamOpen
|
||||
) =>
|
||||
options switch
|
||||
{
|
||||
WriterOptions writerOptions => writerOptions with { LeaveStreamOpen = leaveStreamOpen },
|
||||
ZipWriterOptions zipOptions => zipOptions with { LeaveStreamOpen = leaveStreamOpen },
|
||||
TarWriterOptions tarOptions => tarOptions with { LeaveStreamOpen = leaveStreamOpen },
|
||||
GZipWriterOptions gzipOptions => gzipOptions with { LeaveStreamOpen = leaveStreamOpen },
|
||||
_ => throw new NotSupportedException(
|
||||
$"Cannot set LeaveStreamOpen on options of type {options.GetType().Name}. "
|
||||
+ "Options must be a record type implementing IWriterOptions."
|
||||
),
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Creates a copy with the specified compression level.
|
||||
/// </summary>
|
||||
|
||||
@@ -216,9 +216,9 @@
|
||||
"net10.0": {
|
||||
"Microsoft.NET.ILLink.Tasks": {
|
||||
"type": "Direct",
|
||||
"requested": "[10.0.0, )",
|
||||
"resolved": "10.0.0",
|
||||
"contentHash": "kICGrGYEzCNI3wPzfEXcwNHgTvlvVn9yJDhSdRK+oZQy4jvYH529u7O0xf5ocQKzOMjfS07+3z9PKRIjrFMJDA=="
|
||||
"requested": "[10.0.2, )",
|
||||
"resolved": "10.0.2",
|
||||
"contentHash": "sXdDtMf2qcnbygw9OdE535c2lxSxrZP8gO4UhDJ0xiJbl1wIqXS1OTcTDFTIJPOFd6Mhcm8gPEthqWGUxBsTqw=="
|
||||
},
|
||||
"Microsoft.NETFramework.ReferenceAssemblies": {
|
||||
"type": "Direct",
|
||||
@@ -264,9 +264,9 @@
|
||||
"net8.0": {
|
||||
"Microsoft.NET.ILLink.Tasks": {
|
||||
"type": "Direct",
|
||||
"requested": "[8.0.22, )",
|
||||
"resolved": "8.0.22",
|
||||
"contentHash": "MhcMithKEiyyNkD2ZfbDZPmcOdi0GheGfg8saEIIEfD/fol3iHmcV8TsZkD4ZYz5gdUuoX4YtlVySUU7Sxl9SQ=="
|
||||
"requested": "[8.0.23, )",
|
||||
"resolved": "8.0.23",
|
||||
"contentHash": "GqHiB1HbbODWPbY/lc5xLQH8siEEhNA0ptpJCC6X6adtAYNEzu5ZlqV3YHA3Gh7fuEwgA8XqVwMtH2KNtuQM1Q=="
|
||||
},
|
||||
"Microsoft.NETFramework.ReferenceAssemblies": {
|
||||
"type": "Direct",
|
||||
|
||||
@@ -122,7 +122,7 @@ public class TarBenchmarks : ArchiveBenchmarkBase
|
||||
public async Task TarCreateSmallFilesAsync()
|
||||
{
|
||||
using var outputStream = new MemoryStream();
|
||||
await using var writer = await WriterFactory.OpenAsyncWriter(
|
||||
await using var writer = WriterFactory.OpenAsyncWriter(
|
||||
outputStream,
|
||||
ArchiveType.Tar,
|
||||
new WriterOptions(CompressionType.None) { LeaveStreamOpen = true }
|
||||
|
||||
@@ -98,7 +98,7 @@ public class ZipBenchmarks : ArchiveBenchmarkBase
|
||||
public async Task ZipCreateSmallFilesAsync()
|
||||
{
|
||||
using var outputStream = new MemoryStream();
|
||||
await using var writer = await WriterFactory.OpenAsyncWriter(
|
||||
await using var writer = WriterFactory.OpenAsyncWriter(
|
||||
outputStream,
|
||||
ArchiveType.Zip,
|
||||
new WriterOptions(CompressionType.Deflate) { LeaveStreamOpen = true }
|
||||
|
||||
@@ -383,7 +383,7 @@ public class ArchiveTests : ReaderTests
|
||||
return WriterFactory.OpenWriter(stream, ArchiveType.Zip, writerOptions);
|
||||
}
|
||||
|
||||
protected static async ValueTask<IAsyncWriter> CreateWriterWithLevelAsync(
|
||||
protected static IAsyncWriter CreateWriterWithLevelAsync(
|
||||
Stream stream,
|
||||
CompressionType compressionType,
|
||||
int? compressionLevel = null
|
||||
@@ -392,7 +392,7 @@ public class ArchiveTests : ReaderTests
|
||||
var writerOptions = compressionLevel.HasValue
|
||||
? new WriterOptions(compressionType, compressionLevel.Value) { LeaveStreamOpen = true }
|
||||
: new WriterOptions(compressionType) { LeaveStreamOpen = true };
|
||||
return await WriterFactory.OpenAsyncWriter(
|
||||
return WriterFactory.OpenAsyncWriter(
|
||||
new AsyncOnlyStream(stream),
|
||||
ArchiveType.Zip,
|
||||
writerOptions
|
||||
|
||||
@@ -104,7 +104,7 @@ public class AsyncTests : TestBase
|
||||
await using (var stream = File.Create(outputPath))
|
||||
#endif
|
||||
using (
|
||||
var writer = await WriterFactory.OpenAsyncWriter(
|
||||
var writer = WriterFactory.OpenAsyncWriter(
|
||||
new AsyncOnlyStream(stream),
|
||||
ArchiveType.Zip,
|
||||
new WriterOptions(CompressionType.Deflate) { LeaveStreamOpen = false }
|
||||
|
||||
@@ -24,7 +24,7 @@ public class GZipWriterAsyncTests : WriterTests
|
||||
)
|
||||
)
|
||||
using (
|
||||
var writer = await WriterFactory.OpenAsyncWriter(
|
||||
var writer = WriterFactory.OpenAsyncWriter(
|
||||
new AsyncOnlyStream(stream),
|
||||
ArchiveType.GZip,
|
||||
new WriterOptions(CompressionType.GZip)
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using SharpCompress.Archives;
|
||||
using SharpCompress.Archives.SevenZip;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Common.SevenZip;
|
||||
using SharpCompress.Factories;
|
||||
using SharpCompress.Readers;
|
||||
using Xunit;
|
||||
@@ -344,4 +345,53 @@ public class SevenZipArchiveTests : ArchiveTests
|
||||
// The critical check: within a single folder, the stream should NEVER be recreated
|
||||
Assert.Equal(0, streamRecreationsWithinFolder); // Folder stream should remain the same for all entries in the same folder
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SevenZipArchive_EmptyStream_WriteToDirectory()
|
||||
{
|
||||
// This test specifically verifies that archives with empty-stream entries
|
||||
// (files with size 0 and no compressed data) can be extracted without throwing
|
||||
// NullReferenceException. This was previously failing because the folder was null
|
||||
// for empty-stream entries.
|
||||
var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "7Zip.EmptyStream.7z");
|
||||
using var archive = SevenZipArchive.OpenArchive(testArchive);
|
||||
|
||||
var emptyStreamFileCount = 0;
|
||||
foreach (var entry in archive.Entries)
|
||||
{
|
||||
if (!entry.IsDirectory)
|
||||
{
|
||||
// Verify this is actually an empty-stream entry (HasStream == false)
|
||||
var sevenZipEntry = entry as SevenZipEntry;
|
||||
if (sevenZipEntry?.FilePart.Header.HasStream == false)
|
||||
{
|
||||
emptyStreamFileCount++;
|
||||
}
|
||||
|
||||
// This should not throw NullReferenceException
|
||||
entry.WriteToDirectory(SCRATCH_FILES_PATH);
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure we actually tested empty-stream entries
|
||||
Assert.True(
|
||||
emptyStreamFileCount > 0,
|
||||
"Test archive should contain at least one empty-stream entry"
|
||||
);
|
||||
|
||||
// Verify that empty files were created
|
||||
var extractedFiles = Directory.GetFiles(
|
||||
SCRATCH_FILES_PATH,
|
||||
"*",
|
||||
SearchOption.AllDirectories
|
||||
);
|
||||
Assert.NotEmpty(extractedFiles);
|
||||
|
||||
// All extracted files should be empty (0 bytes)
|
||||
foreach (var file in extractedFiles)
|
||||
{
|
||||
var fileInfo = new FileInfo(file);
|
||||
Assert.Equal(0, fileInfo.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ public class TarArchiveAsyncTests : ArchiveTests
|
||||
using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive)))
|
||||
{
|
||||
using (
|
||||
var writer = await WriterFactory.OpenAsyncWriter(
|
||||
var writer = WriterFactory.OpenAsyncWriter(
|
||||
new AsyncOnlyStream(stream),
|
||||
ArchiveType.Tar,
|
||||
new WriterOptions(CompressionType.None) { LeaveStreamOpen = false }
|
||||
@@ -94,7 +94,7 @@ public class TarArchiveAsyncTests : ArchiveTests
|
||||
// Step 1: create a tar file containing a file with a long name
|
||||
using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive)))
|
||||
using (
|
||||
var writer = await WriterFactory.OpenAsyncWriter(
|
||||
var writer = WriterFactory.OpenAsyncWriter(
|
||||
new AsyncOnlyStream(stream),
|
||||
ArchiveType.Tar,
|
||||
new WriterOptions(CompressionType.None) { LeaveStreamOpen = false }
|
||||
|
||||
@@ -70,7 +70,7 @@ public class WriterTests : TestBase
|
||||
|
||||
writerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default;
|
||||
|
||||
using var writer = await WriterFactory.OpenAsyncWriter(stream, _type, writerOptions);
|
||||
using var writer = WriterFactory.OpenAsyncWriter(stream, _type, writerOptions);
|
||||
await writer.WriteAllAsync(
|
||||
ORIGINAL_FILES_PATH,
|
||||
"*",
|
||||
|
||||
@@ -62,11 +62,7 @@ public class ZipTypesLevelsWithCrcRatioAsyncTests : ArchiveTests
|
||||
// Create zip archive in memory
|
||||
using var zipStream = new MemoryStream();
|
||||
using (
|
||||
var writer = await CreateWriterWithLevelAsync(
|
||||
zipStream,
|
||||
compressionType,
|
||||
compressionLevel
|
||||
)
|
||||
var writer = CreateWriterWithLevelAsync(zipStream, compressionType, compressionLevel)
|
||||
)
|
||||
{
|
||||
await writer.WriteAsync($"file1_{sizeMb}MiB.txt", new MemoryStream(file1Data));
|
||||
@@ -137,7 +133,7 @@ public class ZipTypesLevelsWithCrcRatioAsyncTests : ArchiveTests
|
||||
};
|
||||
|
||||
using (
|
||||
var writer = await WriterFactory.OpenAsyncWriter(
|
||||
var writer = WriterFactory.OpenAsyncWriter(
|
||||
new AsyncOnlyStream(zipStream),
|
||||
ArchiveType.Zip,
|
||||
writerOptions
|
||||
@@ -205,11 +201,7 @@ public class ZipTypesLevelsWithCrcRatioAsyncTests : ArchiveTests
|
||||
// Create archive with specified compression and level
|
||||
using var zipStream = new MemoryStream();
|
||||
using (
|
||||
var writer = await CreateWriterWithLevelAsync(
|
||||
zipStream,
|
||||
compressionType,
|
||||
compressionLevel
|
||||
)
|
||||
var writer = CreateWriterWithLevelAsync(zipStream, compressionType, compressionLevel)
|
||||
)
|
||||
{
|
||||
await writer.WriteAsync(
|
||||
|
||||
BIN
tests/TestArchives/Archives/7Zip.EmptyStream.7z
Normal file
BIN
tests/TestArchives/Archives/7Zip.EmptyStream.7z
Normal file
Binary file not shown.
Reference in New Issue
Block a user