[DatFile, ArchiveTools] Use stream-based archive methods for rebuild

Because of how this worked out, the Archive-to-Archive methods are no longer needed
This commit is contained in:
Matt Nadareski
2017-03-16 22:17:07 -07:00
parent 57851f4bf5
commit 4817271327
2 changed files with 74 additions and 112 deletions

View File

@@ -512,16 +512,23 @@ namespace SabreTools.Helper.Dats
return rebuilt; return rebuilt;
} }
// If we have an archive input, get the real name of the file to use // Get a generic stream for the file
// TODO: Remove the need to extract the file first; reimplement ArchiveToArchive? Stream fileStream = new MemoryStream();
if (isZip == true)
{
// Otherwise, extract the file to the temp folder
file = ArchiveTools.ExtractItem(file, rom.Name, tempDir);
}
// If we couldn't extract the file, then continue, // If we have a zipfile, extract the stream to memory
if (String.IsNullOrEmpty(file)) if (isZip != null)
{
string realName = null;
(fileStream, realName) = ArchiveTools.ExtractStream(file, rom.Name);
}
// Otherwise, just open the filestream
else
{
fileStream = FileTools.TryOpenRead(file);
}
// If the stream is null, then continue
if (fileStream == null)
{ {
return rebuilt; return rebuilt;
} }
@@ -540,10 +547,22 @@ namespace SabreTools.Helper.Dats
// Make sure the output folder is created // Make sure the output folder is created
Directory.CreateDirectory(Path.GetDirectoryName(outfile)); Directory.CreateDirectory(Path.GetDirectoryName(outfile));
// Now copy the file over // Now write the file over
try try
{ {
File.Copy(file, outfile); FileStream writeStream = FileTools.TryCreate(outfile);
// Copy the input stream to the output
int bufferSize = 4096 * 128;
byte[] ibuffer = new byte[bufferSize];
int ilen;
while ((ilen = fileStream.Read(ibuffer, 0, bufferSize)) > 0)
{
writeStream.Write(ibuffer, 0, ilen);
writeStream.Flush();
}
writeStream.Dispose();
if (date && !String.IsNullOrEmpty(item.Date)) if (date && !String.IsNullOrEmpty(item.Date))
{ {
File.SetCreationTime(outfile, DateTime.Parse(item.Date)); File.SetCreationTime(outfile, DateTime.Parse(item.Date));
@@ -558,26 +577,29 @@ namespace SabreTools.Helper.Dats
break; break;
case OutputFormat.TapeArchive: case OutputFormat.TapeArchive:
rebuilt &= ArchiveTools.WriteTAR(file, outDir, item, date: date); rebuilt &= ArchiveTools.WriteTAR(fileStream, outDir, item, date: date);
break; break;
case OutputFormat.Torrent7Zip: case OutputFormat.Torrent7Zip:
rebuilt &= ArchiveTools.WriteTorrent7Zip(file, outDir, item, date: date); rebuilt &= ArchiveTools.WriteTorrent7Zip(fileStream, outDir, item, date: date);
break; break;
case OutputFormat.TorrentGzip: case OutputFormat.TorrentGzip:
rebuilt &= ArchiveTools.WriteTorrentGZ(file, outDir, romba); rebuilt &= ArchiveTools.WriteTorrentGZ(fileStream, outDir, romba);
break; break;
case OutputFormat.TorrentLrzip: case OutputFormat.TorrentLrzip:
break; break;
case OutputFormat.TorrentRar: case OutputFormat.TorrentRar:
break; break;
case OutputFormat.TorrentXZ: case OutputFormat.TorrentXZ:
rebuilt &= ArchiveTools.WriteTorrentXZ(file, outDir, item, date: date); rebuilt &= ArchiveTools.WriteTorrentXZ(fileStream, outDir, item, date: date);
break; break;
case OutputFormat.TorrentZip: case OutputFormat.TorrentZip:
rebuilt &= ArchiveTools.WriteTorrentZip(file, outDir, item, date: date); rebuilt &= ArchiveTools.WriteTorrentZip(fileStream, outDir, item, date: date);
break; break;
} }
} }
// Close the input stream
fileStream?.Dispose();
} }
// If we have no duplicates and we're filtering, rebuild it // If we have no duplicates and we're filtering, rebuild it
@@ -615,22 +637,29 @@ namespace SabreTools.Helper.Dats
return rebuilt; return rebuilt;
} }
// If we have an archive input, get the real name of the file to use // Get a generic stream for the file
if (isZip == true) Stream fileStream = new MemoryStream();
// If we have a zipfile, extract the stream to memory
if (isZip != null)
{ {
// Otherwise, extract the file to the temp folder string realName = null;
machinename = Style.GetFileNameWithoutExtension(file); (fileStream, realName) = ArchiveTools.ExtractStream(file, rom.Name);
file = ArchiveTools.ExtractItem(file, rom.Name, tempDir); }
// Otherwise, just open the filestream
else
{
fileStream = FileTools.TryOpenRead(file);
} }
// If we couldn't extract the file, then continue, // If the stream is null, then continue
if (String.IsNullOrEmpty(file)) if (fileStream == null)
{ {
return rebuilt; return rebuilt;
} }
// Get the item from the current file // Get the item from the current file
Rom item = FileTools.GetFileInfo(file); Rom item = FileTools.GetStreamInfo(fileStream, fileStream.Length, keepReadOpen: true);
item.Machine = new Machine() item.Machine = new Machine()
{ {
Name = Style.GetFileNameWithoutExtension(item.Name), Name = Style.GetFileNameWithoutExtension(item.Name),
@@ -658,7 +687,19 @@ namespace SabreTools.Helper.Dats
// Now copy the file over // Now copy the file over
try try
{ {
File.Copy(file, outfile); FileStream writeStream = FileTools.TryCreate(outfile);
// Copy the input stream to the output
int bufferSize = 4096 * 128;
byte[] ibuffer = new byte[bufferSize];
int ilen;
while ((ilen = fileStream.Read(ibuffer, 0, bufferSize)) > 0)
{
writeStream.Write(ibuffer, 0, ilen);
writeStream.Flush();
}
writeStream.Dispose();
if (date && !String.IsNullOrEmpty(item.Date)) if (date && !String.IsNullOrEmpty(item.Date))
{ {
File.SetCreationTime(outfile, DateTime.Parse(item.Date)); File.SetCreationTime(outfile, DateTime.Parse(item.Date));
@@ -673,25 +714,28 @@ namespace SabreTools.Helper.Dats
break; break;
case OutputFormat.TapeArchive: case OutputFormat.TapeArchive:
rebuilt &= ArchiveTools.WriteTAR(file, outDir, item, date: date); rebuilt &= ArchiveTools.WriteTAR(fileStream, outDir, item, date: date);
break; break;
case OutputFormat.Torrent7Zip: case OutputFormat.Torrent7Zip:
rebuilt &= ArchiveTools.WriteTorrent7Zip(file, outDir, item, date: date); rebuilt &= ArchiveTools.WriteTorrent7Zip(fileStream, outDir, item, date: date);
break; break;
case OutputFormat.TorrentGzip: case OutputFormat.TorrentGzip:
rebuilt &= ArchiveTools.WriteTorrentGZ(file, outDir, romba); rebuilt &= ArchiveTools.WriteTorrentGZ(fileStream, outDir, romba);
break; break;
case OutputFormat.TorrentLrzip: case OutputFormat.TorrentLrzip:
break; break;
case OutputFormat.TorrentRar: case OutputFormat.TorrentRar:
break; break;
case OutputFormat.TorrentXZ: case OutputFormat.TorrentXZ:
rebuilt &= ArchiveTools.WriteTorrentXZ(file, outDir, item, date: date); rebuilt &= ArchiveTools.WriteTorrentXZ(fileStream, outDir, item, date: date);
break; break;
case OutputFormat.TorrentZip: case OutputFormat.TorrentZip:
rebuilt &= ArchiveTools.WriteTorrentZip(file, outDir, item, date: date); rebuilt &= ArchiveTools.WriteTorrentZip(fileStream, outDir, item, date: date);
break; break;
} }
// Close the input stream
fileStream?.Dispose();
} }
// Now we want to take care of headers, if applicable // Now we want to take care of headers, if applicable

View File

@@ -44,88 +44,6 @@ namespace SabreTools.Helper.Tools
{ {
private const int _bufferSize = 4096 * 128; private const int _bufferSize = 4096 * 128;
#region Archive-to-Archive handling
/// <summary>
/// Transfer a single file from one archive to another
/// </summary>
/// <param name="inputArchive">Input archive name</param>
/// <param name="inputType">Input archive type</param>
/// <param name="inputEntry">Input entry name</param>
/// <param name="outputDir">Output directory</param>
/// <param name="outputType">Output archive type</param>
/// <param name="outputEntry">Output Rom information</param>
/// <param name="date">True if dates are preserved, false otherwise (default)</param>
/// <returns>True if the transfer was a success, false otherwise</returns>
public static bool Transfer(string inputArchive, ArchiveType? inputType, string inputEntry, string outputDir,
ArchiveType? outputType, Rom outputEntry, bool date = false)
{
List<string> inputEntries = new List<string>() { inputEntry };
List<Rom> outputEntries = new List<Rom>() { outputEntry };
return Transfer(inputArchive, inputType, inputEntries, outputDir, outputType, outputEntries, date: date);
}
/// <summary>
/// Transfer multiple files from one archive to another
/// </summary>
/// <param name="inputArchive">Input archive name</param>
/// <param name="inputType">Input archive type</param>
/// <param name="inputEntries">Input entry names</param>
/// <param name="outputDir">Output directory</param>
/// <param name="outputType">Output archive type</param>
/// <param name="outputEntries">Output Rom informations</param>
/// <param name="date">True if dates are preserved, false otherwise (default)</param>
/// <returns>True if the transfesr were a success, false otherwise</returns>
public static bool Transfer(string inputArchive, ArchiveType? inputType, List<string> inputEntries, string outputDir,
ArchiveType? outputType, List<Rom> outputEntries, bool date = false)
{
#region Verify inputs
// If the input archive doesn't exist, return false
if (!File.Exists(inputArchive))
{
return false;
}
// If any input entry is blank, return false
foreach (string inputEntry in inputEntries)
{
if (String.IsNullOrEmpty(inputEntry))
{
return false;
}
}
// If any output entry is null or blank, return false
foreach (Rom outputEntry in outputEntries)
{
if (outputEntry == null || outputEntry.Name == null)
{
return false;
}
}
// If the output directory is null, set the default
if (String.IsNullOrEmpty(outputDir))
{
outputDir = "out";
}
// Verify that the output directory exists
if (!Directory.Exists(outputDir))
{
Directory.CreateDirectory(outputDir);
}
#endregion
// If the input archive is
return false;
}
#endregion
#region Extraction #region Extraction
/// <summary> /// <summary>