예제 #1
0
 public static bool EnsureValidArchives(string filePath)
 {
     return(ZipArchive.IsZipFile(filePath) ||
            RarArchive.IsRarFile(filePath) ||
            SevenZipArchive.IsSevenZipFile(filePath) ||
            TarArchive.IsTarFile(filePath));
 }
예제 #2
0
        private CompressionType GetCompressionType(string sourceFile)
        {
            if (SevenZipArchive.IsSevenZipFile(sourceFile))
            {
                return(CompressionType.Sevenzip);
            }

            if (ZipArchive.IsZipFile(sourceFile))
            {
                return(CompressionType.Zip);
            }

            if (GZipArchive.IsGZipFile(sourceFile) && Path.GetExtension(sourceFile).ToLower() == ".gz")
            {
                return(CompressionType.Gzip);
            }

            if (RarArchive.IsRarFile(sourceFile))
            {
                return(CompressionType.Rar);
            }

            if (TarArchive.IsTarFile(sourceFile) && Path.GetExtension(sourceFile).ToLower() == ".tar")
            {
                return(CompressionType.Tar);
            }

            return(CompressionType.None);
        }
예제 #3
0
 /// <summary>
 /// Constructor with a FileInfo object to an existing file.
 /// </summary>
 /// <param name="fileInfo"></param>
 /// <param name="options"></param>
 public static IArchive Open(FileInfo fileInfo, ReaderOptions options = null)
 {
     fileInfo.CheckNotNull(nameof(fileInfo));
     options = options ?? new ReaderOptions {
         LeaveStreamOpen = false
     };
     using (var stream = fileInfo.OpenRead())
     {
         if (ZipArchive.IsZipFile(stream, null))
         {
             return(ZipArchive.Open(fileInfo, options));
         }
         stream.Seek(0, SeekOrigin.Begin);
         if (SevenZipArchive.IsSevenZipFile(stream))
         {
             return(SevenZipArchive.Open(fileInfo, options));
         }
         stream.Seek(0, SeekOrigin.Begin);
         if (GZipArchive.IsGZipFile(stream))
         {
             return(GZipArchive.Open(fileInfo, options));
         }
         stream.Seek(0, SeekOrigin.Begin);
         if (RarArchive.IsRarFile(stream, options))
         {
             return(RarArchive.Open(fileInfo, options));
         }
         stream.Seek(0, SeekOrigin.Begin);
         if (TarArchive.IsTarFile(stream))
         {
             return(TarArchive.Open(fileInfo, options));
         }
         throw new InvalidOperationException("Cannot determine compressed stream type. Supported Archive Formats: Zip, GZip, Tar, Rar, 7Zip");
     }
 }
예제 #4
0
        private static bool IsArchive(Stream stream, out ArchiveType?type)
        {
            type = null;
            stream.CheckNotNull(nameof(stream));

            if (!stream.CanRead || !stream.CanSeek)
            {
                throw new ArgumentException("Stream should be readable and seekable");
            }
            if (ZipArchive.IsZipFile(stream, null))
            {
                type = ArchiveType.Zip;
            }
            stream.Seek(0, SeekOrigin.Begin);
            if (type == null)
            {
                if (SevenZipArchive.IsSevenZipFile(stream))
                {
                    type = ArchiveType.SevenZip;
                }
                stream.Seek(0, SeekOrigin.Begin);
            }
            if (type == null)
            {
                if (GZipArchive.IsGZipFile(stream))
                {
                    type = ArchiveType.GZip;
                }
                stream.Seek(0, SeekOrigin.Begin);
            }
            if (type == null)
            {
                if (RarArchive.IsRarFile(stream))
                {
                    type = ArchiveType.Rar;
                }
                stream.Seek(0, SeekOrigin.Begin);
            }
            if (type == null)
            {
                if (TarArchive.IsTarFile(stream))
                {
                    type = ArchiveType.Tar;
                }
                stream.Seek(0, SeekOrigin.Begin);
            }
            if (type == null)                      //test multipartzip as it could find zips in other non compressed archive types?
            {
                if (ZipArchive.IsZipMulti(stream)) //test the zip (last) file of a multipart zip
                {
                    type = ArchiveType.Zip;
                }
                stream.Seek(0, SeekOrigin.Begin);
            }

            return(type != null);
        }
예제 #5
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 (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));
            }
            throw new InvalidOperationException("Cannot determine compressed stream type.  Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar");
        }
예제 #6
0
        public override bool TestUnZip(string filePath)
        {
            try
            {
                using (var stream = File.OpenRead(filePath))
                {
                    IReader reader = null;

                    if (IsEncryptFile(filePath))
                    {
                        if (ZipArchive.IsZipFile(filePath))
                        {
                            reader = ZipReader.Open(stream, new ReaderOptions {
                                Password = "******"
                            });
                        }
                        else if (RarArchive.IsRarFile(filePath))
                        {
                            reader = RarReader.Open(stream, new ReaderOptions {
                                Password = "******"
                            });
                        }
                    }
                    else
                    {
                        reader = ReaderFactory.Open(stream);
                    }

                    var subGuidDir =
                        new DirectoryInfo(Path.Combine(ZipBase.UnZipRootDir, Guid.NewGuid().ToString("N")));
                    subGuidDir.Create();

                    if (reader != null)
                    {
                        while (reader.MoveToNextEntry())
                        {
                            if (!reader.Entry.IsDirectory)
                            {
                                reader.WriteEntryToDirectory(subGuidDir.FullName,
                                                             new ExtractionOptions {
                                    ExtractFullPath = true, Overwrite = true
                                });
                            }
                        }

                        return(VerifyManager.Verify(subGuidDir.FullName));
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }

            return(false);
        }
예제 #7
0
        public bool ValidateArchive(string filePath)
        {
            if (filePath.Length > 0)
            {
                if (System.IO.File.Exists(filePath))
                {
                    return(RarArchive.IsRarFile(filePath) ? true : false);
                }
            }

            return(false);
        }
예제 #8
0
 public static bool IsRarFile(string fileName)
 {
     try
     {
         return(RarArchive.IsRarFile(fileName));
     }
     catch (Exception ex)
     {
         Logger.Log.Debug(ex.Message);
     }
     return(false);
 }
예제 #9
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.");
        }
예제 #10
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));
        }
예제 #11
0
        /// <summary>
        /// Opens an Archive for random access
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IArchive Open(Stream stream, Options options = Options.KeepStreamsOpen)
        {
            stream.CheckNotNull("stream");
            if (!stream.CanRead || !stream.CanSeek)
            {
                throw new ArgumentException("Stream should be readable and seekable");
            }

            if (ZipArchive.IsZipFile(stream, null))
            {
                stream.Seek(0, SeekOrigin.Begin);
                return(ZipArchive.Open(stream, options, null));
            }
#if RAR
            stream.Seek(0, SeekOrigin.Begin);
            if (RarArchive.IsRarFile(stream, Options.LookForHeader | Options.KeepStreamsOpen))
            {
                stream.Seek(0, SeekOrigin.Begin);
                return(RarArchive.Open(stream, options));
            }
#endif
#if TAR
            stream.Seek(0, SeekOrigin.Begin);
            if (TarArchive.IsTarFile(stream))
            {
                stream.Seek(0, SeekOrigin.Begin);
                return(TarArchive.Open(stream, options));
            }
#endif
#if SEVENZIP
            stream.Seek(0, SeekOrigin.Begin);
            if (SevenZipArchive.IsSevenZipFile(stream))
            {
                stream.Seek(0, SeekOrigin.Begin);
                return(SevenZipArchive.Open(stream, options));
            }
#endif
#if GZIP
            stream.Seek(0, SeekOrigin.Begin);
            if (GZipArchive.IsGZipFile(stream))
            {
                stream.Seek(0, SeekOrigin.Begin);
                return(GZipArchive.Open(stream, options));
            }
#endif
            throw new InvalidOperationException("Cannot determine compressed stream type.  Supported Archive Formats: Zip, GZip, Tar, Rar, 7Zip");
        }
예제 #12
0
 /// <summary>
 /// Constructor with a FileInfo object to an existing file.
 /// </summary>
 /// <param name="fileInfo"></param>
 /// <param name="options"></param>
 public static IArchive Open(FileInfo fileInfo, Options options)
 {
     fileInfo.CheckNotNull("fileInfo");
     using (var stream = fileInfo.OpenRead())
     {
         if (ZipArchive.IsZipFile(stream))
         {
             return(ZipArchive.Open(fileInfo, options));
         }
         stream.Seek(0, SeekOrigin.Begin);
         if (RarArchive.IsRarFile(stream))
         {
             return(RarArchive.Open(fileInfo, options));
         }
         throw new InvalidOperationException("Cannot determine compressed stream type.");
     }
 }
예제 #13
0
        /// <summary>
        /// Constructor with a FileInfo object to an existing file.
        /// </summary>
        /// <param name="fileInfo"></param>
        /// <param name="options"></param>
        public static IArchive Open(FileInfo fileInfo, Options options)
        {
            fileInfo.CheckNotNull("fileInfo");
            using (var stream = fileInfo.OpenRead())
            {
                if (ZipArchive.IsZipFile(stream, null))
                {
                    stream.Dispose();
                    return(ZipArchive.Open(fileInfo, options, null));
                }
#if RAR
                stream.Seek(0, SeekOrigin.Begin);
                if (RarArchive.IsRarFile(stream, Options.LookForHeader | Options.KeepStreamsOpen))
                {
                    stream.Dispose();
                    return(RarArchive.Open(fileInfo, options));
                }
#endif
#if TAR
                stream.Seek(0, SeekOrigin.Begin);
                if (TarArchive.IsTarFile(stream))
                {
                    stream.Dispose();
                    return(TarArchive.Open(fileInfo, options));
                }
#endif
#if SEVENZIP
                stream.Seek(0, SeekOrigin.Begin);
                if (SevenZipArchive.IsSevenZipFile(stream))
                {
                    stream.Dispose();
                    return(SevenZipArchive.Open(fileInfo, options));
                }
#endif
#if GZIP
                stream.Seek(0, SeekOrigin.Begin);
                if (GZipArchive.IsGZipFile(stream))
                {
                    stream.Dispose();
                    return(GZipArchive.Open(fileInfo, options));
                }
#endif
                throw new InvalidOperationException("Cannot determine compressed stream type.");
            }
        }
예제 #14
0
 /// <summary>
 /// Opens an Archive for random access
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="readerOptions"></param>
 /// <returns></returns>
 public static IArchive Open(Stream stream, ReaderOptions?readerOptions = null)
 {
     stream.CheckNotNull(nameof(stream));
     if (!stream.CanRead || !stream.CanSeek)
     {
         throw new ArgumentException("Stream should be readable and seekable");
     }
     readerOptions ??= new ReaderOptions();
     if (ZipArchive.IsZipFile(stream, null))
     {
         stream.Seek(0, SeekOrigin.Begin);
         return(ZipArchive.Open(stream, readerOptions));
     }
     stream.Seek(0, SeekOrigin.Begin);
     if (SevenZipArchive.IsSevenZipFile(stream))
     {
         stream.Seek(0, SeekOrigin.Begin);
         return(SevenZipArchive.Open(stream, readerOptions));
     }
     stream.Seek(0, SeekOrigin.Begin);
     if (GZipArchive.IsGZipFile(stream))
     {
         stream.Seek(0, SeekOrigin.Begin);
         return(GZipArchive.Open(stream, readerOptions));
     }
     stream.Seek(0, SeekOrigin.Begin);
     if (DmgArchive.IsDmgFile(stream))
     {
         stream.Seek(0, SeekOrigin.Begin);
         return(DmgArchive.Open(stream, readerOptions));
     }
     stream.Seek(0, SeekOrigin.Begin);
     if (RarArchive.IsRarFile(stream, readerOptions))
     {
         stream.Seek(0, SeekOrigin.Begin);
         return(RarArchive.Open(stream, readerOptions));
     }
     stream.Seek(0, SeekOrigin.Begin);
     if (TarArchive.IsTarFile(stream))
     {
         stream.Seek(0, SeekOrigin.Begin);
         return(TarArchive.Open(stream, readerOptions));
     }
     throw new InvalidOperationException("Cannot determine compressed stream type. Supported Archive Formats: Zip, GZip, Tar, Rar, 7Zip, LZip, Dmg");
 }
 public static bool TryOpen(Stream stream, ReaderOptions readerOptions, ArchiveTypeMask archiveTypes, out IArchive archive)
 {
     stream.CheckNotNull("stream");
     if (!stream.CanRead || !stream.CanSeek)
     {
         throw new ArgumentException("Stream should be readable and seekable");
     }
     readerOptions = readerOptions ?? new ReaderOptions();
     if (archiveTypes.HasFlag(ArchiveTypeMask.Zip) && ZipArchive.IsZipFile(stream, null))
     {
         stream.Seek(0, SeekOrigin.Begin);
         archive = ZipArchive.Open(stream, readerOptions);
         return(true);
     }
     stream.Seek(0, SeekOrigin.Begin);
     if (archiveTypes.HasFlag(ArchiveTypeMask.SevenZip) && SevenZipArchive.IsSevenZipFile(stream))
     {
         stream.Seek(0, SeekOrigin.Begin);
         archive = SevenZipArchive.Open(stream, readerOptions);
         return(true);
     }
     stream.Seek(0, SeekOrigin.Begin);
     if (archiveTypes.HasFlag(ArchiveTypeMask.GZip) && GZipArchive.IsGZipFile(stream))
     {
         stream.Seek(0, SeekOrigin.Begin);
         archive = GZipArchive.Open(stream, readerOptions);
         return(true);
     }
     stream.Seek(0, SeekOrigin.Begin);
     if (archiveTypes.HasFlag(ArchiveTypeMask.Rar) && RarArchive.IsRarFile(stream, readerOptions))
     {
         stream.Seek(0, SeekOrigin.Begin);
         archive = RarArchive.Open(stream, readerOptions);
         return(true);
     }
     stream.Seek(0, SeekOrigin.Begin);
     if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(stream))
     {
         stream.Seek(0, SeekOrigin.Begin);
         archive = TarArchive.Open(stream, readerOptions);
         return(true);
     }
     archive = null;
     return(false);
 }
예제 #16
0
        public static bool isSupportedArchive(string filePath)
        {
            FileInfo fileInfo = new FileInfo(filePath);

            try
            {
                using (var stream = fileInfo.OpenRead())
                {
                    if (ZipArchive.IsZipFile(stream, null))
                    {
                        stream.Dispose();
                        return(true);
                    }
                    stream.Seek(0, SeekOrigin.Begin);
                    if (SevenZipArchive.IsSevenZipFile(stream))
                    {
                        stream.Dispose();
                        return(true);
                    }
                    stream.Seek(0, SeekOrigin.Begin);
                    if (GZipArchive.IsGZipFile(stream))
                    {
                        stream.Dispose();
                        return(true);
                    }
                    stream.Seek(0, SeekOrigin.Begin);
                    if (RarArchive.IsRarFile(stream, Options.None))
                    {
                        stream.Dispose();
                        return(true);
                    }
                    stream.Seek(0, SeekOrigin.Begin);
                    if (TarArchive.IsTarFile(stream))
                    {
                        stream.Dispose();
                        return(true);
                    }
                    return(false);
                }
            }
            catch (Exception)
            {
                return(false);
            }
        }
예제 #17
0
 bool ValidateAllVolumes(string filePath)
 {
     if (System.IO.File.Exists(filePath))
     {
         if (!RarArchive.IsRarFile(filePath))
         {
             RarArchive archive = RarArchive.Open(filePath);
             foreach (RarArchiveVolume vol in archive.Volumes)
             {
                 if (!ValidateArchive(vol.VolumeFile.FullName))
                 {
                     return(false);
                 }
             }
         }
     }
     return(true);
 }
예제 #18
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.");
        }
예제 #19
0
        public List <string> ProcessArchive(string archivePath, string pathToExtract)
        {
            List <string> exrtactedFiles = new List <string>();

            if (!RarArchive.IsRarFile(archivePath) && !ZipFile.IsZipFile(archivePath))
            {
                return(null);
            }

            if (ZipFile.IsZipFile(archivePath))
            {
                exrtactedFiles = ProcessZipArchive(archivePath, pathToExtract);
            }
            if (RarArchive.IsRarFile(archivePath))
            {
                ProcessRarArchive(archivePath, pathToExtract);
            }
            return(exrtactedFiles);
        }
예제 #20
0
        public override async Task ProcessFile(Stream blobStream)
        {
            var rarReaderOptions = new ReaderOptions()
            {
                ArchiveEncoding = new ArchiveEncoding(Encoding.UTF8, Encoding.UTF8),
                Password        = _secretSettings?.ZipPassword,
                LookForHeader   = true
            };

            if (RarArchive.IsRarFile(blobStream, rarReaderOptions))
            {
                _logger.LogInformation("Blob is a rar file; beginning extraction....");
                blobStream.Position = 0;

                using var reader = RarArchive.Open(blobStream, rarReaderOptions);

                await ExtractArchiveFiles(reader.Entries);
            }
        }
예제 #21
0
        /// <summary>
        /// Opens an Archive for random access
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static IArchive Open(Stream stream, Options options = Options.KeepStreamsOpen)
        {
            stream.CheckNotNull("stream");
            if (!stream.CanRead || !stream.CanSeek)
            {
                throw new ArgumentException("Stream should be readable and seekable");
            }

            if (ZipArchive.IsZipFile(stream))
            {
                stream.Seek(0, SeekOrigin.Begin);
                return(ZipArchive.Open(stream, options));
            }
            stream.Seek(0, SeekOrigin.Begin);
            if (RarArchive.IsRarFile(stream))
            {
                stream.Seek(0, SeekOrigin.Begin);
                return(RarArchive.Open(stream, options));
            }
            throw new InvalidOperationException("Cannot determine compressed stream type.");
        }
 public static bool TryOpen(FileInfo fileInfo, ReaderOptions options, ArchiveTypeMask archiveTypes, out IArchive archive)
 {
     fileInfo.CheckNotNull("fileInfo");
     options = options ?? new ReaderOptions {
         LeaveStreamOpen = false
     };
     using (var stream = fileInfo.OpenRead())
     {
         if (archiveTypes.HasFlag(ArchiveTypeMask.Zip) && ZipArchive.IsZipFile(stream, null))
         {
             archive = ZipArchive.Open(fileInfo, options);
         }
         stream.Seek(0, SeekOrigin.Begin);
         if (archiveTypes.HasFlag(ArchiveTypeMask.SevenZip) && SevenZipArchive.IsSevenZipFile(stream))
         {
             archive = SevenZipArchive.Open(fileInfo, options);
         }
         stream.Seek(0, SeekOrigin.Begin);
         if (archiveTypes.HasFlag(ArchiveTypeMask.GZip) && GZipArchive.IsGZipFile(stream))
         {
             archive = GZipArchive.Open(fileInfo, options);
             return(true);
         }
         stream.Seek(0, SeekOrigin.Begin);
         if (archiveTypes.HasFlag(ArchiveTypeMask.Rar) && RarArchive.IsRarFile(stream, options))
         {
             archive = RarArchive.Open(fileInfo, options);
             return(true);
         }
         stream.Seek(0, SeekOrigin.Begin);
         if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(stream))
         {
             archive = TarArchive.Open(fileInfo, options);
             return(true);
         }
     }
     archive = null;
     return(false);
 }
예제 #23
0
 /// <summary>
 /// Constructor with a FileInfo object to an existing file.
 /// </summary>
 /// <param name="fileInfo"></param>
 /// <param name="options"></param>
 public static IArchive Open(FileInfo fileInfo, Options options)
 {
     //fileInfo.CheckNotNull("fileInfo");
     Utility.CheckNotNull(fileInfo, "fileInfo");
     using (var stream = fileInfo.OpenRead())
     {
         if (ZipArchive.IsZipFile(stream, null))
         {
             stream.Dispose();
             return(ZipArchive.Open(fileInfo, options, null));
         }
         stream.Seek(0, SeekOrigin.Begin);
         if (TarArchive.IsTarFile(stream))
         {
             stream.Dispose();
             return(TarArchive.Open(fileInfo, options));
         }
         stream.Seek(0, SeekOrigin.Begin);
         if (SevenZipArchive.IsSevenZipFile(stream))
         {
             stream.Dispose();
             return(SevenZipArchive.Open(fileInfo, options));
         }
         stream.Seek(0, SeekOrigin.Begin);
         if (GZipArchive.IsGZipFile(stream))
         {
             stream.Dispose();
             return(GZipArchive.Open(fileInfo, options));
         }
         stream.Seek(0, SeekOrigin.Begin);
         if (RarArchive.IsRarFile(stream, Options.LookForHeader | Options.KeepStreamsOpen))
         {
             stream.Dispose();
             return(RarArchive.Open(fileInfo, options));
         }
         throw new InvalidOperationException("Cannot determine compressed stream type. Supported Archive Formats: Zip, GZip, Tar, Rar, 7Zip");
     }
 }
예제 #24
0
        public ArchiveTypes IsArchive(string path)
        {
            var type = ArchiveTypes.None;

            if (RarArchive.IsRarFile(path))
            {
                type = ArchiveTypes.RAR;
            }
            if (ZipArchive.IsZipFile(path))
            {
                type = ArchiveTypes.Zip;
            }
            if (SevenZipArchive.IsSevenZipFile(path))
            {
                type = ArchiveTypes.SevenZip;
            }
            if (TarArchive.IsTarFile(path))
            {
                type = ArchiveTypes.Deflate;
            }

            return(type);
        }
예제 #25
0
 public static bool IsRarFile(string rarFileName)
 {
     return(RarArchive.IsRarFile(rarFileName));
 }
예제 #26
0
 public static bool isArchive(FileInfo file)
 {
     //FIXME This is broken and detects .gba ROMs as stuff
     return(GZipArchive.IsGZipFile(file) || RarArchive.IsRarFile(file) || SevenZipArchive.IsSevenZipFile(file) ||
            TarArchive.IsTarFile(file) || ZipArchive.IsZipFile(file));
 }
예제 #27
0
 public static bool IsRar(Stream stream)
 {
     return(RarArchive.IsRarFile(stream));
 }
        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);
        }