mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-02-04 05:25:00 +00:00
test fixes and fmt
This commit is contained in:
@@ -12,7 +12,8 @@ namespace SharpCompress.Archives;
|
||||
|
||||
public abstract class AbstractWritableArchive<TEntry, TVolume>
|
||||
: AbstractArchive<TEntry, TVolume>,
|
||||
IWritableArchive, IWritableAsyncArchive
|
||||
IWritableArchive,
|
||||
IWritableAsyncArchive
|
||||
where TEntry : IArchiveEntry
|
||||
where TVolume : IVolume
|
||||
{
|
||||
|
||||
@@ -28,13 +28,11 @@ public interface IWritableArchiveCommon
|
||||
|
||||
public interface IWritableArchive : IArchive, IWritableArchiveCommon
|
||||
{
|
||||
|
||||
void SaveTo(Stream stream, WriterOptions options);
|
||||
}
|
||||
|
||||
public interface IWritableAsyncArchive : IAsyncArchive, IWritableArchiveCommon
|
||||
{
|
||||
|
||||
ValueTask SaveToAsync(
|
||||
Stream stream,
|
||||
WriterOptions options,
|
||||
|
||||
@@ -6,12 +6,11 @@ namespace SharpCompress.Archives;
|
||||
|
||||
public static class IWritableArchiveExtensions
|
||||
{
|
||||
|
||||
public static void SaveTo(
|
||||
this IWritableArchive writableArchive,
|
||||
string filePath,
|
||||
WriterOptions? options = null
|
||||
) => writableArchive.SaveTo(new FileInfo(filePath), options ?? new (CompressionType.Deflate));
|
||||
) => writableArchive.SaveTo(new FileInfo(filePath), options ?? new(CompressionType.Deflate));
|
||||
|
||||
public static void SaveTo(
|
||||
this IWritableArchive writableArchive,
|
||||
@@ -20,6 +19,6 @@ public static class IWritableArchiveExtensions
|
||||
)
|
||||
{
|
||||
using var stream = fileInfo.Open(FileMode.Create, FileAccess.Write);
|
||||
writableArchive.SaveTo(stream, options?? new (CompressionType.Deflate));
|
||||
writableArchive.SaveTo(stream, options ?? new(CompressionType.Deflate));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,12 @@ public static class IWritableAsyncArchiveExtensions
|
||||
string filePath,
|
||||
WriterOptions? options = null,
|
||||
CancellationToken cancellationToken = default
|
||||
) => writableArchive.SaveToAsync(new FileInfo(filePath), options ?? new (CompressionType.Deflate), cancellationToken);
|
||||
) =>
|
||||
writableArchive.SaveToAsync(
|
||||
new FileInfo(filePath),
|
||||
options ?? new(CompressionType.Deflate),
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
public static async ValueTask SaveToAsync(
|
||||
this IWritableAsyncArchive writableArchive,
|
||||
@@ -23,7 +28,8 @@ public static class IWritableAsyncArchiveExtensions
|
||||
)
|
||||
{
|
||||
using var stream = fileInfo.Open(FileMode.Create, FileAccess.Write);
|
||||
await writableArchive.SaveToAsync(stream, options?? new (CompressionType.Deflate), cancellationToken).ConfigureAwait(false);
|
||||
await writableArchive
|
||||
.SaveToAsync(stream, options ?? new(CompressionType.Deflate), cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ public static class RarArchiveExtensions
|
||||
public static bool IsMultipartVolume(this IRarArchive archive) =>
|
||||
archive.Volumes.Cast<RarVolume>().First().IsMultiVolume;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// RarArchive is the first volume of a multi-part archive. If MultipartVolume is true and IsFirstVolume is false then the first volume file must be missing.
|
||||
/// </summary>
|
||||
|
||||
@@ -16,19 +16,13 @@ namespace SharpCompress.Archives.Rar;
|
||||
|
||||
public interface IRarArchiveCommon
|
||||
{
|
||||
|
||||
|
||||
int MinVersion { get; }
|
||||
int MaxVersion { get; }
|
||||
}
|
||||
|
||||
public interface IRarArchive : IArchive, IRarArchiveCommon
|
||||
{
|
||||
}
|
||||
public interface IRarArchive : IArchive, IRarArchiveCommon { }
|
||||
|
||||
public interface IRarAsyncArchive : IAsyncArchive, IRarArchiveCommon
|
||||
{
|
||||
}
|
||||
public interface IRarAsyncArchive : IAsyncArchive, IRarArchiveCommon { }
|
||||
|
||||
public class RarArchive : AbstractArchive<RarArchiveEntry, RarVolume>, IRarArchive
|
||||
{
|
||||
|
||||
@@ -71,7 +71,10 @@ public class TarArchive : AbstractWritableArchive<TarArchiveEntry, TarVolume>
|
||||
/// </summary>
|
||||
/// <param name="streams"></param>
|
||||
/// <param name="readerOptions"></param>
|
||||
public static IWritableArchive Open(IEnumerable<Stream> streams, ReaderOptions? readerOptions = null)
|
||||
public static IWritableArchive Open(
|
||||
IEnumerable<Stream> streams,
|
||||
ReaderOptions? readerOptions = null
|
||||
)
|
||||
{
|
||||
streams.NotNull(nameof(streams));
|
||||
var strms = streams.ToArray();
|
||||
|
||||
@@ -92,7 +92,10 @@ public class ZipArchive : AbstractWritableArchive<ZipArchiveEntry, ZipVolume>
|
||||
/// </summary>
|
||||
/// <param name="streams"></param>
|
||||
/// <param name="readerOptions"></param>
|
||||
public static IWritableArchive Open(IEnumerable<Stream> streams, ReaderOptions? readerOptions = null)
|
||||
public static IWritableArchive Open(
|
||||
IEnumerable<Stream> streams,
|
||||
ReaderOptions? readerOptions = null
|
||||
)
|
||||
{
|
||||
streams.NotNull(nameof(streams));
|
||||
var strms = streams.ToArray();
|
||||
|
||||
@@ -126,10 +126,12 @@ public static class AsyncEnumerableExtensions
|
||||
{
|
||||
throw new InvalidOperationException("The source sequence is empty.");
|
||||
}
|
||||
var value = enumerator.Current;
|
||||
var value = enumerator.Current;
|
||||
if (await enumerator.MoveNextAsync())
|
||||
{
|
||||
throw new InvalidOperationException("The source sequence contains more than one element.");
|
||||
throw new InvalidOperationException(
|
||||
"The source sequence contains more than one element."
|
||||
);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -601,10 +601,7 @@ public class ArchiveTests : ReaderTests
|
||||
)
|
||||
)
|
||||
await using (
|
||||
var archive = archiveFactory.OpenAsync(
|
||||
new AsyncOnlyStream(stream),
|
||||
readerOptions
|
||||
)
|
||||
var archive = archiveFactory.OpenAsync(new AsyncOnlyStream(stream), readerOptions)
|
||||
)
|
||||
{
|
||||
try
|
||||
|
||||
@@ -78,8 +78,7 @@ public class GZipArchiveAsyncTests : ArchiveTests
|
||||
#endif
|
||||
await using (var archive = GZipArchive.OpenAsync(stream))
|
||||
{
|
||||
Assert.Throws<InvalidFormatException>(() =>
|
||||
archive.AddEntry("jpg\\test.jpg", jpg));
|
||||
Assert.Throws<InvalidFormatException>(() => archive.AddEntry("jpg\\test.jpg", jpg));
|
||||
await archive.SaveToAsync(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,61 +36,61 @@ public class RarReaderAsyncTests : ReaderTests
|
||||
"Rar5.multi.part06.rar",
|
||||
]);
|
||||
|
||||
private async ValueTask DoRar_Multi_Reader_Async(string[] archives)
|
||||
{
|
||||
using (
|
||||
IReader baseReader = RarReader.Open(
|
||||
archives
|
||||
.Select(s => Path.Combine(TEST_ARCHIVES_PATH, s))
|
||||
.Select(p => File.OpenRead(p))
|
||||
)
|
||||
)
|
||||
{
|
||||
IAsyncReader reader = (IAsyncReader)baseReader;
|
||||
while (await reader.MoveToNextEntryAsync())
|
||||
{
|
||||
await reader.WriteEntryToDirectoryAsync(
|
||||
SCRATCH_FILES_PATH,
|
||||
new ExtractionOptions { ExtractFullPath = true, Overwrite = true }
|
||||
);
|
||||
}
|
||||
}
|
||||
VerifyFiles();
|
||||
}
|
||||
private async ValueTask DoRar_Multi_Reader_Async(string[] archives)
|
||||
{
|
||||
using (
|
||||
IReader baseReader = RarReader.Open(
|
||||
archives
|
||||
.Select(s => Path.Combine(TEST_ARCHIVES_PATH, s))
|
||||
.Select(p => File.OpenRead(p))
|
||||
)
|
||||
)
|
||||
{
|
||||
IAsyncReader reader = (IAsyncReader)baseReader;
|
||||
while (await reader.MoveToNextEntryAsync())
|
||||
{
|
||||
await reader.WriteEntryToDirectoryAsync(
|
||||
SCRATCH_FILES_PATH,
|
||||
new ExtractionOptions { ExtractFullPath = true, Overwrite = true }
|
||||
);
|
||||
}
|
||||
}
|
||||
VerifyFiles();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async ValueTask Rar_Multi_Reader_Encrypted_Async() =>
|
||||
await Assert.ThrowsAsync<InvalidFormatException>(async () =>
|
||||
{
|
||||
string[] archives =
|
||||
[
|
||||
"Rar.EncryptedParts.part01.rar",
|
||||
"Rar.EncryptedParts.part02.rar",
|
||||
"Rar.EncryptedParts.part03.rar",
|
||||
"Rar.EncryptedParts.part04.rar",
|
||||
"Rar.EncryptedParts.part05.rar",
|
||||
"Rar.EncryptedParts.part06.rar",
|
||||
];
|
||||
using (
|
||||
IReader baseReader = RarReader.Open(
|
||||
archives
|
||||
.Select(s => Path.Combine(TEST_ARCHIVES_PATH, s))
|
||||
.Select(p => File.OpenRead(p)),
|
||||
new ReaderOptions { Password = "test" }
|
||||
)
|
||||
)
|
||||
{
|
||||
IAsyncReader reader = (IAsyncReader)baseReader;
|
||||
while (await reader.MoveToNextEntryAsync())
|
||||
{
|
||||
await reader.WriteEntryToDirectoryAsync(
|
||||
SCRATCH_FILES_PATH,
|
||||
new ExtractionOptions { ExtractFullPath = true, Overwrite = true }
|
||||
);
|
||||
}
|
||||
}
|
||||
VerifyFiles();
|
||||
});
|
||||
[Fact]
|
||||
public async ValueTask Rar_Multi_Reader_Encrypted_Async() =>
|
||||
await Assert.ThrowsAsync<InvalidFormatException>(async () =>
|
||||
{
|
||||
string[] archives =
|
||||
[
|
||||
"Rar.EncryptedParts.part01.rar",
|
||||
"Rar.EncryptedParts.part02.rar",
|
||||
"Rar.EncryptedParts.part03.rar",
|
||||
"Rar.EncryptedParts.part04.rar",
|
||||
"Rar.EncryptedParts.part05.rar",
|
||||
"Rar.EncryptedParts.part06.rar",
|
||||
];
|
||||
using (
|
||||
IReader baseReader = RarReader.Open(
|
||||
archives
|
||||
.Select(s => Path.Combine(TEST_ARCHIVES_PATH, s))
|
||||
.Select(p => File.OpenRead(p)),
|
||||
new ReaderOptions { Password = "test" }
|
||||
)
|
||||
)
|
||||
{
|
||||
IAsyncReader reader = (IAsyncReader)baseReader;
|
||||
while (await reader.MoveToNextEntryAsync())
|
||||
{
|
||||
await reader.WriteEntryToDirectoryAsync(
|
||||
SCRATCH_FILES_PATH,
|
||||
new ExtractionOptions { ExtractFullPath = true, Overwrite = true }
|
||||
);
|
||||
}
|
||||
}
|
||||
VerifyFiles();
|
||||
});
|
||||
|
||||
[Fact]
|
||||
public async ValueTask Rar_Multi_Reader_Delete_Files_Async() =>
|
||||
@@ -123,31 +123,31 @@ public class RarReaderAsyncTests : ReaderTests
|
||||
Path.Combine(SCRATCH2_FILES_PATH, file)
|
||||
);
|
||||
}
|
||||
var streams = archives
|
||||
.Select(s => Path.Combine(SCRATCH2_FILES_PATH, s))
|
||||
.Select(File.OpenRead)
|
||||
.ToList();
|
||||
using (IReader baseReader = RarReader.Open(streams))
|
||||
{
|
||||
IAsyncReader reader = (IAsyncReader)baseReader;
|
||||
while (await reader.MoveToNextEntryAsync())
|
||||
{
|
||||
await reader.WriteEntryToDirectoryAsync(
|
||||
SCRATCH_FILES_PATH,
|
||||
new ExtractionOptions { ExtractFullPath = true, Overwrite = true }
|
||||
);
|
||||
}
|
||||
}
|
||||
foreach (var stream in streams)
|
||||
{
|
||||
stream.Dispose();
|
||||
}
|
||||
VerifyFiles();
|
||||
var streams = archives
|
||||
.Select(s => Path.Combine(SCRATCH2_FILES_PATH, s))
|
||||
.Select(File.OpenRead)
|
||||
.ToList();
|
||||
using (IReader baseReader = RarReader.Open(streams))
|
||||
{
|
||||
IAsyncReader reader = (IAsyncReader)baseReader;
|
||||
while (await reader.MoveToNextEntryAsync())
|
||||
{
|
||||
await reader.WriteEntryToDirectoryAsync(
|
||||
SCRATCH_FILES_PATH,
|
||||
new ExtractionOptions { ExtractFullPath = true, Overwrite = true }
|
||||
);
|
||||
}
|
||||
}
|
||||
foreach (var stream in streams)
|
||||
{
|
||||
stream.Dispose();
|
||||
}
|
||||
VerifyFiles();
|
||||
|
||||
foreach (var file in archives.Select(s => Path.Combine(SCRATCH2_FILES_PATH, s)))
|
||||
{
|
||||
File.Delete(file);
|
||||
}
|
||||
foreach (var file in archives.Select(s => Path.Combine(SCRATCH2_FILES_PATH, s)))
|
||||
{
|
||||
File.Delete(file);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -274,24 +274,26 @@ public class RarReaderAsyncTests : ReaderTests
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async ValueTask Rar_Jpg_Reader_Async()
|
||||
{
|
||||
using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.jpeg.jpg")))
|
||||
using (IReader baseReader = RarReader.Open(stream, new ReaderOptions { LookForHeader = true }))
|
||||
{
|
||||
IAsyncReader reader = (IAsyncReader)baseReader;
|
||||
while (await reader.MoveToNextEntryAsync())
|
||||
{
|
||||
Assert.Equal(CompressionType.Rar, reader.Entry.CompressionType);
|
||||
await reader.WriteEntryToDirectoryAsync(
|
||||
SCRATCH_FILES_PATH,
|
||||
new ExtractionOptions { ExtractFullPath = true, Overwrite = true }
|
||||
);
|
||||
}
|
||||
}
|
||||
VerifyFiles();
|
||||
}
|
||||
[Fact]
|
||||
public async ValueTask Rar_Jpg_Reader_Async()
|
||||
{
|
||||
using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.jpeg.jpg")))
|
||||
using (
|
||||
IReader baseReader = RarReader.Open(stream, new ReaderOptions { LookForHeader = true })
|
||||
)
|
||||
{
|
||||
IAsyncReader reader = (IAsyncReader)baseReader;
|
||||
while (await reader.MoveToNextEntryAsync())
|
||||
{
|
||||
Assert.Equal(CompressionType.Rar, reader.Entry.CompressionType);
|
||||
await reader.WriteEntryToDirectoryAsync(
|
||||
SCRATCH_FILES_PATH,
|
||||
new ExtractionOptions { ExtractFullPath = true, Overwrite = true }
|
||||
);
|
||||
}
|
||||
}
|
||||
VerifyFiles();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async ValueTask Rar_Solid_Reader_Async() =>
|
||||
|
||||
@@ -180,7 +180,7 @@ public class Zip64Tests : WriterTests
|
||||
{
|
||||
long count = 0;
|
||||
long size = 0;
|
||||
ZipEntry? prev = null;
|
||||
IEntry? prev = null;
|
||||
using (var fs = File.OpenRead(filename))
|
||||
using (var rd = ZipReader.Open(fs, new ReaderOptions { LookForHeader = false }))
|
||||
{
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Text;
|
||||
using SharpCompress.Archives;
|
||||
using SharpCompress.Archives.Zip;
|
||||
using SharpCompress.Common;
|
||||
using SharpCompress.Common.Zip;
|
||||
using SharpCompress.Readers;
|
||||
using SharpCompress.Writers;
|
||||
using SharpCompress.Writers.Zip;
|
||||
@@ -499,7 +500,7 @@ public class ZipArchiveTests : ArchiveTests
|
||||
|
||||
var expectedComment =
|
||||
"Encoding:utf-8 || Compression:Deflate levelDefault || Encrypt:None || ZIP64:Always\r\nCreated at 2017-Jan-23 14:10:43 || DotNetZip Tool v1.9.1.8\r\nTest zip64 archive";
|
||||
Assert.Equal(expectedComment, reader.Volumes.First().Comment);
|
||||
Assert.Equal(expectedComment, ((ZipVolume)reader.Volumes.First()).Comment);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -824,7 +825,7 @@ public class ZipArchiveTests : ArchiveTests
|
||||
using var archive = ZipArchive.Open(
|
||||
Path.Combine(TEST_ARCHIVES_PATH, "Zip.EntryComment.zip")
|
||||
);
|
||||
var firstEntry = archive.Entries.First();
|
||||
var firstEntry = (ZipArchiveEntry)archive.Entries.First();
|
||||
Assert.Equal(29, firstEntry.Comment!.Length);
|
||||
using var _ = firstEntry.OpenEntryStream();
|
||||
Assert.Equal(29, firstEntry.Comment.Length);
|
||||
|
||||
Reference in New Issue
Block a user