public static bool EnsureValidArchives(string filePath) { return(ZipArchive.IsZipFile(filePath) || RarArchive.IsRarFile(filePath) || SevenZipArchive.IsSevenZipFile(filePath) || TarArchive.IsTarFile(filePath)); }
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); }
/// <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"); } }
public async Task ExtractPackageAsync(byte[] file, string destDirPath, IProgress <double> progress = null, CancellationToken cancellationToken = new CancellationToken()) { long totalSize = file.Length; using (Stream memoryStream = new MemoryStream(file)) { if (SevenZipArchive.IsSevenZipFile(memoryStream)) { // 7z is not a streamable format, thus doesn't function with the ReaderFactory API memoryStream.Position = 0; using (var sevenZipArchive = SevenZipArchive.Open(memoryStream)) { var reader = sevenZipArchive.ExtractAllEntries(); await ExtractFromReaderAsync(reader, destDirPath, totalSize, progress); } } else { memoryStream.Position = 0; using (var factory = ReaderFactory.Open(memoryStream)) { await ExtractFromReaderAsync(factory, destDirPath, totalSize, progress); } } } }
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); }
public static Task <bool> ExtractFileAsync(string password, string zipLocation, string unzipLocation, IProgress <int> progress, CancellationToken stop) { if (zipLocation.EndsWith(".7z") || SevenZipArchive.IsSevenZipFile(zipLocation)) { return(Un7zipFileAsync(password, zipLocation, unzipLocation, progress, stop)); } // default zip return(UnzipFileAsync(password, zipLocation, unzipLocation, progress, stop)); }
private static void Install(string path, string name, byte[] data) { Console.WriteLine($"Installing {name}..."); Directory.CreateDirectory(path); var skip = new[] { "server.cfg", "server-tls.crt", "server-tls.key", "__resource.lua", "fxmanifest.lua", "nfive.yml", "nfive.lock", "config/nfive.yml", "config/database.yml" }; using (var stream = new MemoryStream(data)) { var sevenZip = SevenZipArchive.IsSevenZipFile(stream); stream.Position = 0; using (var archive = sevenZip ? SevenZipArchive.Open(stream) : null) using (var reader = sevenZip ? archive.ExtractAllEntries() : ReaderFactory.Open(stream)) { while (reader.MoveToNextEntry()) { if (reader.Entry.IsDirectory) { continue; } var opts = new ExtractionOptions { ExtractFullPath = true, Overwrite = true, PreserveFileTime = true }; if (skip.Contains(reader.Entry.Key) && File.Exists(Path.Combine(path, reader.Entry.Key))) { opts.Overwrite = false; // TODO: Prompt to overwrite existing config? } reader.WriteEntryToDirectory(path, opts); } } } Console.WriteLine(); }
static public byte[] Decompress(byte[] CompressedBuffer) { using (MemoryStream stream = new MemoryStream(CompressedBuffer)) { if (SevenZipArchive.IsSevenZipFile(stream)) { return(Decompress7Zip(CompressedBuffer)); } else { return(DecompressZip(CompressedBuffer)); } } }
/// <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"); }
/// <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 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); } }
/// <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."); } }
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); }
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); }
/// <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)); } stream.Seek(0, SeekOrigin.Begin); if (RarArchive.IsRarFile(stream)) { stream.Seek(0, SeekOrigin.Begin); return(RarArchive.Open(stream, options)); } stream.Seek(0, SeekOrigin.Begin); if (TarArchive.IsTarFile(stream)) { stream.Seek(0, SeekOrigin.Begin); return(TarArchive.Open(stream, options)); } stream.Seek(0, SeekOrigin.Begin); if (SevenZipArchive.IsSevenZipFile(stream)) { stream.Seek(0, SeekOrigin.Begin); return(SevenZipArchive.Open(stream, options)); } stream.Seek(0, SeekOrigin.Begin); if (GZipArchive.IsGZipFile(stream)) { stream.Seek(0, SeekOrigin.Begin); return(GZipArchive.Open(stream, options)); } throw new InvalidOperationException("Cannot determine compressed stream type."); }
/// <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"); } }
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); }
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)); }
private void InspectFiles(List <string> fileNames) { var threads = new List <Thread>(); foreach (var fileName in fileNames) { var finfo = new FileInfo(fileName); if (pcapReader.CanRead(fileName)) { var reader = new PCAPReader(); var fileReadObjects = reader.Read(fileName); var first = (PCAPBlock)fileReadObjects.First().ReadObject; var last = (PCAPBlock)fileReadObjects.Last().ReadObject; var dsource = new DataSource { FileInfo = finfo, StartTime = first.DateTime, EndTime = last.DateTime, SourceType = SourceType.PCAP, Packets = fileReadObjects.Count }; UpdateList(dsource); continue; } else if (pcapngReader.CanRead(fileName)) { var reader = new PCAPNGReader(); var fileReadObjects = reader.Read(fileName); var first = (PCAPNGBlock)fileReadObjects.First().ReadObject; var last = (PCAPNGBlock)fileReadObjects.Last().ReadObject; var dsource = new DataSource { FileInfo = finfo, StartTime = first.Timestamp, EndTime = last.Timestamp, SourceType = SourceType.PCAPNG, Packets = fileReadObjects.Count }; UpdateList(dsource); continue; } if (SevenZipArchive.IsSevenZipFile(fileName)) { // because of lousy performance when operating on 7zip, we launch a new thread instead of using tasks var thread = new Thread(() => DoSevenZip(finfo)); thread.Start(); threads.Add(thread); continue; } // zip performance is much better and we will not thread DoZip(finfo); } UpdateList("Waiting for all threads to finish"); foreach (Thread thread in threads) { thread.Join(); } UpdateList("All threads finished"); }
public static bool IsSevenZipFile(string sevenZipFilename) { return(SevenZipArchive.IsSevenZipFile(sevenZipFilename)); }
public void EnumerateFiles(List <DataSource> dataSources) { _popup.Show(); _popup.Text = "Starting the parse"; var dic = new Dictionary <string, Func <string, List <FileReadObject> > >(); //SevenZipExtractor.SetLibraryPath(Application.ExecutablePath + @"\7z.dll"); int i = 1; foreach (var source in dataSources) { PacketCounter = 0; PacketTotal = source.Packets; string pre = i + "/" + dataSources.Count + " "; _popup.Text = pre + "Reading from " + source.FileInfo.Name; if (source.SourceType == SourceType.PCAP) { pcapReader.Read(source.FileInfo.FullName); } else if (source.SourceType == SourceType.PCAPNG) { pcapngReader.Read(source.FileInfo.FullName); } else if (source.SourceType == SourceType.Zip) { if (SevenZipArchive.IsSevenZipFile(source.FileInfo.FullName)) { using (SevenZipArchive sevenZipArchive = SevenZipArchive.Open(source.FileInfo.FullName)) { using (var reader = sevenZipArchive.ExtractAllEntries()) { ZipReader(reader, source); } } GC.Collect(); } else { try { using (var filestream = File.OpenRead(source.FileInfo.FullName)) { using (var reader = ReaderFactory.Open(filestream)) { ZipReader(reader, source); } } } catch (InvalidOperationException ex) { } GC.Collect(); } } } _popup.Close(); }
static void Main(string[] args) { #region Startup Verification // insufficient params to proceed if (args.Count() != 2) { Usage(); return; } switch (args[0]) { case "help": case "/help": case "-help": case "--help": Usage(); return; default: break; } var newformat = args[0]; var filename = args[1]; // ensure that the target file exists if (File.Exists($"{filename}") == false) { WriteError($"Fatal Error: Cannot find file '{filename}'"); return; } #endregion string newFileExtension = null; // TODO: check if formats are supported by the library CompressionType?newCompressionType = null; ArchiveType? newArchiveType = MatchArchiveType(newformat, out newCompressionType, out newFileExtension); if (newArchiveType == null) { Usage(); return; } // create a unique temp folder for file extraction var tempfolder = $@"{Path.GetTempPath()}{Guid.NewGuid().ToString()}"; Directory.CreateDirectory(tempfolder); try { using (var reader = SevenZipArchive.IsSevenZipFile(filename) ? SevenZipArchive.Open(filename).ExtractAllEntries() : ReaderFactory.Open(File.OpenRead(filename), Options.LookForHeader)) { WriteInfo($"Converting file '{filename.ToUpper()}' from {reader.ArchiveType.ToString().ToUpper()} to {newformat.ToUpper()}"); // TODO: The following code can be replaced by writing everything to a folder // e.g. reader.WriteAllToDirectory(tempfolder); while (reader.MoveToNextEntry()) { if (reader.Entry.IsDirectory) { WriteInfo1($"Extracting directory: {reader.Entry.Key}..."); reader.WriteEntryToDirectory(tempfolder); } // create temp folder and extract files //Console.WriteLine($"Extracting file: {reader.Entry.Key}..."); WriteInfo2($"Extracting file: {reader.Entry.Key}..."); reader.WriteEntryToFile($@"{tempfolder}\{reader.Entry.Key}"); } } var newArchiveName = $@"{Path.GetFileNameWithoutExtension(filename)}{newFileExtension}"; if (!string.IsNullOrEmpty(Path.GetDirectoryName(filename))) { newArchiveName = $@"{Path.GetDirectoryName(filename)}\{newArchiveName}"; } using (var outStream = File.OpenWrite(newArchiveName)) { using (var archive = WriterFactory.Open(outStream, newArchiveType.Value, newCompressionType.Value)) { //Console.WriteLine($"Writing files to new archive: {newArchiveName}"); WriteInfo1($"Writing files to new archive: {newArchiveName}"); archive.WriteAll(tempfolder, "*", SearchOption.AllDirectories); } } } catch (Exception e) { WriteError($"Critical Error: {e.ToString()}"); } finally { Directory.Delete(tempfolder, true); } }