summary refs log tree commit diff
path: root/crypto/src/util/io/compression/ZLib.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/util/io/compression/ZLib.cs')
-rw-r--r--crypto/src/util/io/compression/ZLib.cs46
1 files changed, 46 insertions, 0 deletions
diff --git a/crypto/src/util/io/compression/ZLib.cs b/crypto/src/util/io/compression/ZLib.cs
new file mode 100644
index 000000000..1254da012
--- /dev/null
+++ b/crypto/src/util/io/compression/ZLib.cs
@@ -0,0 +1,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
+    }
+}