Files
sharpcompress/tests/SharpCompress.Test/WriterTests.cs

110 lines
3.5 KiB
C#
Raw Permalink Normal View History

2022-06-19 22:05:52 +02:00
using System.IO;
using System.Text;
2025-10-27 10:46:08 +00:00
using System.Threading;
using System.Threading.Tasks;
2015-12-30 11:19:42 +00:00
using SharpCompress.Common;
using SharpCompress.IO;
2016-09-26 11:49:49 +01:00
using SharpCompress.Readers;
2026-01-07 15:01:04 +00:00
using SharpCompress.Test.Mocks;
2016-09-26 11:49:49 +01:00
using SharpCompress.Writers;
2015-12-30 11:19:42 +00:00
2022-12-20 15:06:44 +00:00
namespace SharpCompress.Test;
public class WriterTests : TestBase
2015-12-30 11:19:42 +00:00
{
2024-03-14 08:53:08 +00:00
private readonly ArchiveType _type;
2015-12-30 11:19:42 +00:00
2024-03-14 08:53:08 +00:00
protected WriterTests(ArchiveType type) => _type = type;
2015-12-30 11:19:42 +00:00
2022-12-20 15:06:44 +00:00
protected void Write(
CompressionType compressionType,
string archive,
string archiveToVerifyAgainst,
Encoding? encoding = null
)
{
using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive)))
2015-12-30 11:19:42 +00:00
{
2025-01-14 09:07:40 +00:00
var writerOptions = new WriterOptions(compressionType) { LeaveStreamOpen = true };
2020-07-26 14:36:07 +01:00
2022-12-20 15:06:44 +00:00
writerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default;
2021-01-09 13:33:34 +00:00
2026-01-15 11:55:56 +00:00
using var writer = WriterFactory.OpenWriter(stream, _type, writerOptions);
2022-12-20 15:06:44 +00:00
writer.WriteAll(ORIGINAL_FILES_PATH, "*", SearchOption.AllDirectories);
}
CompareArchivesByPath(
Path.Combine(SCRATCH2_FILES_PATH, archive),
Path.Combine(TEST_ARCHIVES_PATH, archiveToVerifyAgainst)
);
2015-12-30 11:19:42 +00:00
2022-12-20 15:06:44 +00:00
using (Stream stream = File.OpenRead(Path.Combine(SCRATCH2_FILES_PATH, archive)))
{
var readerOptions = new ReaderOptions();
2020-07-26 14:36:07 +01:00
2022-12-20 15:06:44 +00:00
readerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default;
2021-01-09 13:33:34 +00:00
2026-01-31 15:53:52 +00:00
using var reader = ReaderFactory.OpenReader(
SharpCompressStream.CreateNonDisposing(stream),
2026-01-31 15:53:52 +00:00
readerOptions
);
reader.WriteAllToDirectory(SCRATCH_FILES_PATH);
2015-12-30 11:19:42 +00:00
}
2022-12-20 15:06:44 +00:00
VerifyFiles();
2015-12-30 11:19:42 +00:00
}
2025-10-27 10:46:08 +00:00
protected async Task WriteAsync(
CompressionType compressionType,
string archive,
string archiveToVerifyAgainst,
Encoding? encoding = null,
CancellationToken cancellationToken = default
)
{
2026-01-07 16:38:48 +00:00
using (
Stream stream = new AsyncOnlyStream(
File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive))
)
)
2025-10-27 10:46:08 +00:00
{
var writerOptions = new WriterOptions(compressionType) { LeaveStreamOpen = true };
writerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default;
2026-02-22 13:19:04 +00:00
await using var writer = await WriterFactory.OpenAsyncWriter(
2026-02-16 10:58:01 +00:00
stream,
_type,
writerOptions,
cancellationToken
);
2025-10-27 10:46:08 +00:00
await writer.WriteAllAsync(
ORIGINAL_FILES_PATH,
"*",
SearchOption.AllDirectories,
cancellationToken
);
}
CompareArchivesByPath(
Path.Combine(SCRATCH2_FILES_PATH, archive),
Path.Combine(TEST_ARCHIVES_PATH, archiveToVerifyAgainst)
);
using (Stream stream = File.OpenRead(Path.Combine(SCRATCH2_FILES_PATH, archive)))
{
var readerOptions = new ReaderOptions();
readerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default;
await using var reader = await ReaderFactory.OpenAsyncReader(
new AsyncOnlyStream(SharpCompressStream.CreateNonDisposing(stream)),
readerOptions,
cancellationToken
2025-10-27 10:46:08 +00:00
);
await reader.WriteAllToDirectoryAsync(
SCRATCH_FILES_PATH,
cancellationToken: cancellationToken
);
2025-10-27 10:46:08 +00:00
}
VerifyFiles();
}
2015-12-30 11:19:42 +00:00
}