示例#1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="XZInputStream"/> class.
        /// </summary>
        /// <param name="stream">
        /// The underlying <see cref="Stream"/> from which to decompress the data.
        /// </param>
        /// <param name="format">
        /// The lzma formats which are supported.
        /// </param>
        /// <param name="ownership">
        /// Determines whether the underlying stream should be disposed of, or not.
        /// </param>
        public XZInputStream(Stream stream, LzmaFormat format = LzmaFormat.Auto, Ownership ownership = Ownership.None)
        {
            this.innerStream = stream ?? throw new ArgumentNullException(nameof(stream));
            this.ownership   = ownership;

            LzmaResult ret;

            switch (format)
            {
            case LzmaFormat.Lzma:
                ret = NativeMethods.lzma_alone_decoder(ref this.lzmaStream, ulong.MaxValue);
                break;

            case LzmaFormat.Xz:
                ret = NativeMethods.lzma_stream_decoder(ref this.lzmaStream, ulong.MaxValue, LzmaDecodeFlags.Concatenated);
                break;

            default:
            case LzmaFormat.Auto:
                ret = NativeMethods.lzma_auto_decoder(ref this.lzmaStream, ulong.MaxValue, LzmaDecodeFlags.Concatenated);
                break;
            }

            this.inbuf  = Marshal.AllocHGlobal(BufSize);
            this.outbuf = Marshal.AllocHGlobal(BufSize);

            this.lzmaStream.AvailIn  = 0;
            this.lzmaStream.NextIn   = (byte *)this.inbuf;
            this.lzmaStream.NextOut  = (byte *)this.outbuf;
            this.lzmaStream.AvailOut = BufSize;

            LzmaException.ThrowOnError(ret);
        }
示例#2
0
        public void Read_InvalidFormat_ReturnsError(string path, LzmaFormat format)
        {
            byte[] data   = File.ReadAllBytes(path);
            byte[] output = new byte[100];

            using (XZDecompressor decompressor = new XZDecompressor(format))
            {
                Assert.Equal(OperationStatus.InvalidData, decompressor.Decompress(data, output, out int bytesConsumed, out int bytesWritten));
            }
        }
示例#3
0
        public void Read_InvalidFormat_Throws(string path, LzmaFormat format)
        {
            using (Stream stream = File.OpenRead(path))
                using (XZInputStream xzStream = new XZInputStream(stream, format))
                {
                    byte[] buffer = new byte[128];
                    Assert.Throws <LzmaException>(() => xzStream.Read(buffer, 0, 128));

                    xzStream.Dispose();

                    Assert.Throws <ObjectDisposedException>(() => xzStream.Read(buffer, 0, 128));
                }
        }
示例#4
0
        public void Read_WithFormat_Works(string path, LzmaFormat format)
        {
            using (Stream stream = File.OpenRead(path))
                using (XZInputStream xzStream = new XZInputStream(stream, format))
                {
                    byte[] buffer = new byte[128];
                    Assert.Equal(14, xzStream.Read(buffer, 0, 128));
                    Assert.Equal(0, xzStream.Read(buffer, 0, 128));

                    Assert.Equal("Hello, World!\n", Encoding.UTF8.GetString(buffer, 0, 14));

                    xzStream.Dispose();

                    Assert.Throws <ObjectDisposedException>(() => xzStream.Read(buffer, 0, 128));
                }
        }
示例#5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="XZDecompressor" /> class.
        /// </summary>
        /// <param name="format">
        /// The format of the data to decompress.
        /// </param>
        public XZDecompressor(LzmaFormat format = LzmaFormat.Auto)
        {
            LzmaResult ret;

            switch (format)
            {
            case LzmaFormat.Lzma:
                ret = NativeMethods.lzma_alone_decoder(ref this.lzmaStream, ulong.MaxValue);
                break;

            case LzmaFormat.Xz:
                ret = NativeMethods.lzma_stream_decoder(ref this.lzmaStream, ulong.MaxValue, LzmaDecodeFlags.Concatenated);
                break;

            default:
            case LzmaFormat.Auto:
                ret = NativeMethods.lzma_auto_decoder(ref this.lzmaStream, ulong.MaxValue, LzmaDecodeFlags.Concatenated);
                break;
            }

            LzmaException.ThrowOnError(ret);
        }