Пример #1
0
        public static byte[] Compress(byte[] decompressedData, CompressionAlgorithm algorithm, CompressionLevel Level)
        {
            var memoryStream = new MemoryStream();

            switch (algorithm)
            {
                case CompressionAlgorithm.GZip:
                    var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true);
                    gZipStream.Write(decompressedData, 0, decompressedData.Length);
                    gZipStream.Close();
                    break;
                case CompressionAlgorithm.Deflate:
                    var compressedDeflateStream = new DeflateStream(memoryStream, CompressionMode.Compress, true);
                    compressedDeflateStream.Write(decompressedData, 0, decompressedData.Length);
                    compressedDeflateStream.Close();
                    break;
                default:
                    throw new NotSupportedException(
                        String.Format("Unknown compression algorithm parameter must be deflate or gzip: {0}", algorithm));
            }

            return memoryStream.ToArray();
        }
Пример #2
0
 public static byte[] Decompress(byte[] compressedData, CompressionAlgorithm algorithm, CompressionLevel Level)
 {
     var memoryStream = new MemoryStream(compressedData);
     byte[] buffer;
     switch (algorithm)
     {
         case CompressionAlgorithm.GZip:
             using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
             {
                 buffer = LoadToBuffer(gZipStream);
             }
             break;
         case CompressionAlgorithm.Deflate:
             using (var deflateStream = new DeflateStream(memoryStream, CompressionMode.Decompress))
             {
                 buffer = LoadToBuffer(deflateStream);
             }
             break;
         default:
             throw new NotSupportedException(
                 String.Format("Unknown compression algorithm parameter must be deflate or gzip: {0}", algorithm));
     }
     return buffer;
 }