private Stream CheckSFX(Stream stream)
        {
            RewindableStream rewindableStream = this.GetRewindableStream(stream);

            stream = rewindableStream;
            BinaryReader reader = new BinaryReader(rewindableStream);

            try
            {
                bool flag;
                int  num = 0;
                goto Label_0138;
Label_001B:
                if (reader.ReadByte() == 0x52)
                {
                    MemoryStream stream3 = new MemoryStream();
                    byte[]       buffer  = reader.ReadBytes(3);
                    if (((buffer[0] == 0x45) && (buffer[1] == 0x7e)) && (buffer[2] == 0x5e))
                    {
                        stream3.WriteByte(0x52);
                        stream3.Write(buffer, 0, 3);
                        rewindableStream.Rewind(stream3);
                        return(stream);
                    }
                    byte[] buffer2 = reader.ReadBytes(3);
                    if (((((buffer[0] == 0x61) && (buffer[1] == 0x72)) && ((buffer[2] == 0x21) && (buffer2[0] == 0x1a))) && (buffer2[1] == 7)) && (buffer2[2] == 0))
                    {
                        stream3.WriteByte(0x52);
                        stream3.Write(buffer, 0, 3);
                        stream3.Write(buffer2, 0, 3);
                        rewindableStream.Rewind(stream3);
                        return(stream);
                    }
                    stream3.Write(buffer, 0, 3);
                    stream3.Write(buffer2, 0, 3);
                    rewindableStream.Rewind(stream3);
                }
                if (num > 0x7fff0)
                {
                    return(stream);
                }
Label_0138:
                flag = true;
                goto Label_001B;
            }
            catch (Exception exception)
            {
                if (!this.Options_HasFlag(SharpCompress.Common.Options.KeepStreamsOpen))
                {
                    reader.Close();
                }
                throw new InvalidFormatException("Error trying to read rar signature.", exception);
            }
            return(stream);
        }
Exemple #2
0
        internal BinaryReader FixStreamedFileLocation(ref RewindableStream rewindableStream)
        {
            if (Header.IsDirectory)
            {
                return(new BinaryReader(rewindableStream));
            }
            if (Header.HasData && !Skipped)
            {
                if (_decompressionStream == null)
                {
                    _decompressionStream = GetCompressedStream();
                }

                _decompressionStream.Skip();

                if (_decompressionStream is DeflateStream deflateStream)
                {
                    rewindableStream.Rewind(deflateStream.InputBuffer);
                }
                Skipped = true;
            }
            var reader = new BinaryReader(rewindableStream);

            _decompressionStream = null;
            return(reader);
        }
Exemple #3
0
        internal BinaryReader FixStreamedFileLocation(ref RewindableStream rewindableStream)
        {
            if (Header.IsDirectory)
            {
                return(new BinaryReader(rewindableStream));
            }
            if (Header.HasData)
            {
                if (decompressionStream == null)
                {
                    decompressionStream = GetCompressedStream();
                }
                decompressionStream.SkipAll();

                DeflateStream deflateStream = decompressionStream as DeflateStream;
                if (deflateStream != null)
                {
                    rewindableStream.Rewind(deflateStream.InputBuffer);
                }
            }
            var reader = new BinaryReader(rewindableStream);

            decompressionStream = null;
            return(reader);
        }
        /// <summary>
        /// Opens a Reader for Non-seeking usage
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IReader Open(Stream stream, ReaderOptions options = null)
        {
            stream.CheckNotNull(nameof(stream));
            options = options != null ?options: new ReaderOptions()
            {
                LeaveStreamOpen = false
            };
            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            rewindableStream.Rewind(false);
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.GZip));
                }
                rewindableStream.Rewind(true);
                return(GZipReader.Open(rewindableStream, options));
            }

            rewindableStream.Rewind(false);

            if (TarArchive.IsTarFile(rewindableStream))
            {
                rewindableStream.Rewind(true);
                return(TarReader.Open(rewindableStream, options));
            }
            rewindableStream.Rewind(false);

            throw new InvalidOperationException("Cannot determine compressed stream type.  Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar, LZip, XZ");
        }
        internal GZipFilePart(Stream stream)
        {
            RewindableStream rewind = new RewindableStream(stream);

            rewind.Recording = true;
            ReadAndValidateGzipHeader(rewind);
            rewind.Rewind();
            this.stream = new GZipStream(rewind, CompressionMode.Decompress);
        }
Exemple #6
0
        /// <summary>
        /// Opens a TarReader for Non-seeking usage with a single volume
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static TarReader Open(Stream stream, ReaderOptions options = null)
        {
            stream.CheckNotNull(nameof(stream));
            options = options ?? new ReaderOptions();
            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.GZip));
                }
                throw new InvalidFormatException("Not a tar file.");
            }

            rewindableStream.Rewind(false);
            if (BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, false);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.BZip2));
                }
                throw new InvalidFormatException("Not a tar file.");
            }

            rewindableStream.Rewind(false);
            if (LZipStream.IsLZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                LZipStream testStream = new LZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.LZip));
                }
                throw new InvalidFormatException("Not a tar file.");
            }
            rewindableStream.Rewind(true);
            return(new TarReader(rewindableStream, options, CompressionType.None));
        }
Exemple #7
0
        public static TarReader Open(Stream stream, Options options = 1)
        {
            stream.CheckNotNull("stream");
            RewindableStream stream2 = new RewindableStream(stream);

            stream2.StartRecording();
            stream2.Rewind(false);
            if (!BZip2Stream.IsBZip2(stream2))
            {
                stream2.Rewind(true);
                return(new TarReader(stream2, CompressionType.None, options));
            }
            stream2.Rewind(false);
            if (!TarArchive.IsTarFile(new BZip2Stream(stream2, CompressionMode.Decompress, false, false)))
            {
                throw new InvalidFormatException("Not a tar file.");
            }
            stream2.Rewind(true);
            return(new TarReader(stream2, CompressionType.BZip2, options));
        }
Exemple #8
0
        /// <summary>
        /// Opens a TarReader for Non-seeking usage with a single volume
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static TarReader Open(Stream stream, Options options = Options.KeepStreamsOpen)
        {
            stream.CheckNotNull("stream");

            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, CompressionType.GZip, options));
                }
                throw new InvalidFormatException("Not a tar file.");
            }

            rewindableStream.Rewind(false);
            if (BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, false);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, CompressionType.BZip2, options));
                }
                throw new InvalidFormatException("Not a tar file.");
            }
            rewindableStream.Rewind(true);
            return(new TarReader(rewindableStream, CompressionType.None, options));
        }
        public void Test()
        {
            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(ms);

            bw.Write(1);
            bw.Write(2);
            bw.Write(3);
            bw.Write(4);
            bw.Write(5);
            bw.Write(6);
            bw.Write(7);
            bw.Flush();
            ms.Position = 0;
            RewindableStream stream = new RewindableStream(ms);

            stream.Recording = true;
            BinaryReader br = new BinaryReader(stream);

            Debug.Assert(br.ReadInt32() == 1);
            Debug.Assert(br.ReadInt32() == 2);
            Debug.Assert(br.ReadInt32() == 3);
            Debug.Assert(br.ReadInt32() == 4);
            stream.Rewind();
            stream.Recording = true;
            Debug.Assert(br.ReadInt32() == 1);
            Debug.Assert(br.ReadInt32() == 2);
            Debug.Assert(br.ReadInt32() == 3);
            Debug.Assert(br.ReadInt32() == 4);
            Debug.Assert(br.ReadInt32() == 5);
            Debug.Assert(br.ReadInt32() == 6);
            Debug.Assert(br.ReadInt32() == 7);
            stream.Rewind();
            stream.Recording = true;
            Debug.Assert(br.ReadInt32() == 1);
            Debug.Assert(br.ReadInt32() == 2);
            Debug.Assert(br.ReadInt32() == 3);
            Debug.Assert(br.ReadInt32() == 4);
        }
Exemple #10
0
        public void TestRewind()
        {
            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(ms);

            bw.Write(1);
            bw.Write(2);
            bw.Write(3);
            bw.Write(4);
            bw.Write(5);
            bw.Write(6);
            bw.Write(7);
            bw.Flush();
            ms.Position = 0;
            RewindableStream stream = new RewindableStream(ms);

            stream.StartRecording();
            BinaryReader br = new BinaryReader(stream);

            Assert.Equal(br.ReadInt32(), 1);
            Assert.Equal(br.ReadInt32(), 2);
            Assert.Equal(br.ReadInt32(), 3);
            Assert.Equal(br.ReadInt32(), 4);
            stream.Rewind(true);
            stream.StartRecording();
            Assert.Equal(br.ReadInt32(), 1);
            Assert.Equal(br.ReadInt32(), 2);
            Assert.Equal(br.ReadInt32(), 3);
            Assert.Equal(br.ReadInt32(), 4);
            Assert.Equal(br.ReadInt32(), 5);
            Assert.Equal(br.ReadInt32(), 6);
            Assert.Equal(br.ReadInt32(), 7);
            stream.Rewind(true);
            stream.StartRecording();
            Assert.Equal(br.ReadInt32(), 1);
            Assert.Equal(br.ReadInt32(), 2);
            Assert.Equal(br.ReadInt32(), 3);
            Assert.Equal(br.ReadInt32(), 4);
        }
Exemple #11
0
        public void TestIncompleteRewind()
        {
            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(ms);

            bw.Write(1);
            bw.Write(2);
            bw.Write(3);
            bw.Write(4);
            bw.Write(5);
            bw.Write(6);
            bw.Write(7);
            bw.Flush();
            ms.Position = 0;
            RewindableStream stream = new RewindableStream(ms);

            stream.StartRecording();
            BinaryReader br = new BinaryReader(stream);

            Assert.Equal(1, br.ReadInt32());
            Assert.Equal(2, br.ReadInt32());
            Assert.Equal(3, br.ReadInt32());
            Assert.Equal(4, br.ReadInt32());
            stream.Rewind(true);
            Assert.Equal(1, br.ReadInt32());
            Assert.Equal(2, br.ReadInt32());
            stream.StartRecording();
            Assert.Equal(3, br.ReadInt32());
            Assert.Equal(4, br.ReadInt32());
            Assert.Equal(5, br.ReadInt32());
            stream.Rewind(true);
            Assert.Equal(3, br.ReadInt32());
            Assert.Equal(4, br.ReadInt32());
            Assert.Equal(5, br.ReadInt32());
            Assert.Equal(6, br.ReadInt32());
            Assert.Equal(7, br.ReadInt32());
        }
Exemple #12
0
        public static Stream DecodeSin(Stream stream, long totalSize)
        {
            var rewindable = new RewindableStream(stream);

            rewindable.StartRecording();
            var reader  = new BinaryReader(rewindable);
            var version = reader.ReadByte();

            switch (version)
            {
            case 1:
            case 2:
                throw new Exception($"SIN version 1 and 2 not supported.");

            case 3:
                rewindable.Rewind(true);
                var tempStream = TempFileStream.Create();
                SinParserV3.CopyTo(reader, tempStream);
                return(tempStream);
            }

            rewindable.Rewind(true);
            return(DecodeTarSin(rewindable, totalSize));
        }
Exemple #13
0
        public static IReader Open(Stream stream, Options options = 1)
        {
            stream.CheckNotNull("stream");
            RewindableStream stream2 = new RewindableStream(stream);

            stream2.StartRecording();
            stream2.Rewind(false);
            if (BZip2Stream.IsBZip2(stream2))
            {
                stream2.Rewind(false);
                if (TarArchive.IsTarFile(new BZip2Stream(stream2, CompressionMode.Decompress, false, false)))
                {
                    stream2.Rewind(true);
                    return(new TarReader(stream2, CompressionType.BZip2, options));
                }
            }
            stream2.Rewind(false);
            if (!TarArchive.IsTarFile(stream2))
            {
                throw new InvalidOperationException("Cannot determine compressed stream type.");
            }
            stream2.Rewind(true);
            return(TarReader.Open(stream2, options));
        }
        /// <summary>
        /// Opens a Reader for Non-seeking usage
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="listener"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IReader OpenReader(Stream stream, IExtractionListener listener,
                                         Options options = Options.KeepStreamsOpen)
        {
            stream.CheckNotNull("stream");

            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.Recording = true;
            if (ZipArchive.IsZipFile(rewindableStream))
            {
                return(ZipReader.Open(rewindableStream, listener, options));
            }
            rewindableStream.Rewind();
            rewindableStream.Recording = true;
            if (GZipReader.IsGZip(rewindableStream))
            {
                rewindableStream.Rewind();
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                rewindableStream.Recording = true;
                if (TarReader.IsTarFile(testStream))
                {
                    rewindableStream.Rewind();
                    return(TarGZipReader.Open(rewindableStream, listener, options));
                }
                rewindableStream.Rewind();
                return(GZipReader.Open(rewindableStream, listener, options));
            }
            rewindableStream.Rewind();
            rewindableStream.Recording = true;
            if (TarReader.IsTarFile(rewindableStream))
            {
                rewindableStream.Rewind();
                return(TarReader.Open(rewindableStream, listener, options));
            }
            rewindableStream.Rewind();
            rewindableStream.Recording = true;
            if (RarArchive.IsRarFile(rewindableStream))
            {
                rewindableStream.Rewind();
                return(RarReader.Open(rewindableStream, listener, options));
            }
            throw new InvalidOperationException("Cannot determine compressed stream type.");
        }
        internal BinaryReader FixStreamedFileLocation(ref RewindableStream rewindableStream)
        {
            if (base.Header.IsDirectory)
            {
                return(new BinaryReader(rewindableStream));
            }
            if (base.Header.HasData)
            {
                if (this.decompressionStream == null)
                {
                    this.decompressionStream = this.GetCompressedStream();
                }
                Utility.SkipAll(this.decompressionStream);
                DeflateStream decompressionStream = this.decompressionStream as DeflateStream;
                if (decompressionStream != null)
                {
                    rewindableStream.Rewind(decompressionStream.InputBuffer);
                }
            }
            BinaryReader reader = new BinaryReader(rewindableStream);

            this.decompressionStream = null;
            return(reader);
        }
Exemple #16
0
        /// <summary>
        /// Opens a Reader for Non-seeking usage
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IReader Open(Stream stream, Options options = Options.KeepStreamsOpen)
        {
            stream.CheckNotNull("stream");

            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (ZipArchive.IsZipFile(rewindableStream, null))
            {
                rewindableStream.Rewind(true);
                return(ZipReader.Open(rewindableStream, null, options));
            }
            rewindableStream.Rewind(false);
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, CompressionType.GZip, options));
                }
                rewindableStream.Rewind(true);
                return(GZipReader.Open(rewindableStream, options));
            }

            rewindableStream.Rewind(false);
            if (BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, false);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, CompressionType.BZip2, options));
                }
            }

            rewindableStream.Rewind(false);
            if (TarArchive.IsTarFile(rewindableStream))
            {
                rewindableStream.Rewind(true);
                return(TarReader.Open(rewindableStream, options));
            }
            rewindableStream.Rewind(false);
            if (RarArchive.IsRarFile(rewindableStream, options))
            {
                rewindableStream.Rewind(true);
                return(RarReader.Open(rewindableStream, options));
            }

            throw new InvalidOperationException("Cannot determine compressed stream type.  Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar");
        }
        internal BinaryReader FixStreamedFileLocation(ref RewindableStream rewindableStream)
        {
            if (Header.IsDirectory)
            {
                return(new BinaryReader(rewindableStream));
            }
            if (Header.HasData && !Skipped)
            {
                _decompressionStream ??= GetCompressedStream();

                if (Header.CompressionMethod != ZipCompressionMethod.None)
                {
                    _decompressionStream.Skip();

                    // If we had TotalIn / TotalOut we could have used them
                    Header.CompressedSize = _decompressionStream.Position;

                    if (_decompressionStream is DeflateStream deflateStream)
                    {
                        rewindableStream.Rewind(deflateStream.InputBuffer);
                    }
                }
                else
                {
                    // We would need to search for the magic word
                    rewindableStream.Position -= 4;
                    var pos = rewindableStream.Position;
                    while (Utility.Find(rewindableStream, new byte[] { 0x50, 0x4b, 0x07, 0x08 }))
                    {
                        // We should probably check CRC32 for positive matching as well
                        var size = rewindableStream.Position - pos;
                        var br   = new BinaryReader(rewindableStream);
                        br.ReadUInt32();
                        br.ReadUInt32(); // CRC32
                        var compressed_size    = br.ReadUInt32();
                        var uncompressed_size  = br.ReadUInt32();
                        var uncompressed_64bit = br.ReadInt64();

                        long test_64bit = (long)uncompressed_size << 32 | (long)compressed_size;

                        if (test_64bit == size && test_64bit == uncompressed_64bit)
                        {
                            Header.CompressedSize      = test_64bit;
                            Header.UncompressedSize    = uncompressed_64bit;
                            rewindableStream.Position -= 24;
                            break;
                        }

                        if (compressed_size == size && compressed_size == uncompressed_size)
                        {
                            Header.CompressedSize      = compressed_size;
                            Header.UncompressedSize    = uncompressed_size;
                            rewindableStream.Position -= 24;
                            break;
                        }
                        rewindableStream.Position -= 20;
                    }
                }

                Skipped = true;
            }
            var reader = new BinaryReader(rewindableStream);

            _decompressionStream = null;
            return(reader);
        }
Exemple #18
0
        private Stream CheckSFX(Stream stream)
        {
            RewindableStream rewindableStream = GetRewindableStream(stream);

            stream = rewindableStream;
            BinaryReader reader = new BinaryReader(rewindableStream);

            try
            {
                int count = 0;
                while (true)
                {
                    byte firstByte = reader.ReadByte();
                    if (firstByte == 0x52)
                    {
                        MemoryStream buffer         = new MemoryStream();
                        byte[]       nextThreeBytes = reader.ReadBytes(3);
                        if ((nextThreeBytes[0] == 0x45) &&
                            (nextThreeBytes[1] == 0x7E) &&
                            (nextThreeBytes[2] == 0x5E))
                        {
                            //old format and isvalid
                            buffer.WriteByte(0x52);
                            buffer.Write(nextThreeBytes, 0, 3);
                            rewindableStream.Rewind(buffer);
                            break;
                        }
                        byte[] secondThreeBytes = reader.ReadBytes(3);
                        if ((nextThreeBytes[0] == 0x61) &&
                            (nextThreeBytes[1] == 0x72) &&
                            (nextThreeBytes[2] == 0x21) &&
                            (secondThreeBytes[0] == 0x1A) &&
                            (secondThreeBytes[1] == 0x07) &&
                            (secondThreeBytes[2] == 0x00))
                        {
                            //new format and isvalid
                            buffer.WriteByte(0x52);
                            buffer.Write(nextThreeBytes, 0, 3);
                            buffer.Write(secondThreeBytes, 0, 3);
                            rewindableStream.Rewind(buffer);
                            break;
                        }
                        buffer.Write(nextThreeBytes, 0, 3);
                        buffer.Write(secondThreeBytes, 0, 3);
                        rewindableStream.Rewind(buffer);
                    }
                    if (count > MAX_SFX_SIZE)
                    {
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                if (!Options.LeaveStreamOpen)
                {
#if NET35
                    reader.Close();
#else
                    reader.Dispose();
#endif
                }
                throw new InvalidFormatException("Error trying to read rar signature.", e);
            }
            return(stream);
        }
        /// <summary>
        /// Opens a Reader for Non-seeking usage
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IReader Open(Stream stream, ReaderOptions options = null)
        {
            stream.CheckNotNull("stream");
            options = options ?? new ReaderOptions()
            {
                LeaveStreamOpen = false
            };
            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (ZipArchive.IsZipFile(rewindableStream, options.Password))
            {
                rewindableStream.Rewind(true);
                return(ZipReader.Open(rewindableStream, options));
            }
            rewindableStream.Rewind(false);
            if (GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.GZip));
                }
                rewindableStream.Rewind(true);
                return(GZipReader.Open(rewindableStream, options));
            }

            rewindableStream.Rewind(false);
            if (BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, true);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.BZip2));
                }
            }

            rewindableStream.Rewind(false);
            if (LZipStream.IsLZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                LZipStream testStream = new LZipStream(rewindableStream, CompressionMode.Decompress, true);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.LZip));
                }
            }
            rewindableStream.Rewind(false);
            if (RarArchive.IsRarFile(rewindableStream, options))
            {
                rewindableStream.Rewind(true);
                return(RarReader.Open(rewindableStream, options));
            }

            rewindableStream.Rewind(false);
            if (TarArchive.IsTarFile(rewindableStream))
            {
                rewindableStream.Rewind(true);
                return(TarReader.Open(rewindableStream, options));
            }
            rewindableStream.Rewind(false);
            if (XZStream.IsXZStream(rewindableStream))
            {
                rewindableStream.Rewind(true);
                XZStream testStream = new XZStream(rewindableStream);
                if (TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    return(new TarReader(rewindableStream, options, CompressionType.Xz));
                }
            }
            throw new InvalidOperationException("Cannot determine compressed stream type.  Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar, LZip, XZ");
        }
        public static bool TryOpen(Stream stream, ReaderOptions options, ArchiveTypeMask archiveTypes, out IReader reader)
        {
            stream.CheckNotNull("stream");
            options = options ?? new ReaderOptions()
            {
                LeaveStreamOpen = false
            };
            RewindableStream rewindableStream = new RewindableStream(stream);

            rewindableStream.StartRecording();
            if (archiveTypes.HasFlag(ArchiveTypeMask.Zip) && ZipArchive.IsZipFile(rewindableStream, options.Password))
            {
                rewindableStream.Rewind(true);
                reader = ZipReader.Open(rewindableStream, options);
                return(true);
            }
            rewindableStream.Rewind(false);
            if (archiveTypes.HasFlag(ArchiveTypeMask.GZip) && GZipArchive.IsGZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
                if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    reader = new TarReader(rewindableStream, options, CompressionType.GZip);
                    return(true);
                }
                rewindableStream.Rewind(true);
                reader = GZipReader.Open(rewindableStream, options);
                return(true);
            }

            rewindableStream.Rewind(false);
            if (archiveTypes.HasFlag(ArchiveTypeMask.BZip2) && BZip2Stream.IsBZip2(rewindableStream))
            {
                rewindableStream.Rewind(false);
                BZip2Stream testStream = new BZip2Stream(new NonDisposingStream(rewindableStream), CompressionMode.Decompress, false);
                if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    reader = new TarReader(rewindableStream, options, CompressionType.BZip2);
                    return(true);
                }
            }

            rewindableStream.Rewind(false);
            if (archiveTypes.HasFlag(ArchiveTypeMask.LZip) && LZipStream.IsLZipFile(rewindableStream))
            {
                rewindableStream.Rewind(false);
                LZipStream testStream = new LZipStream(new NonDisposingStream(rewindableStream), CompressionMode.Decompress);
                if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    reader = new TarReader(rewindableStream, options, CompressionType.LZip);
                    return(true);
                }
            }
            rewindableStream.Rewind(false);
            if (archiveTypes.HasFlag(ArchiveTypeMask.Rar) && RarArchive.IsRarFile(rewindableStream, options))
            {
                rewindableStream.Rewind(true);
                reader = RarReader.Open(rewindableStream, options);
                return(true);
            }

            rewindableStream.Rewind(false);
            if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(rewindableStream))
            {
                rewindableStream.Rewind(true);
                reader = TarReader.Open(rewindableStream, options);
                return(true);
            }
            rewindableStream.Rewind(false);
            if (XZStream.IsXZStream(rewindableStream))
            {
                rewindableStream.Rewind(true);
                XZStream testStream = new XZStream(rewindableStream);
                if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(testStream))
                {
                    rewindableStream.Rewind(true);
                    reader = new TarReader(rewindableStream, options, CompressionType.Xz);
                    return(true);
                }
            }
            reader = null;
            return(false);
        }
Exemple #21
0
        internal IEnumerable <RarHeader> ReadHeaders(Stream stream)
        {
            if (Options.HasFlag(Options.CheckForSFX))
            {
                RewindableStream rewindableStream = new RewindableStream(stream);
                rewindableStream.Recording = true;
                stream = rewindableStream;
                BinaryReader reader = new BinaryReader(rewindableStream);
                try
                {
                    int count = 0;
                    while (true)
                    {
                        byte firstByte = reader.ReadByte();
                        if (firstByte == 0x52)
                        {
                            byte[] nextThreeBytes = reader.ReadBytes(3);
                            if ((nextThreeBytes[0] == 0x45) &&
                                (nextThreeBytes[1] == 0x7E) &&
                                (nextThreeBytes[2] == 0x5E))
                            {
                                //old format and isvalid
                                rewindableStream.Rewind();
                                break;
                            }
                            byte[] secondThreeBytes = reader.ReadBytes(3);
                            if ((nextThreeBytes[0] == 0x61) &&
                                (nextThreeBytes[1] == 0x72) &&
                                (nextThreeBytes[2] == 0x21) &&
                                (secondThreeBytes[0] == 0x1A) &&
                                (secondThreeBytes[1] == 0x07) &&
                                (secondThreeBytes[2] == 0x00))
                            {
                                //new format and isvalid
                                rewindableStream.Rewind();
                                break;
                            }
                        }
                        if (count > MAX_SFX_SIZE)
                        {
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    if (!Options.HasFlag(Options.KeepStreamsOpen))
                    {
#if THREEFIVE
                        reader.Close();
#else
                        reader.Dispose();
#endif
                    }
                    throw new InvalidRarFormatException("Error trying to read rar signature.", e);
                }
            }
            RarHeader header;
            while ((header = ReadNextHeader(stream)) != null)
            {
                yield return(header);

                if (header.HeaderType == HeaderType.EndArchiveHeader)
                {
                    yield break; // the end?
                }
            }
        }
Exemple #22
0
        public static IReader Open(Stream stream, Options options)
        {
            Utility.CheckNotNull(stream, "stream");
            RewindableStream stream2 = new RewindableStream(stream);

            stream2.StartRecording();
            if (ZipArchive.IsZipFile(stream2, null))
            {
                stream2.Rewind(true);
                return(ZipReader.Open(stream2, null, options));
            }
            stream2.Rewind(false);
            if (GZipArchive.IsGZipFile(stream2))
            {
                stream2.Rewind(false);
                GZipStream stream3 = new GZipStream(stream2, CompressionMode.Decompress);
                if (TarArchive.IsTarFile(stream3))
                {
                    stream2.Rewind(true);
                    return(new TarReader(stream2, CompressionType.GZip, options));
                }
                stream2.Rewind(true);
                return(GZipReader.Open(stream2, options));
            }
            stream2.Rewind(false);
            if (BZip2Stream.IsBZip2(stream2))
            {
                stream2.Rewind(false);
                BZip2Stream stream4 = new BZip2Stream(stream2, CompressionMode.Decompress, false, false);
                if (TarArchive.IsTarFile(stream4))
                {
                    stream2.Rewind(true);
                    return(new TarReader(stream2, CompressionType.BZip2, options));
                }
            }
            stream2.Rewind(false);
            if (TarArchive.IsTarFile(stream2))
            {
                stream2.Rewind(true);
                return(TarReader.Open(stream2, options));
            }
            stream2.Rewind(false);
            if (!RarArchive.IsRarFile(stream2, options))
            {
                throw new InvalidOperationException("Cannot determine compressed stream type.  Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar");
            }
            stream2.Rewind(true);
            return(RarReader.Open(stream2, options));
        }