[ArchiveTools] Add extracting a single file

This commit is contained in:
Matt Nadareski
2016-06-15 15:09:57 -07:00
parent 4e79b77bd8
commit e07db57e03

View File

@@ -175,6 +175,70 @@ namespace SabreTools.Helper
return !encounteredErrors; return !encounteredErrors;
} }
/// <summary>
/// Attempt to extract a file from an archive
/// </summary>
/// <param name="input">Name of the archive to be extracted</param>
/// <param name="entryname">Name of the entry to be extracted</param>
/// <param name="tempdir">Temporary directory for archive extraction</param>
/// <param name="logger">Logger object for file and console output</param>
/// <returns>True if the extraction was a success, false otherwise</returns>
public static bool ExtractSingleItemFromArchive(string input, string entryname, string tempdir, Logger logger)
{
bool encounteredErrors = true;
// First get the archive type
ArchiveType? at = GetCurrentArchiveType(input, logger);
// If we got back null, then it's not an archive, so we we return
if (at == null)
{
return encounteredErrors;
}
IReader reader = null;
try
{
reader = ReaderFactory.Open(File.OpenRead(input));
logger.Log("Found archive of type: " + at);
if (at == ArchiveType.Zip || at == ArchiveType.SevenZip || at == ArchiveType.Rar)
{
// Create the temp directory
Directory.CreateDirectory(tempdir);
IArchiveEntry entry;
while ((entry = reader.Entry as IArchiveEntry) != null)
{
if (entry.Key == entryname)
{
entry.WriteToDirectory(tempdir);
}
}
encounteredErrors = false;
}
}
catch (EndOfStreamException)
{
// Catch this but don't count it as an error because SharpCompress is unsafe
}
catch (InvalidOperationException)
{
encounteredErrors = true;
}
catch (Exception ex)
{
logger.Error(ex.ToString());
encounteredErrors = true;
}
finally
{
reader?.Dispose();
}
return !encounteredErrors;
}
/// <summary> /// <summary>
/// Generate a list of RomData objects from the header values in an archive /// Generate a list of RomData objects from the header values in an archive
/// </summary> /// </summary>