From e6e938788c2f29d6d795ec486facdab8303f78ae Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 15 Feb 2018 18:58:29 -0800 Subject: [PATCH] [Enums, Utilities] Remove dependence on SharpCompress for enum --- SabreTools.Library/Data/Enums.cs | 7 +- SabreTools.Library/Tools/Utilities.cs | 164 ++++++++++++++------------ 2 files changed, 91 insertions(+), 80 deletions(-) diff --git a/SabreTools.Library/Data/Enums.cs b/SabreTools.Library/Data/Enums.cs index d18cdfaa..ff3f857a 100644 --- a/SabreTools.Library/Data/Enums.cs +++ b/SabreTools.Library/Data/Enums.cs @@ -95,11 +95,14 @@ public enum FileType { None = 0, + + // Archival types SevenZipArchive, CHD, GZipArchive, - RARArchive, - TARArchive, + RarArchive, + TarArchive, + ZipArchive, } /// diff --git a/SabreTools.Library/Tools/Utilities.cs b/SabreTools.Library/Tools/Utilities.cs index e5ab6a51..5092b451 100644 --- a/SabreTools.Library/Tools/Utilities.cs +++ b/SabreTools.Library/Tools/Utilities.cs @@ -37,7 +37,6 @@ using Stream = System.IO.Stream; using StreamReader = System.IO.StreamReader; #endif using NaturalSort; -using SharpCompress.Common; namespace SabreTools.Library.Tools { @@ -446,7 +445,7 @@ namespace SabreTools.Library.Tools BaseArchive archive = null; // First get the archive type - ArchiveType? at = GetArchiveType(input); + FileType? at = GetFileType(input); // If we got back null, then it's not an archive, so we we return if (at == null) @@ -458,21 +457,25 @@ namespace SabreTools.Library.Tools Globals.Logger.Verbose("Found archive of type: {0}", at); switch (at) { - case ArchiveType.GZip: + case FileType.GZipArchive: archive = new GZipArchive(input); break; - case ArchiveType.Rar: + case FileType.RarArchive: archive = new RarArchive(input); break; - case ArchiveType.SevenZip: + case FileType.SevenZipArchive: archive = new SevenZipArchive(input); break; - case ArchiveType.Tar: + case FileType.TarArchive: archive = new TapeArchive(input); break; - case ArchiveType.Zip: + case FileType.ZipArchive: archive = new TorrentZipArchive(input); break; + case FileType.CHD: + default: + // We ignore these types for now + break; } return archive; @@ -483,19 +486,19 @@ namespace SabreTools.Library.Tools /// /// SharpCompress.Common.ArchiveType representing the archive to create /// Archive object representing the inputs - public static BaseArchive GetArchive(ArchiveType archiveType) + public static BaseArchive GetArchive(FileType archiveType) { switch (archiveType) { - case ArchiveType.GZip: + case FileType.GZipArchive: return new GZipArchive(); - case ArchiveType.Rar: + case FileType.RarArchive: return new RarArchive(); - case ArchiveType.SevenZip: + case FileType.SevenZipArchive: return new SevenZipArchive(); - case ArchiveType.Tar: + case FileType.TarArchive: return new TapeArchive(); - case ArchiveType.Zip: + case FileType.ZipArchive: return new TorrentZipArchive(); default: return null; @@ -1043,65 +1046,6 @@ namespace SabreTools.Library.Tools return datItem; } - /// - /// Returns the archive type of an input file - /// - /// Input file to check - /// ArchiveType of inputted file (null on error) - public static ArchiveType? GetArchiveType(string input) - { - ArchiveType? outtype = null; - - // If the file is null, then we have no archive type - if (input == null) - { - return outtype; - } - - // First line of defense is going to be the extension, for better or worse - if (!HasValidArchiveExtension(input)) - { - return outtype; - } - - // Read the first bytes of the file and get the magic number - try - { - byte[] magic = new byte[8]; - BinaryReader br = new BinaryReader(TryOpenRead(input)); - magic = br.ReadBytes(8); - br.Dispose(); - - // Now try to match it to a known signature - if (magic.StartsWith(Constants.SevenZipSignature)) - { - outtype = ArchiveType.SevenZip; - } - else if (magic.StartsWith(Constants.GzSignature)) - { - outtype = ArchiveType.GZip; - } - else if (magic.StartsWith(Constants.RarSignature) || magic.StartsWith(Constants.RarFiveSignature)) - { - outtype = ArchiveType.Rar; - } - else if (magic.StartsWith(Constants.TarSignature) || magic.StartsWith(Constants.TarZeroSignature)) - { - outtype = ArchiveType.Tar; - } - else if (magic.StartsWith(Constants.ZipSignature) || magic.StartsWith(Constants.ZipSignatureEmpty) || magic.StartsWith(Constants.ZipSignatureSpanned)) - { - outtype = ArchiveType.Zip; - } - } - catch (Exception) - { - // Don't log file open errors - } - - return outtype; - } - /// /// Get what type of DAT the input file is /// @@ -1328,6 +1272,69 @@ namespace SabreTools.Library.Tools return datItem; } + /// + /// Returns the file type of an input file + /// + /// Input file to check + /// FileType of inputted file (null on error) + public static FileType? GetFileType(string input) + { + FileType? outFileType = null; + + // If the file is null, then we have no archive type + if (input == null) + { + return outFileType; + } + + // First line of defense is going to be the extension, for better or worse + if (!HasValidArchiveExtension(input)) + { + return outFileType; + } + + // Read the first bytes of the file and get the magic number + try + { + byte[] magic = new byte[8]; + BinaryReader br = new BinaryReader(TryOpenRead(input)); + magic = br.ReadBytes(8); + br.Dispose(); + + // Now try to match it to a known signature + if (magic.StartsWith(Constants.SevenZipSignature)) + { + outFileType = FileType.SevenZipArchive; + } + else if (magic.StartsWith(Constants.CHDSignature)) + { + outFileType = FileType.CHD; + } + else if (magic.StartsWith(Constants.GzSignature)) + { + outFileType = FileType.GZipArchive; + } + else if (magic.StartsWith(Constants.RarSignature) || magic.StartsWith(Constants.RarFiveSignature)) + { + outFileType = FileType.RarArchive; + } + else if (magic.StartsWith(Constants.TarSignature) || magic.StartsWith(Constants.TarZeroSignature)) + { + outFileType = FileType.TarArchive; + } + else if (magic.StartsWith(Constants.ZipSignature) || magic.StartsWith(Constants.ZipSignatureEmpty) || magic.StartsWith(Constants.ZipSignatureSpanned)) + { + outFileType = FileType.ZipArchive; + } + } + catch (Exception) + { + // Don't log file open errors + } + + return outFileType; + } + /// /// Get if the current file should be scanned internally and externally /// @@ -1341,26 +1348,27 @@ namespace SabreTools.Library.Tools shouldExternalProcess = true; shouldInternalProcess = true; - ArchiveType? archiveType = GetArchiveType(input); - switch (archiveType) + FileType? fileType = GetFileType(input); + switch (fileType) { + case FileType.CHD: case null: shouldExternalProcess = true; shouldInternalProcess = false; break; - case ArchiveType.GZip: + case FileType.GZipArchive: shouldExternalProcess = ((archiveScanLevel & ArchiveScanLevel.GZipExternal) != 0); shouldInternalProcess = ((archiveScanLevel & ArchiveScanLevel.GZipInternal) != 0); break; - case ArchiveType.Rar: + case FileType.RarArchive: shouldExternalProcess = ((archiveScanLevel & ArchiveScanLevel.RarExternal) != 0); shouldInternalProcess = ((archiveScanLevel & ArchiveScanLevel.RarInternal) != 0); break; - case ArchiveType.SevenZip: + case FileType.SevenZipArchive: shouldExternalProcess = ((archiveScanLevel & ArchiveScanLevel.SevenZipExternal) != 0); shouldInternalProcess = ((archiveScanLevel & ArchiveScanLevel.SevenZipInternal) != 0); break; - case ArchiveType.Zip: + case FileType.ZipArchive: shouldExternalProcess = ((archiveScanLevel & ArchiveScanLevel.ZipExternal) != 0); shouldInternalProcess = ((archiveScanLevel & ArchiveScanLevel.ZipInternal) != 0); break;