Ejemplo n.º 1
0
        /// <summary>解压缩数据流</summary>
        /// <param name="inStream">输入流</param>
        /// <param name="outStream">输出流。如果不指定,则内部实例化一个内存流</param>
        /// <remarks>返回输出流,注意此时指针位于末端</remarks>
        public static Stream DecompressLzma(this Stream inStream, Stream outStream = null)
        {
            if (outStream == null)
            {
                outStream = new MemoryStream();
            }

            // 第三个参数为true,保持数据流打开,内部不应该干涉外部,不要关闭外部的数据流
            using (var stream = new LzmaStream(inStream, CompressionMode.Decompress))
            {
                stream.CopyTo(outStream);
                stream.Close();
            }

            return(outStream);
        }
Ejemplo n.º 2
0
        /// <summary>压缩数据流</summary>
        /// <param name="inStream">输入流</param>
        /// <param name="outStream">输出流。如果不指定,则内部实例化一个内存流</param>
        /// <param name="level">压缩等级</param>
        /// <remarks>返回输出流,注意此时指针位于末端</remarks>
        public static Stream CompressLzma(this Stream inStream, Stream outStream = null, Int32 level = 4)
        {
            if (outStream == null)
            {
                outStream = new MemoryStream();
            }

            // 第三个参数为true,保持数据流打开,内部不应该干涉外部,不要关闭外部的数据流
            using (var stream = new LzmaStream(outStream, CompressionMode.Compress, level))
            {
                inStream.CopyTo(stream);
                stream.Flush();
                stream.Close();
            }

            return(outStream);
        }
Ejemplo n.º 3
0
        private void CloseWriting7Zip(ICodeProgress p = null)
        {
            if (_compressed)
            {
                _lzmaStream.Close();
            }

            _packStreamSize = (UInt64)_zipFs.Position - _packStreamStart;

            Create7ZStructure();

            byte[] newHeaderByte;
            using (Stream headerMem = new MemoryStream())
            {
                using (BinaryWriter headerBw = new BinaryWriter(headerMem))
                {
                    _header.WriteHeader(headerBw);
                    newHeaderByte      = new byte[headerMem.Length];
                    headerMem.Position = 0;
                    headerMem.Read(newHeaderByte, 0, newHeaderByte.Length);
                }
            }

            CRC mainHeadercrc = new CRC();

            mainHeadercrc.Update(newHeaderByte, 0, (uint)newHeaderByte.Length);
            UInt32 mainHeaderCRC = mainHeadercrc.GetDigest();

            UInt64       headerpos = (UInt64)_zipFs.Position;
            BinaryWriter bw        = new BinaryWriter(_zipFs);

            bw.Write(newHeaderByte);

            _signatureHeader.WriteFinal(bw, headerpos, (ulong)newHeaderByte.Length, mainHeaderCRC);

            WriteRomVault7Zip(bw, headerpos, (ulong)newHeaderByte.Length, mainHeaderCRC);

            _zipFs.Flush();
            _zipFs.Close();
            _zipFs.Dispose();
        }
Ejemplo n.º 4
0
        private void CloseWriting7Zip()
        {
            if (_compressed)
            {
                _lzmaStream.Close();
            }

            _packStreamSize = (ulong)_zipFs.Position - _packStreamStart;

            Create7ZStructure();

            byte[] newHeaderByte;
            using (Stream headerMem = new MemoryStream())
            {
                using (BinaryWriter headerBw = new BinaryWriter(headerMem))
                {
                    _header.WriteHeader(headerBw);
                    newHeaderByte      = new byte[headerMem.Length];
                    headerMem.Position = 0;
                    headerMem.Read(newHeaderByte, 0, newHeaderByte.Length);
                }
            }

            uint mainHeaderCRC = Utils.CRC.CalculateDigest(newHeaderByte, 0, (uint)newHeaderByte.Length);

            ulong        headerpos = (ulong)_zipFs.Position;
            BinaryWriter bw        = new BinaryWriter(_zipFs);

            bw.Write(newHeaderByte);

            _signatureHeader.WriteFinal(bw, headerpos, (ulong)newHeaderByte.Length, mainHeaderCRC);

            WriteRomVault7Zip(bw, headerpos, (ulong)newHeaderByte.Length, mainHeaderCRC);

            _zipFs.Flush();
            _zipFs.Close();
            _zipFs.Dispose();
        }
Ejemplo n.º 5
0
        private void CloseWriting7Zip()
        {
            if (_compressed)
            {
                _lzmaStream.Close();
            }

            _packStreamSize = (ulong)_zipFs.Position - _packStreamStart;

            Create7ZStructure();

            byte[] newHeaderByte;
            using (Stream headerMem = new MemoryStream())
            {
                using (BinaryWriter headerBw = new BinaryWriter(headerMem, Encoding.UTF8, true))
                {
                    _header.WriteHeader(headerBw);

                    newHeaderByte      = new byte[headerMem.Length];
                    headerMem.Position = 0;
                    headerMem.Read(newHeaderByte, 0, newHeaderByte.Length);
                }
            }

            uint mainHeaderCRC = CRC.CalculateDigest(newHeaderByte, 0, (uint)newHeaderByte.Length);

            #region Header Compression
            long packedHeaderPos        = _zipFs.Position;
            LzmaEncoderProperties ep    = new LzmaEncoderProperties(true, GetDictionarySizeFromUncompressedSize((ulong)newHeaderByte.Length), 64);
            LzmaStream            lzs   = new LzmaStream(ep, false, _zipFs);
            byte[] lzmaStreamProperties = lzs.Properties;
            lzs.Write(newHeaderByte, 0, newHeaderByte.Length);
            lzs.Close();

            StreamsInfo streamsInfo = new StreamsInfo
            {
                PackPosition = (ulong)(packedHeaderPos - _baseOffset),
                Folders      = new[] {
                    new Folder {
                        BindPairs = new BindPair[0],
                        Coders    = new [] {
                            new Coder {
                                Method        = new byte[] { 3, 1, 1 },
                                NumInStreams  = 1,
                                NumOutStreams = 1,
                                Properties    = lzmaStreamProperties
                            }
                        },
                        UnpackedStreamSizes = new[] { (ulong)newHeaderByte.Length },
                        UnpackCRC           = mainHeaderCRC
                    }
                },
                PackedStreams = new[] {
                    new PackedStreamInfo
                    {
                        PackedSize     = (ulong)(_zipFs.Position - packedHeaderPos),
                        StreamPosition = 0
                    }
                }
            };

            using (Stream headerMem = new MemoryStream())
            {
                using (BinaryWriter bw = new BinaryWriter(headerMem, Encoding.UTF8, true))
                {
                    bw.Write((byte)HeaderProperty.kEncodedHeader);
                    streamsInfo.WriteHeader(bw);

                    newHeaderByte      = new byte[headerMem.Length];
                    headerMem.Position = 0;
                    headerMem.Read(newHeaderByte, 0, newHeaderByte.Length);
                }
            }
            mainHeaderCRC = CRC.CalculateDigest(newHeaderByte, 0, (uint)newHeaderByte.Length);
            #endregion


            using (BinaryWriter bw = new BinaryWriter(_zipFs, Encoding.UTF8, true))
            {
                ulong headerPosition = (ulong)_zipFs.Position + 32; //tzip header is 32 bytes
                WriteRomVault7Zip(bw, headerPosition, (ulong)newHeaderByte.Length, mainHeaderCRC);

                _zipFs.Write(newHeaderByte, 0, newHeaderByte.Length);
                _signatureHeader.WriteFinal(bw, headerPosition, (ulong)newHeaderByte.Length, mainHeaderCRC);
            }
            _zipFs.Flush();
            _zipFs.Close();
            _zipFs.Dispose();
        }