mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
[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:
@@ -512,16 +512,23 @@ namespace SabreTools.Helper.Dats
|
||||
return rebuilt;
|
||||
}
|
||||
|
||||
// If we have an archive input, get the real name of the file to use
|
||||
// TODO: Remove the need to extract the file first; reimplement ArchiveToArchive?
|
||||
if (isZip == true)
|
||||
// Get a generic stream for the file
|
||||
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
|
||||
file = ArchiveTools.ExtractItem(file, rom.Name, tempDir);
|
||||
string realName = null;
|
||||
(fileStream, realName) = ArchiveTools.ExtractStream(file, rom.Name);
|
||||
}
|
||||
// Otherwise, just open the filestream
|
||||
else
|
||||
{
|
||||
fileStream = FileTools.TryOpenRead(file);
|
||||
}
|
||||
|
||||
// If we couldn't extract the file, then continue,
|
||||
if (String.IsNullOrEmpty(file))
|
||||
// If the stream is null, then continue
|
||||
if (fileStream == null)
|
||||
{
|
||||
return rebuilt;
|
||||
}
|
||||
@@ -540,10 +547,22 @@ namespace SabreTools.Helper.Dats
|
||||
// Make sure the output folder is created
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(outfile));
|
||||
|
||||
// Now copy the file over
|
||||
// Now write the file over
|
||||
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))
|
||||
{
|
||||
File.SetCreationTime(outfile, DateTime.Parse(item.Date));
|
||||
@@ -558,26 +577,29 @@ namespace SabreTools.Helper.Dats
|
||||
|
||||
break;
|
||||
case OutputFormat.TapeArchive:
|
||||
rebuilt &= ArchiveTools.WriteTAR(file, outDir, item, date: date);
|
||||
rebuilt &= ArchiveTools.WriteTAR(fileStream, outDir, item, date: date);
|
||||
break;
|
||||
case OutputFormat.Torrent7Zip:
|
||||
rebuilt &= ArchiveTools.WriteTorrent7Zip(file, outDir, item, date: date);
|
||||
rebuilt &= ArchiveTools.WriteTorrent7Zip(fileStream, outDir, item, date: date);
|
||||
break;
|
||||
case OutputFormat.TorrentGzip:
|
||||
rebuilt &= ArchiveTools.WriteTorrentGZ(file, outDir, romba);
|
||||
rebuilt &= ArchiveTools.WriteTorrentGZ(fileStream, outDir, romba);
|
||||
break;
|
||||
case OutputFormat.TorrentLrzip:
|
||||
break;
|
||||
case OutputFormat.TorrentRar:
|
||||
break;
|
||||
case OutputFormat.TorrentXZ:
|
||||
rebuilt &= ArchiveTools.WriteTorrentXZ(file, outDir, item, date: date);
|
||||
rebuilt &= ArchiveTools.WriteTorrentXZ(fileStream, outDir, item, date: date);
|
||||
break;
|
||||
case OutputFormat.TorrentZip:
|
||||
rebuilt &= ArchiveTools.WriteTorrentZip(file, outDir, item, date: date);
|
||||
rebuilt &= ArchiveTools.WriteTorrentZip(fileStream, outDir, item, date: date);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Close the input stream
|
||||
fileStream?.Dispose();
|
||||
}
|
||||
|
||||
// If we have no duplicates and we're filtering, rebuild it
|
||||
@@ -615,22 +637,29 @@ namespace SabreTools.Helper.Dats
|
||||
return rebuilt;
|
||||
}
|
||||
|
||||
// If we have an archive input, get the real name of the file to use
|
||||
if (isZip == true)
|
||||
// Get a generic stream for the file
|
||||
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
|
||||
machinename = Style.GetFileNameWithoutExtension(file);
|
||||
file = ArchiveTools.ExtractItem(file, rom.Name, tempDir);
|
||||
string realName = null;
|
||||
(fileStream, realName) = ArchiveTools.ExtractStream(file, rom.Name);
|
||||
}
|
||||
// Otherwise, just open the filestream
|
||||
else
|
||||
{
|
||||
fileStream = FileTools.TryOpenRead(file);
|
||||
}
|
||||
|
||||
// If we couldn't extract the file, then continue,
|
||||
if (String.IsNullOrEmpty(file))
|
||||
// If the stream is null, then continue
|
||||
if (fileStream == null)
|
||||
{
|
||||
return rebuilt;
|
||||
}
|
||||
|
||||
// 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()
|
||||
{
|
||||
Name = Style.GetFileNameWithoutExtension(item.Name),
|
||||
@@ -658,7 +687,19 @@ namespace SabreTools.Helper.Dats
|
||||
// Now copy the file over
|
||||
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))
|
||||
{
|
||||
File.SetCreationTime(outfile, DateTime.Parse(item.Date));
|
||||
@@ -673,25 +714,28 @@ namespace SabreTools.Helper.Dats
|
||||
|
||||
break;
|
||||
case OutputFormat.TapeArchive:
|
||||
rebuilt &= ArchiveTools.WriteTAR(file, outDir, item, date: date);
|
||||
rebuilt &= ArchiveTools.WriteTAR(fileStream, outDir, item, date: date);
|
||||
break;
|
||||
case OutputFormat.Torrent7Zip:
|
||||
rebuilt &= ArchiveTools.WriteTorrent7Zip(file, outDir, item, date: date);
|
||||
rebuilt &= ArchiveTools.WriteTorrent7Zip(fileStream, outDir, item, date: date);
|
||||
break;
|
||||
case OutputFormat.TorrentGzip:
|
||||
rebuilt &= ArchiveTools.WriteTorrentGZ(file, outDir, romba);
|
||||
rebuilt &= ArchiveTools.WriteTorrentGZ(fileStream, outDir, romba);
|
||||
break;
|
||||
case OutputFormat.TorrentLrzip:
|
||||
break;
|
||||
case OutputFormat.TorrentRar:
|
||||
break;
|
||||
case OutputFormat.TorrentXZ:
|
||||
rebuilt &= ArchiveTools.WriteTorrentXZ(file, outDir, item, date: date);
|
||||
rebuilt &= ArchiveTools.WriteTorrentXZ(fileStream, outDir, item, date: date);
|
||||
break;
|
||||
case OutputFormat.TorrentZip:
|
||||
rebuilt &= ArchiveTools.WriteTorrentZip(file, outDir, item, date: date);
|
||||
rebuilt &= ArchiveTools.WriteTorrentZip(fileStream, outDir, item, date: date);
|
||||
break;
|
||||
}
|
||||
|
||||
// Close the input stream
|
||||
fileStream?.Dispose();
|
||||
}
|
||||
|
||||
// Now we want to take care of headers, if applicable
|
||||
|
||||
@@ -44,88 +44,6 @@ namespace SabreTools.Helper.Tools
|
||||
{
|
||||
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
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user