Esempio n. 1
0
        public static byte[] Compress(byte[] inputBytes)
        {
            MemoryStream inStream  = new MemoryStream(inputBytes);
            MemoryStream outStream = new MemoryStream();
            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);
            return(outStream.ToArray());
        }
Esempio n. 2
0
        public static byte[] Compress(byte[] data)
        {
            var encoder = new LZMAEncoder();

            encoder.SetCoderProperties(_defaultIds, _defaultProperties);
            using (var input = new MemoryStream(data))
                using (var output = new MemoryStream())
                {
                    output.Position += 4;
                    encoder.WriteCoderProperties(output);
                    encoder.Code(input, output, -1, -1, null);

                    byte[] compressed           = output.ToArray();
                    byte[] compressedLengthData = BitConverter.GetBytes(compressed.Length);

                    Buffer.BlockCopy(compressedLengthData, 0, compressed, 0, 4);
                    return(compressed);
                }
        }
Esempio n. 3
0
        private ResourceDef Compress(ResourceDef resource)
        {
            CoderPropID[] propIDs =
            {
                CoderPropID.DictionarySize,
                CoderPropID.PosStateBits,
                CoderPropID.LitContextBits,
                CoderPropID.LitPosBits,
                CoderPropID.Algorithm,
                CoderPropID.NumFastBytes,
                CoderPropID.MatchFinder,
                CoderPropID.EndMarker
            };
            object[] properties =
            {
                (Int32)(1 << 23),
                (Int32)(2),
                (Int32)(3),
                (Int32)(0),
                (Int32)(2),
                (Int32)(128),
                "bt4",
                true
            };

            MemoryStream outStream = new MemoryStream();

            LZMAEncoder encoder = new LZMAEncoder();

            encoder.SetCoderProperties(propIDs, properties);
            encoder.WriteCoderProperties(outStream);

            MemoryStream inStream = new MemoryStream(resource.Data);

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

            byte[] compressed = outStream.ToArray();
            return(new ResourceDef(resource.Name, compressed));
        }