Пример #1
0
        protected override IReader CreateReaderForSolidExtraction()
        {
            var stream = Volumes.Single().Stream;

            stream.Position = 0;
            return(GZipReader.Open(stream));
        }
Пример #2
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, 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");
        }
Пример #3
0
    void UnTarGZip(string path, string destination)
    {
        using (var packageStream = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            using (var gzipReader = GZipReader.Open(packageStream))
            {
                gzipReader.MoveToNextEntry();
                using (var compressionStream = gzipReader.OpenEntryStream())
                {
                    using (var reader = TarReader.Open(compressionStream))
                    {
                        while (reader.MoveToNextEntry())
                        {
                            var entryDestination = Path.Combine(destination, reader.Entry.Key);
                            if (EnvironmentInfo.IsWin && File.Exists(entryDestination))
                            {
                                // In Windows, remove existing files before overwrite, to prevent existing filename case sticking
                                File.Delete(entryDestination);
                            }

                            reader.WriteEntryToDirectory(destination, new ExtractionOptions {
                                ExtractFullPath = true, Overwrite = true
                            });
                        }
                    }
                }
            }
        }
    }
Пример #4
0
        protected override Stream GetCompressionStream(Stream stream)
        {
            var gzipReader = GZipReader.Open(stream);

            gzipReader.MoveToNextEntry();
            return(gzipReader.OpenEntryStream());
        }
Пример #5
0
        protected override IReader CreateReaderForSolidExtraction()
        {
            Stream stream = Enumerable.Single <GZipVolume>(base.Volumes).Stream;

            stream.Position = 0L;
            return(GZipReader.Open(stream, Options.KeepStreamsOpen));
        }
Пример #6
0
        /// <inheritdoc />
        public void ExtractAllFromGz(Stream source, string targetPath, bool overwriteExistingFiles)
        {
            using var reader = GZipReader.Open(source);
            var options = new ExtractionOptions
            {
                ExtractFullPath = true,
                Overwrite       = overwriteExistingFiles
            };

            reader.WriteAllToDirectory(targetPath, options);
        }
Пример #7
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");
        }
Пример #8
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));
        }
Пример #9
0
        public void ExtractAllFromGz(Stream source, string targetPath, bool overwriteExistingFiles)
        {
            using (var reader = GZipReader.Open(source))
            {
                var options = new ExtractionOptions();
                options.ExtractFullPath = true;

                if (overwriteExistingFiles)
                {
                    options.Overwrite = true;
                }

                reader.WriteAllToDirectory(targetPath, options);
            }
        }
Пример #10
0
        /// <inheritdoc />
        public void ExtractFirstFileFromGz(Stream source, string targetPath, string defaultFileName)
        {
            using var reader = GZipReader.Open(source);
            if (reader.MoveToNextEntry())
            {
                var entry = reader.Entry;

                var filename = entry.Key;
                if (string.IsNullOrWhiteSpace(filename))
                {
                    filename = defaultFileName;
                }

                reader.WriteEntryToFile(Path.Combine(targetPath, filename));
            }
        }
Пример #11
0
        public IEnumerable <Tuple <string, Stream> > DecompressStreamFiles(Stream stream, ref bool mergeStreams)
        {
            var    response   = new List <Tuple <string, Stream> >();
            Stream mStream    = default(Stream);
            var    gzipReader = GZipReader.Open(stream);

            while (gzipReader.MoveToNextEntry())
            {
                mStream = new MemoryStream();
                gzipReader.WriteEntryTo(mStream);

                if (!gzipReader.Entry.IsDirectory)
                {
                    response.Add(Tuple.Create <string, Stream>(gzipReader.Entry.Key.Replace(@"\", " "), mStream));
                }
            }
            return(response);
        }
Пример #12
0
        /// <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.");
        }
Пример #13
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, 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);
        }