mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-09-22 15:04:43 +00:00
big clean up
This commit is contained in:
@@ -6,116 +6,101 @@ using SharpCompress.Compressors.Deflate;
|
||||
using SharpCompress.IO;
|
||||
using Xunit;
|
||||
|
||||
namespace SharpCompress.Test.Streams
|
||||
namespace SharpCompress.Test.Streams;
|
||||
|
||||
public class ZLibBaseStreamTests
|
||||
{
|
||||
public class ZLibBaseStreamTests
|
||||
[Fact]
|
||||
public void TestChunkedZlibCompressesEverything()
|
||||
{
|
||||
[Fact]
|
||||
public void TestChunkedZlibCompressesEverything()
|
||||
var plainData = new byte[]
|
||||
{
|
||||
byte[] plainData = new byte[]
|
||||
{
|
||||
0xf7,
|
||||
0x1b,
|
||||
0xda,
|
||||
0x0f,
|
||||
0xb6,
|
||||
0x2b,
|
||||
0x3d,
|
||||
0x91,
|
||||
0xd7,
|
||||
0xe1,
|
||||
0xb5,
|
||||
0x11,
|
||||
0x34,
|
||||
0x5a,
|
||||
0x51,
|
||||
0x3f,
|
||||
0x8b,
|
||||
0xce,
|
||||
0x49,
|
||||
0xd2
|
||||
};
|
||||
byte[] buf = new byte[plainData.Length * 2];
|
||||
0xf7,
|
||||
0x1b,
|
||||
0xda,
|
||||
0x0f,
|
||||
0xb6,
|
||||
0x2b,
|
||||
0x3d,
|
||||
0x91,
|
||||
0xd7,
|
||||
0xe1,
|
||||
0xb5,
|
||||
0x11,
|
||||
0x34,
|
||||
0x5a,
|
||||
0x51,
|
||||
0x3f,
|
||||
0x8b,
|
||||
0xce,
|
||||
0x49,
|
||||
0xd2
|
||||
};
|
||||
var buf = new byte[plainData.Length * 2];
|
||||
|
||||
MemoryStream plainStream1 = new MemoryStream(plainData);
|
||||
DeflateStream compressor1 = new DeflateStream(plainStream1, CompressionMode.Compress);
|
||||
// This is enough to read the entire data
|
||||
int realCompressedSize = compressor1.Read(buf, 0, plainData.Length * 2);
|
||||
var plainStream1 = new MemoryStream(plainData);
|
||||
var compressor1 = new DeflateStream(plainStream1, CompressionMode.Compress);
|
||||
// This is enough to read the entire data
|
||||
var realCompressedSize = compressor1.Read(buf, 0, plainData.Length * 2);
|
||||
|
||||
MemoryStream plainStream2 = new MemoryStream(plainData);
|
||||
DeflateStream compressor2 = new DeflateStream(plainStream2, CompressionMode.Compress);
|
||||
int total = 0;
|
||||
int r = -1; // Jumpstart
|
||||
while (r != 0)
|
||||
{
|
||||
// Reading in chunks
|
||||
r = compressor2.Read(buf, 0, plainData.Length);
|
||||
total += r;
|
||||
}
|
||||
|
||||
Assert.Equal(total, realCompressedSize);
|
||||
var plainStream2 = new MemoryStream(plainData);
|
||||
var compressor2 = new DeflateStream(plainStream2, CompressionMode.Compress);
|
||||
var total = 0;
|
||||
var r = -1; // Jumpstart
|
||||
while (r != 0)
|
||||
{
|
||||
// Reading in chunks
|
||||
r = compressor2.Read(buf, 0, plainData.Length);
|
||||
total += r;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Zlib_should_read_the_previously_written_message()
|
||||
{
|
||||
var message = new string('a', 131073); // 131073 causes the failure, but 131072 (-1) doesn't
|
||||
var bytes = Encoding.ASCII.GetBytes(message);
|
||||
Assert.Equal(total, realCompressedSize);
|
||||
}
|
||||
|
||||
using (var inputStream = new MemoryStream(bytes))
|
||||
{
|
||||
using (var compressedStream = new MemoryStream())
|
||||
using (var byteBufferStream = new BufferedStream(inputStream)) // System.IO
|
||||
{
|
||||
Compress(byteBufferStream, compressedStream, compressionLevel: 1);
|
||||
compressedStream.Position = 0;
|
||||
[Fact]
|
||||
public void Zlib_should_read_the_previously_written_message()
|
||||
{
|
||||
var message = new string('a', 131073); // 131073 causes the failure, but 131072 (-1) doesn't
|
||||
var bytes = Encoding.ASCII.GetBytes(message);
|
||||
|
||||
using (var decompressedStream = new MemoryStream())
|
||||
{
|
||||
Decompress(compressedStream, decompressedStream);
|
||||
using var inputStream = new MemoryStream(bytes);
|
||||
using var compressedStream = new MemoryStream();
|
||||
using var byteBufferStream = new BufferedStream(inputStream); // System.IO
|
||||
Compress(byteBufferStream, compressedStream, compressionLevel: 1);
|
||||
compressedStream.Position = 0;
|
||||
|
||||
byteBufferStream.Position = 0;
|
||||
var result = Encoding.ASCII.GetString(GetBytes(byteBufferStream));
|
||||
result.Should().Be(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
using var decompressedStream = new MemoryStream();
|
||||
Decompress(compressedStream, decompressedStream);
|
||||
|
||||
private void Compress(Stream input, Stream output, int compressionLevel)
|
||||
{
|
||||
using (
|
||||
var zlibStream = new ZlibStream(
|
||||
NonDisposingStream.Create(output),
|
||||
CompressionMode.Compress,
|
||||
(CompressionLevel)compressionLevel
|
||||
)
|
||||
)
|
||||
{
|
||||
zlibStream.FlushMode = FlushType.Sync;
|
||||
input.CopyTo(zlibStream);
|
||||
}
|
||||
}
|
||||
byteBufferStream.Position = 0;
|
||||
var result = Encoding.ASCII.GetString(GetBytes(byteBufferStream));
|
||||
result.Should().Be(message);
|
||||
}
|
||||
|
||||
private void Decompress(Stream input, Stream output)
|
||||
{
|
||||
using (
|
||||
var zlibStream = new ZlibStream(
|
||||
NonDisposingStream.Create(input),
|
||||
CompressionMode.Decompress
|
||||
)
|
||||
)
|
||||
{
|
||||
zlibStream.CopyTo(output);
|
||||
}
|
||||
}
|
||||
private void Compress(Stream input, Stream output, int compressionLevel)
|
||||
{
|
||||
using var zlibStream = new ZlibStream(
|
||||
NonDisposingStream.Create(output),
|
||||
CompressionMode.Compress,
|
||||
(CompressionLevel)compressionLevel
|
||||
);
|
||||
zlibStream.FlushMode = FlushType.Sync;
|
||||
input.CopyTo(zlibStream);
|
||||
}
|
||||
|
||||
byte[] GetBytes(BufferedStream stream)
|
||||
{
|
||||
byte[] bytes = new byte[stream.Length];
|
||||
stream.Read(bytes, 0, (int)stream.Length);
|
||||
return bytes;
|
||||
}
|
||||
private void Decompress(Stream input, Stream output)
|
||||
{
|
||||
using var zlibStream = new ZlibStream(
|
||||
NonDisposingStream.Create(input),
|
||||
CompressionMode.Decompress
|
||||
);
|
||||
zlibStream.CopyTo(output);
|
||||
}
|
||||
|
||||
private byte[] GetBytes(BufferedStream stream)
|
||||
{
|
||||
var bytes = new byte[stream.Length];
|
||||
stream.Read(bytes, 0, (int)stream.Length);
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user