예제 #1
0
        /// <summary>
        /// Compresses data using an specific compression level.
        /// </summary>
        /// <param name="inData">The original input data.</param>
        /// <param name="outData">The compressed output data.</param>
        /// <param name="level">The compression level to use.</param>
        /// <param name="adler32">The output adler32 of the data.</param>
        /// <exception cref="NotPackableException">
        /// Thrown when the internal compression stream errors in any way.
        /// </exception>
        public static void Compress(byte[] inData, out byte[] outData, ZlibCompression level, out int adler32)
        {
            using (var outMemoryStream = new MemoryStream())
                using (var outZStream = new ZOutputStream(outMemoryStream, level))
                    using (Stream inMemoryStream = new MemoryStream(inData))
                    {
                        try
                        {
                            inMemoryStream.CopyTo(outZStream);
                        }
                        catch (ZStreamException)
                        {
                            // the compression or decompression failed.
                        }

                        try
                        {
                            outZStream.Flush();
                        }
                        catch (StackOverflowException ex)
                        {
                            throw new NotPackableException("Compression Failed due to a stack overflow.", ex);
                        }

                        try
                        {
                            outZStream.Finish();
                        }
                        catch (ZStreamException ex)
                        {
                            throw new NotPackableException("Compression Failed.", ex);
                        }

                        outData = outMemoryStream.ToArray();
                        adler32 = (int)(outZStream.Z.Adler & 0xffff);
                    }
        }
예제 #2
0
        /// <summary>
        /// Decompresses data.
        /// </summary>
        /// <param name="inData">The compressed input data.</param>
        /// <param name="outData">The decompressed output data.</param>
        /// <exception cref="NotUnpackableException">
        /// Thrown when the internal decompression stream errors in any way.
        /// </exception>
        public static void Decompress(byte[] inData, out byte[] outData)
        {
            using (var outMemoryStream = new MemoryStream())
                using (var outZStream = new ZOutputStream(outMemoryStream))
                    using (Stream inMemoryStream = new MemoryStream(inData))
                    {
                        try
                        {
                            inMemoryStream.CopyTo(outZStream);
                        }
                        catch (ZStreamException)
                        {
                            // the compression or decompression failed.
                        }

                        try
                        {
                            outZStream.Flush();
                        }
                        catch (StackOverflowException ex)
                        {
                            throw new NotPackableException("Decompression Failed due to a stack overflow.", ex);
                        }

                        try
                        {
                            outZStream.Finish();
                        }
                        catch (ZStreamException ex)
                        {
                            throw new NotUnpackableException("Decompression Failed.", ex);
                        }

                        outData = outMemoryStream.ToArray();
                    }
        }