[FileTools] Manual dispose of file stream (thanks edc!)

This commit is contained in:
Matt Nadareski
2016-09-22 14:45:28 -07:00
parent 5074f820c9
commit 062d43b3c2

View File

@@ -492,64 +492,64 @@ namespace SabreTools.Helper
return encounteredErrors; return encounteredErrors;
} }
FileStream fs = null;
try try
{ {
using (FileStream fs = File.OpenRead(input)) fs = File.OpenRead(input);
if (at == ArchiveType.SevenZip && sevenzip != ArchiveScanLevel.External)
{ {
if (at == ArchiveType.SevenZip && sevenzip != ArchiveScanLevel.External) using (SevenZipArchive sza = SevenZipArchive.Open(fs))
{
using (SevenZipArchive sza = SevenZipArchive.Open(fs))
{
logger.Log("Found archive of type: " + at);
// Create the temp directory
Directory.CreateDirectory(tempDir);
// Extract all files to the temp directory
foreach (IArchiveEntry iae in sza.Entries)
{
iae.WriteToDirectory(tempDir, ExtractOptions.PreserveFileTime | ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
}
encounteredErrors = false;
}
}
else if (at == ArchiveType.GZip && gz != ArchiveScanLevel.External)
{ {
logger.Log("Found archive of type: " + at); logger.Log("Found archive of type: " + at);
// Create the temp directory // Create the temp directory
Directory.CreateDirectory(tempDir); Directory.CreateDirectory(tempDir);
using (FileStream outstream = File.Create(Path.Combine(tempDir, Path.GetFileNameWithoutExtension(input)))) // Extract all files to the temp directory
foreach (IArchiveEntry iae in sza.Entries)
{ {
using (GZipStream gzstream = new GZipStream(fs, CompressionMode.Decompress)) iae.WriteToDirectory(tempDir, ExtractOptions.PreserveFileTime | ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
{
gzstream.CopyTo(outstream);
}
} }
encounteredErrors = false; encounteredErrors = false;
} }
else }
else if (at == ArchiveType.GZip && gz != ArchiveScanLevel.External)
{
logger.Log("Found archive of type: " + at);
// Create the temp directory
Directory.CreateDirectory(tempDir);
using (FileStream outstream = File.Create(Path.Combine(tempDir, Path.GetFileNameWithoutExtension(input))))
{ {
using (IReader reader = ReaderFactory.Open(fs)) using (GZipStream gzstream = new GZipStream(fs, CompressionMode.Decompress))
{ {
logger.Log("Found archive of type: " + at); gzstream.CopyTo(outstream);
}
}
encounteredErrors = false;
}
else
{
using (IReader reader = ReaderFactory.Open(fs))
{
logger.Log("Found archive of type: " + at);
if ((at == ArchiveType.Zip && zip != ArchiveScanLevel.External) || if ((at == ArchiveType.Zip && zip != ArchiveScanLevel.External) ||
(at == ArchiveType.Rar && rar != ArchiveScanLevel.External)) (at == ArchiveType.Rar && rar != ArchiveScanLevel.External))
{
// Create the temp directory
Directory.CreateDirectory(tempDir);
// Extract all files to the temp directory
bool succeeded = reader.MoveToNextEntry();
while (succeeded)
{ {
// Create the temp directory reader.WriteEntryToDirectory(tempDir, ExtractOptions.PreserveFileTime | ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
Directory.CreateDirectory(tempDir); succeeded = reader.MoveToNextEntry();
// Extract all files to the temp directory
bool succeeded = reader.MoveToNextEntry();
while (succeeded)
{
reader.WriteEntryToDirectory(tempDir, ExtractOptions.PreserveFileTime | ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
succeeded = reader.MoveToNextEntry();
}
encounteredErrors = false;
} }
encounteredErrors = false;
} }
} }
} }
@@ -567,6 +567,11 @@ namespace SabreTools.Helper
// Don't log file open errors // Don't log file open errors
encounteredErrors = true; encounteredErrors = true;
} }
finally
{
fs.Close();
fs.Dispose();
}
return encounteredErrors; return encounteredErrors;
} }