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

55 lines
1.7 KiB
C#
Raw Permalink Normal View History

2022-06-19 22:05:52 +02:00
using System.IO;
using System.Text;
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;
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
2024-03-14 08:53:08 +00:00
using var writer = WriterFactory.Open(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
2025-07-20 18:11:37 +01:00
using var reader = ReaderFactory.Open(
SharpCompressStream.Create(stream, leaveOpen: true),
readerOptions
);
2022-12-20 15:06:44 +00:00
reader.WriteAllToDirectory(
SCRATCH_FILES_PATH,
2024-03-14 08:37:17 +00:00
new ExtractionOptions { ExtractFullPath = true }
2022-12-20 15:06:44 +00:00
);
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
}
}