Beispiel #1
0
        public static async Task <string> GZipUncompress([NotNull] this byte[] buffer)
        {
            if (buffer == null || buffer.LongLength == 0)
            {
                throw new ArgumentNullException("buffer");
            }

            using (var ms = new MemoryStream(buffer))
                using (var gzs = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Decompress, true))
                    using (var output = new MemoryStream())
                    {
                        await gzs.CopyToAsync(output);

                        var array = output.ToArray();
                        var str   = Encoding.UTF8.GetString(array);
                        return(str);
                    }
        }
Beispiel #2
0
        /// <summary>
        /// Un-compresses a ZLIB-compressed (Unix-style GZIP compression) byte array to the original source text
        /// </summary>
        /// <param name="buffer">The byte array that represents the compressed data to un-compress</param>
        /// <returns>The original UTF-8 string that was compressed</returns>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="buffer"/> is null or an empty string</exception>
        /// <exception cref="DecoderFallbackException">Thrown when characters are provided in the <param name="buffer"> that cannot be represented by UTF-8</param></exception>
        /// <exception cref="ArgumentException">Thrown when a byte array is passed in for the <paramref name="buffer"/> contains invalid data for the decompression routine.</exception>
        /// <exception cref="ObjectDisposedException">Thrown when the source or destination stream are disposed at the time they are internally copied for compression.  Should never occur.</exception>
        /// <exception cref="NotSupportedException">Thrown when the source stream does not support reading or the destination stream does not support writing.  Should never occur.</exception>
        public static async Task <string> GZipUncompressAsync(this byte[] buffer, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (buffer == null || buffer.LongLength == 0)
            {
                throw new ArgumentNullException("buffer");
            }

            using (var ms = new MemoryStream(buffer))
                using (var gzs = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Decompress, true))
                    using (var output = new MemoryStream())
                    {
                        // Buffer size is default for underlying Stream.CopyToAsync()
                        await gzs.CopyToAsync(output, 81920, cancellationToken);

                        var array = output.ToArray();
                        var str   = Encoding.UTF8.GetString(array);
                        return(str);
                    }
        }
Beispiel #3
0
        public static async Task <byte[]> GZipCompress([NotNull] this string text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                throw new ArgumentNullException("text");
            }

            var buffer = Encoding.UTF8.GetBytes(text);

            using (var ms = new MemoryStream(buffer))
                using (var gzs = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Compress, true))
                    using (var output = new MemoryStream())
                    {
                        await gzs.CopyToAsync(output);

                        var array = output.ToArray();
                        return(array);
                    }
        }
Beispiel #4
0
        /// <summary>
        /// Compresses a string using ZLIB compression (Unix-style GZIP compression)
        /// </summary>
        /// <param name="text">The text to compress</param>
        /// <returns>A ZLIB compressed byte array representation of <paramref name="text"/></returns>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="text"/> is null or an empty string</exception>
        /// <exception cref="EncoderFallbackException">Thrown when characters are provided in the <param name="text"> that cannot be represented by UTF-8</param></exception>
        /// <exception cref="ObjectDisposedException">Thrown when the source or destination stream are disposed at the time they are internally copied for compression.  Should never occur.</exception>
        /// <exception cref="NotSupportedException">Thrown when the source stream does not support reading or the destination stream does not support writing.  Should never occur.</exception>
        public static async Task <byte[]> GZipCompressAsync(this string text, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                throw new ArgumentNullException("text");
            }

            var buffer = Encoding.UTF8.GetBytes(text);

            using (var ms = new MemoryStream(buffer))
                using (var gzs = new Ionic.Zlib.ZlibStream(ms, Ionic.Zlib.CompressionMode.Compress, true))
                    using (var output = new MemoryStream())
                    {
                        // Buffer size is default for underlying Stream.CopyToAsync()
                        await gzs.CopyToAsync(output, 81920, cancellationToken);

                        var array = output.ToArray();
                        return(array);
                    }
        }