Exemple #1
0
        public void SimpleReadWithSmallBuffer()
        {
            using var packStream   = GetType().OpenSiblingResourceStream("r.lzma");
            using var unpackTarget = new MemoryStream();
            using (var lzmaStream = new LzmaStream(packStream, CompressionMode.Decompress, bufferSize: 10))
                lzmaStream.CopyTo(unpackTarget);

            unpackTarget.Seek(0, SeekOrigin.Begin);
            using var unpackReader = new StreamReader(unpackTarget);
            var unpacked = unpackReader.ReadToEnd();

            Assert.AreEqual(GetReference(), unpacked);
        }
Exemple #2
0
        private void Pack(LzmaCompressionParameters lzmaCompressionParameters)
        {
            using var rawStream  = GetType().OpenSiblingResourceStream("flowers.bmp");
            using var packTarget = new MemoryStream();
            using (var lzmaStream = new LzmaStream(packTarget, lzmaCompressionParameters))
                rawStream.CopyTo(lzmaStream);

            using var packSource = new MemoryStream(packTarget.ToArray());
            using var rawTarget  = new MemoryStream();
            using (var unpackLzmaStream = new LzmaStream(packSource, CompressionMode.Decompress))
                unpackLzmaStream.CopyTo(rawTarget);

            Assert.IsTrue(IsEqual(rawStream, rawTarget));
        }
Exemple #3
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);
        }
        internal static MemoryStream SevenZipDecompress(Stream compressedStream, long outputSize)
        {
            //manually read the LZMA properties
            var props = new byte[5];

            compressedStream.Read(props, 0, 5);

            var ms = new MemoryStream((int)outputSize);

            using (var lzmaStream = new LzmaStream(props, compressedStream, -1, outputSize))
            {
                lzmaStream.CopyTo(ms);
            }

            ms.Position = 0;
            return(ms);
        }