Esempio n. 1
0
        internal static void WriteLzmaProperties(LzmaEncoder encoder)
        {
            #region LZMA properties definition

            CoderPropID[] propIDs =
            {
                CoderPropID.DictionarySize,
                CoderPropID.PosStateBits,
                CoderPropID.LitContextBits,
                CoderPropID.LitPosBits,
                CoderPropID.Algorithm,
                CoderPropID.NumFastBytes,
                CoderPropID.MatchFinder,
                CoderPropID.EndMarker
            };
            object[] properties =
            {
                _lzmaDictionarySize,
                2,
                3,
                0,
                2,
                256,
                "bt4",
                false
            };

            #endregion

            encoder.SetCoderProperties(propIDs, properties);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var  coder      = new LzmaEncoder();
            long inputSize  = 0;
            long outputSize = 0;

            using (var inStream = File.OpenRead(@"c:\tmp\test.tar"))
                using (var outStream = File.Create(@"c:\tmp\test.lzma"))
                {
                    var coderProperties = new Dictionary <CoderPropID, object>()
                    {
                        { CoderPropID.DictionarySize, 1024 * 1024 }
                    };
                    coder.SetCoderProperties(coderProperties);
                    coder.Code(inStream, outStream, -1, -1, new ConsoleProgress(inStream.Length));
                    inputSize  = inStream.Length;
                    outputSize = outStream.Length;
                }

            Console.WriteLine();
            var ratio = Math.Round((double)inputSize / (double)outputSize, 2);

            Console.WriteLine("Input Size: {0}", inputSize);
            Console.WriteLine("Output Size: {0}", outputSize);
            Console.WriteLine("ratio: {0}", ratio);
            Console.WriteLine("=============== All done ===============");
            long  x = -1;
            ulong y = (ulong)x;

            Console.WriteLine(y);
            Console.ReadKey();
        }
Esempio n. 3
0
        public void LzmaCompressAndDecompressShouldBeOk()
        {
            var source = this.CreateCompressionSourceData();

            byte[] compressedBytes   = null;
            byte[] properties        = null;
            var    encoderProperties = new Dictionary <CoderPropID, object>()
            {
                { CoderPropID.DictionarySize, 1024 * 512 } //512kb
            };

            using (var inputStream = new MemoryStream(source, false))
                using (var outputStream = new MemoryStream())
                    using (var propertiesStream = new MemoryStream())
                    {
                        var encoder = new LzmaEncoder();
                        encoder.SetCoderProperties(encoderProperties);
                        encoder.WriteCoderProperties(propertiesStream);
                        properties = propertiesStream.ToArray();

                        encoder.Code(inputStream, outputStream, -1, -1, null);
                        compressedBytes = outputStream.ToArray();
                        Assert.NotEqual(source, outputStream.ToArray());
                    }

            using (var inputStream = new MemoryStream(compressedBytes, false))
                using (var outputStream = new MemoryStream())
                {
                    var decoder = new LzmaDecoder();
                    decoder.SetDecoderProperties(properties);
                    decoder.Code(inputStream, outputStream, compressedBytes.Length, source.Length, null);
                    Assert.Equal(source, outputStream.ToArray());
                }
        }
Esempio n. 4
0
        /// <summary>写入数据并压缩</summary>
        /// <param name="buffer"></param>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (_Mode != CompressionMode.Compress)
            {
                throw new InvalidOperationException();
            }

            if (_Encoder == null)
            {
                _Encoder = new LzmaEncoder();

                #region 计算压缩等级
                CoderPropID[] propIDs =
                {
                    CoderPropID.DictionarySize,
                    CoderPropID.PosStateBits,
                    CoderPropID.LitContextBits,
                    CoderPropID.LitPosBits,
                    CoderPropID.Algorithm,
                    CoderPropID.NumFastBytes,
                    CoderPropID.MatchFinder,
                    CoderPropID.EndMarker
                };
                object[] properties =
                {
                    (Int32)1 << Get(_Level, 0, 29),
                        (Int32)2,
                        (Int32)3,
                        (Int32)0,
                        (Int32)2,           // Algorithm
                        (Int32)256,         // NumFastBytes
                        "bt4",              // MatchFinder MF
                        false               // EndMarker EOF
                };
                //object[] properties =
                //{
                //    (Int32)Get(_Level, 0, 29),
                //    (Int32)Get(_Level, 0, 4),
                //    (Int32)Get(_Level, 0, 8),
                //    (Int32)Get(_Level, 0, 4),
                //    (Int32)Get(_Level, 0, 2),   // Algorithm
                //    (Int32)Get(_Level, 0, 128), // NumFastBytes
                //    "bt4",  // MatchFinder MF
                //    false   // EndMarker EOF
                //};
                #endregion

                _Encoder.SetCoderProperties(propIDs, properties);
                _Encoder.WriteCoderProperties(BaseStream);

                // 8字节长度
                BaseStream.Write(BitConverter.GetBytes((Int64)0));
            }

            var ms = new MemoryStream(buffer, offset, count);
            _Encoder.Code(ms, BaseStream, -1, -1, null);
        }
Esempio n. 5
0
        /// <summary>
        /// Compresses the given stream and writes to the output stream.
        /// </summary>
        /// <param name="inStream">The input stream.</param>
        /// <param name="outStream">The output stream.</param>
        /// <param name="progress">The progress callback, if desired.</param>
        public static void Compress(Stream inStream, Stream outStream, ICodeProgress progress = null)
        {
            lock (m_EncoderLock)
            {
                if (m_Encoder == null)
                {
                    m_Encoder = new LzmaEncoder();
                    m_Encoder.SetCoderProperties(propIDs, properties);
                }

                m_Encoder.WriteCoderProperties(outStream);
                long fileSize = inStream.Length;
                for (int i = 0; i < 8; i++)
                {
                    outStream.WriteByte((Byte)(fileSize >> (8 * i)));
                }
                m_Encoder.Code(inStream, outStream, -1, -1, progress);
            }
        }
Esempio n. 6
0
        public static void Compress(Stream inStream, Stream outStream)
        {
            BinaryWriter writer = new BinaryWriter(outStream);
            //writer.WriteNucleusHeader();
            //writer.WriteNucleusLZMAHeader();
            //writer.Flush();

            LzmaEncoder encoder = new LzmaEncoder();

            encoder.SetCoderProperties(propIDs, properties);
            encoder.WriteCoderProperties(outStream);
            long fileSize = inStream.Length;

            for (int i = 0; i < 8; i++)
            {
                outStream.WriteByte((byte)(fileSize >> (8 * i)));
            }

            encoder.Code(inStream, outStream, -1, -1, null);
        }