summary refs log tree commit diff
path: root/crypto/src/util/io/compression/ZLib.cs
blob: 1254da012dee14e2de82f32cbe62c6e95203b1a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.IO;

#if NET6_0_OR_GREATER
using System.IO.Compression;
#else
using Org.BouncyCastle.Utilities.Zlib;
#endif

namespace Org.BouncyCastle.Utilities.IO.Compression
{
    internal static class ZLib
    {
        internal static Stream CompressOutput(Stream stream, int zlibCompressionLevel, bool leaveOpen = false)
        {
#if NET6_0_OR_GREATER
            return new ZLibStream(stream, GetCompressionLevel(zlibCompressionLevel), leaveOpen);
#else
            return leaveOpen
                ?   new ZOutputStreamLeaveOpen(stream, zlibCompressionLevel, false)
                :   new ZOutputStream(stream, zlibCompressionLevel, false);
#endif
        }

        internal static Stream DecompressInput(Stream stream)
        {
#if NET6_0_OR_GREATER
            return new ZLibStream(stream, CompressionMode.Decompress, leaveOpen: false);
#else
            return new ZInputStream(stream);
#endif
        }

#if NET6_0_OR_GREATER
        internal static CompressionLevel GetCompressionLevel(int zlibCompressionLevel)
        {
            return zlibCompressionLevel switch
            {
                0           => CompressionLevel.NoCompression,
                1 or 2 or 3 => CompressionLevel.Fastest,
                7 or 8 or 9 => CompressionLevel.SmallestSize,
                _           => CompressionLevel.Optimal,
            };
        }
#endif
    }
}