[ArchiveTools] Move 7z read to 7z.dll wrapper

This commit is contained in:
Matt Nadareski
2017-03-14 13:56:27 -07:00
parent 0bd2c3b84e
commit d16ce5eb10

View File

@@ -27,7 +27,6 @@ using ROMVault2.SupportedFiles.Zip;
using SevenZip; using SevenZip;
using SharpCompress.Archives; using SharpCompress.Archives;
using SharpCompress.Archives.Rar; using SharpCompress.Archives.Rar;
using SharpCompress.Archives.SevenZip;
using SharpCompress.Archives.Tar; using SharpCompress.Archives.Tar;
using SharpCompress.Common; using SharpCompress.Common;
using SharpCompress.Readers; using SharpCompress.Readers;
@@ -58,6 +57,8 @@ namespace SabreTools.Helper.Tools
/// <returns>True if the extraction was a success, false otherwise</returns> /// <returns>True if the extraction was a success, false otherwise</returns>
public static bool ExtractArchive(string input, string outDir, ArchiveScanLevel archiveScanLevel) public static bool ExtractArchive(string input, string outDir, ArchiveScanLevel archiveScanLevel)
{ {
// Set internal variables
SevenZipBase.SetLibraryPath("7za.dll");
bool encounteredErrors = true; bool encounteredErrors = true;
// First get the archive type // First get the archive type
@@ -80,12 +81,21 @@ namespace SabreTools.Helper.Tools
Directory.CreateDirectory(outDir); Directory.CreateDirectory(outDir);
// Extract all files to the temp directory // Extract all files to the temp directory
SevenZipArchive sza = SevenZipArchive.Open(File.OpenRead(input)); SevenZipExtractor sza = new SevenZipExtractor(File.OpenRead(input))
foreach (SevenZipArchiveEntry entry in sza.Entries)
{ {
entry.WriteToDirectory(outDir, new ExtractionOptions{ PreserveFileTime = true, ExtractFullPath = true, Overwrite = true }); PreserveDirectoryStructure = true,
};
try
{
sza.ExtractArchive(outDir);
encounteredErrors = false;
} }
encounteredErrors = false; catch
{
encounteredErrors = true;
}
sza.Dispose(); sza.Dispose();
} }
@@ -223,6 +233,8 @@ namespace SabreTools.Helper.Tools
/// <returns>Name of the extracted file, null on error</returns> /// <returns>Name of the extracted file, null on error</returns>
public static string ExtractItem(string input, string entryName, string tempDir) public static string ExtractItem(string input, string entryName, string tempDir)
{ {
// Set internal variables
SevenZipBase.SetLibraryPath("7za.dll");
string realEntry = ""; string realEntry = "";
// Set the real entry name // Set the real entry name
@@ -242,13 +254,13 @@ namespace SabreTools.Helper.Tools
switch (at) switch (at)
{ {
case ArchiveType.SevenZip: case ArchiveType.SevenZip:
SevenZipArchive sza = SevenZipArchive.Open(input, new ReaderOptions { LeaveStreamOpen = false, }); SevenZipExtractor sza = new SevenZipExtractor(input);
foreach (SevenZipArchiveEntry entry in sza.Entries) foreach (ArchiveFileInfo entry in sza.ArchiveFileData)
{ {
Globals.Logger.Verbose("Current entry name: '" + entry.Key + "'"); Globals.Logger.Verbose("Current entry name: '" + entry.FileName + "'");
if (entry != null && !entry.IsDirectory && entry.Key.Contains(entryName)) if (entry != null && !entry.IsDirectory && entry.FileName.Contains(entryName))
{ {
realEntry = entry.Key; realEntry = entry.FileName;
// Get the output path // Get the output path
realEntry = Path.Combine(Path.GetFullPath(tempDir), realEntry); realEntry = Path.Combine(Path.GetFullPath(tempDir), realEntry);
@@ -258,7 +270,7 @@ namespace SabreTools.Helper.Tools
} }
// Write the file out // Write the file out
entry.WriteToFile(realEntry); sza.ExtractFile(entry.Index, File.OpenWrite(realEntry));
break; break;
} }
} }
@@ -403,6 +415,8 @@ namespace SabreTools.Helper.Tools
/// <returns>List of RomData objects representing the found data</returns> /// <returns>List of RomData objects representing the found data</returns>
public static List<Rom> GetArchiveFileInfo(string input) public static List<Rom> GetArchiveFileInfo(string input)
{ {
// Set internal variables
SevenZipBase.SetLibraryPath("7za.dll");
List<Rom> roms = new List<Rom>(); List<Rom> roms = new List<Rom>();
string gamename = Path.GetFileNameWithoutExtension(input); string gamename = Path.GetFileNameWithoutExtension(input);
@@ -444,20 +458,20 @@ namespace SabreTools.Helper.Tools
switch (at) switch (at)
{ {
case ArchiveType.SevenZip: case ArchiveType.SevenZip:
SevenZipArchive sza = SevenZipArchive.Open(input, new ReaderOptions { LeaveStreamOpen = false }); SevenZipExtractor sza = new SevenZipExtractor(input);
foreach (SevenZipArchiveEntry entry in sza.Entries) foreach (ArchiveFileInfo entry in sza.ArchiveFileData)
{ {
if (entry != null && !entry.IsDirectory) if (entry != null && !entry.IsDirectory)
{ {
Globals.Logger.Verbose("Entry found: '" + entry.Key + "': " Globals.Logger.Verbose("Entry found: '" + entry.FileName + "': "
+ (size == 0 ? entry.Size : size) + ", " + (size == 0 ? (long)entry.Size : size) + ", "
+ (crc == "" ? entry.Crc.ToString("X").ToLowerInvariant() : crc)); + (crc == "" ? entry.Crc.ToString("X").ToLowerInvariant() : crc));
roms.Add(new Rom roms.Add(new Rom
{ {
Type = ItemType.Rom, Type = ItemType.Rom,
Name = entry.Key, Name = entry.FileName,
Size = (size == 0 ? entry.Size : size), Size = (size == 0 ? (long)entry.Size : size),
CRC = (crc == "" ? entry.Crc.ToString("X").ToLowerInvariant() : crc), CRC = (crc == "" ? entry.Crc.ToString("X").ToLowerInvariant() : crc),
Machine = new Machine Machine = new Machine
@@ -1075,6 +1089,8 @@ namespace SabreTools.Helper.Tools
/// <returns>0 if the file isn't 7z, 1 if the file is t7z, 2 if the file is 7z</returns> /// <returns>0 if the file isn't 7z, 1 if the file is t7z, 2 if the file is 7z</returns>
public static int IsT7z(string filename) public static int IsT7z(string filename)
{ {
// Set internal variables
SevenZipBase.SetLibraryPath("7za.dll");
int ist7z = 0; int ist7z = 0;
if (File.Exists(filename)) if (File.Exists(filename))