コード例 #1
0
        public void Deflate_Compress()

        {
            compressedData.Clear();



            byte[] testData = new byte[35000];

            for (int i = 0; i < testData.Length; ++i)
            {
                testData[i] = 5;
            }



            using (Deflater def = new Deflater((CompressLevel)5))

            {
                def.DataAvailable += new DataAvailableHandler(CDataAvail);

                def.Add(testData);

                def.Finish();

                adler1 = def.Checksum;
            }
        }
コード例 #2
0
        public static byte[] Compress(byte[] input)
        {
            using (var output = new MemoryStream())
            {
                using (var deflater = new Deflater(CompressLevel.Best))
                {
                    deflater.DataAvailable += output.Write;
                    deflater.Add(input, 0, input.Length);
                }

                return(output.ToArray());
            }
        }
コード例 #3
0
ファイル: ZLib.cs プロジェクト: benamehdi7/tesv-snip
        /// <summary>
        /// Compress a stream with ZLib
        /// </summary>
        /// <param name="snipStreamWrapper">A stream wrapper object</param>
        /// <param name="compressLevel">Compression level of stream.</param>
        public static void Compress(SnipStreamWrapper snipStreamWrapper, CompressLevel compressLevel)
        {
            var numBytesAddressing = snipStreamWrapper.OutputBufferLength;

            ZLibWrapper.CopyByteArrayToInputBuffer(snipStreamWrapper.OutputBuffer, 0,
                                                   snipStreamWrapper.OutputBufferLength);

            if (_deflaterBest == null)
            {
                _deflaterBest = new Deflater(CompressLevel.Best);
                _deflaterBest.DataAvailable += ZLibWrapper.WriteInOutputBuffer;
            }

            if (_deflaterNone == null)
            {
                _deflaterNone = new Deflater(CompressLevel.None);
                _deflaterNone.DataAvailable += ZLibWrapper.WriteInOutputBuffer;
            }

            if (_deflaterFastest == null)
            {
                _deflaterFastest = new Deflater(CompressLevel.Fastest);
                _deflaterFastest.DataAvailable += ZLibWrapper.WriteInOutputBuffer;
            }

            if (_deflaterDefault == null)
            {
                _deflaterDefault = new Deflater(CompressLevel.Default);
                _deflaterDefault.DataAvailable += ZLibWrapper.WriteInOutputBuffer;
            }

            try
            {
                while (numBytesAddressing > 0u)
                {
                    uint numBytes = Math.Min(numBytesAddressing, 8192u); //8192u); 65536u

                    if (compressLevel == CompressLevel.None)
                    {
                        _deflaterNone.Add(ZLibWrapper.ReadBytes((int)numBytes, ZLibBufferType.InputBuffer));
                    }

                    if (compressLevel == CompressLevel.Best)
                    {
                        _deflaterBest.Add(ZLibWrapper.ReadBytes((int)numBytes, ZLibBufferType.InputBuffer));
                    }

                    if (compressLevel == CompressLevel.Default)
                    {
                        _deflaterDefault.Add(ZLibWrapper.ReadBytes((int)numBytes, ZLibBufferType.InputBuffer));
                    }

                    if (compressLevel == CompressLevel.Fastest)
                    {
                        _deflaterFastest.Add(ZLibWrapper.ReadBytes((int)numBytes, ZLibBufferType.InputBuffer));
                    }

                    numBytesAddressing -= numBytes;
                }

                string deflateErrorMsg = string.Empty;
                if (compressLevel == CompressLevel.None)
                {
                    deflateErrorMsg = _deflaterNone._ztream.msg;
                }

                if (compressLevel == CompressLevel.Best)
                {
                    deflateErrorMsg = _deflaterBest._ztream.msg;
                }

                if (compressLevel == CompressLevel.Default)
                {
                    deflateErrorMsg = _deflaterDefault._ztream.msg;
                }

                if (compressLevel == CompressLevel.Fastest)
                {
                    deflateErrorMsg = _deflaterFastest._ztream.msg;
                }

                if (!string.IsNullOrWhiteSpace(deflateErrorMsg))
                {
                    MessageBox.Show(string.Format("ZLib.Compress: {0}", deflateErrorMsg),
                                    TranslateUI.TranslateUiGlobalization.ResManager.GetString("ZLib_Error"),
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }

                if (compressLevel == CompressLevel.None)
                {
                    _deflaterNone.Finish(); // flush zlib buffer
                }
                if (compressLevel == CompressLevel.Best)
                {
                    _deflaterBest.Finish(); // flush zlib buffer
                }
                if (compressLevel == CompressLevel.Default)
                {
                    _deflaterDefault.Finish(); // flush zlib buffer
                }
                if (compressLevel == CompressLevel.Fastest)
                {
                    _deflaterFastest.Finish(); // flush zlib buffer
                }
                ZLibWrapper.Position(0, ZLibBufferType.OutputBuffer);
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    string.Format(TranslateUI.TranslateUiGlobalization.ResManager.GetString(name: "MSG_ErrorWithNewLine"), arg0: ex),
                    TranslateUI.TranslateUiGlobalization.ResManager.GetString(name: "ZLib_Compress"),
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
            finally
            {
                if (ZLibWrapper.OutputBufferLength == 0)
                {
                    MessageBox.Show(
                        TranslateUI.TranslateUiGlobalization.ResManager.GetString(name: "ZLib_OutputBufferEmpty"),
                        TranslateUI.TranslateUiGlobalization.ResManager.GetString(name: "ZLib_Compress"),
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }
            }
        }