mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-11-15 17:02:03 +00:00
more awaits
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Archives.GZip;
|
||||
using SharpCompress.Archives.Rar;
|
||||
using SharpCompress.Archives.SevenZip;
|
||||
using SharpCompress.Archives.Tar;
|
||||
//using SharpCompress.Archives.Rar;
|
||||
//using SharpCompress.Archives.SevenZip;
|
||||
//using SharpCompress.Archives.Tar;
|
||||
using SharpCompress.Archives.Zip;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Readers;
|
||||
@@ -18,7 +20,7 @@ namespace SharpCompress.Archives
|
||||
/// <param name="stream"></param>
|
||||
/// <param name="readerOptions"></param>
|
||||
/// <returns></returns>
|
||||
public static IArchive Open(Stream stream, ReaderOptions? readerOptions = null)
|
||||
public static async ValueTask<IArchive> OpenAsync(Stream stream, ReaderOptions? readerOptions = null)
|
||||
{
|
||||
stream.CheckNotNull(nameof(stream));
|
||||
if (!stream.CanRead || !stream.CanSeek)
|
||||
@@ -26,25 +28,25 @@ namespace SharpCompress.Archives
|
||||
throw new ArgumentException("Stream should be readable and seekable");
|
||||
}
|
||||
readerOptions ??= new ReaderOptions();
|
||||
if (ZipArchive.IsZipFile(stream, null))
|
||||
if (await ZipArchive.IsZipFileAsync(stream, null))
|
||||
{
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
return ZipArchive.Open(stream, readerOptions);
|
||||
}
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
if (SevenZipArchive.IsSevenZipFile(stream))
|
||||
/*if (SevenZipArchive.IsSevenZipFile(stream))
|
||||
{
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
return SevenZipArchive.Open(stream, readerOptions);
|
||||
}
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
stream.Seek(0, SeekOrigin.Begin); */
|
||||
if (GZipArchive.IsGZipFile(stream))
|
||||
{
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
return GZipArchive.Open(stream, readerOptions);
|
||||
}
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
if (RarArchive.IsRarFile(stream, readerOptions))
|
||||
/* if (RarArchive.IsRarFile(stream, readerOptions))
|
||||
{
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
return RarArchive.Open(stream, readerOptions);
|
||||
@@ -54,7 +56,7 @@ namespace SharpCompress.Archives
|
||||
{
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
return TarArchive.Open(stream, readerOptions);
|
||||
}
|
||||
} */
|
||||
throw new InvalidOperationException("Cannot determine compressed stream type. Supported Archive Formats: Zip, GZip, Tar, Rar, 7Zip, LZip");
|
||||
}
|
||||
|
||||
@@ -63,7 +65,7 @@ namespace SharpCompress.Archives
|
||||
return type switch
|
||||
{
|
||||
ArchiveType.Zip => ZipArchive.Create(),
|
||||
ArchiveType.Tar => TarArchive.Create(),
|
||||
//ArchiveType.Tar => TarArchive.Create(),
|
||||
ArchiveType.GZip => GZipArchive.Create(),
|
||||
_ => throw new NotSupportedException("Cannot create Archives of type: " + type)
|
||||
};
|
||||
@@ -74,10 +76,10 @@ namespace SharpCompress.Archives
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <param name="options"></param>
|
||||
public static IArchive Open(string filePath, ReaderOptions? options = null)
|
||||
public static ValueTask<IArchive> OpenAsync(string filePath, ReaderOptions? options = null)
|
||||
{
|
||||
filePath.CheckNotNullOrEmpty(nameof(filePath));
|
||||
return Open(new FileInfo(filePath), options);
|
||||
return OpenAsync(new FileInfo(filePath), options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -85,28 +87,28 @@ namespace SharpCompress.Archives
|
||||
/// </summary>
|
||||
/// <param name="fileInfo"></param>
|
||||
/// <param name="options"></param>
|
||||
public static IArchive Open(FileInfo fileInfo, ReaderOptions? options = null)
|
||||
public static async ValueTask<IArchive> OpenAsync(FileInfo fileInfo, ReaderOptions? options = null)
|
||||
{
|
||||
fileInfo.CheckNotNull(nameof(fileInfo));
|
||||
options ??= new ReaderOptions { LeaveStreamOpen = false };
|
||||
|
||||
using var stream = fileInfo.OpenRead();
|
||||
if (ZipArchive.IsZipFile(stream, null))
|
||||
await using var stream = fileInfo.OpenRead();
|
||||
if (await ZipArchive.IsZipFileAsync(stream, null))
|
||||
{
|
||||
return ZipArchive.Open(fileInfo, options);
|
||||
}
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
if (SevenZipArchive.IsSevenZipFile(stream))
|
||||
/*if (SevenZipArchive.IsSevenZipFile(stream))
|
||||
{
|
||||
return SevenZipArchive.Open(fileInfo, options);
|
||||
}
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
stream.Seek(0, SeekOrigin.Begin); */
|
||||
if (GZipArchive.IsGZipFile(stream))
|
||||
{
|
||||
return GZipArchive.Open(fileInfo, options);
|
||||
}
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
if (RarArchive.IsRarFile(stream, options))
|
||||
/*if (RarArchive.IsRarFile(stream, options))
|
||||
{
|
||||
return RarArchive.Open(fileInfo, options);
|
||||
}
|
||||
@@ -114,20 +116,22 @@ namespace SharpCompress.Archives
|
||||
if (TarArchive.IsTarFile(stream))
|
||||
{
|
||||
return TarArchive.Open(fileInfo, options);
|
||||
}
|
||||
} */
|
||||
throw new InvalidOperationException("Cannot determine compressed stream type. Supported Archive Formats: Zip, GZip, Tar, Rar, 7Zip");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract to specific directory, retaining filename
|
||||
/// </summary>
|
||||
public static void WriteToDirectory(string sourceArchive, string destinationDirectory,
|
||||
ExtractionOptions? options = null)
|
||||
public static async ValueTask WriteToDirectory(string sourceArchive,
|
||||
string destinationDirectory,
|
||||
ExtractionOptions? options = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
using IArchive archive = Open(sourceArchive);
|
||||
using IArchive archive = await OpenAsync(sourceArchive);
|
||||
foreach (IArchiveEntry entry in archive.Entries)
|
||||
{
|
||||
entry.WriteToDirectory(destinationDirectory, options);
|
||||
await entry.WriteEntryToDirectoryAsync(destinationDirectory, options, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.IO;
|
||||
|
||||
@@ -6,7 +8,7 @@ namespace SharpCompress.Archives
|
||||
{
|
||||
public static class IArchiveEntryExtensions
|
||||
{
|
||||
public static void WriteTo(this IArchiveEntry archiveEntry, Stream streamToWriteTo)
|
||||
public static async ValueTask WriteToAsync(this IArchiveEntry archiveEntry, Stream streamToWriteTo, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (archiveEntry.IsDirectory)
|
||||
{
|
||||
@@ -22,11 +24,11 @@ namespace SharpCompress.Archives
|
||||
{
|
||||
return;
|
||||
}
|
||||
using (entryStream)
|
||||
await using (entryStream)
|
||||
{
|
||||
using (Stream s = new ListeningStream(streamListener, entryStream))
|
||||
await using (Stream s = new ListeningStream(streamListener, entryStream))
|
||||
{
|
||||
s.TransferTo(streamToWriteTo);
|
||||
await s.TransferToAsync(streamToWriteTo, cancellationToken);
|
||||
}
|
||||
}
|
||||
streamListener.FireEntryExtractionEnd(archiveEntry);
|
||||
@@ -35,29 +37,30 @@ namespace SharpCompress.Archives
|
||||
/// <summary>
|
||||
/// Extract to specific directory, retaining filename
|
||||
/// </summary>
|
||||
public static void WriteToDirectory(this IArchiveEntry entry, string destinationDirectory,
|
||||
ExtractionOptions? options = null)
|
||||
public static ValueTask WriteEntryToDirectoryAsync(this IArchiveEntry entry,
|
||||
string destinationDirectory,
|
||||
ExtractionOptions? options = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ExtractionMethods.WriteEntryToDirectory(entry, destinationDirectory, options,
|
||||
entry.WriteToFile);
|
||||
return ExtractionMethods.WriteEntryToDirectoryAsync(entry, destinationDirectory, options,
|
||||
entry.WriteToFileAsync, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract to specific file
|
||||
/// </summary>
|
||||
public static void WriteToFile(this IArchiveEntry entry,
|
||||
public static ValueTask WriteToFileAsync(this IArchiveEntry entry,
|
||||
string destinationFileName,
|
||||
ExtractionOptions? options = null)
|
||||
ExtractionOptions? options = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
ExtractionMethods.WriteEntryToFile(entry, destinationFileName, options,
|
||||
(x, fm) =>
|
||||
return ExtractionMethods.WriteEntryToFileAsync(entry, destinationFileName, options,
|
||||
async (x, fm, ct) =>
|
||||
{
|
||||
using (FileStream fs = File.Open(destinationFileName, fm))
|
||||
{
|
||||
entry.WriteTo(fs);
|
||||
}
|
||||
});
|
||||
await using FileStream fs = File.Open(x, fm);
|
||||
await entry.WriteToAsync(fs, ct);
|
||||
}, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Common;
|
||||
|
||||
namespace SharpCompress.Archives
|
||||
@@ -8,12 +10,14 @@ namespace SharpCompress.Archives
|
||||
/// <summary>
|
||||
/// Extract to specific directory, retaining filename
|
||||
/// </summary>
|
||||
public static void WriteToDirectory(this IArchive archive, string destinationDirectory,
|
||||
ExtractionOptions? options = null)
|
||||
public static async ValueTask WriteToDirectoryAsync(this IArchive archive,
|
||||
string destinationDirectory,
|
||||
ExtractionOptions? options = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
foreach (IArchiveEntry entry in archive.Entries.Where(x => !x.IsDirectory))
|
||||
{
|
||||
entry.WriteToDirectory(destinationDirectory, options);
|
||||
await entry.WriteEntryToDirectoryAsync(destinationDirectory, options, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,29 +60,37 @@ namespace SharpCompress.Archives.Zip
|
||||
return new ZipArchive(stream, readerOptions ?? new ReaderOptions());
|
||||
}
|
||||
|
||||
public static bool IsZipFile(string filePath, string? password = null)
|
||||
public static ValueTask<bool> IsZipFile(string filePath, string? password = null)
|
||||
{
|
||||
return IsZipFile(new FileInfo(filePath), password);
|
||||
return IsZipFileAsync(new FileInfo(filePath), password);
|
||||
}
|
||||
|
||||
public static bool IsZipFile(FileInfo fileInfo, string? password = null)
|
||||
public static async ValueTask<bool> IsZipFileAsync(FileInfo fileInfo, string? password = null)
|
||||
{
|
||||
if (!fileInfo.Exists)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
using (Stream stream = fileInfo.OpenRead())
|
||||
{
|
||||
return IsZipFile(stream, password);
|
||||
}
|
||||
|
||||
await using Stream stream = fileInfo.OpenRead();
|
||||
return await IsZipFileAsync(stream, password);
|
||||
}
|
||||
|
||||
public static bool IsZipFile(Stream stream, string? password = null)
|
||||
public static async ValueTask<bool> IsZipFileAsync(Stream stream, string? password = null)
|
||||
{
|
||||
StreamingZipHeaderFactory headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding());
|
||||
StreamingZipHeaderFactory headerFactory = new(password, new ArchiveEncoding());
|
||||
try
|
||||
{
|
||||
ZipHeader? header = headerFactory.ReadStreamHeader(stream).FirstOrDefault(x => x.ZipHeaderType != ZipHeaderType.Split);
|
||||
var headers = headerFactory.ReadStreamHeader(stream);
|
||||
ZipHeader? header = null;
|
||||
await foreach (ZipHeader zipHeader in headers)
|
||||
{
|
||||
if (zipHeader.ZipHeaderType == ZipHeaderType.Split)
|
||||
{
|
||||
header = zipHeader;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (header is null)
|
||||
{
|
||||
return false;
|
||||
@@ -196,7 +204,7 @@ namespace SharpCompress.Archives.Zip
|
||||
|
||||
public static ZipArchive Create()
|
||||
{
|
||||
return new ZipArchive();
|
||||
return new();
|
||||
}
|
||||
|
||||
protected override IReader CreateReaderForSolidExtraction()
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace SharpCompress.Readers
|
||||
await reader.WriteEntryToAsync(stream);
|
||||
}
|
||||
|
||||
public static async ValueTask WriteEntryTo(this IReader reader, FileInfo filePath)
|
||||
public static async ValueTask WriteEntryToAsync(this IReader reader, FileInfo filePath)
|
||||
{
|
||||
await using Stream stream = filePath.Open(FileMode.Create);
|
||||
await reader.WriteEntryToAsync(stream);
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Archives.GZip;
|
||||
using SharpCompress.Archives.Rar;
|
||||
using SharpCompress.Archives.Tar;
|
||||
//using SharpCompress.Archives.Rar;
|
||||
//using SharpCompress.Archives.Tar;
|
||||
using SharpCompress.Archives.Zip;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Compressors;
|
||||
@@ -10,8 +11,8 @@ using SharpCompress.Compressors.BZip2;
|
||||
using SharpCompress.Compressors.Deflate;
|
||||
using SharpCompress.IO;
|
||||
using SharpCompress.Readers.GZip;
|
||||
using SharpCompress.Readers.Rar;
|
||||
using SharpCompress.Readers.Tar;
|
||||
//using SharpCompress.Readers.Rar;
|
||||
//using SharpCompress.Readers.Tar;
|
||||
using SharpCompress.Readers.Zip;
|
||||
using SharpCompress.Compressors.LZMA;
|
||||
using SharpCompress.Compressors.Xz;
|
||||
@@ -26,16 +27,16 @@ namespace SharpCompress.Readers
|
||||
/// <param name="stream"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static IReader Open(Stream stream, ReaderOptions? options = null)
|
||||
public static async ValueTask<IReader> OpenAsync(Stream stream, ReaderOptions? options = null)
|
||||
{
|
||||
stream.CheckNotNull(nameof(stream));
|
||||
options = options ?? new ReaderOptions()
|
||||
{
|
||||
LeaveStreamOpen = false
|
||||
};
|
||||
RewindableStream rewindableStream = new RewindableStream(stream);
|
||||
options ??= new ReaderOptions()
|
||||
{
|
||||
LeaveStreamOpen = false
|
||||
};
|
||||
RewindableStream rewindableStream = new(stream);
|
||||
rewindableStream.StartRecording();
|
||||
if (ZipArchive.IsZipFile(rewindableStream, options.Password))
|
||||
if (await ZipArchive.IsZipFileAsync(rewindableStream, options.Password))
|
||||
{
|
||||
rewindableStream.Rewind(true);
|
||||
return ZipReader.Open(rewindableStream, options);
|
||||
@@ -44,12 +45,12 @@ namespace SharpCompress.Readers
|
||||
if (GZipArchive.IsGZipFile(rewindableStream))
|
||||
{
|
||||
rewindableStream.Rewind(false);
|
||||
GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
|
||||
/*GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
|
||||
if (TarArchive.IsTarFile(testStream))
|
||||
{
|
||||
rewindableStream.Rewind(true);
|
||||
return new TarReader(rewindableStream, options, CompressionType.GZip);
|
||||
}
|
||||
} */
|
||||
rewindableStream.Rewind(true);
|
||||
return GZipReader.Open(rewindableStream, options);
|
||||
}
|
||||
@@ -58,27 +59,27 @@ namespace SharpCompress.Readers
|
||||
if (BZip2Stream.IsBZip2(rewindableStream))
|
||||
{
|
||||
rewindableStream.Rewind(false);
|
||||
BZip2Stream testStream = new BZip2Stream(new NonDisposingStream(rewindableStream), CompressionMode.Decompress, false);
|
||||
/*BZip2Stream testStream = new BZip2Stream(new NonDisposingStream(rewindableStream), CompressionMode.Decompress, false);
|
||||
if (TarArchive.IsTarFile(testStream))
|
||||
{
|
||||
rewindableStream.Rewind(true);
|
||||
return new TarReader(rewindableStream, options, CompressionType.BZip2);
|
||||
}
|
||||
} */
|
||||
}
|
||||
|
||||
rewindableStream.Rewind(false);
|
||||
if (LZipStream.IsLZipFile(rewindableStream))
|
||||
{
|
||||
rewindableStream.Rewind(false);
|
||||
LZipStream testStream = new LZipStream(new NonDisposingStream(rewindableStream), CompressionMode.Decompress);
|
||||
if (TarArchive.IsTarFile(testStream))
|
||||
{
|
||||
rewindableStream.Rewind(true);
|
||||
return new TarReader(rewindableStream, options, CompressionType.LZip);
|
||||
}
|
||||
/* LZipStream testStream = new LZipStream(new NonDisposingStream(rewindableStream), CompressionMode.Decompress);
|
||||
if (TarArchive.IsTarFile(testStream))
|
||||
{
|
||||
rewindableStream.Rewind(true);
|
||||
return new TarReader(rewindableStream, options, CompressionType.LZip);
|
||||
} */
|
||||
}
|
||||
rewindableStream.Rewind(false);
|
||||
if (RarArchive.IsRarFile(rewindableStream, options))
|
||||
/* if (RarArchive.IsRarFile(rewindableStream, options))
|
||||
{
|
||||
rewindableStream.Rewind(true);
|
||||
return RarReader.Open(rewindableStream, options);
|
||||
@@ -89,17 +90,17 @@ namespace SharpCompress.Readers
|
||||
{
|
||||
rewindableStream.Rewind(true);
|
||||
return TarReader.Open(rewindableStream, options);
|
||||
}
|
||||
} */
|
||||
rewindableStream.Rewind(false);
|
||||
if (XZStream.IsXZStream(rewindableStream))
|
||||
{
|
||||
rewindableStream.Rewind(true);
|
||||
XZStream testStream = new XZStream(rewindableStream);
|
||||
if (TarArchive.IsTarFile(testStream))
|
||||
{
|
||||
rewindableStream.Rewind(true);
|
||||
return new TarReader(rewindableStream, options, CompressionType.Xz);
|
||||
}
|
||||
/* XZStream testStream = new XZStream(rewindableStream);
|
||||
if (TarArchive.IsTarFile(testStream))
|
||||
{
|
||||
rewindableStream.Rewind(true);
|
||||
return new TarReader(rewindableStream, options, CompressionType.Xz);
|
||||
} */
|
||||
}
|
||||
throw new InvalidOperationException("Cannot determine compressed stream type. Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar, LZip, XZ");
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace SharpCompress.Writers
|
||||
|
||||
protected WriterOptions WriterOptions { get; }
|
||||
|
||||
public abstract Task WriteAsync(string filename, Stream source, DateTime? modificationTime, CancellationToken cancellationToken);
|
||||
public abstract ValueTask WriteAsync(string filename, Stream source, DateTime? modificationTime, CancellationToken cancellationToken);
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace SharpCompress.Writers.GZip
|
||||
return OutputStream.DisposeAsync();
|
||||
}
|
||||
|
||||
public override async Task WriteAsync(string filename, Stream source, DateTime? modificationTime, CancellationToken cancellationToken)
|
||||
public override async ValueTask WriteAsync(string filename, Stream source, DateTime? modificationTime, CancellationToken cancellationToken)
|
||||
{
|
||||
if (_wroteToStream)
|
||||
{
|
||||
@@ -40,7 +40,7 @@ namespace SharpCompress.Writers.GZip
|
||||
GZipStream stream = (GZipStream)OutputStream;
|
||||
stream.FileName = filename;
|
||||
stream.LastModified = modificationTime;
|
||||
await source.TransferToAsync(stream);
|
||||
await source.TransferToAsync(stream, cancellationToken);
|
||||
_wroteToStream = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,6 @@ namespace SharpCompress.Writers
|
||||
public interface IWriter : IAsyncDisposable
|
||||
{
|
||||
ArchiveType WriterType { get; }
|
||||
Task WriteAsync(string filename, Stream source, DateTime? modificationTime, CancellationToken cancellationToken = default);
|
||||
ValueTask WriteAsync(string filename, Stream source, DateTime? modificationTime, CancellationToken cancellationToken = default);
|
||||
}
|
||||
}
|
||||
@@ -9,12 +9,12 @@ namespace SharpCompress.Writers
|
||||
{
|
||||
public static class IWriterExtensions
|
||||
{
|
||||
public static Task WriteAsync(this IWriter writer, string entryPath, Stream source, CancellationToken cancellationToken = default)
|
||||
public static ValueTask WriteAsync(this IWriter writer, string entryPath, Stream source, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return writer.WriteAsync(entryPath, source, null, cancellationToken);
|
||||
}
|
||||
|
||||
public static async Task WriteAsync(this IWriter writer, string entryPath, FileInfo source, CancellationToken cancellationToken = default)
|
||||
public static async ValueTask WriteAsync(this IWriter writer, string entryPath, FileInfo source, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (!source.Exists)
|
||||
{
|
||||
@@ -26,21 +26,21 @@ namespace SharpCompress.Writers
|
||||
}
|
||||
}
|
||||
|
||||
public static Task WriteAsync(this IWriter writer, string entryPath, string source, CancellationToken cancellationToken = default)
|
||||
public static ValueTask WriteAsync(this IWriter writer, string entryPath, string source, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return writer.WriteAsync(entryPath, new FileInfo(source), cancellationToken);
|
||||
}
|
||||
|
||||
public static Task WriteAllAsync(this IWriter writer, string directory, string searchPattern = "*", SearchOption option = SearchOption.TopDirectoryOnly, CancellationToken cancellationToken = default)
|
||||
public static ValueTask WriteAllAsync(this IWriter writer, string directory, string searchPattern = "*", SearchOption option = SearchOption.TopDirectoryOnly, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return writer.WriteAllAsync(directory, searchPattern, null, option, cancellationToken);
|
||||
}
|
||||
|
||||
public static async Task WriteAllAsync(this IWriter writer,
|
||||
string directory,
|
||||
string searchPattern = "*",
|
||||
Expression<Func<string, bool>>? fileSearchFunc = null,
|
||||
SearchOption option = SearchOption.TopDirectoryOnly, CancellationToken cancellationToken = default)
|
||||
public static async ValueTask WriteAllAsync(this IWriter writer,
|
||||
string directory,
|
||||
string searchPattern = "*",
|
||||
Expression<Func<string, bool>>? fileSearchFunc = null,
|
||||
SearchOption option = SearchOption.TopDirectoryOnly, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (!Directory.Exists(directory))
|
||||
{
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
using System.IO;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Writers.GZip;
|
||||
using SharpCompress.Writers.Tar;
|
||||
//using SharpCompress.Writers.Tar;
|
||||
using SharpCompress.Writers.Zip;
|
||||
|
||||
namespace SharpCompress.Writers
|
||||
@@ -26,9 +26,10 @@ namespace SharpCompress.Writers
|
||||
return new ZipWriter(stream, new ZipWriterOptions(writerOptions));
|
||||
}
|
||||
case ArchiveType.Tar:
|
||||
{
|
||||
return new TarWriter(stream, new TarWriterOptions(writerOptions));
|
||||
}
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
//return new TarWriter(stream, new TarWriterOptions(writerOptions));
|
||||
}
|
||||
default:
|
||||
{
|
||||
throw new NotSupportedException("Archive Type does not have a Writer: " + archiveType);
|
||||
|
||||
@@ -96,21 +96,21 @@ namespace SharpCompress.Writers.Zip
|
||||
}
|
||||
}
|
||||
|
||||
public override Task WriteAsync(string filename, Stream source, DateTime? modificationTime, CancellationToken cancellationToken)
|
||||
public override ValueTask WriteAsync(string filename, Stream source, DateTime? modificationTime, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return WriteAsync(filename, source, new ZipWriterEntryOptions()
|
||||
{
|
||||
ModificationDateTime = modificationTime
|
||||
});
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task WriteAsync(string entryPath, Stream source, ZipWriterEntryOptions zipWriterEntryOptions)
|
||||
public async ValueTask WriteAsync(string entryPath, Stream source, ZipWriterEntryOptions zipWriterEntryOptions, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await using Stream output = await WriteToStreamAsync(entryPath, zipWriterEntryOptions);
|
||||
await source.TransferToAsync(output);
|
||||
await using Stream output = await WriteToStreamAsync(entryPath, zipWriterEntryOptions, cancellationToken);
|
||||
await source.TransferToAsync(output, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<Stream> WriteToStreamAsync(string entryPath, ZipWriterEntryOptions options)
|
||||
public async ValueTask<Stream> WriteToStreamAsync(string entryPath, ZipWriterEntryOptions options, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var compression = ToZipCompressionMethod(options.CompressionType ?? compressionType);
|
||||
|
||||
@@ -130,7 +130,7 @@ namespace SharpCompress.Writers.Zip
|
||||
useZip64 = options.EnableZip64.Value;
|
||||
}
|
||||
|
||||
var headersize = (uint)(await WriteHeaderAsync(entryPath, options, entry, useZip64));
|
||||
var headersize = (uint)(await WriteHeaderAsync(entryPath, options, entry, useZip64, cancellationToken));
|
||||
streamPosition += headersize;
|
||||
return new ZipWritingStream(this, OutputStream, entry, compression,
|
||||
options.DeflateCompressionLevel ?? compressionLevel);
|
||||
@@ -149,7 +149,8 @@ namespace SharpCompress.Writers.Zip
|
||||
return filename.Trim('/');
|
||||
}
|
||||
|
||||
private async Task<int> WriteHeaderAsync(string filename, ZipWriterEntryOptions zipWriterEntryOptions, ZipCentralDirectoryEntry entry, bool useZip64)
|
||||
private async Task<int> WriteHeaderAsync(string filename, ZipWriterEntryOptions zipWriterEntryOptions, ZipCentralDirectoryEntry entry, bool useZip64,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
// We err on the side of caution until the zip specification clarifies how to support this
|
||||
if (!OutputStream.CanSeek && useZip64)
|
||||
@@ -163,7 +164,7 @@ namespace SharpCompress.Writers.Zip
|
||||
// TODO: Use stackalloc when we exclusively support netstandard2.1 or higher
|
||||
using var intBuf = MemoryPool<byte>.Shared.Rent(4);
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(intBuf.Memory.Span, ZipHeaderFactory.ENTRY_HEADER_BYTES);
|
||||
await OutputStream.WriteAsync(intBuf.Memory);
|
||||
await OutputStream.WriteAsync(intBuf.Memory, cancellationToken);
|
||||
if (explicitZipCompressionInfo == ZipCompressionMethod.Deflate)
|
||||
{
|
||||
if (OutputStream.CanSeek && useZip64)
|
||||
@@ -171,14 +172,14 @@ namespace SharpCompress.Writers.Zip
|
||||
using var buf1 = MemoryPool<byte>.Shared.Rent(2);
|
||||
buf1.Memory.Span[0] = 45;
|
||||
buf1.Memory.Span[1] = 0;
|
||||
await OutputStream.WriteAsync(buf1.Memory); //smallest allowed version for zip64
|
||||
await OutputStream.WriteAsync(buf1.Memory, cancellationToken); //smallest allowed version for zip64
|
||||
}
|
||||
else
|
||||
{
|
||||
using var buf1 = MemoryPool<byte>.Shared.Rent(2);
|
||||
buf1.Memory.Span[0] = 20;
|
||||
buf1.Memory.Span[1] = 0;
|
||||
await OutputStream.WriteAsync(buf1.Memory); //older version which is more compatible
|
||||
await OutputStream.WriteAsync(buf1.Memory, cancellationToken); //older version which is more compatible
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -186,7 +187,7 @@ namespace SharpCompress.Writers.Zip
|
||||
using var buf1 = MemoryPool<byte>.Shared.Rent(2);
|
||||
buf1.Memory.Span[0] = 63;
|
||||
buf1.Memory.Span[1] = 0;
|
||||
await OutputStream.WriteAsync(buf1.Memory); //version says we used PPMd or LZMA
|
||||
await OutputStream.WriteAsync(buf1.Memory, cancellationToken); //version says we used PPMd or LZMA
|
||||
}
|
||||
HeaderFlags flags = Equals(WriterOptions.ArchiveEncoding.GetEncoding(), Encoding.UTF8) ? HeaderFlags.Efs : 0;
|
||||
if (!OutputStream.CanSeek)
|
||||
@@ -200,20 +201,20 @@ namespace SharpCompress.Writers.Zip
|
||||
}
|
||||
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(intBuf.Memory.Span, (ushort)flags);
|
||||
await OutputStream.WriteAsync(intBuf.Memory.Slice(0,2));
|
||||
await OutputStream.WriteAsync(intBuf.Memory.Slice(0,2), cancellationToken);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(intBuf.Memory.Span, (ushort)explicitZipCompressionInfo);
|
||||
await OutputStream.WriteAsync(intBuf.Memory.Slice(0,2)); // zipping method
|
||||
await OutputStream.WriteAsync(intBuf.Memory.Slice(0,2), cancellationToken); // zipping method
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(intBuf.Memory.Span, zipWriterEntryOptions.ModificationDateTime.DateTimeToDosTime());
|
||||
await OutputStream.WriteAsync(intBuf.Memory);
|
||||
await OutputStream.WriteAsync(intBuf.Memory, cancellationToken);
|
||||
|
||||
// zipping date and time
|
||||
using var buf2 = MemoryPool<byte>.Shared.Rent(12);
|
||||
buf2.Memory.Span.Clear();
|
||||
await OutputStream.WriteAsync(buf2.Memory);
|
||||
await OutputStream.WriteAsync(buf2.Memory, cancellationToken);
|
||||
|
||||
// unused CRC, un/compressed size, updated later
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(intBuf.Memory.Span, (ushort)encodedFilename.Length);
|
||||
await OutputStream.WriteAsync(intBuf.Memory.Slice(0,2));// filename length
|
||||
await OutputStream.WriteAsync(intBuf.Memory.Slice(0,2), cancellationToken);// filename length
|
||||
|
||||
var extralength = 0;
|
||||
if (OutputStream.CanSeek && useZip64)
|
||||
@@ -222,14 +223,14 @@ namespace SharpCompress.Writers.Zip
|
||||
}
|
||||
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(intBuf.Memory.Span, (ushort)extralength);
|
||||
await OutputStream.WriteAsync(intBuf.Memory.Slice(0,2));// extra length
|
||||
await OutputStream.WriteAsync(encodedFilename, 0, encodedFilename.Length);
|
||||
await OutputStream.WriteAsync(intBuf.Memory.Slice(0,2), cancellationToken);// extra length
|
||||
await OutputStream.WriteAsync(encodedFilename, 0, encodedFilename.Length, cancellationToken);
|
||||
|
||||
if (extralength != 0)
|
||||
{
|
||||
using var buf3 = MemoryPool<byte>.Shared.Rent(extralength);
|
||||
buf2.Memory.Span.Clear();
|
||||
await OutputStream.WriteAsync(buf3.Memory); // reserve space for zip64 data
|
||||
await OutputStream.WriteAsync(buf3.Memory, cancellationToken); // reserve space for zip64 data
|
||||
entry.Zip64HeaderOffset = (ushort)(6 + 2 + 2 + 4 + 12 + 2 + 2 + encodedFilename.Length);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Archives;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.IO;
|
||||
@@ -12,24 +13,24 @@ namespace SharpCompress.Test
|
||||
{
|
||||
public class ArchiveTests : ReaderTests
|
||||
{
|
||||
protected void ArchiveStreamReadExtractAll(string testArchive, CompressionType compression)
|
||||
protected async ValueTask ArchiveStreamReadExtractAll(string testArchive, CompressionType compression)
|
||||
{
|
||||
testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive);
|
||||
ArchiveStreamReadExtractAll(testArchive.AsEnumerable(), compression);
|
||||
await ArchiveStreamReadExtractAll(testArchive.AsEnumerable(), compression);
|
||||
}
|
||||
|
||||
|
||||
protected void ArchiveStreamReadExtractAll(IEnumerable<string> testArchives, CompressionType compression)
|
||||
protected async ValueTask ArchiveStreamReadExtractAll(IEnumerable<string> testArchives, CompressionType compression)
|
||||
{
|
||||
foreach (var path in testArchives)
|
||||
{
|
||||
using (var stream = new NonDisposingStream(File.OpenRead(path), true))
|
||||
using (var archive = ArchiveFactory.Open(stream))
|
||||
await using (var stream = new NonDisposingStream(File.OpenRead(path), true))
|
||||
using (var archive = await ArchiveFactory.OpenAsync(stream))
|
||||
{
|
||||
Assert.True(archive.IsSolid);
|
||||
using (var reader = archive.ExtractAllEntries())
|
||||
await using (var reader = archive.ExtractAllEntries())
|
||||
{
|
||||
UseReader(reader, compression);
|
||||
await ReadAsync(reader, compression);
|
||||
}
|
||||
VerifyFiles();
|
||||
|
||||
@@ -40,7 +41,7 @@ namespace SharpCompress.Test
|
||||
}
|
||||
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
|
||||
{
|
||||
entry.WriteToDirectory(SCRATCH_FILES_PATH,
|
||||
await entry.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH,
|
||||
new ExtractionOptions
|
||||
{
|
||||
ExtractFullPath = true,
|
||||
@@ -53,29 +54,29 @@ namespace SharpCompress.Test
|
||||
}
|
||||
}
|
||||
|
||||
protected void ArchiveStreamRead(string testArchive, ReaderOptions readerOptions = null)
|
||||
protected ValueTask ArchiveStreamReadAsync(string testArchive, ReaderOptions readerOptions = null)
|
||||
{
|
||||
testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive);
|
||||
ArchiveStreamRead(readerOptions, testArchive.AsEnumerable());
|
||||
return ArchiveStreamReadAsync(readerOptions, testArchive.AsEnumerable());
|
||||
}
|
||||
|
||||
protected void ArchiveStreamRead(ReaderOptions readerOptions = null, params string[] testArchives)
|
||||
protected ValueTask ArchiveStreamReadAsync(ReaderOptions readerOptions = null, params string[] testArchives)
|
||||
{
|
||||
ArchiveStreamRead(readerOptions, testArchives.Select(x => Path.Combine(TEST_ARCHIVES_PATH, x)));
|
||||
return ArchiveStreamReadAsync(readerOptions, testArchives.Select(x => Path.Combine(TEST_ARCHIVES_PATH, x)));
|
||||
}
|
||||
|
||||
protected void ArchiveStreamRead(ReaderOptions readerOptions, IEnumerable<string> testArchives)
|
||||
protected async ValueTask ArchiveStreamReadAsync(ReaderOptions readerOptions, IEnumerable<string> testArchives)
|
||||
{
|
||||
foreach (var path in testArchives)
|
||||
{
|
||||
using (var stream = new NonDisposingStream(File.OpenRead(path), true))
|
||||
using (var archive = ArchiveFactory.Open(stream, readerOptions))
|
||||
using (var archive = await ArchiveFactory.OpenAsync(stream, readerOptions))
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
|
||||
{
|
||||
entry.WriteToDirectory(SCRATCH_FILES_PATH,
|
||||
await entry.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH,
|
||||
new ExtractionOptions()
|
||||
{
|
||||
ExtractFullPath = true,
|
||||
@@ -95,14 +96,14 @@ namespace SharpCompress.Test
|
||||
}
|
||||
}
|
||||
|
||||
protected void ArchiveFileRead(string testArchive, ReaderOptions readerOptions = null)
|
||||
protected async ValueTask ArchiveFileReadAsync(string testArchive, ReaderOptions readerOptions = null)
|
||||
{
|
||||
testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive);
|
||||
using (var archive = ArchiveFactory.Open(testArchive, readerOptions))
|
||||
using (var archive = await ArchiveFactory.OpenAsync(testArchive, readerOptions))
|
||||
{
|
||||
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
|
||||
{
|
||||
entry.WriteToDirectory(SCRATCH_FILES_PATH,
|
||||
await entry.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH,
|
||||
new ExtractionOptions()
|
||||
{
|
||||
ExtractFullPath = true,
|
||||
@@ -116,14 +117,14 @@ namespace SharpCompress.Test
|
||||
/// <summary>
|
||||
/// Demonstrate the ExtractionOptions.PreserveFileTime and ExtractionOptions.PreserveAttributes extract options
|
||||
/// </summary>
|
||||
protected void ArchiveFileReadEx(string testArchive)
|
||||
protected async ValueTask ArchiveFileReadEx(string testArchive)
|
||||
{
|
||||
testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive);
|
||||
using (var archive = ArchiveFactory.Open(testArchive))
|
||||
using (var archive = await ArchiveFactory.OpenAsync(testArchive))
|
||||
{
|
||||
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
|
||||
{
|
||||
entry.WriteToDirectory(SCRATCH_FILES_PATH,
|
||||
await entry.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH,
|
||||
new ExtractionOptions()
|
||||
{
|
||||
ExtractFullPath = true,
|
||||
|
||||
@@ -16,13 +16,13 @@ namespace SharpCompress.Test.GZip
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GZip_Archive_Generic()
|
||||
public async ValueTask GZip_Archive_Generic()
|
||||
{
|
||||
using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")))
|
||||
using (var archive = ArchiveFactory.Open(stream))
|
||||
using (var archive = await ArchiveFactory.OpenAsync(stream))
|
||||
{
|
||||
var entry = archive.Entries.First();
|
||||
entry.WriteToFile(Path.Combine(SCRATCH_FILES_PATH, entry.Key));
|
||||
await entry.WriteToFileAsync(Path.Combine(SCRATCH_FILES_PATH, entry.Key));
|
||||
|
||||
long size = entry.Size;
|
||||
var scratch = new FileInfo(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar"));
|
||||
@@ -31,18 +31,18 @@ namespace SharpCompress.Test.GZip
|
||||
Assert.Equal(size, scratch.Length);
|
||||
Assert.Equal(size, test.Length);
|
||||
}
|
||||
CompareArchivesByPath(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar"),
|
||||
Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar"));
|
||||
await CompareArchivesByPathAsync(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar"),
|
||||
Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GZip_Archive()
|
||||
public async ValueTask GZip_Archive()
|
||||
{
|
||||
using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")))
|
||||
using (var archive = GZipArchive.Open(stream))
|
||||
{
|
||||
var entry = archive.Entries.First();
|
||||
entry.WriteToFile(Path.Combine(SCRATCH_FILES_PATH, entry.Key));
|
||||
await entry.WriteToFileAsync(Path.Combine(SCRATCH_FILES_PATH, entry.Key));
|
||||
|
||||
long size = entry.Size;
|
||||
var scratch = new FileInfo(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar"));
|
||||
@@ -51,8 +51,8 @@ namespace SharpCompress.Test.GZip
|
||||
Assert.Equal(size, scratch.Length);
|
||||
Assert.Equal(size, test.Length);
|
||||
}
|
||||
CompareArchivesByPath(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar"),
|
||||
Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar"));
|
||||
await CompareArchivesByPathAsync(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar"),
|
||||
Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar"));
|
||||
}
|
||||
|
||||
|
||||
@@ -95,11 +95,11 @@ namespace SharpCompress.Test.GZip
|
||||
entryStream.CopyTo(tarStream);
|
||||
}
|
||||
Assert.Equal(size, tarStream.Length);
|
||||
using (var entryStream = archiveEntry.OpenEntryStream())
|
||||
/*using (var entryStream = archiveEntry.OpenEntryStream())
|
||||
{
|
||||
var result = Archives.Tar.TarArchive.IsTarFile(entryStream);
|
||||
}
|
||||
Assert.Equal(size, tarStream.Length);
|
||||
Assert.Equal(size, tarStream.Length); */
|
||||
using (var entryStream = archiveEntry.OpenEntryStream())
|
||||
{
|
||||
tarStream = new MemoryStream();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.IO;
|
||||
using Xunit;
|
||||
@@ -13,20 +14,20 @@ namespace SharpCompress.Test.GZip
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GZip_Reader_Generic()
|
||||
public async ValueTask GZip_Reader_Generic()
|
||||
{
|
||||
Read("Tar.tar.gz", CompressionType.GZip);
|
||||
await ReadAsync("Tar.tar.gz", CompressionType.GZip);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void GZip_Reader_Generic2()
|
||||
public async ValueTask GZip_Reader_Generic2()
|
||||
{
|
||||
//read only as GZip itme
|
||||
using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")))
|
||||
using (var reader = SharpCompress.Readers.GZip.GZipReader.Open(new RewindableStream(stream)))
|
||||
await using (var reader = SharpCompress.Readers.GZip.GZipReader.Open(new RewindableStream(stream)))
|
||||
{
|
||||
while (reader.MoveToNextEntry()) // Crash here
|
||||
while (await reader.MoveToNextEntry()) // Crash here
|
||||
{
|
||||
Assert.NotEqual(0, reader.Entry.Size);
|
||||
Assert.NotEqual(0, reader.Entry.Crc);
|
||||
|
||||
@@ -18,25 +18,25 @@ namespace SharpCompress.Test.GZip
|
||||
[Fact]
|
||||
public async Task GZip_Writer_Generic()
|
||||
{
|
||||
using (Stream stream = File.Open(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz"), FileMode.OpenOrCreate, FileAccess.Write))
|
||||
await using (Stream stream = File.Open(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz"), FileMode.OpenOrCreate, FileAccess.Write))
|
||||
await using (var writer = WriterFactory.Open(stream, ArchiveType.GZip, CompressionType.GZip))
|
||||
{
|
||||
await writer.WriteAsync("Tar.tar", Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar"));
|
||||
}
|
||||
CompareArchivesByPath(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz"),
|
||||
Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"));
|
||||
await CompareArchivesByPathAsync(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz"),
|
||||
Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GZip_Writer()
|
||||
{
|
||||
using (Stream stream = File.Open(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz"), FileMode.OpenOrCreate, FileAccess.Write))
|
||||
await using (Stream stream = File.Open(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz"), FileMode.OpenOrCreate, FileAccess.Write))
|
||||
await using (var writer = new GZipWriter(stream))
|
||||
{
|
||||
await writer.WriteAsync("Tar.tar", Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar"));
|
||||
}
|
||||
CompareArchivesByPath(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz"),
|
||||
Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"));
|
||||
await CompareArchivesByPathAsync(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz"),
|
||||
Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -44,7 +44,7 @@ namespace SharpCompress.Test.GZip
|
||||
{
|
||||
await Assert.ThrowsAsync<InvalidFormatException>(async () =>
|
||||
{
|
||||
using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")))
|
||||
await using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")))
|
||||
await using (var writer = WriterFactory.Open(stream, ArchiveType.GZip, CompressionType.BZip2))
|
||||
{
|
||||
}
|
||||
@@ -54,14 +54,14 @@ namespace SharpCompress.Test.GZip
|
||||
[Fact]
|
||||
public async Task GZip_Writer_Entry_Path_With_Dir()
|
||||
{
|
||||
using (Stream stream = File.Open(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz"), FileMode.OpenOrCreate, FileAccess.Write))
|
||||
await using (Stream stream = File.Open(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz"), FileMode.OpenOrCreate, FileAccess.Write))
|
||||
await using (var writer = new GZipWriter(stream))
|
||||
{
|
||||
var path = Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar");
|
||||
await writer.WriteAsync(path, path); //covers issue #532
|
||||
}
|
||||
CompareArchivesByPath(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz"),
|
||||
Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"));
|
||||
await CompareArchivesByPathAsync(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz"),
|
||||
Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.IO;
|
||||
using SharpCompress.Readers;
|
||||
@@ -9,21 +10,21 @@ namespace SharpCompress.Test
|
||||
{
|
||||
public abstract class ReaderTests : TestBase
|
||||
{
|
||||
protected void Read(string testArchive, CompressionType expectedCompression, ReaderOptions options = null)
|
||||
protected async ValueTask ReadAsync(string testArchive, CompressionType expectedCompression, ReaderOptions options = null)
|
||||
{
|
||||
testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive);
|
||||
|
||||
options = options ?? new ReaderOptions();
|
||||
options ??= new ReaderOptions();
|
||||
|
||||
options.LeaveStreamOpen = true;
|
||||
ReadImpl(testArchive, expectedCompression, options);
|
||||
await ReadImplAsync(testArchive, expectedCompression, options);
|
||||
|
||||
options.LeaveStreamOpen = false;
|
||||
ReadImpl(testArchive, expectedCompression, options);
|
||||
await ReadImplAsync(testArchive, expectedCompression, options);
|
||||
VerifyFiles();
|
||||
}
|
||||
|
||||
private void ReadImpl(string testArchive, CompressionType expectedCompression, ReaderOptions options)
|
||||
private async ValueTask ReadImplAsync(string testArchive, CompressionType expectedCompression, ReaderOptions options)
|
||||
{
|
||||
using (var file = File.OpenRead(testArchive))
|
||||
{
|
||||
@@ -31,9 +32,9 @@ namespace SharpCompress.Test
|
||||
{
|
||||
using (var testStream = new TestStream(protectedStream))
|
||||
{
|
||||
using (var reader = ReaderFactory.Open(testStream, options))
|
||||
await using (var reader = await ReaderFactory.OpenAsync(testStream, options))
|
||||
{
|
||||
UseReader(reader, expectedCompression);
|
||||
await ReadAsync(reader, expectedCompression);
|
||||
protectedStream.ThrowOnDispose = false;
|
||||
Assert.False(testStream.IsDisposed, "{nameof(testStream)} prematurely closed");
|
||||
}
|
||||
@@ -47,14 +48,14 @@ namespace SharpCompress.Test
|
||||
}
|
||||
}
|
||||
|
||||
public void UseReader(IReader reader, CompressionType expectedCompression)
|
||||
public async ValueTask ReadAsync(IReader reader, CompressionType expectedCompression)
|
||||
{
|
||||
while (reader.MoveToNextEntry())
|
||||
while (await reader.MoveToNextEntry())
|
||||
{
|
||||
if (!reader.Entry.IsDirectory)
|
||||
{
|
||||
Assert.Equal(expectedCompression, reader.Entry.CompressionType);
|
||||
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions()
|
||||
await reader.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH, new ExtractionOptions()
|
||||
{
|
||||
ExtractFullPath = true,
|
||||
Overwrite = true
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using SharpCompress.Readers;
|
||||
using Xunit;
|
||||
|
||||
@@ -188,7 +189,7 @@ namespace SharpCompress.Test
|
||||
Assert.Equal(fi1.Attributes, fi2.Attributes);
|
||||
}
|
||||
|
||||
protected void CompareArchivesByPath(string file1, string file2, Encoding encoding = null)
|
||||
protected async ValueTask CompareArchivesByPathAsync(string file1, string file2, Encoding encoding = null)
|
||||
{
|
||||
ReaderOptions readerOptions = new ReaderOptions { LeaveStreamOpen = false };
|
||||
readerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default;
|
||||
@@ -196,16 +197,16 @@ namespace SharpCompress.Test
|
||||
//don't compare the order. OS X reads files from the file system in a different order therefore makes the archive ordering different
|
||||
var archive1Entries = new List<string>();
|
||||
var archive2Entries = new List<string>();
|
||||
using (var archive1 = ReaderFactory.Open(File.OpenRead(file1), readerOptions))
|
||||
using (var archive2 = ReaderFactory.Open(File.OpenRead(file2), readerOptions))
|
||||
await using (var archive1 = await ReaderFactory.OpenAsync(File.OpenRead(file1), readerOptions))
|
||||
await using (var archive2 = await ReaderFactory.OpenAsync(File.OpenRead(file2), readerOptions))
|
||||
{
|
||||
while (archive1.MoveToNextEntry())
|
||||
while (await archive1.MoveToNextEntry())
|
||||
{
|
||||
Assert.True(archive2.MoveToNextEntry());
|
||||
Assert.True(await archive2.MoveToNextEntry());
|
||||
archive1Entries.Add(archive1.Entry.Key);
|
||||
archive2Entries.Add(archive2.Entry.Key);
|
||||
}
|
||||
Assert.False(archive2.MoveToNextEntry());
|
||||
Assert.False(await archive2.MoveToNextEntry());
|
||||
}
|
||||
archive1Entries.Sort();
|
||||
archive2Entries.Sort();
|
||||
|
||||
@@ -33,8 +33,8 @@ namespace SharpCompress.Test
|
||||
await writer.WriteAllAsync(ORIGINAL_FILES_PATH, "*", SearchOption.AllDirectories);
|
||||
}
|
||||
}
|
||||
CompareArchivesByPath(Path.Combine(SCRATCH2_FILES_PATH, archive),
|
||||
Path.Combine(TEST_ARCHIVES_PATH, archiveToVerifyAgainst));
|
||||
await CompareArchivesByPathAsync(Path.Combine(SCRATCH2_FILES_PATH, archive),
|
||||
Path.Combine(TEST_ARCHIVES_PATH, archiveToVerifyAgainst));
|
||||
|
||||
using (Stream stream = File.OpenRead(Path.Combine(SCRATCH2_FILES_PATH, archive)))
|
||||
{
|
||||
@@ -42,9 +42,9 @@ namespace SharpCompress.Test
|
||||
|
||||
readerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default;
|
||||
|
||||
using (var reader = ReaderFactory.Open(new NonDisposingStream(stream), readerOptions))
|
||||
await using (var reader = await ReaderFactory.OpenAsync(new NonDisposingStream(stream), readerOptions))
|
||||
{
|
||||
reader.WriteAllToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions()
|
||||
await reader.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH, new ExtractionOptions()
|
||||
{
|
||||
ExtractFullPath = true
|
||||
});
|
||||
|
||||
@@ -100,7 +100,7 @@ namespace SharpCompress.Test.Zip
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RunSingleTest(long files, long filesize, bool set_zip64, bool forward_only, long write_chunk_size = 1024 * 1024, string filename = "zip64-test.zip")
|
||||
public async ValueTask RunSingleTest(long files, long filesize, bool set_zip64, bool forward_only, long write_chunk_size = 1024 * 1024, string filename = "zip64-test.zip")
|
||||
{
|
||||
filename = Path.Combine(SCRATCH2_FILES_PATH, filename);
|
||||
|
||||
@@ -114,7 +114,7 @@ namespace SharpCompress.Test.Zip
|
||||
await CreateZipArchive(filename, files, filesize, write_chunk_size, set_zip64, forward_only);
|
||||
}
|
||||
|
||||
var resForward = ReadForwardOnly(filename);
|
||||
var resForward = await ReadForwardOnly(filename);
|
||||
if (resForward.Item1 != files)
|
||||
{
|
||||
throw new Exception($"Incorrect number of items reported: {resForward.Item1}, should have been {files}");
|
||||
@@ -125,7 +125,7 @@ namespace SharpCompress.Test.Zip
|
||||
throw new Exception($"Incorrect combined size reported: {resForward.Item2}, should have been {files * filesize}");
|
||||
}
|
||||
|
||||
var resArchive = ReadArchive(filename);
|
||||
var resArchive = await ReadArchive(filename);
|
||||
if (resArchive.Item1 != files)
|
||||
{
|
||||
throw new Exception($"Incorrect number of items reported: {resArchive.Item1}, should have been {files}");
|
||||
@@ -168,15 +168,15 @@ namespace SharpCompress.Test.Zip
|
||||
}
|
||||
}
|
||||
|
||||
public Tuple<long, long> ReadForwardOnly(string filename)
|
||||
public async ValueTask<(long, long)> ReadForwardOnly(string filename)
|
||||
{
|
||||
long count = 0;
|
||||
long size = 0;
|
||||
Common.Zip.ZipEntry prev = null;
|
||||
using (var fs = File.OpenRead(filename))
|
||||
using (var rd = ZipReader.Open(fs, new ReaderOptions() { LookForHeader = false }))
|
||||
await using (var rd = ZipReader.Open(fs, new ReaderOptions() { LookForHeader = false }))
|
||||
{
|
||||
while (rd.MoveToNextEntry())
|
||||
while (await rd.MoveToNextEntry())
|
||||
{
|
||||
using (rd.OpenEntryStream())
|
||||
{ }
|
||||
@@ -196,14 +196,14 @@ namespace SharpCompress.Test.Zip
|
||||
size += prev.Size;
|
||||
}
|
||||
|
||||
return new Tuple<long, long>(count, size);
|
||||
return (count, size);
|
||||
}
|
||||
|
||||
public Tuple<long, long> ReadArchive(string filename)
|
||||
public async ValueTask<(long, long)> ReadArchive(string filename)
|
||||
{
|
||||
using (var archive = ArchiveFactory.Open(filename))
|
||||
using (var archive = await ArchiveFactory.OpenAsync(filename))
|
||||
{
|
||||
return new Tuple<long, long>(
|
||||
return (
|
||||
archive.Entries.Count(),
|
||||
archive.Entries.Select(x => x.Size).Sum()
|
||||
);
|
||||
|
||||
@@ -21,139 +21,139 @@ namespace SharpCompress.Test.Zip
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_ZipX_ArchiveStreamRead()
|
||||
public async ValueTask Zip_ZipX_ArchiveStreamRead()
|
||||
{
|
||||
ArchiveStreamRead("Zip.zipx");
|
||||
await ArchiveStreamReadAsync("Zip.zipx");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_BZip2_Streamed_ArchiveStreamRead()
|
||||
public async ValueTask Zip_BZip2_Streamed_ArchiveStreamRead()
|
||||
{
|
||||
ArchiveStreamRead("Zip.bzip2.dd.zip");
|
||||
await ArchiveStreamReadAsync("Zip.bzip2.dd.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_BZip2_ArchiveStreamRead()
|
||||
public async ValueTask Zip_BZip2_ArchiveStreamRead()
|
||||
{
|
||||
ArchiveStreamRead("Zip.bzip2.zip");
|
||||
await ArchiveStreamReadAsync("Zip.bzip2.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_Deflate_Streamed2_ArchiveStreamRead()
|
||||
public async ValueTask Zip_Deflate_Streamed2_ArchiveStreamRead()
|
||||
{
|
||||
ArchiveStreamRead("Zip.deflate.dd-.zip");
|
||||
await ArchiveStreamReadAsync("Zip.deflate.dd-.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_Deflate_Streamed_ArchiveStreamRead()
|
||||
public async ValueTask Zip_Deflate_Streamed_ArchiveStreamRead()
|
||||
{
|
||||
ArchiveStreamRead("Zip.deflate.dd.zip");
|
||||
await ArchiveStreamReadAsync("Zip.deflate.dd.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_Deflate_ArchiveStreamRead()
|
||||
public async ValueTask Zip_Deflate_ArchiveStreamRead()
|
||||
{
|
||||
ArchiveStreamRead("Zip.deflate.zip");
|
||||
await ArchiveStreamReadAsync("Zip.deflate.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_Deflate64_ArchiveStreamRead()
|
||||
public async ValueTask Zip_Deflate64_ArchiveStreamRead()
|
||||
{
|
||||
ArchiveStreamRead("Zip.deflate64.zip");
|
||||
await ArchiveStreamReadAsync("Zip.deflate64.zip");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_LZMA_Streamed_ArchiveStreamRead()
|
||||
public async ValueTask Zip_LZMA_Streamed_ArchiveStreamRead()
|
||||
{
|
||||
ArchiveStreamRead("Zip.lzma.dd.zip");
|
||||
await ArchiveStreamReadAsync("Zip.lzma.dd.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_LZMA_ArchiveStreamRead()
|
||||
public async ValueTask Zip_LZMA_ArchiveStreamRead()
|
||||
{
|
||||
ArchiveStreamRead("Zip.lzma.zip");
|
||||
await ArchiveStreamReadAsync("Zip.lzma.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_PPMd_Streamed_ArchiveStreamRead()
|
||||
public async ValueTask Zip_PPMd_Streamed_ArchiveStreamRead()
|
||||
{
|
||||
ArchiveStreamRead("Zip.ppmd.dd.zip");
|
||||
await ArchiveStreamReadAsync("Zip.ppmd.dd.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_PPMd_ArchiveStreamRead()
|
||||
public async ValueTask Zip_PPMd_ArchiveStreamRead()
|
||||
{
|
||||
ArchiveStreamRead("Zip.ppmd.zip");
|
||||
await ArchiveStreamReadAsync("Zip.ppmd.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_None_ArchiveStreamRead()
|
||||
public async ValueTask Zip_None_ArchiveStreamRead()
|
||||
{
|
||||
ArchiveStreamRead("Zip.none.zip");
|
||||
await ArchiveStreamReadAsync("Zip.none.zip");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_BZip2_Streamed_ArchiveFileRead()
|
||||
public async ValueTask Zip_BZip2_Streamed_ArchiveFileRead()
|
||||
{
|
||||
ArchiveFileRead("Zip.bzip2.dd.zip");
|
||||
await ArchiveStreamReadAsync("Zip.bzip2.dd.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_BZip2_ArchiveFileRead()
|
||||
public async ValueTask Zip_BZip2_ArchiveFileRead()
|
||||
{
|
||||
ArchiveFileRead("Zip.bzip2.zip");
|
||||
await ArchiveStreamReadAsync("Zip.bzip2.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_Deflate_Streamed2_ArchiveFileRead()
|
||||
public async ValueTask Zip_Deflate_Streamed2_ArchiveFileRead()
|
||||
{
|
||||
ArchiveFileRead("Zip.deflate.dd-.zip");
|
||||
await ArchiveStreamReadAsync("Zip.deflate.dd-.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_Deflate_Streamed_ArchiveFileRead()
|
||||
public async ValueTask Zip_Deflate_Streamed_ArchiveFileRead()
|
||||
{
|
||||
ArchiveFileRead("Zip.deflate.dd.zip");
|
||||
await ArchiveStreamReadAsync("Zip.deflate.dd.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_Deflate_ArchiveFileRead()
|
||||
public async ValueTask Zip_Deflate_ArchiveFileRead()
|
||||
{
|
||||
ArchiveFileRead("Zip.deflate.zip");
|
||||
await ArchiveStreamReadAsync("Zip.deflate.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_Deflate64_ArchiveFileRead()
|
||||
public async ValueTask Zip_Deflate64_ArchiveFileRead()
|
||||
{
|
||||
ArchiveFileRead("Zip.deflate64.zip");
|
||||
await ArchiveStreamReadAsync("Zip.deflate64.zip");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_LZMA_Streamed_ArchiveFileRead()
|
||||
public async ValueTask Zip_LZMA_Streamed_ArchiveFileRead()
|
||||
{
|
||||
ArchiveFileRead("Zip.lzma.dd.zip");
|
||||
await ArchiveStreamReadAsync("Zip.lzma.dd.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_LZMA_ArchiveFileRead()
|
||||
public async ValueTask Zip_LZMA_ArchiveFileRead()
|
||||
{
|
||||
ArchiveFileRead("Zip.lzma.zip");
|
||||
await ArchiveStreamReadAsync("Zip.lzma.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_PPMd_Streamed_ArchiveFileRead()
|
||||
public async ValueTask Zip_PPMd_Streamed_ArchiveFileRead()
|
||||
{
|
||||
ArchiveFileRead("Zip.ppmd.dd.zip");
|
||||
await ArchiveStreamReadAsync("Zip.ppmd.dd.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_PPMd_ArchiveFileRead()
|
||||
public async ValueTask Zip_PPMd_ArchiveFileRead()
|
||||
{
|
||||
ArchiveFileRead("Zip.ppmd.zip");
|
||||
await ArchiveStreamReadAsync("Zip.ppmd.zip");
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_None_ArchiveFileRead()
|
||||
public async ValueTask Zip_None_ArchiveFileRead()
|
||||
{
|
||||
ArchiveFileRead("Zip.none.zip");
|
||||
await ArchiveStreamReadAsync("Zip.none.zip");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_Zip64_ArchiveStreamRead()
|
||||
public async ValueTask Zip_Zip64_ArchiveStreamRead()
|
||||
{
|
||||
ArchiveStreamRead("Zip.zip64.zip");
|
||||
await ArchiveStreamReadAsync("Zip.zip64.zip");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_Zip64_ArchiveFileRead()
|
||||
public async ValueTask Zip_Zip64_ArchiveFileRead()
|
||||
{
|
||||
ArchiveFileRead("Zip.zip64.zip");
|
||||
await ArchiveStreamReadAsync("Zip.zip64.zip");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Zip_Random_Write_Remove()
|
||||
public async ValueTask Zip_Random_Write_Remove()
|
||||
{
|
||||
string scratchPath = Path.Combine(SCRATCH_FILES_PATH, "Zip.deflate.mod.zip");
|
||||
string unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip");
|
||||
@@ -169,7 +169,7 @@ namespace SharpCompress.Test.Zip
|
||||
|
||||
await archive.SaveToAsync(scratchPath, writerOptions);
|
||||
}
|
||||
CompareArchivesByPath(modified, scratchPath, Encoding.GetEncoding(866));
|
||||
await CompareArchivesByPathAsync(modified, scratchPath, Encoding.GetEncoding(866));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -189,7 +189,7 @@ namespace SharpCompress.Test.Zip
|
||||
|
||||
await archive.SaveToAsync(scratchPath, writerOptions);
|
||||
}
|
||||
CompareArchivesByPath(modified, scratchPath, Encoding.GetEncoding(866));
|
||||
await CompareArchivesByPathAsync(modified, scratchPath, Encoding.GetEncoding(866));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -211,20 +211,16 @@ namespace SharpCompress.Test.Zip
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_Removal_Poly()
|
||||
public async ValueTask Zip_Removal_Poly()
|
||||
{
|
||||
|
||||
string scratchPath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip");
|
||||
|
||||
|
||||
|
||||
using (ZipArchive vfs = (ZipArchive)ArchiveFactory.Open(scratchPath))
|
||||
{
|
||||
var e = vfs.Entries.First(v => v.Key.EndsWith("jpg"));
|
||||
vfs.RemoveEntry(e);
|
||||
Assert.Null(vfs.Entries.FirstOrDefault(v => v.Key.EndsWith("jpg")));
|
||||
Assert.Null(((IArchive)vfs).Entries.FirstOrDefault(v => v.Key.EndsWith("jpg")));
|
||||
}
|
||||
using ZipArchive vfs = (ZipArchive)await ArchiveFactory.OpenAsync(scratchPath);
|
||||
var e = vfs.Entries.First(v => v.Key.EndsWith("jpg"));
|
||||
vfs.RemoveEntry(e);
|
||||
Assert.Null(vfs.Entries.FirstOrDefault(v => v.Key.EndsWith("jpg")));
|
||||
Assert.Null(((IArchive)vfs).Entries.FirstOrDefault(v => v.Key.EndsWith("jpg")));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -287,7 +283,7 @@ namespace SharpCompress.Test.Zip
|
||||
|
||||
await archive.SaveToAsync(scratchPath, writerOptions);
|
||||
}
|
||||
CompareArchivesByPath(unmodified, scratchPath, Encoding.GetEncoding(866));
|
||||
await CompareArchivesByPathAsync(unmodified, scratchPath, Encoding.GetEncoding(866));
|
||||
Directory.Delete(SCRATCH_FILES_PATH, true);
|
||||
}
|
||||
|
||||
@@ -321,7 +317,7 @@ namespace SharpCompress.Test.Zip
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_Deflate_WinzipAES_Read()
|
||||
public async ValueTask Zip_Deflate_WinzipAES_Read()
|
||||
{
|
||||
using (var reader = ZipArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.WinzipAES.zip"), new ReaderOptions()
|
||||
{
|
||||
@@ -330,7 +326,7 @@ namespace SharpCompress.Test.Zip
|
||||
{
|
||||
foreach (var entry in reader.Entries.Where(x => !x.IsDirectory))
|
||||
{
|
||||
entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions()
|
||||
await entry.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH, new ExtractionOptions()
|
||||
{
|
||||
ExtractFullPath = true,
|
||||
Overwrite = true
|
||||
@@ -375,7 +371,7 @@ namespace SharpCompress.Test.Zip
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_BZip2_Pkware_Read()
|
||||
public async ValueTask Zip_BZip2_Pkware_Read()
|
||||
{
|
||||
using (var reader = ZipArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.pkware.zip"), new ReaderOptions()
|
||||
{
|
||||
@@ -384,7 +380,7 @@ namespace SharpCompress.Test.Zip
|
||||
{
|
||||
foreach (var entry in reader.Entries.Where(x => !x.IsDirectory))
|
||||
{
|
||||
entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions()
|
||||
await entry.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH, new ExtractionOptions()
|
||||
{
|
||||
ExtractFullPath = true,
|
||||
Overwrite = true
|
||||
@@ -438,45 +434,41 @@ namespace SharpCompress.Test.Zip
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_Deflate_PKWear_Multipy_Entry_Access()
|
||||
public async ValueTask Zip_Deflate_PKWear_Multipy_Entry_Access()
|
||||
{
|
||||
string zipFile = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.pkware.zip");
|
||||
|
||||
using (FileStream fileStream = File.Open(zipFile, FileMode.Open))
|
||||
await using FileStream fileStream = File.Open(zipFile, FileMode.Open);
|
||||
using IArchive archive = await ArchiveFactory.OpenAsync(fileStream, new ReaderOptions { Password = "12345678" });
|
||||
var entries = archive.Entries.Where(entry => !entry.IsDirectory);
|
||||
foreach (IArchiveEntry entry in entries)
|
||||
{
|
||||
using (IArchive archive = ArchiveFactory.Open(fileStream, new ReaderOptions { Password = "12345678" }))
|
||||
for (var i = 0; i < 100; i++)
|
||||
{
|
||||
var entries = archive.Entries.Where(entry => !entry.IsDirectory);
|
||||
foreach (IArchiveEntry entry in entries)
|
||||
await using (var memoryStream = new MemoryStream())
|
||||
await using (Stream entryStream = entry.OpenEntryStream())
|
||||
{
|
||||
for (var i = 0; i < 100; i++)
|
||||
{
|
||||
using (var memoryStream = new MemoryStream())
|
||||
using (Stream entryStream = entry.OpenEntryStream())
|
||||
{
|
||||
entryStream.CopyTo(memoryStream);
|
||||
}
|
||||
}
|
||||
await entryStream.CopyToAsync(memoryStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[SkippableFact]
|
||||
public void Zip_Evil_Throws_Exception()
|
||||
public async ValueTask Zip_Evil_Throws_Exception()
|
||||
{
|
||||
//windows only because of the paths
|
||||
Skip.IfNot(Environment.OSVersion.Platform == PlatformID.Win32NT);
|
||||
|
||||
string zipFile = Path.Combine(TEST_ARCHIVES_PATH, "Zip.Evil.zip");
|
||||
|
||||
Assert.ThrowsAny<Exception>(() =>
|
||||
await Assert.ThrowsAnyAsync<Exception>(async () =>
|
||||
{
|
||||
using (var archive = ZipArchive.Open(zipFile))
|
||||
{
|
||||
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
|
||||
{
|
||||
entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions()
|
||||
await entry.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH, new ExtractionOptions()
|
||||
{
|
||||
ExtractFullPath = true,
|
||||
Overwrite = true
|
||||
|
||||
@@ -19,21 +19,21 @@ namespace SharpCompress.Test.Zip
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Issue_269_Double_Skip()
|
||||
public async ValueTask Issue_269_Double_Skip()
|
||||
{
|
||||
var path = Path.Combine(TEST_ARCHIVES_PATH, "PrePostHeaders.zip");
|
||||
using (Stream stream = new ForwardOnlyStream(File.OpenRead(path)))
|
||||
using (IReader reader = ReaderFactory.Open(stream))
|
||||
await using (Stream stream = new ForwardOnlyStream(File.OpenRead(path)))
|
||||
await using (IReader reader = await ReaderFactory.OpenAsync(stream))
|
||||
{
|
||||
int count = 0;
|
||||
while (reader.MoveToNextEntry())
|
||||
while (await reader.MoveToNextEntry())
|
||||
{
|
||||
count++;
|
||||
if (!reader.Entry.IsDirectory)
|
||||
{
|
||||
if (count % 2 != 0)
|
||||
{
|
||||
reader.WriteEntryTo(Stream.Null);
|
||||
await reader.WriteEntryToAsync(Stream.Null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,52 +41,52 @@ namespace SharpCompress.Test.Zip
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_Zip64_Streamed_Read()
|
||||
public async ValueTask Zip_Zip64_Streamed_Read()
|
||||
{
|
||||
Read("Zip.zip64.zip", CompressionType.Deflate);
|
||||
await ReadAsync("Zip.zip64.zip", CompressionType.Deflate);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_ZipX_Streamed_Read()
|
||||
public async ValueTask Zip_ZipX_Streamed_Read()
|
||||
{
|
||||
Read("Zip.zipx", CompressionType.LZMA);
|
||||
await ReadAsync("Zip.zipx", CompressionType.LZMA);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_BZip2_Streamed_Read()
|
||||
public async ValueTask Zip_BZip2_Streamed_Read()
|
||||
{
|
||||
Read("Zip.bzip2.dd.zip", CompressionType.BZip2);
|
||||
await ReadAsync("Zip.bzip2.dd.zip", CompressionType.BZip2);
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_BZip2_Read()
|
||||
public async ValueTask Zip_BZip2_Read()
|
||||
{
|
||||
Read("Zip.bzip2.zip", CompressionType.BZip2);
|
||||
await ReadAsync("Zip.bzip2.zip", CompressionType.BZip2);
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_Deflate_Streamed2_Read()
|
||||
public async ValueTask Zip_Deflate_Streamed2_Read()
|
||||
{
|
||||
Read("Zip.deflate.dd-.zip", CompressionType.Deflate);
|
||||
await ReadAsync("Zip.deflate.dd-.zip", CompressionType.Deflate);
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_Deflate_Streamed_Read()
|
||||
public async ValueTask Zip_Deflate_Streamed_Read()
|
||||
{
|
||||
Read("Zip.deflate.dd.zip", CompressionType.Deflate);
|
||||
await ReadAsync("Zip.deflate.dd.zip", CompressionType.Deflate);
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_Deflate_Streamed_Skip()
|
||||
public async ValueTask Zip_Deflate_Streamed_Skip()
|
||||
{
|
||||
using (Stream stream = new ForwardOnlyStream(File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip"))))
|
||||
using (IReader reader = ReaderFactory.Open(stream))
|
||||
await using (Stream stream = new ForwardOnlyStream(File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip"))))
|
||||
await using (IReader reader = await ReaderFactory.OpenAsync(stream))
|
||||
{
|
||||
int x = 0;
|
||||
while (reader.MoveToNextEntry())
|
||||
while (await reader.MoveToNextEntry())
|
||||
{
|
||||
if (!reader.Entry.IsDirectory)
|
||||
{
|
||||
x++;
|
||||
if (x % 2 == 0)
|
||||
{
|
||||
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH,
|
||||
await reader.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH,
|
||||
new ExtractionOptions()
|
||||
{
|
||||
ExtractFullPath = true,
|
||||
@@ -98,64 +98,64 @@ namespace SharpCompress.Test.Zip
|
||||
}
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_Deflate_Read()
|
||||
public async ValueTask Zip_Deflate_Read()
|
||||
{
|
||||
Read("Zip.deflate.zip", CompressionType.Deflate);
|
||||
await ReadAsync("Zip.deflate.zip", CompressionType.Deflate);
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_Deflate64_Read()
|
||||
public async ValueTask Zip_Deflate64_Read()
|
||||
{
|
||||
Read("Zip.deflate64.zip", CompressionType.Deflate64);
|
||||
await ReadAsync("Zip.deflate64.zip", CompressionType.Deflate64);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_LZMA_Streamed_Read()
|
||||
public async ValueTask Zip_LZMA_Streamed_Read()
|
||||
{
|
||||
Read("Zip.lzma.dd.zip", CompressionType.LZMA);
|
||||
await ReadAsync("Zip.lzma.dd.zip", CompressionType.LZMA);
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_LZMA_Read()
|
||||
public async ValueTask Zip_LZMA_Read()
|
||||
{
|
||||
Read("Zip.lzma.zip", CompressionType.LZMA);
|
||||
await ReadAsync("Zip.lzma.zip", CompressionType.LZMA);
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_PPMd_Streamed_Read()
|
||||
public async ValueTask Zip_PPMd_Streamed_Read()
|
||||
{
|
||||
Read("Zip.ppmd.dd.zip", CompressionType.PPMd);
|
||||
await ReadAsync("Zip.ppmd.dd.zip", CompressionType.PPMd);
|
||||
}
|
||||
[Fact]
|
||||
public void Zip_PPMd_Read()
|
||||
public async ValueTask Zip_PPMd_Read()
|
||||
{
|
||||
Read("Zip.ppmd.zip", CompressionType.PPMd);
|
||||
await ReadAsync("Zip.ppmd.zip", CompressionType.PPMd);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_None_Read()
|
||||
public async ValueTask Zip_None_Read()
|
||||
{
|
||||
Read("Zip.none.zip", CompressionType.None);
|
||||
await ReadAsync("Zip.none.zip", CompressionType.None);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_Deflate_NoEmptyDirs_Read()
|
||||
public async ValueTask Zip_Deflate_NoEmptyDirs_Read()
|
||||
{
|
||||
Read("Zip.deflate.noEmptyDirs.zip", CompressionType.Deflate);
|
||||
await ReadAsync("Zip.deflate.noEmptyDirs.zip", CompressionType.Deflate);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_BZip2_PkwareEncryption_Read()
|
||||
public async ValueTask Zip_BZip2_PkwareEncryption_Read()
|
||||
{
|
||||
using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.pkware.zip")))
|
||||
using (var reader = ZipReader.Open(stream, new ReaderOptions()
|
||||
await using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.pkware.zip")))
|
||||
await using (var reader = ZipReader.Open(stream, new ReaderOptions()
|
||||
{
|
||||
Password = "test"
|
||||
}))
|
||||
{
|
||||
while (reader.MoveToNextEntry())
|
||||
while (await reader.MoveToNextEntry())
|
||||
{
|
||||
if (!reader.Entry.IsDirectory)
|
||||
{
|
||||
Assert.Equal(CompressionType.BZip2, reader.Entry.CompressionType);
|
||||
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions()
|
||||
await reader.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH, new ExtractionOptions()
|
||||
{
|
||||
ExtractFullPath = true,
|
||||
Overwrite = true
|
||||
@@ -167,17 +167,17 @@ namespace SharpCompress.Test.Zip
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_Reader_Disposal_Test()
|
||||
public async ValueTask Zip_Reader_Disposal_Test()
|
||||
{
|
||||
using (TestStream stream = new TestStream(File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip"))))
|
||||
await using (TestStream stream = new TestStream(File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip"))))
|
||||
{
|
||||
using (var reader = ReaderFactory.Open(stream))
|
||||
await using (var reader = await ReaderFactory.OpenAsync(stream))
|
||||
{
|
||||
while (reader.MoveToNextEntry())
|
||||
while (await reader.MoveToNextEntry())
|
||||
{
|
||||
if (!reader.Entry.IsDirectory)
|
||||
{
|
||||
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH,
|
||||
await reader.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH,
|
||||
new ExtractionOptions()
|
||||
{
|
||||
ExtractFullPath = true,
|
||||
@@ -191,16 +191,16 @@ namespace SharpCompress.Test.Zip
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_Reader_Disposal_Test2()
|
||||
public async ValueTask Zip_Reader_Disposal_Test2()
|
||||
{
|
||||
using (TestStream stream = new TestStream(File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip"))))
|
||||
{
|
||||
var reader = ReaderFactory.Open(stream);
|
||||
while (reader.MoveToNextEntry())
|
||||
var reader = await ReaderFactory.OpenAsync(stream);
|
||||
while (await reader.MoveToNextEntry())
|
||||
{
|
||||
if (!reader.Entry.IsDirectory)
|
||||
{
|
||||
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH,
|
||||
await reader.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH,
|
||||
new ExtractionOptions()
|
||||
{
|
||||
ExtractFullPath = true,
|
||||
@@ -213,25 +213,25 @@ namespace SharpCompress.Test.Zip
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_LZMA_WinzipAES_Read()
|
||||
public async ValueTask Zip_LZMA_WinzipAES_Read()
|
||||
{
|
||||
Assert.Throws<NotSupportedException>(() =>
|
||||
await Assert.ThrowsAsync<NotSupportedException>(async () =>
|
||||
{
|
||||
using (
|
||||
Stream stream =
|
||||
File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH,
|
||||
"Zip.lzma.WinzipAES.zip")))
|
||||
using (var reader = ZipReader.Open(stream, new ReaderOptions()
|
||||
await using (var reader = ZipReader.Open(stream, new ReaderOptions()
|
||||
{
|
||||
Password = "test"
|
||||
}))
|
||||
{
|
||||
while (reader.MoveToNextEntry())
|
||||
while (await reader.MoveToNextEntry())
|
||||
{
|
||||
if (!reader.Entry.IsDirectory)
|
||||
{
|
||||
Assert.Equal(CompressionType.Unknown, reader.Entry.CompressionType);
|
||||
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH,
|
||||
await reader.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH,
|
||||
new ExtractionOptions()
|
||||
{
|
||||
ExtractFullPath = true,
|
||||
@@ -245,20 +245,20 @@ namespace SharpCompress.Test.Zip
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_Deflate_WinzipAES_Read()
|
||||
public async ValueTask Zip_Deflate_WinzipAES_Read()
|
||||
{
|
||||
using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.WinzipAES.zip")))
|
||||
using (var reader = ZipReader.Open(stream, new ReaderOptions()
|
||||
await using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.WinzipAES.zip")))
|
||||
await using (var reader = ZipReader.Open(stream, new ReaderOptions()
|
||||
{
|
||||
Password = "test"
|
||||
}))
|
||||
{
|
||||
while (reader.MoveToNextEntry())
|
||||
while (await reader.MoveToNextEntry())
|
||||
{
|
||||
if (!reader.Entry.IsDirectory)
|
||||
{
|
||||
Assert.Equal(CompressionType.Unknown, reader.Entry.CompressionType);
|
||||
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH,
|
||||
await reader.WriteEntryToDirectoryAsync(SCRATCH_FILES_PATH,
|
||||
new ExtractionOptions()
|
||||
{
|
||||
ExtractFullPath = true,
|
||||
@@ -292,10 +292,10 @@ namespace SharpCompress.Test.Zip
|
||||
stream = new MemoryStream(memory.ToArray());
|
||||
await File.WriteAllBytesAsync(Path.Combine(SCRATCH_FILES_PATH, "foo.zip"), memory.ToArray());
|
||||
|
||||
using (IReader zipReader = ZipReader.Open(new NonDisposingStream(stream, true)))
|
||||
await using (IReader zipReader = ZipReader.Open(new NonDisposingStream(stream, true)))
|
||||
{
|
||||
var i = 0;
|
||||
while (zipReader.MoveToNextEntry())
|
||||
while (await zipReader.MoveToNextEntry())
|
||||
{
|
||||
await using (EntryStream entry = zipReader.OpenEntryStream())
|
||||
{
|
||||
@@ -318,16 +318,16 @@ namespace SharpCompress.Test.Zip
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zip_None_Issue86_Streamed_Read()
|
||||
public async ValueTask Zip_None_Issue86_Streamed_Read()
|
||||
{
|
||||
var keys = new string[] { "Empty1", "Empty2", "Dir1/", "Dir2/", "Fake1", "Fake2", "Internal.zip" };
|
||||
|
||||
using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.none.issue86.zip")))
|
||||
using (var reader = ZipReader.Open(stream))
|
||||
await using (var reader = ZipReader.Open(stream))
|
||||
{
|
||||
foreach (var key in keys)
|
||||
{
|
||||
reader.MoveToNextEntry();
|
||||
await reader.MoveToNextEntry();
|
||||
|
||||
Assert.Equal(reader.Entry.Key, key);
|
||||
|
||||
@@ -337,7 +337,7 @@ namespace SharpCompress.Test.Zip
|
||||
}
|
||||
}
|
||||
|
||||
Assert.False(reader.MoveToNextEntry());
|
||||
Assert.False(await reader.MoveToNextEntry());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user