<PackageReference Include="SSH.NET" Version="2024.2.0" />

Zlib

public class Zlib : Compressor
Represents the "zlib" compression algorithm.
using System.IO; using System.IO.Compression; namespace Renci.SshNet.Compression { public class Zlib : Compressor { private readonly ZLibStream _compressor; private readonly ZLibStream _decompressor; private MemoryStream _compressorStream; private MemoryStream _decompressorStream; private bool _isDisposed; public override string Name => "zlib"; public Zlib() : this(false) { } protected Zlib(bool delayedCompression) : base(delayedCompression) { _compressorStream = new MemoryStream(); _decompressorStream = new MemoryStream(); _compressor = new ZLibStream(_compressorStream, CompressionMode.Compress); _decompressor = new ZLibStream(_decompressorStream, CompressionMode.Decompress); } protected override byte[] CompressCore(byte[] data, int offset, int length) { _compressorStream.SetLength(0); _compressor.Write(data, offset, length); _compressor.Flush(); return _compressorStream.ToArray(); } protected override byte[] DecompressCore(byte[] data, int offset, int length) { _decompressorStream.Write(data, offset, length); _decompressorStream.Position = 0; using (MemoryStream memoryStream = new MemoryStream()) { _decompressor.CopyTo(memoryStream); _decompressorStream.SetLength(0); return memoryStream.ToArray(); } } protected override void Dispose(bool disposing) { base.Dispose(disposing); if (!_isDisposed && disposing) { MemoryStream compressorStream = _compressorStream; if (compressorStream != null) { compressorStream.Dispose(); _compressorStream = null; } MemoryStream decompressorStream = _decompressorStream; if (decompressorStream != null) { decompressorStream.Dispose(); _decompressorStream = null; } _isDisposed = true; } } } }