Example #1
2
        public static void Decompress(Stream inStream, Stream outStream)
        {
            byte[] properties = new byte[5];
            if (inStream.Read(properties, 0, 5) != 5)
                throw new Exception("Input .lzma is too short");

            SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
            decoder.SetDecoderProperties(properties);
            long outSize = 0;
            for (int i = 0; i < 8; i++)
            {
                int v = inStream.ReadByte();
                outSize |= ((long)(byte)v) << (8 * i);
            }
            long compressedSize = inStream.Length - inStream.Position;
            decoder.Code(inStream, outStream, compressedSize, outSize, null);
        }
        public static byte[] Decompress(byte[] inputBytes, int outSize)
        {
            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"));
            decoder.SetDecoderProperties(properties2);

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

            byte[] b = newOutStream.ToArray();
			
			/*if(b.Length > outSize)
			{
				byte[] output = new byte[outSize];
				Array.Copy(b,output,outSize);
				return output;
			}*/

            return b;
        }
Example #3
0
		public static void DecompressStrLZMA (byte[] inBytes, uint inLen, ref byte[] outBytes, ref uint outLen)
		{
			SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder ();
			MemoryStream inStream = new MemoryStream(inBytes);

			//uint saveinsize = inLen;
			//uint saveoutsize = (uint)(saveinsize * 1.1 + 1026 * 16);
			//outBytes = new byte[saveoutsize];
			//MemoryStream outStream = new MemoryStream (outBytes);
            uint saveoutsize = 0;
            MemoryStream outStream = null;

			// 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);

            // 分配解压缓冲区
            saveoutsize = (uint)(fileLength * 1.1 + 1026 * 16);
            outBytes = new byte[saveoutsize];
            outStream = new MemoryStream (outBytes);

			// Decompress the file.
			coder.SetDecoderProperties (properties);
			coder.Code (inStream, outStream, inStream.Length, fileLength, null);		// 输入长度是 inStream.Length ,不是 inStream.Length - LZMA_HEADER_LEN, outSize 要填未压缩后的字符串的长度,inSize,outSize 这两个参数都要填写,否则会报错
			outStream.Flush ();
			outStream.Close ();
			inStream.Close ();

			outLen = (uint)fileLength;		// 返回解压后的长度,就是压缩的时候保存的数据长度
		}
Example #4
0
        public static void Decompress(Stream inStream, Stream outStream, IProgress <ProgressReport> progress)
        {
            byte[] properties = new byte[5];

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

            SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.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;
            var  lzmaProgress   = new LzmaProgress(progress, "Decompressing", outSize, MeasureBy.Output);

            lzmaProgress.SetProgress(0, 0);
            decoder.Code(inStream, outStream, compressedSize, outSize, lzmaProgress);
            lzmaProgress.SetProgress(inStream.Length, outSize);
        }
Example #5
0
 }     // COMPLEATE!
 public static Byte [] DeCompress(FileStream FSTM)
 {
     try
     {
         using (MemoryStream MemStm = new MemoryStream())
         {
             SevenZip.Compression.LZMA.Decoder Decoder = new SevenZip.Compression.LZMA.Decoder();
             Byte [] properties = new Byte [5];
             FSTM.Read(properties, 0, 5);
             Byte [] fileLengthBytes = new Byte [8];
             FSTM.Read(fileLengthBytes, 0, 8);
             Decoder.SetDecoderProperties(properties);
             long len = BitConverter.ToInt64(fileLengthBytes, 0);
             if (len == 0)
             {
                 throw new DataMalFormedException("Byte[] Dupe.AdvancedDupeRevisions.DeCompress(System.IO.FileStream)");
             }
             Decoder.Code(FSTM, MemStm, FSTM.Position - FSTM.Length, len, null);
             return(MemStm.ToArray());
         }
     }
     catch (AdvancedDupeLibraryException)
     {
         throw;
     }
     catch
     {
         throw new LMZAException();
     }
 }
        private 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);
                }
            }

            File.Delete(inFile); //Cleanup
        }
Example #7
0
        public void uncomress(EndiannessAwareBinaryReader reader, BinaryWriter writer, Int32 size)
        {
            bool accurate = true;

            SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();
            Int64 begin             = writer.BaseStream.Position;
            Int32 shift_back        = version == 12 ? 7 : 9;
            Int32 uncompressed_size = version == 12 ? 0x100000 : 0x8000;
            Int32 properties_value  = version == 12 ? 171 : 93;

            while (writer.BaseStream.Position - begin < size)
            {
                Int32 chunk_size = 0;
                if (!accurate)
                {
                    chunk_size = reader.ReadInt32();
                }
                else
                {
                    chunk_size = (Int32)round((UInt32)reader.ReadInt32()) - shift_back;
                }
                byte[] properties = reader.ReadBytes(5);
                if (properties[0] != properties_value || BitConverter.ToInt32(properties, 1) != uncompressed_size)
                {
                    reader.BaseStream.Position -= shift_back;
                    writer.Write(reader.ReadBytes(uncompressed_size));
                }
                else
                {
                    coder.SetDecoderProperties(properties);
                    coder.Code(reader.BaseStream, writer.BaseStream, chunk_size, Math.Min(uncompressed_size, size - (writer.BaseStream.Position - begin)), null);
                    writer.Write(reader.ReadBytes((int)(round((UInt32)reader.BaseStream.Position) - reader.BaseStream.Position)));
                }
            }
        }
Example #8
0
        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;
        }
Example #9
0
 public static string DecompressString(string compressedText, string failedReturn)
 {
     if (compressedText.HasContent(10) == false)
     {
         return(failedReturn);
     }
     try
     {
         MemoryStream msi = new MemoryStream(Convert.FromBase64String(compressedText));
         MemoryStream mso = new MemoryStream();
         SevenZip.Compression.LZMA.Decoder dec = new SevenZip.Compression.LZMA.Decoder();
         byte[] props  = new byte[5]; msi.Read(props, 0, 5);
         byte[] length = new byte[4]; msi.Read(length, 0, 4);
         int    len    = BitConverter.ToInt32(length, 0);
         dec.SetDecoderProperties(props);
         dec.Code(msi, mso, msi.Length, len, null);
         var res = Encoding.UTF8.GetString(mso.ToArray());
         msi.Close(); msi.Dispose();
         mso.Close(); mso.Dispose();
         return(res);
     }
     catch (Exception ex)
     {
         Debug.LogError(ex.Message + " | " + ex.StackTrace);
     }
     return(failedReturn);
 }
Example #10
0
        public static void DecompressFile(string inFile, string outFile, CompressionFormat format)
        {
            using (FileStream input = new FileStream(inFile, FileMode.Open, FileAccess.Read))
                using (FileStream output = new FileStream(outFile, FileMode.Create))
                {
                    if (format == CompressionFormat.LZMA)
                    {
                        // Credit: http://stackoverflow.com/questions/7646328/how-to-use-the-7z-sdk-to-compress-and-decompress-a-file
                        Compression.LZMA.Decoder coder = new 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);

                        coder.SetDecoderProperties(properties);
                        coder.Code(input, output, input.Length, fileLength, null);
                    }
                    else if (format == CompressionFormat.GZIP)
                    {
                        using (GZipStream decompressionStream = new GZipStream(input, CompressionMode.Decompress))
                        {
                            decompressionStream.CopyTo(output);
                        }
                    }
                    else
                    {
                        input.CopyTo(output);
                    }
                }
        }
Example #11
0
        private static SevenZip.Compression.LZMA.Decoder GetDecoder()
        {
            var decoder = new SevenZip.Compression.LZMA.Decoder();

            decoder.SetDecoderProperties(Data.Properties);
            return(decoder);
        }
Example #12
0
        /**  同步解压一个文件  **/
        private static void DeCompress(object obj)
        {
            FileChangeInfo info         = (FileChangeInfo)obj;
            string         inpath       = info.inpath;
            string         outpath      = info.outpath;
            CodeProgress   codeProgress = null;

            codeProgress = new CodeProgress(info.progressPercentDelegate, info.progressDelegate);

            try
            {
                SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
                FileStream inputFS  = new FileStream(inpath, FileMode.Open);
                FileStream outputFS = new FileStream(outpath, FileMode.Create);

                int    propertiesSize = 5;
                byte[] properties     = new byte[propertiesSize];
                inputFS.Read(properties, 0, properties.Length);

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

                decoder.SetDecoderProperties(properties);
                decoder.Code(inputFS, outputFS, inputFS.Length, fileLength, codeProgress);
                outputFS.Flush();
                outputFS.Close();
                inputFS.Close();
                Debug.Log("解压完毕");
            }
            catch (Exception ex)
            {
                Debug.Log(ex);
            }
        }
 public override void DeSerialize(DataReader data)
 {
     header.DeSerialize(data);
     uint n1 = BitConverter.ToUInt32(new byte[] { 1, 0, 0, 0 }, 0);
     data.byteOrder = DataReader.ByteOrder.Little;
     //compress with lzma
     if (!header.signature.Contains("UnityRaw")) {
         SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
         data.position = header.headerSize;
         byte[] properties = data.ReadBytes(5);
         long uncompressFileSize = data.ReadInt64();
         decoder.SetDecoderProperties(properties);
         MemoryStream outMs = new MemoryStream((int)uncompressFileSize);
         decoder.Code(data.BaseStream,outMs,data.BaseStream.Length-header.headerSize,uncompressFileSize,null);
         data.Close();
         data = new DataReader(outMs);
         data.position = 0;
     }
     data.byteOrder = DataReader.ByteOrder.Big;
     numOfEntryCount = data.ReadInt32();
     entrys = new SerializeBundleEntry[numOfEntryCount];
     for (int i = 0; i < numOfEntryCount; i++) {
         entrys[i] = new SerializeBundleEntry();
         entrys[i].DeSerialize(data);
     }
 }
Example #14
0
        /// <summary>
        /// lzma decode
        /// </summary>
        /// <param name="src"></param>
        /// <param name="dst"></param>
        public static void Decode(Stream src, Stream dst)
        {
            byte[] properties = new byte[5];
            if (src.Read(properties, 0, 5) != 5)
            {
                throw (new Exception("input .lzma is too short"));
            }
            var decoder = new SevenZip.Compression.LZMA.Decoder();

            decoder.SetDecoderProperties(properties);
            long outSize = 0;

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

            decoder.Code(src, dst, compressedSize, outSize, null);
        }
Example #15
0
        public static void Decompress(Stream inStream, Stream outStream)
        {
            var decoder = new SevenZip.Compression.LZMA.Decoder();

            var properties = new byte[5];

            if (inStream.Read(properties, 0, 5) != 5)
            {
                throw new FileLoadException();
            }

            decoder.SetDecoderProperties(properties);

            long outSize = 0;

            for (int i = 0; i < 8; i++)
            {
                int v = inStream.ReadByte();
                if (v < 0)
                {
                    throw new FileLoadException();
                }

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

            decoder.Code(inStream, outStream, inStream.Length - inStream.Position, outSize, null);
        }
Example #16
0
        private static void DecompressLZMA(string fileName, string targetFileName)
        {
            byte[] header = new byte[HeaderLength];
            HeaderFWS.CopyTo(header, 0);

            LZMADecoder coder = new LZMADecoder();

            using (FileStream input = new FileStream(fileName, FileMode.Open))
            {
                input.Seek(HeaderTypeLength, SeekOrigin.Begin);
                input.Read(header, HeaderTypeLength, HeaderVersionLength + HeaderSizeLength);

                // Read the decompressed file size.
                byte[] fileLengthBytes = new byte[HeaderSizeLength];
                input.Seek(HeaderTypeLength + HeaderVersionLength, SeekOrigin.Begin);
                input.Read(fileLengthBytes, 0, fileLengthBytes.Length);
                int fileLength = BitConverter.ToInt32(fileLengthBytes, 0);

                input.Seek(HeaderLength + HeaderLZMACompressedSizeLength, SeekOrigin.Begin);

                using (FileStream output = new FileStream(targetFileName, FileMode.Create))
                {
                    output.Write(header, 0, header.Length);

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


                    coder.SetDecoderProperties(properties);
                    coder.Code(input, output, input.Length, fileLength, null);
                }
            }
        }
Example #17
0
        public Stream Decompress(Stream stream)
        {
            using (var reader = new FileReader(stream))
            {
                reader.SetByteOrder(false);

                var output = new System.IO.MemoryStream();
                if (reader.CheckSignature(4, "LZMA", 1))
                {
                    byte unk   = reader.ReadByte(); //0xFF
                    uint magic = reader.ReadUInt32();
                    reader.ReadByte();              //padding
                    UseLZMAMagicHeader = true;
                }

                byte[] properties       = reader.ReadBytes(5); //Property and dictionary size
                ulong  decompressedSize = reader.ReadUInt64();

                var compressedSize = stream.Length - reader.Position;
                var copressedBytes = reader.ReadBytes((int)compressedSize);

                SevenZip.Compression.LZMA.Decoder decode = new SevenZip.Compression.LZMA.Decoder();
                decode.SetDecoderProperties(properties);

                MemoryStream ms = new MemoryStream(copressedBytes);
                decode.Code(ms, output, compressedSize, (int)decompressedSize, null);

                return(output);
            }
        }
        //解压数据
        private void UnLzmaDataToFile(string outPath, byte[] memoryDD, int dl)
        {
            FileStream   output = new FileStream(outPath, FileMode.Create);
            MemoryStream ms     = new MemoryStream(memoryDD);

            try
            {
                SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();
                byte[] properties = new byte[5];
                ms.Read(properties, 0, 5);
                byte[] fileLengthBytes = new byte[8];
                ms.Read(fileLengthBytes, 0, 8);
                long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);
                coder.SetDecoderProperties(properties);
                coder.Code(ms, output, dl, fileLength, null);
                output.Flush();
            }
            catch (Exception exc)
            {
                _isError = true;
                JW.Common.Log.LogE("Un LZMA Data Failed:" + outPath + "---" + exc.ToString());
            }
            finally
            {
                output.Close();
                output.Dispose();
                output = null;
                ms.Close();
                ms.Dispose();
                ms = null;
            }
        }
Example #19
0
        /// <summary>
        /// LZMA解压缩
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string UnCompress(string value)
        {
            byte[] byteArray = Convert.FromBase64String(value);
            byte[] tmpArray;
            using (MemoryStream outStream = new MemoryStream())
            {
                using (MemoryStream inStream = new MemoryStream(byteArray))
                {
                    byte[] properties = new byte[5];
                    if (inStream.Read(properties, 0, 5) != 5)
                    {
                        throw (new Exception("input .lzma is too short"));
                    }
                    SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.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);
                    tmpArray = outStream.ToArray();
                }
            }
            return(Encoding.UTF8.GetString(tmpArray));
        }
Example #20
0
        public byte[] DecompressFromBuffer(byte[] enc)
        {
            SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();

            using (MemoryStream ims = new MemoryStream(enc))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] properties = new byte[5];
                    ims.Read(properties, 0, 5);

                    // Read in the decompress file size.
                    byte[] fileLengthBytes = new byte[8];
                    ims.Read(fileLengthBytes, 0, 8);

                    long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);

                    coder.SetDecoderProperties(properties);
                    coder.Code(ims, ms, ims.Length, fileLength, null);

                    ms.Flush();
                    return(ms.ToArray());
                }
            }
        }
Example #21
0
            public static string Decode(byte[] str)
            {
                string buffer = null;

                MemoryStream ins  = new MemoryStream(str);
                MemoryStream outs = new MemoryStream();

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

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

                lzma.SetDecoderProperties(properties);
                lzma.Code(ins, outs, ins.Length, fileLength, null);

                foreach (var item in outs.ToArray())
                {
                    buffer += (char)item;
                }

                //outs.Flush();
                outs.Close();
                outs.Dispose();

                ins.Close();
                ins.Dispose();

                return(buffer);
            }
Example #22
0
        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);
        }
Example #23
0
        private Stream DecompressLZMA(Stream streamIn)
        {
            var properties = new byte[5];

            if (streamIn.Read(properties, 0, 5) != 5)
            {
                throw (new Exception("input .lzma is too short"));
            }
            var decoder = new SevenZip.Compression.LZMA.Decoder();

            decoder.SetDecoderProperties(properties);

            long outSize = 0;

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

            var outStream = new MemoryStream();

            decoder.Code(streamIn, outStream, compressedSize, outSize, null);
            outStream.Seek(0, 0);
            return(outStream);
        }
Example #24
0
        /// <summary>
        /// Uncompress LZMA compressed swf
        /// </summary>
        protected void inflateLZMA()
        {
            // read size
            br.BaseStream.Position = 4;                            // skip signature
            int size           = Convert.ToInt32(br.ReadUInt32()); // uncompressed size
            int compressedSize = Convert.ToInt32(br.ReadUInt32()); // compressed size

            // read swf head
            byte[] uncompressed = new byte[size];
            br.BaseStream.Position = 0;
            br.Read(uncompressed, 0, 8); // header data (signature and uncompressed size) is not compressed
            br.BaseStream.Position = 12; // skip compressed size

            byte[] properties = br.ReadBytes(5);
            byte[] compressed = br.ReadBytes(compressedSize);

            SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
            decoder.SetDecoderProperties(properties);
            // new memory stream for uncompressed swf
            MemoryStream m = new MemoryStream(uncompressed);

            m.Position = 8;
            decoder.Code(new MemoryStream(compressed), m, compressedSize, size - 8, null);

            br = new BinaryReader(m);
            br.BaseStream.Position = 0;
        }
Example #25
0
            public byte[] Decompress(byte[] buffer)
            {
                MemoryStream result      = new MemoryStream();
                int          outSize     = BitConverter.ToInt32(buffer, 12);
                int          streamCount = (outSize + 0xffff) >> 16;
                int          offset      = 0x18 + streamCount * 2 + 5;

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

                decoder.SetDecoderProperties(new MemoryStream(buffer, 0x18, 5).ToArray());

                for (int i = 0; i < streamCount; i++)
                {
                    int streamSize = (buffer[5 + 0x18 + i * 2]) + (buffer[6 + 0x18 + i * 2] << 8);
                    if (streamSize != 0)
                    {
                        decoder.Code(new MemoryStream(buffer, offset, streamSize), result, streamSize, Math.Min(outSize, 0x10000), null);
                    }
                    else
                    {
                        result.Write(buffer, offset, streamSize = Math.Min(outSize, 0x10000));
                    }
                    outSize -= 0x10000;
                    offset  += streamSize;
                }

                return(result.ToArray());
            }
        // unzip contained 7zip node
        protected override RecordNode DecodeDelegate()
        {
#if DEBUG
            Console.WriteLine("decompressing");
#endif
            List <EsfNode> values           = compressedNode.Values;
            byte[]         data             = (values[0] as EsfValueNode <byte[]>).Value;
            ParentNode     infoNode         = compressedNode.Children[0];
            uint           size             = (infoNode.Values[0] as EsfValueNode <uint>).Value;
            byte[]         decodeProperties = (infoNode.Values[1] as EsfValueNode <byte[]>).Value;

            LzmaDecoder decoder = new LzmaDecoder();
            decoder.SetDecoderProperties(decodeProperties);
            // DecompressionCodeProgress progress = new DecompressionCodeProgress(this);

            byte[] outData = new byte[size];
            using (MemoryStream inStream = new MemoryStream(data, false), outStream = new MemoryStream(outData)) {
                decoder.Code(inStream, outStream, data.Length, size, null);
                outData = outStream.ToArray();
            }
            EsfNode       result;
            AbcaFileCodec codec = new AbcaFileCodec();

            result = codec.Parse(outData);
            using (BinaryReader reader = new BinaryReader(new MemoryStream(outData))) {
                result = codec.Parse(reader);
            }
            return(result as RecordNode);
        }
Example #27
0
        public static byte[] Decompress(byte[] inputBytes, long decompressedSize)
        {
            var compressed = new MemoryStream(inputBytes);

            //var decoder = new Decoder();
            SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.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());
        }
Example #28
0
        static void UnZip(Stream inStream, Stream outStream)
        {
            byte[] properties = new byte[5];
            if (inStream.Read(properties, 0, 5) != 5)
            {
                throw (new Exception("input .lzma is too short"));
            }
            SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.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);
        }
Example #29
0
        public static void DecompressStrLZMA(byte[] inBytes, uint inLen, ref byte[] outBytes, ref uint outLen)
        {
            SevenZip.Compression.LZMA.Decoder coder = new SevenZip.Compression.LZMA.Decoder();
            MemoryStream inStream = new MemoryStream(inBytes);

            uint saveinsize  = inLen;
            uint saveoutsize = (uint)(saveinsize * 1.1 + 1026 * 16);

            outBytes = new byte[saveoutsize];
            MemoryStream outStream = new MemoryStream(outBytes);

            // 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);                         // 输入长度是 inStream.Length ,不是 inStream.Length - LZMA_HEADER_LEN, outSize 要填未压缩后的字符串的长度,inSize,outSize 这两个参数都要填写,否则会报错
            outStream.Flush();
            outStream.Close();
            inStream.Close();

            outLen = (uint)fileLength;                          // 返回解压后的长度,就是压缩的时候保存的数据长度
        }
Example #30
0
        public byte[] DecompressLzma(byte[] data, int decompressedSize)
        {
            try
            {
                var unpackedData = new byte[decompressedSize];

                var hash = SHA1.Create().ComputeHash(data);

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

                var decompressed = new MemoryStream();

                using (var compressed = new MemoryStream(data))
                {
                    var props = new byte[5];

                    compressed.Read(props, 0, 5);
                    decoder.SetDecoderProperties(props);
                    decoder.Code(compressed, decompressed, data.Length, decompressedSize, null);
                }

                return(decompressed.ToArray());
            }
            catch
            {
                return(null);
            }
        }
Example #31
0
        public static byte[] Decompress(CFLLoader.CompressionType compType, byte[] ins)
        {
            if (compType == CFLLoader.CompressionType.None)
            {
                return(ins);
            }

            var coder  = new Decoder();
            var input  = new MemoryStream(ins);
            var output = new MemoryStream();

            // Read the decoder properties
            var properties = new byte[5];

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

            coder.SetDecoderProperties(properties);
            coder.Code(input, output, input.Length, fileLength, null);
            output.Flush();
            output.Position = 0;
            return(output.ToArray());
        }
Example #32
0
        unsafe public static void ReadCompressedDatImageFunction(byte *destination, int destinationLength, byte *source, int sourceLength)
        {
            if (_decoder == null)
            {
                _decoder = new SevenZip.Compression.LZMA.Decoder();
            }

            byte[] coderProperties = new byte[5];
            Marshal.Copy(new IntPtr(source), coderProperties, 0, 5);

            if (_decoderProperties == null ||
                _decoderProperties[0] != coderProperties[0] ||
                _decoderProperties[1] != coderProperties[1] ||
                _decoderProperties[2] != coderProperties[2] ||
                _decoderProperties[3] != coderProperties[3] ||
                _decoderProperties[4] != coderProperties[4])
            {
                _decoderProperties = coderProperties;
                _decoder.SetDecoderProperties(coderProperties);
            }

            using var imageDecompressedStream = new UnmanagedMemoryStream(destination, destinationLength, destinationLength, FileAccess.Write);
            using var imageStream             = new UnmanagedMemoryStream(source + 5, sourceLength - 5, sourceLength - 5, FileAccess.Read);
            _decoder.Code(imageStream, imageDecompressedStream, sourceLength - 5, destinationLength, null);
        }
Example #33
0
        static byte[] DecompressFileLZMA(byte[] data)
        {
            if (null == data)
            {
                return(null);
            }

            var coder  = new SevenZip.Compression.LZMA.Decoder();
            var input  = new System.IO.MemoryStream(data);
            var output = new System.IO.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 = System.BitConverter.ToInt64(fileLengthBytes, 0);

            // Decompress the file.
            coder.SetDecoderProperties(properties);
            coder.Code(input, output, input.Length, fileLength, null);
            output.Flush();
            byte[] returnData = output.ToArray();
            output.Close();
            input.Close();
            return(returnData);
        }
Example #34
0
        public static bool DownloadDependencies(string uri, string outputName)
        {
            // attempt download
            using (var wc = new WebClient())
            {
                var zippedBytes = wc.DownloadData(uri);

                using (var outStream = new MemoryStream())
                {
                    using (var ms = new MemoryStream(zippedBytes))
                    {
                        if (zippedBytes.Length < (5 + 8))
                        {
                            throw (new Exception("input .lzma is too short"));
                        }

                        var    br         = new BinaryReader(ms);
                        byte[] properties = br.ReadBytes(5);
                        var    outSize    = br.ReadInt64();

                        var decoder = new SevenZip.Compression.LZMA.Decoder();
                        decoder.SetDecoderProperties(properties);
                        decoder.Code(ms, outStream, ms.Length - ms.Position, outSize, null);
                    }

                    var outBytes = outStream.ToArray();
                    File.WriteAllBytes(outputName, outBytes);
                }
            }

            return(File.Exists(outputName));
        }
Example #35
0
        public static byte[] Decompress(byte[] inputBytes, int outSize)
        {
            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"));
            }
            decoder.SetDecoderProperties(properties2);

            long compressedSize = newInStream.Length - newInStream.Position;

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

            byte[] b = newOutStream.ToArray();

            /*if(b.Length > outSize)
             * {
             *      byte[] output = new byte[outSize];
             *      Array.Copy(b,output,outSize);
             *      return output;
             * }*/

            return(b);
        }
Example #36
0
        public static byte[] Decompress(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;
        }
Example #37
0
            private void HandleData(BinaryReader reader)
            {
                int compressedSize = _mFileContent.Length - ReplaySize;
                if (!Compressed)
                {
                    _mData = reader.ReadBytes(compressedSize);
                    DataReader = new BinaryReader(new MemoryStream(_mData));
                    return;
                }

                var inData = new byte[compressedSize];
                Array.Copy(_mFileContent, ReplaySize, inData, 0, compressedSize);
                var outData = new byte[Header.DataSize];

                var lzma = new Decoder();
                lzma.SetDecoderProperties(Header.Props);
                lzma.Code(new MemoryStream(inData), new MemoryStream(outData), compressedSize, Header.DataSize, null);

                _mData = outData;
                DataReader = new BinaryReader(new MemoryStream(_mData));
            }
Example #38
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 = BitConverter.ToInt64 (fileLengthBytes, 0);

			// Decompress the file.
			coder.SetDecoderProperties (properties);
			coder.Code (input, output, input.Length, fileLength, null);
			output.Flush ();
			output.Close ();
			input.Close ();
		}
Example #39
0
        private Stream DecompressLZMA(Stream streamIn) {
            byte[] properties = new byte[5];
            if (streamIn.Read(properties, 0, 5) != 5)
                throw (new Exception("input .lzma is too short"));
            SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
            decoder.SetDecoderProperties(properties);

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

            MemoryStream outStream = new MemoryStream();
            decoder.Code(streamIn, outStream, compressedSize, outSize, null);
            outStream.Seek(0, 0);
            return outStream;
        }
Example #40
0
        public static void Decompress(Stream inStream, Stream outStream, IProgress<ProgressReport> progress)
        {
            byte[] properties = new byte[5];

            if (inStream.Read(properties, 0, 5) != 5)
                throw (new Exception("input .lzma is too short"));

            SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.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;
            var lzmaProgress = new LzmaProgress(progress, "Decompressing", outSize, MeasureBy.Output);
            lzmaProgress.SetProgress(0, 0);
            decoder.Code(inStream, outStream, compressedSize, outSize, lzmaProgress);
            lzmaProgress.SetProgress(inStream.Length, outSize);
        }
Example #41
0
            public byte[] Decompress( byte[] buffer )
            {
                MemoryStream result = new MemoryStream();
                int outSize = BitConverter.ToInt32( buffer, 12 );
                int streamCount = ( outSize + 0xffff ) >> 16;
                int offset = 0x18 + streamCount * 2 + 5;

                var decoder = new SevenZip.Compression.LZMA.Decoder();
                decoder.SetDecoderProperties( new MemoryStream( buffer, 0x18, 5 ).ToArray() );

                for ( int i = 0; i < streamCount; i++ ) {
                    int streamSize = ( buffer[5 + 0x18 + i * 2] ) + ( buffer[6 + 0x18 + i * 2] << 8 );
                    if ( streamSize != 0 )
                        decoder.Code( new MemoryStream( buffer, offset, streamSize ), result, streamSize, Math.Min( outSize, 0x10000 ), null );
                    else
                        result.Write( buffer, offset, streamSize = Math.Min( outSize, 0x10000 ) );
                    outSize -= 0x10000;
                    offset += streamSize;
                }

                return result.ToArray();
            }
Example #42
0
File: Z.cs Project: 9001/Loopstream
 public static string lzd(byte[] bytes)
 {
     MemoryStream msi = new MemoryStream(bytes);
     MemoryStream mso = new MemoryStream();
     SevenZip.Compression.LZMA.Decoder dec = new SevenZip.Compression.LZMA.Decoder();
     byte[] props = new byte[5]; msi.Read(props, 0, 5);
     byte[] length = new byte[8]; msi.Read(length, 0, 8);
     long len = BitConverter.ToInt64(length, 0);
     dec.SetDecoderProperties(props);
     dec.Code(msi, mso, msi.Length, len, null);
     bytes = mso.ToArray();
     return Encoding.UTF8.GetString(bytes);
 }
Example #43
0
        /// <summary>
        /// Processes compressed message.
        /// </summary>
        /// <param name="compressed">Compressed message</param>
        public void ProcessCompressedMessage(ref byte[] compressed)
        {
            // write the parts into a stream
             MemoryStream inData = new MemoryStream(compressed);
             inData.Position = 0;

             // decompress the stream
             MemoryStream outData = new MemoryStream();
             Decoder decompressor = new Decoder();

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

             // Read in the decompress file size.
             byte[] dataLengthBytes = new byte[8];
             inData.Read(dataLengthBytes, 0, 8);
             long dataLength = BitConverter.ToInt64(dataLengthBytes, 0);

             decompressor.SetDecoderProperties(properties);
             decompressor.Code(inData, outData, inData.Length, dataLength, null);
             outData.Position = 0;

             // extract the actual message data
             byte messageCode = (byte)outData.ReadByte();
             byte[] messageLengthData = new byte[4];
             outData.Read(messageLengthData, 0, 4);
             int messageLength = BitConverter.ToInt32(messageLengthData, 0);
             byte[] messageData = new byte[messageLength];
             outData.Read(messageData, 0, messageLength);

             Console.WriteLine("> !COMPRESSED-TCP! {0} -> {1} bytes", inData.Length, outData.Length);

             // close the streams
             inData.Close();
             outData.Close();

             // fire the completed multipart message callback
             _CompletedReceivedMultipartCallback(this, ref messageCode, ref messageData);
        }
Example #44
0
        /// <summary>
        /// Decompress stream to stream (most efficient!).
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Stream DecompressAsStream(Stream value)
        {
            Contract.Requires(null != value, "value");

            // Read header
            var properties = new byte[5];
            if (value.Read(properties, 0, 5) != 5) {
                throw new ArgumentException("Stream too short", "value");
            }

            // Prepare decoder
            var decoder = new SevenZip.Compression.LZMA.Decoder();
            decoder.SetDecoderProperties(properties);

            // Retrieve size from header
            long outSize = 0;
            for (int i = 0; i < 8; i++) {
                int a = value.ReadByte();
                if (a < 0) {
                    throw new ArgumentException("Can't read 1", "value");
                }
                outSize |= ((long)(byte)a) << (8 * i);
            }
            long compressedSize = value.Length - value.Position;

            // Prepare output stream
            var outStream = new MemoryStream();
            try {
                // Decode
                decoder.Code(value, outStream, compressedSize, outSize, null);

                // Convert output to string
                return outStream;
            } finally {
                outStream.Dispose();
            }
        }
Example #45
0
        void SzReadAndDecodePackedStreams2(
            FileStream inStream,
            SzData sd,
            out byte[] outBuffer,
            ulong baseOffset,
            ArchiveDatabase db,
            out ulong[] unPackSizes,
            out bool[] digestsDefined,
            out uint[] digests
            )
        {

            uint numUnPackStreams = 0;
            ulong dataStartPos = 0;
            Folder folder;
            ulong unPackSize;
            //ulong outRealSize;
            ulong packSize = 0;
            //uint i = 0;

            outBuffer = null;

            szReadStreamsInfo(sd, out dataStartPos, db, out numUnPackStreams, out unPackSizes,
                out digestsDefined, out digests);
            //RINOK(SzReadStreamsInfo(sd, &dataStartPos, db,
            //&numUnPackStreams,  unPackSizes, digestsDefined, digests, 
            //allocTemp->Alloc, allocTemp));

            dataStartPos += baseOffset;
            if (db.NumFolders != 1)
            {
                throw new Exception("db.NumFolders value unexpected: " + db.NumFolders);
            }

            folder = db.Folders[0];
            unPackSize = folder.GetUnPackSize();

            //Console.WriteLine("datastartpos : " + dataStartPos);
            inStream.Seek((long)dataStartPos, SeekOrigin.Begin);

            //#ifndef _LZMA_IN_CB
            for (uint i = 0; i < db.NumPackStreams; i++)
              packSize += db.PackSizes[i];
          //Console.WriteLine("packsize: " + packSize);

            //RINOK(MySzInAlloc((void **)inBuffer, (size_t)packSize, allocTemp->Alloc));

            //RINOK(SafeReadDirect(inStream, *inBuffer, (size_t)packSize));
            //#endif

            outBuffer = new byte[unPackSize];
            //if (!SzByteBufferCreate(outBuffer, (size_t)unPackSize, allocTemp->Alloc))
             //   return SZE_OUTOFMEMORY;

            byte[]inbuffer = new byte[ packSize ];
            inStream.Read( inbuffer, 0, (int)packSize );

            //Console.WriteLine("NumCoders: " + ( folder.Coders.GetUpperBound(0) + 1 ));
            //Console.WriteLine("Coder: " + folder.Coders[0]);
            //Console.WriteLine("Coder num properties: " + folder.Coders[0].Properties.GetUpperBound(0) + 1);
            //DumpBytes(folder.Coders[0].Properties, folder.Coders[0].Properties.GetUpperBound(0) + 1);

            inStream.Seek((long)dataStartPos, SeekOrigin.Begin);
            //byte[] properties = new byte[5];
            //inStream.Read(properties, 0, 5);
            Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
            decoder.SetDecoderProperties(folder.Coders[0].Properties);
            MemoryStream outStream = new MemoryStream(outBuffer);
            decoder.Code(inStream, outStream, (long)( packSize - 5 ), (long)unPackSize, null);
            outStream.Close();
            //DumpBytes(outBuffer, (int)unPackSize);
            //Console.WriteLine(outBuffer.GetUpperBound(0) + 1);
            //for (ulong i = 0; i < unPackSize; i++)
            //{
            //    Console.WriteLine( (char)outBuffer[i]);
            //}
            //SzDecode(db.PackSizes, folder,
                //#ifdef _LZMA_IN_CB
              //      inStream,
                //#else
                //*inBuffer, 
                //#endif
                //    outBuffer, unPackSize,
                  //  out outRealSize);
            //if (outRealSize != (uint)unPackSize)
            //{
             //   throw new Exception("OutRealSize != unPackSize " + outRealSize + " vs " + unPackSize);
            //}
            //if (folder.UnPackCRCDefined)
            //{
                // note to self: to do
                //if (!CrcVerifyDigest(folder->UnPackCRC, outBuffer->Items, (size_t)unPackSize))
                //{
                //  return SZE_FAIL;
                //}
            //}
        }
Example #46
0
        public void ProcessMultipartMessage(ref byte[] data)
        {
            // create the multipart message
             byte[] partData = new byte[data.Length - MultipartHeaderLength];
             for (int i = 0; i < partData.Length; i++)
             {
            partData[i] = data[i + MultipartHeaderLength];
             }

             MultipartMessage message = new MultipartMessage()
             {
            Data = partData,
            MessageID = BitConverter.ToUInt32(data, OffsetMultipartMessageID - MessageHeaderLength),
            PartIndex = BitConverter.ToUInt16(data, OffsetMultipartPartIndex - MessageHeaderLength),
            PartCount = BitConverter.ToUInt16(data, OffsetMultipartPartCount - MessageHeaderLength)
             };

             Console.WriteLine("< !MULTIPART-COMPRESSED!: {0} ({1} / {2})", message.MessageID, message.PartIndex + 1, message.PartCount);

             // lock this crucial part to preven conflicts
             lock (_MultipartMessageLock)
             {
            // add the message to the list
            if (!_MultipartMessageCache.ContainsKey(message.MessageID))
            {
               _MultipartMessageCache.Add(message.MessageID, new List<MultipartMessage>());
            }
            List<MultipartMessage> messages = _MultipartMessageCache[message.MessageID];
            messages.Add(message);

            if (messages.Count == message.PartCount)
            {
               // all parts have arrived
               // sort the list
               messages.Sort();

               // write the parts into a stream
               MemoryStream inData = new MemoryStream();
               foreach (MultipartMessage part in messages)
               {
                  inData.Write(part.Data, 0, part.Data.Length);
               }
               inData.Position = 0;

               // decompress the stream
               MemoryStream outData = new MemoryStream();
               Decoder decompressor = new Decoder();

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

               // Read in the decompress file size.
               byte[] dataLengthBytes = new byte[8];
               inData.Read(dataLengthBytes, 0, 8);
               long dataLength = BitConverter.ToInt64(dataLengthBytes, 0);

               decompressor.SetDecoderProperties(properties);
               decompressor.Code(inData, outData, inData.Length, dataLength, null);
               outData.Position = 0;

               // extract the actual message data
               byte messageCode = (byte)outData.ReadByte();
               byte[] messageLengthData = new byte[4];
               outData.Read(messageLengthData, 0, 4);
               int messageLength = BitConverter.ToInt32(messageLengthData, 0);
               byte[] messageData = new byte[messageLength];
               outData.Read(messageData, 0, messageLength);

               // close the streams
               inData.Close();
               outData.Close();

               // fire the completed multipart message callback
               _CompletedReceivedMultipartCallback(this, ref messageCode, ref messageData);
            }
             }
        }
Example #47
0
        private void Uncompress()
        {
            try
            {
                MemoryStream inStream = new MemoryStream(Gladkikh.Sims2.Combiner.Properties.Resources.sims2addons7zip, 0,
                                                         Gladkikh.Sims2.Combiner.Properties.Resources.sims2addons7zip.Length);

                FileStream outStream = new FileStream("sims2addons.mdb", FileMode.Create);

                byte[] properties = new byte[5];

                if (inStream.Read(properties, 0, 5) != 5)
                    throw (new Exception("input .lzma is too short"));

                SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.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);

                inStream.Close();
                outStream.Close();
            }
            catch(IOException e)
            {

                MessageBox.Show(e.Message, "Some IO error happend when BD recreating");
                Application.Exit();

            }
        }
Example #48
0
        public byte[] DecompressLzma(byte[] data, int decompressedSize)
        {
            try
            {
                var unpackedData = new byte[decompressedSize];

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

                var decompressed = new MemoryStream();

                using (var compressed = new MemoryStream(data))
                {


                    var props = new byte[5];

                    compressed.Read(props, 0, 5);
                    decoder.SetDecoderProperties(props);
                    decoder.Code(compressed, decompressed, data.Length, decompressedSize, null);
                }

                return decompressed.ToArray();
            }
            catch
            {
                return null;
            }
        }
        public void Decompress(Stream inStream, Stream outStream, bool closeInStream)
        {
            inStream.Position = 0;

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

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

            long outSize = 0;

            if (BitConverter.IsLittleEndian)
            {
                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);

            if (closeInStream)
                inStream.Close();
        }
        /// <summary>
        /// Uncompress LZMA compressed swf
        /// </summary>
        protected void inflateLZMA()
        {

            // read size
            br.BaseStream.Position = 4; // skip signature
            int size = Convert.ToInt32(br.ReadUInt32()); // uncompressed size
            int compressedSize = Convert.ToInt32(br.ReadUInt32()); // compressed size

            // read swf head
            byte[] uncompressed = new byte[size];
            br.BaseStream.Position = 0;
            br.Read(uncompressed, 0, 8); // header data (signature and uncompressed size) is not compressed                                 
            br.BaseStream.Position = 12; // skip compressed size

            byte[] properties = br.ReadBytes(5);
            byte[] compressed = br.ReadBytes(compressedSize);

            SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
            decoder.SetDecoderProperties(properties);
            // new memory stream for uncompressed swf
            MemoryStream m = new MemoryStream(uncompressed);

            m.Position = 8;
            decoder.Code(new MemoryStream(compressed), m, compressedSize, size-8, null);

            br = new BinaryReader(m);
            br.BaseStream.Position = 0;
        }
Example #51
0
        public static byte[] Decompress(byte[] buffer)
        {
            using (MemoryStream ms = new MemoryStream(buffer))
            using (BinaryReader reader = new BinaryReader(ms))
            {
                if (reader.ReadUInt16() != VZipHeader)
                {
                    throw new Exception("Expecting VZipHeader at start of stream");
                }

                if (reader.ReadChar() != Version)
                {
                    throw new Exception("Expecting VZip version 'a'");
                }

                // Sometimes this is a creation timestamp (e.g. for Steam Client VZips).
                // Sometimes this is a CRC32 (e.g. for depot chunks).
                /* uint creationTimestampOrSecondaryCRC = */ reader.ReadUInt32();

                byte[] properties = reader.ReadBytes(5);
                byte[] compressedBuffer = reader.ReadBytes((int)ms.Length - HeaderLength - FooterLength - 5);

                uint outputCRC = reader.ReadUInt32();
                uint sizeDecompressed = reader.ReadUInt32();

                if (reader.ReadUInt16() != VZipFooter)
                {
                    throw new Exception("Expecting VZipFooter at end of stream");
                }

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

                using (MemoryStream inputStream = new MemoryStream(compressedBuffer))
                using (MemoryStream outStream = new MemoryStream((int)sizeDecompressed))
                {
                    decoder.Code(inputStream, outStream, compressedBuffer.Length, sizeDecompressed, null);

                    var outData = outStream.ToArray();
                    if (Crc32.Compute(outData) != outputCRC)
                    {
                        throw new InvalidDataException("CRC does not match decompressed data. VZip data may be corrupted.");
                    }

                    return outData;
                }
            }
        }
Example #52
0
		private Stream GetLzmaStream (Stream in_stream)
		{
			// From LzmaAlone.cs
			byte[] properties = new byte [5];
			if (in_stream.Read (properties, 0, 5) != 5)
				throw new Exception ("input .lzma is too short");

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

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

			// FIXME: Man pages are small enough to use a MemoryStream to store the
			// entire uncompressed file.
			// Still, a proper stream based approach would be good. Unfortunately,
			// LZMA does not provide a streaming interface. Current hacks involve
			// a separate synchronized thread.
			MemoryStream out_stream = new MemoryStream ((int) out_size); // outsize is long but this constructor is resizable
			decoder.Code (in_stream, out_stream, compressed_size, out_size, null);
			//Log.Debug ("Decoded {0} bytes to {1} bytes", compressed_size, out_size);
			out_stream.Position = 0;
			return out_stream;
		}
Example #53
0
            public static string Decode(byte[] str)
            {
                string buffer = null;

                MemoryStream ins = new MemoryStream(str);
                MemoryStream outs = new MemoryStream();

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

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

                lzma.SetDecoderProperties(properties);
                lzma.Code(ins, outs, ins.Length, fileLength, null);

                foreach (var item in outs.ToArray()) buffer += (char)item;

                //outs.Flush();
                outs.Close();
                outs.Dispose();

                ins.Close();
                ins.Dispose();

                return buffer;
            }
Example #54
0
        bool extract(Stream si, long end, string filename, ICP icp)
        {
            SevenZip.Compression.LZMA.Decoder dec = new SevenZip.Compression.LZMA.Decoder();
            //using (MemoryStream msi = new MemoryStream(GamePatcher.Properties.Resources.kengeki1))
            using (FileStream fso = new FileStream(filename, FileMode.Create))
            {
                byte[] props = new byte[5];
                si.Read(props, 0, 5);

                byte[] length = new byte[8];
                si.Read(length, 0, 8);

                long len = BitConverter.ToInt64(length, 0);

                dec.SetDecoderProperties(props);
                //dec.Code(si, fso, si.Length, len, null);
                dec.Code(si, fso, end, len, icp);
            }
            //si.Close();
            return true;
        }
        private byte[] ReadShaderChunk(int offset)
        {
            var prevPos = Reader.BaseStream.Position;

            Reader.BaseStream.Position = offset;

            var chunkSize = Reader.ReadUInt32();

            if (Reader.ReadUInt32() != 0x414D5A4C)
            {
                throw new InvalidDataException("Not LZMA?");
            }

            var uncompressedSize = Reader.ReadUInt32();
            var compressedSize = Reader.ReadUInt32();

            Console.WriteLine("Chunk size: {0}", chunkSize);
            Console.WriteLine("Compressed size: {0}", compressedSize);
            Console.WriteLine("Uncompressed size: {0} ({1:P2} compression)", uncompressedSize, (uncompressedSize - compressedSize) / (double)uncompressedSize);

            var decoder = new Decoder();
            decoder.SetDecoderProperties(Reader.ReadBytes(5));

            var compressedBuffer = Reader.ReadBytes((int)compressedSize);

            Reader.BaseStream.Position = prevPos;

            using (var inputStream = new MemoryStream(compressedBuffer))
            using (var outStream = new MemoryStream((int)uncompressedSize))
            {
                decoder.Code(inputStream, outStream, compressedBuffer.Length, uncompressedSize, null);

                return outStream.ToArray();
            }
        }