SetDecoderProperties() public méthode

public SetDecoderProperties ( byte properties ) : void
properties byte
Résultat void
Exemple #1
1
    public static System.IO.Stream DeCompress(System.IO.Stream stream, uint length, bool EOF = false)
    {
        SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
        int tagsize = 5;

        int filelen = -1;//如果压缩时使用了结束标志选项,则这里传-1,解压缩会知道文件长度
        if (!EOF)
        {
            //读取文件长度
            byte[] lbuf = new byte[4];
            stream.Read(lbuf, 0, 4);
            filelen = (int)BitConverter.ToUInt32(lbuf, 0);
            tagsize += 4;
        }
        //读取压缩属性
        byte[] properties = new byte[5];
        if (stream.Read(properties, 0, 5) != 5)
            throw (new Exception("input .lzma is too short"));
        decoder.SetDecoderProperties(properties);
        System.IO.MemoryStream sout = new System.IO.MemoryStream();
        decoder.Code(stream, sout, length - tagsize, filelen, null);
        sout.Position = 0;
        return sout;
    }
    public static void DecompressFileLZMA(string inFile, string outFile)
    {
        Debug.Log("DecompressFileLZMA in File is :" + inFile);
        Debug.Log("DecompressFileLZMA in outFile is :" + outFile);
        SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();
        FileStream input = new FileStream(inFile, FileMode.Open, FileAccess.Read);
        string     path  = outFile.Substring(0, outFile.LastIndexOf("/"));

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        FileStream output = new FileStream(outFile, FileMode.Create);

        // Read the decoder properties
        byte[] properties = new byte[5];
        input.Read(properties, 0, 5);

        // Read in the decompress file size.
        byte [] fileLengthBytes = new byte[8];
        input.Read(fileLengthBytes, 0, 8);
        long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

        // Decompress the file.
        coder.SetDecoderProperties(properties);
        coder.Code(input, output, input.Length, fileLength, null);
        output.Flush();
        output.Close();
        input.Close();
    }
Exemple #3
0
        // public static byte[] GetDecompressedResourceFromAssembly(Assembly assembly, string resourceName)
        // {
        //     // Get the resource
        //     Stream str = assembly.GetManifestResourceStream(resourceName);
        //     byte[] b = new byte[(int) str.Length];
        //     str.Read(b, 0, b.Length);
        //     // decompress the resource
        //     byte[] b2 = Decompress(b);
        //    return b2;
        //}


        public static byte[] Decompress(byte[] inputBytes)
        {
            MemoryStream newInStream = new MemoryStream(inputBytes);
            Decoder      decoder     = new Decoder();

            newInStream.Seek(0, 0);
            MemoryStream newOutStream = new MemoryStream();

            byte[] properties2 = new byte[5];
            if (newInStream.Read(properties2, 0, 5) != 5)
            {
                throw (new Exception("input .lzma is too short"));
            }
            long outSize = 0;

            for (int i = 0; i < 8; i++)
            {
                int v = newInStream.ReadByte();
                if (v < 0)
                {
                    throw (new Exception("Can't Read 1"));
                }
                outSize |= ((long)(byte)v) << (8 * i);
            }
            decoder.SetDecoderProperties(properties2);
            long compressedSize = newInStream.Length - newInStream.Position;

            decoder.Code(newInStream, newOutStream, compressedSize, outSize, null);
            byte[] b = newOutStream.ToArray();
            return(b);
        }
 private static byte[] DecodeLzma(byte[] lzmaByteArray)
 {
     byte[] result = null;
     Decoder decoder = new Decoder();
     using (MemoryStream memoryStream = new MemoryStream(lzmaByteArray))
     {
         memoryStream.Seek(0L, SeekOrigin.Begin);
         using (MemoryStream memoryStream2 = new MemoryStream())
         {
             byte[] array = new byte[5];
             if (memoryStream.Read(array, 0, 5) != 5)
             {
                 throw new Exception("input .lzma is too short");
             }
             long num = 0L;
             for (int i = 0; i < 8; i++)
             {
                 int num2 = memoryStream.ReadByte();
                 if (num2 < 0)
                 {
                     throw new Exception("Can't Read 1");
                 }
                 num |= (long)((long)((ulong)((byte)num2)) << 8 * i);
             }
             decoder.SetDecoderProperties(array);
             long inSize = memoryStream.Length - memoryStream.Position;
             decoder.Code(memoryStream, memoryStream2, inSize, num, null);
             result = memoryStream2.ToArray();
         }
     }
     return result;
 }
    //解压缩文件
    public static void DecompressFileLZMA(byte[] inFile, string outFile, Action callBack)
    {
        DGFileUtil.CreateDirectoryWhenNotExists(outFile);

        SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();
        MemoryStream input  = new MemoryStream(inFile);
        FileStream   output = new FileStream(outFile, FileMode.Create);

        // Read the decoder properties
        byte[] properties = new byte[5];
        input.Read(properties, 0, 5);

        // Read in the decompress file size.
        byte [] fileLengthBytes = new byte[8];
        input.Read(fileLengthBytes, 0, 8);
        long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

        // Decompress the file.
        coder.SetDecoderProperties(properties);
        coder.Code(input, output, input.Length, fileLength, null);
        output.Flush();
        output.Close();
        input.Close();
        output.Dispose();
        input.Dispose();
        if (callBack != null)
        {
            callBack();
        }
    }
        public static byte[] Decompress(byte[] inputBytes, long decompressedSize)
        {
            var compressed = new MemoryStream(inputBytes);
            var decoder    = new Decoder();

            var properties2 = new byte[5];

            if (compressed.Read(properties2, 0, 5) != 5)
            {
                throw (new Exception("input .lzma is too short"));
            }

            decoder.SetDecoderProperties(properties2);

            var compressedSize = compressed.Length - compressed.Position;
            var decompressed   = new MemoryStream();

            decoder.Code(compressed, decompressed, compressedSize, decompressedSize, null);

            if (decompressed.Length != decompressedSize)
            {
                throw new Exception("Decompression Error");
            }

            return(decompressed.ToArray());
        }
Exemple #7
0
        public static byte[] Decompress(byte[] inputBytes)
        {
            MemoryStream memoryStream1 = new MemoryStream(inputBytes);
            Decoder      decoder       = new Decoder();

            memoryStream1.Seek(0L, SeekOrigin.Begin);
            MemoryStream memoryStream2 = new MemoryStream();

            byte[] numArray = new byte[5];
            if (memoryStream1.Read(numArray, 0, 5) != 5)
            {
                throw new Exception("input .lzma is too short");
            }
            long outSize = 0;

            for (int index = 0; index < 8; ++index)
            {
                int num = memoryStream1.ReadByte();
                if (num < 0)
                {
                    throw new Exception("Can't Read 1");
                }
                outSize |= (long)(byte)num << 8 * index;
            }
            decoder.SetDecoderProperties(numArray);
            long inSize = memoryStream1.Length - memoryStream1.Position;

            decoder.Code((Stream)memoryStream1, (Stream)memoryStream2, inSize, outSize, (ICodeProgress)null);
            return(memoryStream2.ToArray());
        }
Exemple #8
0
 public static byte[] Decompress(this byte[] buffer)
 {
     if (buffer.Length > 17 && buffer.IsCompressed())
     {
         MemoryStream inputStream = new MemoryStream(buffer);
         int lzmaID = inputStream.ReadInt();
         int acutalSize = inputStream.ReadInt();
         int lzmaSize = inputStream.ReadInt();
         byte[] props = inputStream.ReadBytes(5);
         int lzmaBufferSize = buffer.Length - 17;
         if (lzmaBufferSize != lzmaSize)
             throw new Exception(string.Format("LZMA data corruption. Expected {0} bytes got {1}", lzmaSize, lzmaBufferSize));
         byte[] uncompressedBuffer = new byte[acutalSize];
         MemoryStream outputStream = new MemoryStream(uncompressedBuffer);
         try
         {
             LZMADecoder = new Decoder();
             LZMADecoder.SetDecoderProperties(props);
             LZMADecoder.Code(inputStream, outputStream, inputStream.Length, outputStream.Length, null);
         }
         catch (Exception e)
         {
             throw new Exception(e.Message);
         }
         return uncompressedBuffer;
     }
     else
         throw new Exception("Buffer is not compressed!");
 }
        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="OriPath">原文件地址</param>
        /// <param name="destPath">目标文件</param>
        public static void Unzip(string OriPath, string destPath)
        {
            FileStream inStream  = new FileStream(OriPath, FileMode.Open);
            FileStream outStream = new FileStream(destPath, FileMode.OpenOrCreate);
            Decoder    decoder   = new Decoder();

            byte[] properties = new byte[5];
            if (inStream.Read(properties, 0, 5) != 5)
            {
                throw (new Exception("input .lzma is too short"));
            }
            decoder.SetDecoderProperties(properties);
            long outSize = 0;

            for (int i = 0; i < 8; i++)
            {
                int v = inStream.ReadByte();
                if (v < 0)
                {
                    throw (new Exception("Can't Read 1"));
                }
                outSize |= ((long)(byte)v) << (8 * i);
            }
            long compressedSize = inStream.Length - inStream.Position;

            decoder.Code(inStream, outStream, compressedSize, outSize, null);
            //关闭文件流
            inStream.Close();
            outStream.Close();
        }
Exemple #10
0
    private static void DecompressFileLZMA(string inFile, string outFile)
    {
        System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
        stopwatch.Start();

        SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();
        FileStream input  = new FileStream(inFile, FileMode.Open);
        FileStream output = new FileStream(outFile, FileMode.Create);

        // Read the decoder properties
        byte[] properties = new byte[5];
        input.Read(properties, 0, 5);

        // Read in the decompress file size.
        byte[] fileLengthBytes = new byte[8];
        input.Read(fileLengthBytes, 0, 8);
        long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

        // Decompress the file.
        coder.SetDecoderProperties(properties);
        coder.Code(input, output, input.Length, fileLength, null);
        output.Flush();
        output.Close();
        input.Flush();
        input.Close();

        stopwatch.Stop();
        Debug.LogFormat("7z encode time: {0}", stopwatch.Elapsed.TotalSeconds);
    }
Exemple #11
0
        public static MemoryStream Decompress(Stream newInStream)
        {
            //newInStream.Seek(0, SeekOrigin.Begin);
            newInStream.Position = 0;

            MemoryStream newOutStream = new MemoryStream();

            byte[] properties2 = new byte[5];
            if (newInStream.Read(properties2, 0, 5) != 5)
            {
                throw (new Exception("input .lzma is too short"));
            }
            long outSize = 0;

            for (int i = 0; i < 8; i++)
            {
                int v = newInStream.ReadByte();
                if (v < 0)
                {
                    throw (new Exception("Can't Read 1"));
                }
                outSize |= ((long)(byte)v) << (8 * i);
            }

            lock (_access) {
                decoder.SetDecoderProperties(properties2);
                long compressedSize = newInStream.Length - newInStream.Position;
                decoder.Code(newInStream, newOutStream, compressedSize, outSize, null);
            }

            newOutStream.Seek(0, SeekOrigin.Begin);
            return(newOutStream);
        }
        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="OriPath">原文件地址</param>
        /// <param name="destPath">目标文件</param>
        public static byte[] Unzip(string filename)
        {
            FileStream   inStream  = new FileStream(filename, FileMode.Open);
            MemoryStream outStream = new MemoryStream();
            Decoder      decoder   = new Decoder();

            byte[] properties = new byte[5];
            if (inStream.Read(properties, 0, 5) != 5)
            {
                throw (new Exception("input .lzma is too short"));
            }
            decoder.SetDecoderProperties(properties);
            long outSize = 0;

            for (int i = 0; i < 8; i++)
            {
                int v = inStream.ReadByte();
                if (v < 0)
                {
                    throw (new Exception("Can't Read 1"));
                }
                outSize |= ((long)(byte)v) << (8 * i);
            }
            long compressedSize = inStream.Length - inStream.Position;

            decoder.Code(inStream, outStream, compressedSize, outSize, null);
            //关闭文件流
            inStream.Close();
            //outStream.Close();
            byte[] b = outStream.ToArray();
            return(b);
        }
Exemple #13
0
        public static void DecompressFileLZMA(string inFile, string outFile)
        {
            using (FileStream input = new FileStream(inFile, FileMode.Open))
            {
                using (FileStream output = new FileStream(outFile, FileMode.Create))
                {
                    SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();

                    byte[] properties = new byte[5];
                    if (input.Read(properties, 0, 5) != 5)
                    {
                        throw (new Exception("input .lzma is too short"));
                    }
                    decoder.SetDecoderProperties(properties);

                    long outSize = 0;
                    for (int i = 0; i < 8; i++)
                    {
                        int v = input.ReadByte();
                        if (v < 0)
                        {
                            throw (new Exception("Can't Read 1"));
                        }
                        outSize |= ((long)(byte)v) << (8 * i);
                    }
                    long compressedSize = input.Length - input.Position;

                    decoder.Code(input, output, compressedSize, outSize, null);
                }
            }
        }
Exemple #14
0
    public static byte[] DecompressLzmaStream(Stream input)
    {
        SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();

        byte[] properties = new byte[5];
        if (input.Read(properties, 0, 5) != 5)
        {
            throw (new Exception("input .lzma is too short"));
        }
        decoder.SetDecoderProperties(properties);

        long outSize = 0;

        for (int i = 0; i < 8; i++)
        {
            int v = input.ReadByte();
            if (v < 0)
            {
                throw (new Exception("Can't Read 1"));
            }
            outSize |= ((long)(byte)v) << (8 * i);
        }
        long compressedSize = input.Length - input.Position;

        byte[] buffer = new byte[outSize];
        using (MemoryStream output = new MemoryStream(buffer))
        {
            decoder.Code(input, output, compressedSize, outSize, null);
        }

        return(buffer);
    }
        private static Stream DecompressInternal(Stream inStream)
        {
            var outStream = new MemoryStream();
            var decoder   = new LZMA.Decoder();

            var properties = new byte[5];

            if (inStream.Read(properties, 0, 5) != 5)
            {
                throw new InvalidDataException("Stream has an invalid header.");
            }
            else
            {
                // Make sure we have the 4 bytes representing the uncompressed size.
                // The Clash of Clans LZMA header's uncompressed size is 4 bytes instead of 8.
                var outSizeBytes = new byte[4];
                if (inStream.Read(outSizeBytes, 0, 4) != 4)
                {
                    throw new InvalidDataException("Stream has an invalid header.");
                }

                var inSize  = inStream.Length - inStream.Position;
                var outSize = BitConverter.ToInt32(outSizeBytes, 0);
                decoder.SetDecoderProperties(properties);
                decoder.Code(inStream, outStream, inSize, outSize, null);
            }

            outStream.Seek(0, SeekOrigin.Begin);
            return(outStream);
        }
Exemple #16
0
        public static void Decompress(Stream input, Stream output, Action <long, long> onProgress = null)
        {
            Decoder decoder = new SevenZip.Compression.LZMA.Decoder();

            byte[] properties = new byte[5];
            if (input.Read(properties, 0, 5) != 5)
            {
                throw new Exception("input .lzma is too short");
            }
            decoder.SetDecoderProperties(properties);

            long fileLength = 0;

            for (int i = 0; i < 8; i++)
            {
                int v = input.ReadByte();
                if (v < 0)
                {
                    throw new Exception("Can't Read 1");
                }
                fileLength |= ((long)(byte)v) << (8 * i);
            }

            ICodeProgress prg = null;

            if (onProgress != null)
            {
                prg = new DelegateCodeProgress(onProgress);
            }
            long compressedSize = input.Length - input.Position;

            decoder.Code(input, output, compressedSize, fileLength, prg);
        }
        public static byte[] Decompress(byte[] inputBytes)
        {
            MemoryStream newInStream = new MemoryStream(inputBytes);

            SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();

            newInStream.Seek(0, 0);
            MemoryStream newOutStream = new MemoryStream();

            byte[] properties2 = new byte[5];
            if (newInStream.Read(properties2, 0, 5) != 5)
                throw (new Exception("input .lzma is too short"));
            long outSize = 0;
            for (int i = 0; i < 8; i++)
            {
                int v = newInStream.ReadByte();
                if (v < 0)
                    throw (new Exception("Can't Read 1"));
                outSize |= ((long)(byte)v) << (8 * i);
            }
            decoder.SetDecoderProperties(properties2);

            long compressedSize = newInStream.Length - newInStream.Position;
            decoder.Code(newInStream, newOutStream, compressedSize, outSize, null);

            byte[] b = newOutStream.ToArray();

            return b;
        }
Exemple #18
0
        public static MemoryStream Decompress(FileStream inStream)
        {
            var decoder = new Decoder();

            var properties = new byte[5];
            if (inStream.Read(properties, 0, 5) != 5)
                throw (new Exception("input .lzma is too short"));
            decoder.SetDecoderProperties(properties);

            long outSize = 0;
            for (var i = 0; i < 8; i++)
            {
                var v = inStream.ReadByte();
                if (v < 0)
                    break;
                outSize |= ((long) (byte) v) << (8 * i);
            }
            var compressedSize = inStream.Length - inStream.Position;

            var outStream = new MemoryStream();
            decoder.Code(inStream, outStream, compressedSize, outSize, null);
            outStream.Flush();
            outStream.Position = 0;
            return outStream;
        }
Exemple #19
0
        int LzmaDecompressBlock(uint packed_size, uint unpacked_size)
        {
            var decoder = new LZMA.Decoder();
            var props   = m_input.ReadBytes(5);

            decoder.SetDecoderProperties(props);
            var buffer = PrepareBuffer(unpacked_size);

            using (var output = new MemoryStream(buffer))
            {
                decoder.Code(m_input, output, packed_size - 5, unpacked_size, null);
                return((int)output.Length);
            }
        }
        public static void StreamDecompress(Stream compressedStream, Stream decompressedStream, long compressedSize, long decompressedSize)
        {
            long    basePosition = compressedStream.Position;
            Decoder decoder      = new Decoder();

            byte[] properties = new byte[5];
            if (compressedStream.Read(properties, 0, 5) != 5)
            {
                throw new Exception("input .lzma is too short");
            }
            decoder.SetDecoderProperties(properties);

            decoder.Code(compressedStream, decompressedStream, compressedSize - 5, decompressedSize, null);
            compressedStream.Position = basePosition + compressedSize;
        }
Exemple #21
0
        public static byte[] Decompress(byte[] data)
        {
            using (MemoryStream instrm = new MemoryStream (data))
            using (MemoryStream outstrm = new MemoryStream ()) {
                Decoder decoder = new Decoder ();
                byte[] tmp = new byte[5];
                instrm.Read (tmp, 0, 5);
                decoder.SetDecoderProperties (tmp);

                instrm.Read (tmp, 0, 4);
                int outsize = (tmp[0] << 24) | (tmp[1] << 16) | (tmp[2] << 8) | tmp[3];
                decoder.Code (instrm, outstrm, data.Length - 9, outsize, null);
                outstrm.Close ();
                return outstrm.ToArray ();
            }
        }
Exemple #22
0
    static void Decompress(Stream input, Stream output)
    {
        SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();
        // Read the decoder properties
        byte[] properties = new byte[5];
        input.Read(properties, 0, 5);

        // Read in the decompress file size.
        byte[] fileLengthBytes = new byte[8];
        input.Read(fileLengthBytes, 0, 8);
        long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

        // Decompress the file.
        coder.SetDecoderProperties(properties);
        coder.Code(input, output, input.Length, fileLength, null);
    }
Exemple #23
0
        private static byte[] LZMADecode(Stream inStream, int compressedSize, int uncompressedSize)
        {
            var decoder    = new LZMA.Decoder();
            var properties = new byte[5];

            if (inStream.Read(properties, 0, 5) != 5)
            {
                throw new ArgumentException("Input data is too short.");
            }
            decoder.SetDecoderProperties(properties);
            using (var outLZMA = new MemoryStream())
            {
                decoder.Code(inStream, outLZMA, compressedSize, uncompressedSize, null);
                return(outLZMA.ToArray());
            }
        }
Exemple #24
0
        public static void DecompressFileLZMA(string sourceFile, string destinationFile)
        {
            // make sure the source file is there
            if (File.Exists(sourceFile) == false)
            {
                throw new FileNotFoundException("Unable to find the specified file.", sourceFile);
            }

            FileStream sourceStream      = null;
            FileStream destinationStream = null;

            try
            {
                var coder = new SevenZip.Compression.LZMA.Decoder();
                sourceStream      = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
                destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write);

                // Read the decoder properties
                byte[] properties = new byte[5];
                sourceStream.Read(properties, 0, 5);

                // Read in the decompress file size.
                byte[] fileLengthBytes = new byte[8];
                sourceStream.Read(fileLengthBytes, 0, 8);
                long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

                coder.SetDecoderProperties(properties);
                coder.Code(sourceStream, destinationStream, sourceStream.Length, fileLength, null);
                destinationStream.Flush();
            }
            finally
            {
                // Make sure we allways close all streams
                if (sourceStream != null)
                {
                    sourceStream.Close();
                }

                //if (compressedStream != null)
                //    compressedStream.Close();

                if (destinationStream != null)
                {
                    destinationStream.Close();
                }
            }
        }
        static public void uncompress(Stream inStream, Stream outStream)
        {
            var coder = new lzma.Decoder();

            // Read the decoder properties
            byte[] properties = new byte[5];
            inStream.Read(properties, 0, 5);

            // Read in the decompress file size.
            byte[] fileLengthBytes = new byte[8];
            inStream.Read(fileLengthBytes, 0, 8);
            long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

            // Decompress the file.
            coder.SetDecoderProperties(properties);
            coder.Code(inStream, outStream, inStream.Length, fileLength, null);
        }
Exemple #26
0
        protected override void Process()
        {
            var decoder = new LZMA.Decoder();

            byte[] properties = new byte[5];

            InputStream.Read(properties, 0, 5);

            byte[] stringLengthBytes = new byte[8];

            InputStream.Read(stringLengthBytes, 0, 8);

            long stringLength = BitConverter.ToInt64(stringLengthBytes, 0);

            decoder.SetDecoderProperties(properties);
            decoder.Code(InputStream, OutputStream, InputStream.Length, stringLength, null);
        }
Exemple #27
0
        /// <summary>
        /// Decompress LZMA compressed stream into outStream.
        /// Remeber to set desirable positions in input and output streams.
        /// </summary>
        /// <param name="inStream">Input stream</param>
        /// <param name="outStream">Output stream</param>
        public static void Decompress(Stream inStream, Stream outStream)
        {
            byte[] properties = new byte[5];
            if (inStream.Read(properties, 0, 5) != 5)
            {
                throw (new Exception("Input stream is too short."));
            }

            Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder();
            decoder.SetDecoderProperties(properties);

            var  br = new BinaryReader(inStream, Encoding.UTF8);
            long decompressedSize = br.ReadInt64();
            long compressedSize   = br.ReadInt64();

            decoder.Code(inStream, outStream, compressedSize, decompressedSize, null);
        }
        public static void Decompress(Stream inputStream, Stream outputStream, long outputSize)
        {
            Decoder decoder = new Decoder();

            var properties2 = new byte[5];

            if (inputStream.Read(properties2, 0, 5) != 5)
            {
                throw new Exception("Input LZMA is too short");
            }

            decoder.SetDecoderProperties(properties2);

            var compressedSize = inputStream.Length - inputStream.Position;

            decoder.Code(inputStream, outputStream, compressedSize, outputSize, null);
        }
Exemple #29
0
        private static byte[] LZMADecode(byte[] inputData, int uncompressedSize)
        {
            if (inputData.Length < 5)
            {
                throw new ArgumentException("Input data is too short.");
            }
            var decoder    = new LZMA.Decoder();
            var properties = new byte[5];

            Array.Copy(inputData, 0, properties, 0, 5);
            decoder.SetDecoderProperties(properties);
            using (var outLZMA = new MemoryStream())
                using (var inLZMA = new MemoryStream(inputData, 13, inputData.Length - 13))
                {
                    decoder.Code(inLZMA, outLZMA, inputData.Length - 13, uncompressedSize, null);
                    return(outLZMA.ToArray());
                }
        }
Exemple #30
0
    public static byte[] DecompressFileLZMA(byte[] inBytes)
    {
        SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();
        var input = new MemoryStream(inBytes);
        var output = new MemoryStream();

        byte[] ret = null;


        try
        {
            // Read the decoder properties
            byte[] properties = new byte[5];
            input.Read(properties, 0, 5);

            // Read in the decompress file size.
            byte[] fileLengthBytes = new byte[8];
            input.Read(fileLengthBytes, 0, 8);
            long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

            // Decompress the file.
            coder.SetDecoderProperties(properties);
            coder.Code(input, output, input.Length, fileLength, null);
            output.Flush();

            ret = output.GetBuffer();
        }
        catch
        {
            throw;
        }
        finally
        {
            output.Close();
            output.Dispose();
            input.Close();
            input.Dispose();

        }


        return ret;

    }
Exemple #31
0
        public void uncompress(Stream inStream, Stream outStream)
        {

            var coder = new lzma.Decoder();

            // Read the decoder properties
            byte[] properties = new byte[5];
            inStream.Read(properties, 0, 5);

            // Read in the decompress file size.
            byte[] fileLengthBytes = new byte[8];
            inStream.Read(fileLengthBytes, 0, 8);
            long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

            // Decompress the file.
            coder.SetDecoderProperties(properties);
            coder.Code(inStream, outStream, inStream.Length, fileLength, null);

        }
Exemple #32
0
        // 使用LZMA算法解压文件
        public static void DecompressFileLZMA(string inFile, string outFile)
        {
            SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();
            FileStream input  = new FileStream(inFile, FileMode.Open);
            FileStream output = new FileStream(outFile, FileMode.Create);

            byte[] properties = new byte[5];
            input.Read(properties, 0, 5);

            byte[] fileLengthBytes = new byte[8];
            input.Read(fileLengthBytes, 0, 8);
            long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

            coder.SetDecoderProperties(properties);
            coder.Code(input, output, input.Length, fileLength, null);
            output.Flush();
            output.Close();
            input.Close();
        }
Exemple #33
0
    public static void DecompressFileLZMA(string inFile, string outFile)
    {
        SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();
        FileStream input = new FileStream(inFile, FileMode.Open);
        FileStream output = new FileStream(outFile, FileMode.Create);

        // Read the decoder properties
        byte[] properties = new byte[5];
        input.Read(properties, 0, 5);

        // Read in the decompress file size.
        byte[] fileLengthBytes = new byte[8];
        input.Read(fileLengthBytes, 0, 8);
        long fileLength = System.BitConverter.ToInt64(fileLengthBytes, 0);

        coder.SetDecoderProperties(properties);
        coder.Code(input, output, input.Length, fileLength, null);
        output.Flush();
        output.Close();
    }
Exemple #34
0
        public static MemoryStream StreamDecompress(MemoryStream newInStream, long outSize)
        {
            Decoder decoder = new Decoder();

            newInStream.Seek(0, 0);
            MemoryStream newOutStream = new MemoryStream();

            byte[] properties2 = new byte[5];
            if (newInStream.Read(properties2, 0, 5) != 5)
            {
                throw (new Exception("input .lzma is too short"));
            }
            decoder.SetDecoderProperties(properties2);

            long compressedSize = newInStream.Length - newInStream.Position;

            decoder.Code(newInStream, newOutStream, compressedSize, outSize, null);

            newOutStream.Position = 0;
            return(newOutStream);
        }
Exemple #35
0
        private static byte[] decompress(byte[] compressed)
        {
            byte[] retVal = null;

            SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();

            using (Stream strmInStream = new MemoryStream(compressed))
            {
                strmInStream.Seek(0, 0);

                using (MemoryStream strmOutStream = new MemoryStream())
                {
                    byte[] properties2 = new byte[5];
                    if (strmInStream.Read(properties2, 0, 5) != 5)
                    {
                        throw (new Exception("input .lzma is too short"));
                    }

                    long outSize = 0;
                    for (int i = 0; i < 8; i++)
                    {
                        int v = strmInStream.ReadByte();
                        if (v < 0)
                        {
                            throw (new Exception("Can't Read 1"));
                        }
                        outSize |= ((long)(byte)v) << (8 * i);
                    } //Next i

                    decoder.SetDecoderProperties(properties2);

                    long compressedSize = strmInStream.Length - strmInStream.Position;
                    decoder.Code(strmInStream, strmOutStream, compressedSize, outSize, null);

                    retVal = strmOutStream.ToArray();
                } // End Using newOutStream
            }     // End Using newInStream

            return(retVal);
        }
Exemple #36
0
        protected override string DecompressAll(Stream fileList, Stream compressedStream)
        {
            var sfs = new SparseFileWriterStream(fileList);

            byte[] buffer  = new byte[5];
            var    decoder = new Decoder();

            compressedStream.Read(buffer, 0, 5);
            decoder.SetDecoderProperties(buffer);

            try
            {
                decoder.Code(compressedStream, sfs, compressedStream.Length - compressedStream.Position, sfs.Length,
                             null);
            }
            finally
            {
                sfs.Close();
            }

            return(sfs.BaseDirectory);
        }
        public static void DecompressFileLZMA(string inFile, string outFile)
        {
            Decoder    coder  = new Decoder();
            FileStream input  = new FileStream(inFile, FileMode.Open);
            FileStream output = new FileStream(outFile, FileMode.Create);

            // Read the decoder properties
            byte[] properties = new byte[5];
            input.Read(properties, 0, 5);

            // Read in the decompress file size.
            byte[] fileLengthBytes = new byte[8];
            input.Read(fileLengthBytes, 0, 8);
            long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

            // Decompress the file.
            coder.SetDecoderProperties(properties);
            coder.Code(input, output, input.Length, fileLength, null);
            output.Flush();
            output.Close();
            input.Close();
        }
        public static byte[] Decompress(byte[] inputBytes, long decompressedSize)
        {
            var compressed = new MemoryStream(inputBytes);
            var decoder = new Decoder();

            var properties2 = new byte[5];
            if (compressed.Read(properties2, 0, 5) != 5)
            {
                throw (new Exception("input .lzma is too short"));
            }

            decoder.SetDecoderProperties(properties2);

            var compressedSize = compressed.Length - compressed.Position;
            var decompressed = new MemoryStream();
            decoder.Code(compressed, decompressed, compressedSize, decompressedSize, null);

            if (decompressed.Length != decompressedSize)
                throw new Exception("Decompression Error");

            return decompressed.ToArray();
        }
        public byte[] readCompressedData(int size)
        {
            Decoder decoder = new Decoder ();

            MemoryStream newOutStream = new MemoryStream ();

            int compressedSize = readLittleInt ();

            byte[] properties = ReadBytes (5);
            if (properties.Length == 0) {
                throw new IOException ("End of file reached while parsing the file!");
            }

            long accPos = BaseStream.Position;

            decoder.SetDecoderProperties (properties);
            decoder.Code (BaseStream, newOutStream, compressedSize, size, null);

            BaseStream.Position = accPos + compressedSize;

            return newOutStream.ToArray ();
        }
    static bool DeCompressFile(string inFile, string outFile)
    {
        SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();
        FileStream input  = new FileStream(inFile, FileMode.Open);
        FileStream output = new FileStream(outFile, FileMode.Create);

        // Read the decoder properties
        byte[] properties = new byte[5];
        input.Read(properties, 0, 5);

        // Read in the decompress file size.
        byte [] fileLengthBytes = new byte[8];
        input.Read(fileLengthBytes, 0, 8);
        long fileLength = System.BitConverter.ToInt64(fileLengthBytes, 0);

        // Decompress the file.
        coder.SetDecoderProperties(properties);
        coder.Code(input, output, input.Length, fileLength, null);
        output.Flush();
        output.Close();
        input.Close();
        return(true);
    }
Exemple #41
0
	public static byte[] Decompress(Byte[] bytes) {
		Decoder coder = new Decoder();

		using (MemoryStream input = new MemoryStream(bytes)) {
			using (MemoryStream output = new MemoryStream()) {
				// Read the decoder properties
				byte[] properties = new byte[5];
				input.Read(properties, 0, 5);

				// Read in the decompress file size.
				byte[] fileLengthBytes = new byte[8];
				input.Read(fileLengthBytes, 0, 8);
				long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

				// Decompress the file.
				coder.SetDecoderProperties(properties);
				coder.Code(input, output, input.Length, fileLength, null);
				
				return output.ToArray();
			}
		}


	}
Exemple #42
0
 public static byte[] Decompress(Stream inStream)
 {
     MemoryStream outStream = new MemoryStream();
     byte[] buffer = new byte[5];
     if (inStream.Read(buffer, 0, 5) != 5)
     {
         throw new Exception("Err");
     }
     Decoder decoder = new Decoder();
     decoder.SetDecoderProperties(buffer);
     long outSize = 0L;
     for (int i = 0; i < 8; i++)
     {
         int num3 = inStream.ReadByte();
         if (num3 < 0)
         {
             throw new Exception("Err");
         }
         outSize |= ((byte)num3) << (8 * i);
     }
     long inSize = inStream.Length - inStream.Position;
     decoder.Code(inStream, outStream, inSize, outSize, null);
     return outStream.ToArray();
 }
    public static void DecompressByteLZMA(byte[] inFileBytes, string outFile)
    {
        SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();


        MemoryStream memoryStream = new MemoryStream(inFileBytes);

        string path = outFile.Substring(0, outFile.LastIndexOf("/"));

        Debug.Log("OutFile is " + path);

        if (!Directory.Exists(path))
        {
            Debug.Log("CreateDirectory : " + path);
            Directory.CreateDirectory(path);
        }


        FileStream output = new FileStream(outFile, FileMode.Create);


        // Read the decoder properties
        byte[] properties = new byte[5];
        memoryStream.Read(properties, 0, 5);

        // Read in the decompress file size.
        byte [] fileLengthBytes = new byte[8];
        memoryStream.Read(fileLengthBytes, 0, 8);
        long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

        // Decompress the file.
        coder.SetDecoderProperties(properties);
        coder.Code(memoryStream, output, memoryStream.Length, fileLength, null);
        output.Flush();
        output.Close();
    }
Exemple #44
0
        public void Extract(
            FileStream inStream,
            ArchiveDatabaseEx db,
            uint fileIndex,
            out uint blockIndex,
            out byte[] outBuffer,
            out ulong outBufferSize,
            ref ulong offset,
            ref ulong outSizeProcessed)
        {
            //Console.WriteLine("Extract >>>");
            uint folderIndex = db.FileIndexToFolderIndexMap[fileIndex];
            offset = 0;
            outSizeProcessed = 0;
            outBuffer = null;
            if (folderIndex == UInt32.MaxValue)
            {
                //Console.WriteLine("folderindex is UInt32.MaxValue");
                blockIndex = folderIndex;
                outBuffer = null;
                outBufferSize = 0;
                return;
            }

            Folder folder = db.Database.Folders[folderIndex];
            ulong unPackSize = folder.GetUnPackSize();
            //#ifndef _LZMA_IN_CB
            ulong packSize = db.GetFolderFullPackSize(folderIndex);
            //Console.WriteLine("packsize: " + packSize + " unpacksize: " + unPackSize );
            //Byte *inBuffer = 0;
            //size_t processedSize;
            //#endif
            blockIndex = folderIndex;
            outBuffer = null;

            //Console.WriteLine("folderstreampos: " + db.GetFolderStreamPos(folderIndex, 0));
            inStream.Seek((long)db.GetFolderStreamPos(folderIndex, 0), SeekOrigin.Begin);
            //RINOK(inStream->Seek(inStream, SzArDbGetFolderStreamPos(db, folderIndex, 0)));

            //#ifndef _LZMA_IN_CB
            //if (packSize != 0)
            //{
            // inBuffer = (Byte *)allocTemp->Alloc((size_t)packSize);
            // if (inBuffer == 0)
            //   return SZE_OUTOFMEMORY;
            // }
            // res = inStream->Read(inStream, inBuffer, (size_t)packSize, &processedSize);
            // if (res == SZ_OK && processedSize != (size_t)packSize)
            //  res = SZE_FAIL;
            //#endif
            outBufferSize = unPackSize;
            if (unPackSize != 0)
            {
                outBuffer = new byte[unPackSize];
                //*outBuffer = (Byte *)allocMain->Alloc((size_t)unPackSize);
                //if (*outBuffer == 0)
                //res = SZE_OUTOFMEMORY;
            }
            ulong outRealSize;
            Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder();
            decoder.SetDecoderProperties(folder.Coders[0].Properties);
            Stream outStream = new MemoryStream(outBuffer);
            decoder.Code(inStream, outStream, (long)packSize, (long)unPackSize, null);
            //SzDecode(db.Database.PackSizes +
            // db.FolderStartPackStreamIndex[folderIndex], folder,
            //  //#ifdef _LZMA_IN_CB
            // inStream,
            // //#else
            // //inBuffer,
            //   //#endif
            //outBuffer, unPackSize, out outRealSize);
            outStream.Close();
            //Console.WriteLine( Encoding.UTF8.GetString( outBuffer, 0, (int)unPackSize ) );
            outRealSize = unPackSize;
            if (outRealSize == unPackSize)
            {
                //if (folde.UnPackCRCDefined)
                //{
                // if (!CrcVerifyDigest(folder->UnPackCRC, *outBuffer, (size_t)unPackSize))
                //  res = SZE_FAIL;
                //}
            }
            else
            {
                throw new Exception("Unpack size was different from packsize: " + unPackSize + " vs " + outRealSize);
                //res = SZE_FAIL;
            }
            UInt32 i;
            FileItem fileItem = db.Database.Files[fileIndex];
            offset = 0;
            for (i = db.FolderStartFileIndex[folderIndex]; i < fileIndex; i++)
            {
                offset += (UInt32)db.Database.Files[i].Size;
            }
            outSizeProcessed = fileItem.Size;
            if (offset + outSizeProcessed > outBufferSize)
            {
                throw new Exception("offset + outsizeprocessed > outbuffersize " + offset + " + " + outSizeProcessed + " > " + outBufferSize);
                // return SZE_FAIL;
            }
            //if (fileItem->IsFileCRCDefined)
            //{
            //   if (!CrcVerifyDigest(fileItem->FileCRC, *outBuffer + *offset, *outSizeProcessed))
            //      res = SZE_FAIL;
            //}
        }
 /// <summary>
 /// Decompress byte array compressed with LZMA algorithm (C# inside)
 /// </summary>
 /// <param name="data">Byte array to decompress</param>
 /// <returns>Decompressed byte array</returns>
 public static byte[] ExtractBytes(byte[] data)
 {
     using (var inStream = new MemoryStream(data))
     {
         var decoder = new Decoder();
         inStream.Seek(0, 0);
         using (var outStream = new MemoryStream())
         {
             long outSize;
             decoder.SetDecoderProperties(GetLzmaProperties(inStream, out outSize));
             decoder.Code(inStream, outStream, inStream.Length - inStream.Position, outSize, null);
             return outStream.ToArray();
         }
     }
 }
    /// <summary>Uncompresses a stream of LZMA-compressed data into a memory stream</summary>
    /// <param name="source">Source stream containing the LZMA-compressed data</param>
    /// <param name="compressedLength">Length of the compressed data</param>
    /// <param name="uncompressedLength">Length the uncompressed data will have</param>
    /// <returns>A memory stream containing the uncompressed data</returns>
    private Stream uncompress(Stream source, int compressedLength, int uncompressedLength) {
      BinaryReader reader = new BinaryReader(source);

      // Build a memory chunk we can uncompress into
      MemoryStream uncompressedMemory = new MemoryStream(uncompressedLength);

      // Set up the LZMA decoder and decode the compressed asset into the memory chunk
      Decoder decoder = new Decoder();
      decoder.SetDecoderProperties(reader.ReadBytes(5));
      decoder.Code(
        source, uncompressedMemory,
        compressedLength - 5, uncompressedLength,
        null
      );

      // Done, set the file pointer to the beginning of the memory chunk and return it
      uncompressedMemory.Position = 0;
      return uncompressedMemory;
    }
Exemple #47
0
        /// <summary>
        /// Decompresses the specified <see cref="byte"/> into
        /// LZMA.
        /// </summary>
        /// <param name="bytes">The <see cref="byte"/> array to decompress.</param>
        /// <returns>The decompressed <see cref="byte"/> array.</returns>
        public static byte[] Decompress(byte[] bytes)
        {
            if (bytes == null)
                throw new ArgumentNullException("bytes");

            using (var outStream = new MemoryStream())
            {
                using (var inStream = new MemoryStream(bytes))
                {
                    var decoder = new Decoder();

                    inStream.Seek(0, 0);
                    var properties = new byte[5];
                    if (inStream.Read(properties, 0, 5) != 5)
                        throw new Exception("Input .lzma is too short");

                    var outSize = 0L;
                    for (int i = 0; i < 8; i++)
                    {
                        var v = inStream.ReadByte();
                        if (v < 0)
                            throw new Exception("Can't Read 1");

                        outSize |= ((long)v) << (8 * i);
                    }

                    var compressedSize = inStream.Length - inStream.Position;
                    decoder.SetDecoderProperties(properties);
                    decoder.Code(inStream, outStream, compressedSize, outSize, null);
                    return outStream.ToArray();
                }
            }
        }
        public static bool Decompress(string FileName, out  byte[] outputBytes)
        {
            outputBytes = null;
            if (!File.Exists(FileName))
                return false;

            using(FileStream newInStream = File.OpenRead(FileName)) {
                SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
                newInStream.Seek(0, 0);
                MemoryStream newOutStream = new MemoryStream();
                byte[] properties2 = new byte[5];
                if (newInStream.Read(properties2, 0, 5) != 5)
                    throw (new Exception("input .lzma is too short"));
                long outSize = 0;
                for (int i = 0; i < 8; i++) {
                    int v = newInStream.ReadByte();
                    if (v < 0)
                        throw (new Exception("Can't Read 1"));
                    outSize |= ((long)(byte)v) << (8 * i);
                }
                decoder.SetDecoderProperties(properties2);
                long compressedSize = newInStream.Length - newInStream.Position;
                decoder.Code(newInStream, newOutStream, compressedSize, outSize, null);
                outputBytes = newOutStream.ToArray();
            }
            return true;
        }
Exemple #49
0
    private static void Decompress(Stream inputStream, Stream outputStream)
    {
        SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();

        // Read decoder properties
        byte[] properties = new byte[5]; // 5 comes from kPropSize (LzmaEncoder.cs)
        inputStream.Read(properties, 0, 5);

        // Read the size of the output stream.
        byte [] fileLengthBytes = new byte[8];
        inputStream.Read(fileLengthBytes, 0, 8);
        long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

        // Decode
        coder.SetDecoderProperties(properties);
        coder.Code(inputStream, outputStream, inputStream.Length, fileLength, null);
        outputStream.Flush();
        outputStream.Close();
    }
Exemple #50
0
 static byte[] SevenZipDecompress(byte[] data)
 {
     var reader = new BinaryReader(new MemoryStream(data));
     int totalSize = reader.ReadInt32();
     var props = reader.ReadBytes(5);
     var decoder = new Decoder();
     decoder.SetDecoderProperties(props);
     if ((long)totalSize != reader.ReadInt64())
         throw new ApplicationException("Invalid total size");
     long compressedSize = data.Length - props.Length - 8;
     var decompressed = new byte[totalSize];
     decoder.Code(reader.BaseStream, new MemoryStream(decompressed, true), compressedSize, totalSize, null);
     return decompressed;
 }
		public static byte[] SevenZipDecompress(byte[] data) {
			var reader = new BinaryReader(new MemoryStream(data));
			var props = reader.ReadBytes(5);
			var decoder = new Decoder();
			decoder.SetDecoderProperties(props);
			long totalSize = reader.ReadInt64();
			long compressedSize = data.Length - props.Length - 8;
			var decompressed = new byte[totalSize];
			decoder.Code(reader.BaseStream, new MemoryStream(decompressed, true), compressedSize, totalSize, null);
			return decompressed;
		}
 /// <summary>
 /// Decompress the specified stream (C# inside)
 /// </summary>
 /// <param name="inStream">The source compressed stream</param>
 /// <param name="outStream">The destination uncompressed stream</param>
 /// <param name="inLength">The length of compressed data (null for inStream.Length)</param>
 /// <param name="codeProgressEvent">The event for handling the code progress</param>
 public static void DecompressStream(Stream inStream, Stream outStream, int? inLength,
                                     EventHandler<ProgressEventArgs> codeProgressEvent)
 {
     if (!inStream.CanRead || !outStream.CanWrite)
     {
         throw new ArgumentException("The specified streams are invalid.");
     }
     var decoder = new Decoder();
     long outSize, inSize = (inLength.HasValue ? inLength.Value : inStream.Length) - inStream.Position;
     decoder.SetDecoderProperties(GetLzmaProperties(inStream, out outSize));
     decoder.Code(
         inStream, outStream, inSize, outSize,
         new LzmaProgressCallback(inSize, codeProgressEvent));
 }
Exemple #53
0
        public static void ExpandNatives()
        {
            Decoder decoder = new Decoder();
            MemoryStream output = new MemoryStream();
            using (Stream input = File.Open(Path.Combine(DotMinecraft, "bin/" + GetNativeArchiveFile()), FileMode.Open))
            {
                Decoder coder = new Decoder();

                byte[] properties = new byte[5];
                input.Read(properties, 0, 5);

                // Read in the decompress file size.
                byte[] fileLengthBytes = new byte[8];
                input.Read(fileLengthBytes, 0, 8);
                long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

                coder.SetDecoderProperties(properties);
                coder.Code(input, output, input.Length, fileLength, null);
            }
            output.Seek(0, SeekOrigin.Begin);
            FastZip fastZip = new FastZip();
            fastZip.ExtractZip(output, Path.Combine(DotMinecraft, "bin/natives"), FastZip.Overwrite.Always, null, "", "", false, false);
            File.Delete(Path.Combine(DotMinecraft, "bin/" + GetNativeArchiveFile()));
        }
Exemple #54
0
		static int Main2(string[] args)
		{
			System.Console.WriteLine("\nLZMA# 4.61  2008-11-23\n");

			if (args.Length == 0)
			{
				PrintHelp();
				return 0;
			}

			SwitchForm[] kSwitchForms = new SwitchForm[13];
			int sw = 0;
			kSwitchForms[sw++] = new SwitchForm("?", SwitchType.Simple, false);
			kSwitchForms[sw++] = new SwitchForm("H", SwitchType.Simple, false);
			kSwitchForms[sw++] = new SwitchForm("A", SwitchType.UnLimitedPostString, false, 1);
			kSwitchForms[sw++] = new SwitchForm("D", SwitchType.UnLimitedPostString, false, 1);
			kSwitchForms[sw++] = new SwitchForm("FB", SwitchType.UnLimitedPostString, false, 1);
			kSwitchForms[sw++] = new SwitchForm("LC", SwitchType.UnLimitedPostString, false, 1);
			kSwitchForms[sw++] = new SwitchForm("LP", SwitchType.UnLimitedPostString, false, 1);
			kSwitchForms[sw++] = new SwitchForm("PB", SwitchType.UnLimitedPostString, false, 1);
			kSwitchForms[sw++] = new SwitchForm("MF", SwitchType.UnLimitedPostString, false, 1);
			kSwitchForms[sw++] = new SwitchForm("EOS", SwitchType.Simple, false);
			kSwitchForms[sw++] = new SwitchForm("SI", SwitchType.Simple, false);
			kSwitchForms[sw++] = new SwitchForm("SO", SwitchType.Simple, false);
			kSwitchForms[sw++] = new SwitchForm("T", SwitchType.UnLimitedPostString, false, 1);


			Parser parser = new Parser(sw);
			try
			{
				parser.ParseStrings(kSwitchForms, args);
			}
			catch
			{
				return IncorrectCommand();
			}

			if (parser[(int)Key.Help1].ThereIs || parser[(int)Key.Help2].ThereIs)
			{
				PrintHelp();
				return 0;
			}

			System.Collections.ArrayList nonSwitchStrings = parser.NonSwitchStrings;

			int paramIndex = 0;
			if (paramIndex >= nonSwitchStrings.Count)
				return IncorrectCommand();
			string command = (string)nonSwitchStrings[paramIndex++];
			command = command.ToLower();

			bool dictionaryIsDefined = false;
			Int32 dictionary = 1 << 21;
			if (parser[(int)Key.Dictionary].ThereIs)
			{
				Int32 dicLog;
				if (!GetNumber((string)parser[(int)Key.Dictionary].PostStrings[0], out dicLog))
					IncorrectCommand();
				dictionary = (Int32)1 << dicLog;
				dictionaryIsDefined = true;
			}
			string mf = "bt4";
			if (parser[(int)Key.MatchFinder].ThereIs)
				mf = (string)parser[(int)Key.MatchFinder].PostStrings[0];
			mf = mf.ToLower();

			if (command == "b")
			{
				const Int32 kNumDefaultItereations = 10;
				Int32 numIterations = kNumDefaultItereations;
				if (paramIndex < nonSwitchStrings.Count)
					if (!GetNumber((string)nonSwitchStrings[paramIndex++], out numIterations))
						numIterations = kNumDefaultItereations;
				return LzmaBench.LzmaBenchmark(numIterations, (UInt32)dictionary);
			}

			string train = "";
			if (parser[(int)Key.Train].ThereIs)
				train = (string)parser[(int)Key.Train].PostStrings[0];

			bool encodeMode = false;
			if (command == "e")
				encodeMode = true;
			else if (command == "d")
				encodeMode = false;
			else
				IncorrectCommand();

			bool stdInMode = parser[(int)Key.StdIn].ThereIs;
			bool stdOutMode = parser[(int)Key.StdOut].ThereIs;

			Stream inStream = null;
			if (stdInMode)
			{
				throw (new Exception("Not implemeted"));
			}
			else
			{
				if (paramIndex >= nonSwitchStrings.Count)
					IncorrectCommand();
				string inputName = (string)nonSwitchStrings[paramIndex++];
				inStream = new FileStream(inputName, FileMode.Open, FileAccess.Read);
			}

			FileStream outStream = null;
			if (stdOutMode)
			{
				throw (new Exception("Not implemeted"));
			}
			else
			{
				if (paramIndex >= nonSwitchStrings.Count)
					IncorrectCommand();
				string outputName = (string)nonSwitchStrings[paramIndex++];
				outStream = new FileStream(outputName, FileMode.Create, FileAccess.Write);
			}

			FileStream trainStream = null;
			if (train.Length != 0)
				trainStream = new FileStream(train, FileMode.Open, FileAccess.Read);

			if (encodeMode)
			{
				if (!dictionaryIsDefined)
					dictionary = 1 << 23;

				Int32 posStateBits = 2;
				Int32 litContextBits = 3; // for normal files
				// UInt32 litContextBits = 0; // for 32-bit data
				Int32 litPosBits = 0;
				// UInt32 litPosBits = 2; // for 32-bit data
				Int32 algorithm = 2;
				Int32 numFastBytes = 128;

				bool eos = parser[(int)Key.EOS].ThereIs || stdInMode;

				if (parser[(int)Key.Mode].ThereIs)
					if (!GetNumber((string)parser[(int)Key.Mode].PostStrings[0], out algorithm))
						IncorrectCommand();

				if (parser[(int)Key.FastBytes].ThereIs)
					if (!GetNumber((string)parser[(int)Key.FastBytes].PostStrings[0], out numFastBytes))
						IncorrectCommand();
				if (parser[(int)Key.LitContext].ThereIs)
					if (!GetNumber((string)parser[(int)Key.LitContext].PostStrings[0], out litContextBits))
						IncorrectCommand();
				if (parser[(int)Key.LitPos].ThereIs)
					if (!GetNumber((string)parser[(int)Key.LitPos].PostStrings[0], out litPosBits))
						IncorrectCommand();
				if (parser[(int)Key.PosBits].ThereIs)
					if (!GetNumber((string)parser[(int)Key.PosBits].PostStrings[0], out posStateBits))
						IncorrectCommand();

				CoderPropID[] propIDs = 
				{
					CoderPropID.DictionarySize,
					CoderPropID.PosStateBits,
					CoderPropID.LitContextBits,
					CoderPropID.LitPosBits,
					CoderPropID.Algorithm,
					CoderPropID.NumFastBytes,
					CoderPropID.MatchFinder,
					CoderPropID.EndMarker
				};
				object[] properties = 
				{
					(Int32)(dictionary),
					(Int32)(posStateBits),
					(Int32)(litContextBits),
					(Int32)(litPosBits),
					(Int32)(algorithm),
					(Int32)(numFastBytes),
					mf,
					eos
				};

				Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder();
				encoder.SetCoderProperties(propIDs, properties);
				encoder.WriteCoderProperties(outStream);
				Int64 fileSize;
				if (eos || stdInMode)
					fileSize = -1;
				else
					fileSize = inStream.Length;
				for (int i = 0; i < 8; i++)
					outStream.WriteByte((Byte)(fileSize >> (8 * i)));
				if (trainStream != null)
				{
					CDoubleStream doubleStream = new CDoubleStream();
					doubleStream.s1 = trainStream;
					doubleStream.s2 = inStream;
					doubleStream.fileIndex = 0;
					inStream = doubleStream;
					long trainFileSize = trainStream.Length;
					doubleStream.skipSize = 0;
					if (trainFileSize > dictionary)
						doubleStream.skipSize = trainFileSize - dictionary;
					trainStream.Seek(doubleStream.skipSize, SeekOrigin.Begin);
					encoder.SetTrainSize((uint)(trainFileSize - doubleStream.skipSize));
				}
				encoder.Code(inStream, outStream, -1, -1, null);
			}
			else if (command == "d")
			{
				byte[] properties = new byte[5];
				if (inStream.Read(properties, 0, 5) != 5)
					throw (new Exception("input .lzma is too short"));
				Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder();
				decoder.SetDecoderProperties(properties);
				if (trainStream != null)
				{
					if (!decoder.Train(trainStream))
						throw (new Exception("can't train"));
				}
				long outSize = 0;
				for (int i = 0; i < 8; i++)
				{
					int v = inStream.ReadByte();
					if (v < 0)
						throw (new Exception("Can't Read 1"));
					outSize |= ((long)(byte)v) << (8 * i);
				}
				long compressedSize = inStream.Length - inStream.Position;
				decoder.Code(inStream, outStream, compressedSize, outSize, null);
			}
			else
				throw (new Exception("Command Error"));
			return 0;
		}
Exemple #55
0
        private void FileDownloaded(Uri uri, byte[] raw, string error, object decompressionTask)
        {
            SetTaskProgressBar_ThreadSafe(100, "");
            if (error != null)
            {
                if (decompressionTask != null)
                {
                    CompressionTask task = decompressionTask as CompressionTask;
                    if (task != null)
                        task.done = true;
                }
                IncrementPatchProgressBar_ThreadSafe(true);
                Log_ThreadSafe("\tFailed to download " + uri.ToString() + ": " + error);
                return;
            }
            if (decompressionTask != null)
            {
                string file = "?";
                CompressionTask task = null;
                Stream inStream = null;
                Stream outStream = null;
                try
                {
                    task = (CompressionTask)decompressionTask;
                    file = task.dl.destination.Name;
                    task.dl.destination.Directory.Create();

                    inStream = task.dl.tempCompressed.OpenRead();
                    outStream = task.dl.destination.OpenWrite();

                    byte[] properties = new byte[5];
                    if (inStream.Read(properties, 0, 5) != 5)
                        throw (new Exception("input .lzma is too short"));
                    LZMA.Decoder decoder = new LZMA.Decoder();
                    decoder.SetDecoderProperties(properties);
                    long outSize = 0;
                    for (int i = 0; i < 8; i++)
                    {
                        int v = inStream.ReadByte();
                        if (v < 0)
                            throw (new Exception("Can't Read 1"));
                        outSize |= ((long)(byte)v) << (8 * i);
                    }
                    long compressedSize = inStream.Length - inStream.Position;

                    decoder.Code(inStream, outStream, compressedSize, outSize, null);
                    task.done = true;
                }
                catch (Exception e)
                {
                    if (task != null)
                        task.done = true;
                    IncrementPatchProgressBar_ThreadSafe(true);
                    Log_ThreadSafe("\tFailed during decompression of " + file + ": " + e.ToString());
                }
                finally
                {
                    if (inStream != null)
                        inStream.Close();
                    if (outStream != null)
                        outStream.Close();
                    try
                    {
                        if (task != null)
                            task.dl.tempCompressed.Delete();
                    }
                    catch (Exception) { }
                }
            }
            IncrementPatchProgressBar_ThreadSafe(false);
        }
Exemple #56
0
        public static byte[] Uncompress(byte[] bytes)
        {
            if (bytes.Length < 5)
            {
                throw new Exception("LZMA data is too short.");
            }

            using (MemoryStream input = new MemoryStream(bytes))
            {
                // read the decoder properties
                byte[] properties = new byte[5];
                if (input.Read(properties, 0, 5) != 5)
                {
                    throw (new Exception("LZMA data is too short."));
                }

                long outSize = 0;

                if (BitConverter.IsLittleEndian)
                {
                    for (int i = 0; i < 8; i++)
                    {
                        int v = input.ReadByte();
                        if (v < 0)
                        {
                            throw (new Exception("Can't Read 1."));
                        }

                        outSize |= ((long)v) << (8 * i);
                    }
                }

                MemoryStream output = new MemoryStream();
                long compressedSize = input.Length - input.Position;
                Decoder decoder = new Decoder();
                decoder.SetDecoderProperties(properties);
                decoder.Code(input, output, compressedSize, outSize, null);
                return output.ToArray();
            }
        }
Exemple #57
0
		static public int LzmaBenchmark(Int32 numIterations, UInt32 dictionarySize)
		{
			if (numIterations <= 0)
				return 0;
			if (dictionarySize < (1 << 18))
			{
				System.Console.WriteLine("\nError: dictionary size for benchmark must be >= 19 (512 KB)");
				return 1;
			}
			System.Console.Write("\n       Compressing                Decompressing\n\n");

			Compression.LZMA.Encoder encoder = new Compression.LZMA.Encoder();
			Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder();


			CoderPropID[] propIDs = 
			{ 
				CoderPropID.DictionarySize,
			};
			object[] properties = 
			{
				(Int32)(dictionarySize),
			};

			UInt32 kBufferSize = dictionarySize + kAdditionalSize;
			UInt32 kCompressedBufferSize = (kBufferSize / 2) + kCompressedAdditionalSize;

			encoder.SetCoderProperties(propIDs, properties);
			System.IO.MemoryStream propStream = new System.IO.MemoryStream();
			encoder.WriteCoderProperties(propStream);
			byte[] propArray = propStream.ToArray();

			CBenchRandomGenerator rg = new CBenchRandomGenerator();

			rg.Set(kBufferSize);
			rg.Generate();
			CRC crc = new CRC();
			crc.Init();
			crc.Update(rg.Buffer, 0, rg.BufferSize);

			CProgressInfo progressInfo = new CProgressInfo();
			progressInfo.ApprovedStart = dictionarySize;

			UInt64 totalBenchSize = 0;
			UInt64 totalEncodeTime = 0;
			UInt64 totalDecodeTime = 0;
			UInt64 totalCompressedSize = 0;

			MemoryStream inStream = new MemoryStream(rg.Buffer, 0, (int)rg.BufferSize);
			MemoryStream compressedStream = new MemoryStream((int)kCompressedBufferSize);
			CrcOutStream crcOutStream = new CrcOutStream();
			for (Int32 i = 0; i < numIterations; i++)
			{
				progressInfo.Init();
				inStream.Seek(0, SeekOrigin.Begin);
				compressedStream.Seek(0, SeekOrigin.Begin);
				encoder.Code(inStream, compressedStream, -1, -1, progressInfo);
				TimeSpan sp2 = DateTime.UtcNow - progressInfo.Time;
				UInt64 encodeTime = (UInt64)sp2.Ticks;

				long compressedSize = compressedStream.Position;
				if (progressInfo.InSize == 0)
					throw (new Exception("Internal ERROR 1282"));

				UInt64 decodeTime = 0;
				for (int j = 0; j < 2; j++)
				{
					compressedStream.Seek(0, SeekOrigin.Begin);
					crcOutStream.Init();

					decoder.SetDecoderProperties(propArray);
					UInt64 outSize = kBufferSize;
					System.DateTime startTime = DateTime.UtcNow;
					decoder.Code(compressedStream, crcOutStream, 0, (Int64)outSize, null);
					TimeSpan sp = (DateTime.UtcNow - startTime);
					decodeTime = (ulong)sp.Ticks;
					if (crcOutStream.GetDigest() != crc.GetDigest())
						throw (new Exception("CRC Error"));
				}
				UInt64 benchSize = kBufferSize - (UInt64)progressInfo.InSize;
				PrintResults(dictionarySize, encodeTime, benchSize, false, 0);
				System.Console.Write("     ");
				PrintResults(dictionarySize, decodeTime, kBufferSize, true, (ulong)compressedSize);
				System.Console.WriteLine();

				totalBenchSize += benchSize;
				totalEncodeTime += encodeTime;
				totalDecodeTime += decodeTime;
				totalCompressedSize += (ulong)compressedSize;
			}
			System.Console.WriteLine("---------------------------------------------------");
			PrintResults(dictionarySize, totalEncodeTime, totalBenchSize, false, 0);
			System.Console.Write("     ");
			PrintResults(dictionarySize, totalDecodeTime,
					kBufferSize * (UInt64)numIterations, true, totalCompressedSize);
			System.Console.WriteLine("    Average");
			return 0;
		}
Exemple #58
0
        /// <summary>
        /// Decompresses the inputBytes from Lzma
        /// </summary>
        /// <param name="inputBytes">Bytes to decompress</param>
        /// <returns>Decompressed bytes</returns>
        public static byte[] Decompress(byte[] inputBytes)
        {
            if (inputBytes == null)
                throw new ArgumentNullException("inputBytes");

            var decoder = new Decoder();
            var newOutStream = new MemoryStream();
            var newInStream = new MemoryStream(inputBytes);

            newInStream.Seek(0, 0);
            var properties2 = new byte[5];
            if (newInStream.Read(properties2, 0, 5) != 5)
                throw (new Exception("input .lzma is too short"));

            var outSize = 0L;
            for (int i = 0; i < 8; i++)
            {
                var v = newInStream.ReadByte();
                if (v < 0)
                    throw (new Exception("Can't Read 1"));

                outSize |= ((long)(byte)v) << (8 * i);
            }

            var compressedSize = newInStream.Length - newInStream.Position;
            decoder.SetDecoderProperties(properties2);
            decoder.Code(newInStream, newOutStream, compressedSize, outSize, null);
            return newOutStream.ToArray();
        }
        /// <summary>
        /// Decompress LZMA compressed stream into outStream.
        /// Remeber to set desirable positions in input and output streams.
        /// </summary>
        /// <param name="inStream">Input stream</param>
        /// <param name="outStream">Output stream</param>
        public static void Decompress(Stream inStream, Stream outStream)
        {
            byte[] properties = new byte[5];
            if (inStream.Read(properties, 0, 5) != 5)
                throw (new Exception("Input stream is too short."));

            Compression.LZMA.Decoder decoder = new Compression.LZMA.Decoder();
            decoder.SetDecoderProperties(properties);

            var br = new BinaryReader(inStream, Encoding.UTF8);
            long decompressedSize = br.ReadInt64();
            long compressedSize = br.ReadInt64();
            decoder.Code(inStream, outStream, compressedSize, decompressedSize, null);
        }
Exemple #60
0
    public void Read(System.IO.Stream instream)
    {
        {//Header
            byte[] b = new byte[28];
            instream.Read(b, 0, 28);
            header.fourcc = BitConverter.ToUInt32(b, 0);
            int fourcc = MakeFourCC('K', 'F', 'N', 'T');
            header.version = BitConverter.ToUInt32(b, 4);
            header.start_ptr = BitConverter.ToUInt32(b, 8);
            header.validate_chars = BitConverter.ToUInt32(b, 12);
            header.non_empty_chars = BitConverter.ToUInt32(b, 16);
            header.char_size = BitConverter.ToUInt32(b, 20);
            header._base = BitConverter.ToInt16(b, 24);
            header.scale = BitConverter.ToInt16(b, 25);
        }
        {
            //instream.Seek(header.start_ptr, System.IO.SeekOrigin.Begin);
            Dictionary<Int32, Int32> nedata = new Dictionary<int, int>();
            for (int i = 0; i < header.non_empty_chars; i++)
            {
                byte[] buf = new byte[8];
                instream.Read(buf, 0, 8);
                int l = BitConverter.ToInt32(buf, 0);
                int r = BitConverter.ToInt32(buf, 4);
                chardata[l] = new KeyValuePair<int, uint>(r, 0);
                //nedata[l] = r;
            }
            Dictionary<Int32, UInt32> vdata = new Dictionary<int, UInt32>();
            for (int i = 0; i < header.validate_chars; i++)
            {
                byte[] buf = new byte[8];
                instream.Read(buf, 0, 8);
                int l = BitConverter.ToInt32(buf, 0);
                uint r = BitConverter.ToUInt32(buf, 4);
                if (chardata.ContainsKey(l))
                {
                    chardata[l] = new KeyValuePair<int, uint>(chardata[l].Key, r);
                }
                else
                {
                    chardata[l] = new KeyValuePair<int, uint>(-1, r);
                }
                //vdata[l] = r;
            }
        }
        {//FontInfo
            fontinfo = new List<font_info>();
            for (int i = 0; i < header.non_empty_chars; i++)
            {
                byte[] buf = new byte[8];
                instream.Read(buf, 0, 8);
                font_info info;
                info.top = BitConverter.ToInt16(buf, 0);
                info.left = BitConverter.ToInt16(buf, 2);
                info.width = BitConverter.ToUInt16(buf, 4);
                info.height = BitConverter.ToUInt16(buf, 6);
                fontinfo.Add(info);
            }
        }
        {//FontData
            float timer = 0;
            fontdata = new List<byte[]>();
            for (int i = 0; i < header.non_empty_chars; i++)
            {
                DateTime t = DateTime.Now;
                byte[] buf = new byte[8];
                instream.Read(buf, 0, 8);
                ulong len = BitConverter.ToUInt64(buf, 0);
                byte[] data = new byte[len];
                instream.Read(data, 0, (int)len);

                System.IO.MemoryStream sin = new System.IO.MemoryStream(data);

                System.IO.MemoryStream sout = new System.IO.MemoryStream();
                SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
                byte[] properties = new byte[5];
                if (sin.Read(properties, 0, 5) != 5)
                    throw (new Exception("input .lzma is too short"));
                decoder.SetDecoderProperties(properties);

                //sin.Read(buf, 0,2);
                int len2 = (int)(header.char_size * header.char_size);

                decoder.Code(sin, sout, (long)len - 5, (int)len2, null);
                sout.Position = 0;
                byte[] decodedata = new byte[len2];
                sout.Read(decodedata, 0, len2);

                fontdata.Add(decodedata);
                DateTime t2 = DateTime.Now;
                float delta = (float)(t2 - t).TotalSeconds;
                timer += delta;
            }
            Debug.Log("Load time total:" + timer + " per:" + (timer / header.non_empty_chars));
        }
    }