[ArchiveTools] Fix 7z archive handling

This commit is contained in:
Matt Nadareski
2016-08-18 15:48:45 -07:00
parent 1837c16969
commit 88edc5afa3

View File

@@ -1,6 +1,8 @@
using SharpCompress.Archive; using SharpCompress.Archive;
using SharpCompress.Archive.Zip; using SharpCompress.Archive.Zip;
using SharpCompress.Archive.SevenZip;
using SharpCompress.Common; using SharpCompress.Common;
using SharpCompress.Common.SevenZip;
using SharpCompress.Reader; using SharpCompress.Reader;
using SharpCompress.Reader.Zip; using SharpCompress.Reader.Zip;
using SharpCompress.Writer; using SharpCompress.Writer;
@@ -170,41 +172,56 @@ namespace SabreTools.Helper
} }
IReader reader = null; IReader reader = null;
SevenZipArchive sza = null;
try try
{ {
reader = ReaderFactory.Open(File.OpenRead(input)); if (at == ArchiveType.SevenZip && sevenzip != ArchiveScanLevel.External)
logger.Log("Found archive of type: " + at);
if ((at == ArchiveType.Zip && zip != ArchiveScanLevel.External) ||
(at == ArchiveType.SevenZip && sevenzip != ArchiveScanLevel.External) ||
(at == ArchiveType.Rar && rar != ArchiveScanLevel.External))
{ {
sza = SevenZipArchive.Open(File.OpenRead(input));
logger.Log("Found archive of type: " + at);
// Create the temp directory // Create the temp directory
Directory.CreateDirectory(tempdir); Directory.CreateDirectory(tempdir);
// Extract all files to the temp directory // Extract all files to the temp directory
reader.WriteAllToDirectory(tempdir, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); sza.WriteToDirectory(tempdir, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
encounteredErrors = false; encounteredErrors = false;
} }
else if (at == ArchiveType.GZip && gz != ArchiveScanLevel.External) else
{ {
// Close the original archive handle reader = ReaderFactory.Open(File.OpenRead(input));
reader.Dispose(); logger.Log("Found archive of type: " + at);
// Create the temp directory if ((at == ArchiveType.Zip && zip != ArchiveScanLevel.External) ||
Directory.CreateDirectory(tempdir); (at == ArchiveType.Rar && rar != ArchiveScanLevel.External))
using (FileStream itemstream = File.OpenRead(input))
{ {
using (FileStream outstream = File.Create(tempdir + Path.GetFileNameWithoutExtension(input))) // Create the temp directory
Directory.CreateDirectory(tempdir);
// Extract all files to the temp directory
reader.WriteAllToDirectory(tempdir, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
encounteredErrors = false;
}
else if (at == ArchiveType.GZip && gz != ArchiveScanLevel.External)
{
// Close the original archive handle
reader.Dispose();
// Create the temp directory
Directory.CreateDirectory(tempdir);
using (FileStream itemstream = File.OpenRead(input))
{ {
using (GZipStream gzstream = new GZipStream(itemstream, CompressionMode.Decompress)) using (FileStream outstream = File.Create(tempdir + Path.GetFileNameWithoutExtension(input)))
{ {
gzstream.CopyTo(outstream); using (GZipStream gzstream = new GZipStream(itemstream, CompressionMode.Decompress))
{
gzstream.CopyTo(outstream);
}
} }
} }
encounteredErrors = false;
} }
encounteredErrors = false;
} }
} }
catch (EndOfStreamException) catch (EndOfStreamException)
@@ -223,6 +240,7 @@ namespace SabreTools.Helper
finally finally
{ {
reader?.Dispose(); reader?.Dispose();
sza?.Dispose();
} }
return encounteredErrors; return encounteredErrors;