示例#1
0
        public static byte[] Decompress(byte[] inputBytes)
        {
            MemoryStream newInStream = new MemoryStream(inputBytes);

            Compression.LZMA.Decoder decoder = new 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);
        }
        /// <summary>
        /// Internal wrapper implementation for SevenZip.Compression.LZMA.Decoder.
        /// </summary>
        /// <exception cref="ArgumentException" />
        /// <exception cref="InsufficientFreeSpaceException" />
        /// <remarks>Implementation taken from LzmaAlone.cs (in the LZMA SDK).</remarks>
        void decoder_DoWork(object sender, DoWorkEventArgs e)
        {
            if (!AllowConcurrentDecoding)
                concurrency.WaitOne(); // block until signaled

            StreamDecoder decoder = (StreamDecoder)sender;
            Stream inStream = (Stream)e.Argument;
            DateTime start = DateTime.Now;
            
            byte[] properties = new byte[5];
            if (inStream.Read(properties, 0, 5) != 5)
                throw new ArgumentException("LZMA input data is too short");

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

            outSize = 0;
            for (int i = 0; i < 8; i++)
            {
                int v = inStream.ReadByte();
                if (v < 0)
                    throw new ArgumentException("LZMA input data is empty/unreadable");
                outSize |= ((long)(byte)v) << (8 * i);
            }

            try
            {
                if (!decoder.FreeSpaceRequired(outSize))
                {
                    Exception blame = new Exception(String.Format("Sorry, {0} didn't explain what went wrong.", this.GetType()));
                    throw new InsufficientFreeSpaceException(blame); // unknown issue; name and shame :)
                }
            }
            catch (InsufficientFreeSpaceException)
            {
                throw; // rethrow; this is already of the correct type
            }
            catch (Exception ex)
            {
                throw new InsufficientFreeSpaceException(ex); // other known issue; convert to InsufficientFreeSpaceException
            }

            long compressedSize = inStream.Length - inStream.Position;
            lzmaDecoder.Code(inStream, outStream, compressedSize, outSize, this);

            TimeSpan elapsed = DateTime.Now - start;
            double speed = outSize / 1024 / elapsed.TotalSeconds;
            Debug.WriteLine(String.Format("LZMA decompression took {0}s. for {1}(c.)/{2}(u.) bytes at {3}KB/s", elapsed.TotalSeconds, compressedSize, outSize, (int)speed));
            
            if (!AllowConcurrentDecoding)
                concurrency.Set(); // reset signal
        }