Exemple #1
0
        /// <summary>
        /// 압축된 데이타를 복원한다.
        /// </summary>
        /// <param name="input">복원할 Data</param>
        /// <returns>복원된 Data</returns>
        public override byte[] Decompress(byte[] input)
        {
            if (IsDebugEnabled)
            {
                log.Debug(CompressorTool.SR.DecompressStartMsg);
            }

            // check input data
            if (input.IsZeroLength())
            {
                if (IsDebugEnabled)
                {
                    log.Debug(CompressorTool.SR.InvalidInputDataMsg);
                }

                return(CompressorTool.EmptyBytes);
            }

            byte[] output;
            using (var compressedStream = new MemoryStream(input)) {
                using (var gzs = new GZipInputStream(compressedStream))
                    using (var extractedStream = new MemoryStream(input.Length * 2)) {
                        StreamTool.CopyStreamToStream(gzs, extractedStream, CompressorTool.BUFFER_SIZE);
                        output = extractedStream.ToArray();
                    }
            }

            if (IsDebugEnabled)
            {
                log.Debug(CompressorTool.SR.DecompressResultMsg, input.Length, output.Length, output.Length / (double)input.Length);
            }

            return(output);
        }
        /// <summary>
        /// 압축된 데이타를 복원한다.
        /// </summary>
        /// <param name="input">복원할 Data</param>
        /// <returns>복원된 Data</returns>
        public override byte[] Decompress(byte[] input)
        {
            if (IsDebugEnabled)
            {
                log.Debug(CompressorTool.SR.DecompressStartMsg);
            }

            // check input data
            if (input.IsZeroLength())
            {
                if (IsDebugEnabled)
                {
                    log.Debug(CompressorTool.SR.InvalidInputDataMsg);
                }

                return(new byte[0]);
            }

            byte[] output;

            var outStream = new MemoryStream(input.Length * 2);

            try {
                using (var inStream = new MemoryStream(input))
                    using (var zlib = new ZlibStream(inStream, CompressionMode.Decompress)) {
                        StreamTool.CopyStreamToStream(zlib, outStream, CompressorTool.BUFFER_SIZE);
                        output = outStream.ToArray();
                    }
            }
            catch (Exception ex) {
                if (log.IsErrorEnabled)
                {
                    log.ErrorException("압축 복원 중 예외가 발생했습니다.", ex);
                }

                throw;
            }
            finally {
                outStream.Close();
            }

            if (IsDebugEnabled)
            {
                log.Debug(CompressorTool.SR.DecompressResultMsg, input.Length, output.Length, output.Length / (double)input.Length);
            }

            return(output);
        }
        public void BZip2_Compress_Extract_Test()
        {
            var plainStream = PlainText.ToStream();

            plainStream.Seek(0, SeekOrigin.Begin);

            var plainData = Encoding.UTF8.GetBytes(PlainText);

            byte[] compressedData;
            byte[] extractedData;

            // Compress
            using (var compressedStream = new MemoryStream())
                using (var bz2 = new BZip2OutputStream(compressedStream)) {
                    bz2.Write(plainData, 0, plainData.Length);
                    bz2.Close();
                    compressedData = compressedStream.ToArray();
                }

            Assert.IsNotNull(compressedData);

            // Array.Resize(ref compressedData, compressedData.Length+1);
            // compressedData[compressedData.Length - 1] = (byte)0;

            // Extract
            using (var compressedStream = new MemoryStream(compressedData))
                using (var bz2 = new BZip2InputStream(compressedStream))
                    using (var extractedStream = new MemoryStream()) {
                        StreamTool.CopyStreamToStream(bz2, extractedStream);
                        extractedData = extractedStream.ToArray();
                    }


            Assert.IsNotNull(extractedData);
            string extractedText = Encoding.UTF8.GetString(extractedData).TrimEnd('\0');

            Assert.AreEqual(PlainText, extractedText);
        }
        public void GZip_Compress_Extract_Test()
        {
            var plainStream = PlainText.ToStream();

            plainStream.Seek(0, SeekOrigin.Begin);

            var plainData = Encoding.UTF8.GetBytes(PlainText);

            byte[] compressedData;
            byte[] extractedData;

            // Compress
            using (var compressedStream = new MemoryStream())
                using (var gzs = new GZipOutputStream(compressedStream)) {
                    gzs.SetLevel(5);
                    gzs.Write(plainData, 0, plainData.Length);
                    gzs.Finish();
                    compressedData = compressedStream.ToArray();
                }

            Assert.IsNotNull(compressedData);

            // Extract
            using (var compressedStream = new MemoryStream(compressedData)) {
                // compressedStream.Seek(0, SeekOrigin.Begin);
                using (var gzs = new GZipInputStream(compressedStream))
                    using (var extractedStream = new MemoryStream()) {
                        StreamTool.CopyStreamToStream(gzs, extractedStream);
                        extractedData = extractedStream.ToArray();
                    }
            }

            Assert.IsNotNull(extractedData);
            string extractedText = Encoding.UTF8.GetString(extractedData).TrimEnd('\0');

            Assert.AreEqual(PlainText, extractedText);
        }