mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
[ArchiveTools] Get TAR working properly
This commit is contained in:
@@ -1102,6 +1102,13 @@ namespace SabreTools.Helper.Tools
|
|||||||
public static bool WriteTAR(List<string> inputFiles, string outDir, List<Rom> roms, Logger logger, bool date = false)
|
public static bool WriteTAR(List<string> inputFiles, string outDir, List<Rom> roms, Logger logger, bool date = false)
|
||||||
{
|
{
|
||||||
bool success = false;
|
bool success = false;
|
||||||
|
string tempFile = Path.GetTempFileName();
|
||||||
|
|
||||||
|
// If either list of roms is null or empty, return
|
||||||
|
if (inputFiles == null || roms == null || inputFiles.Count == 0 || roms.Count == 0)
|
||||||
|
{
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
// If the number of inputs is less than the number of available roms, return
|
// If the number of inputs is less than the number of available roms, return
|
||||||
if (inputFiles.Count < roms.Count)
|
if (inputFiles.Count < roms.Count)
|
||||||
@@ -1122,8 +1129,8 @@ namespace SabreTools.Helper.Tools
|
|||||||
string archiveFileName = Path.Combine(outDir, Style.RemovePathUnsafeCharacters(roms[0].Machine.Name) + (roms[0].Machine.Name.EndsWith(".tar") ? "" : ".tar"));
|
string archiveFileName = Path.Combine(outDir, Style.RemovePathUnsafeCharacters(roms[0].Machine.Name) + (roms[0].Machine.Name.EndsWith(".tar") ? "" : ".tar"));
|
||||||
|
|
||||||
// Set internal variables
|
// Set internal variables
|
||||||
Stream tarstream = new MemoryStream();
|
TarArchive oldTarFile = TarArchive.Create();
|
||||||
TarArchive tarchive = TarArchive.Create();
|
TarArchive tarFile = TarArchive.Create();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -1133,9 +1140,9 @@ namespace SabreTools.Helper.Tools
|
|||||||
Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName));
|
Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName));
|
||||||
}
|
}
|
||||||
|
|
||||||
// We can only overwrite at this point, sorry
|
// If the archive doesn't exist, create it and put the single file
|
||||||
tarstream = File.Open(archiveFileName, FileMode.Create, FileAccess.Write);
|
if (!File.Exists(archiveFileName))
|
||||||
|
{
|
||||||
// Map all inputs to index
|
// Map all inputs to index
|
||||||
Dictionary<string, int> inputIndexMap = new Dictionary<string, int>();
|
Dictionary<string, int> inputIndexMap = new Dictionary<string, int>();
|
||||||
for (int i = 0; i < inputFiles.Count; i++)
|
for (int i = 0; i < inputFiles.Count; i++)
|
||||||
@@ -1147,28 +1154,106 @@ namespace SabreTools.Helper.Tools
|
|||||||
List<string> keys = inputIndexMap.Keys.ToList();
|
List<string> keys = inputIndexMap.Keys.ToList();
|
||||||
keys.Sort(ZipFile.TorrentZipStringCompare);
|
keys.Sort(ZipFile.TorrentZipStringCompare);
|
||||||
|
|
||||||
// Now add each of the files
|
// Now add all of the files in order
|
||||||
foreach (string key in keys)
|
foreach (string key in keys)
|
||||||
{
|
{
|
||||||
|
// Get the index mapped to the key
|
||||||
int index = inputIndexMap[key];
|
int index = inputIndexMap[key];
|
||||||
tarchive.AddEntry(key, inputFiles[index]);
|
|
||||||
|
// Copy the input stream to the output
|
||||||
|
tarFile.AddEntry(roms[index].Name, inputFiles[index]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tarchive.SaveTo(tarstream, new WriterOptions(CompressionType.None));
|
// Otherwise, sort the input files and write out in the correct order
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Open the old archive for reading
|
||||||
|
oldTarFile = TarArchive.Open(archiveFileName);
|
||||||
|
|
||||||
|
// Get a list of all current entries
|
||||||
|
List<string> entries = oldTarFile.Entries.Select(i => i.Key).ToList();
|
||||||
|
|
||||||
|
// Map all inputs to index
|
||||||
|
Dictionary<string, int> inputIndexMap = new Dictionary<string, int>();
|
||||||
|
for (int i = 0; i < inputFiles.Count; i++)
|
||||||
|
{
|
||||||
|
// If the old one contains the new file, then just skip out
|
||||||
|
if (entries.Contains(roms[i].Name.Replace('\\', '/')))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
inputIndexMap.Add(roms[i].Name.Replace('\\', '/'), -(i + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then add all of the old entries to it too
|
||||||
|
for (int i = 0; i < entries.Count; i++)
|
||||||
|
{
|
||||||
|
inputIndexMap.Add(entries[i], i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the number of entries is the same as the old archive, skip out
|
||||||
|
if (inputIndexMap.Keys.Count <= entries.Count)
|
||||||
|
{
|
||||||
|
success = true;
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the order for the entries with the new file
|
||||||
|
List<string> keys = inputIndexMap.Keys.ToList();
|
||||||
|
keys.Sort(ZipFile.TorrentZipStringCompare);
|
||||||
|
|
||||||
|
// Copy over all files to the new archive
|
||||||
|
foreach (string key in keys)
|
||||||
|
{
|
||||||
|
// Get the index mapped to the key
|
||||||
|
int index = inputIndexMap[key];
|
||||||
|
|
||||||
|
// If we have the input file, add it now
|
||||||
|
if (index < 0)
|
||||||
|
{
|
||||||
|
// Copy the input file to the output
|
||||||
|
tarFile.AddEntry(roms[-index - 1].Name, inputFiles[-index - 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, copy the file from the old archive
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Get the stream from the original archive
|
||||||
|
string tempEntry = Path.GetTempFileName();
|
||||||
|
oldTarFile.Entries.Where(e => e.Key == key).ToList()[0].WriteToFile(tempEntry);
|
||||||
|
|
||||||
|
// Copy the input stream to the output
|
||||||
|
tarFile.AddEntry(key, tempEntry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close the output tar file
|
||||||
|
tarFile.SaveTo(tempFile, new WriterOptions(CompressionType.None));
|
||||||
|
|
||||||
success = true;
|
success = true;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine(ex);
|
||||||
success = false;
|
success = false;
|
||||||
logger.Error(ex.ToString());
|
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
tarstream?.Dispose();
|
tarFile.Dispose();
|
||||||
tarchive?.Dispose();
|
oldTarFile.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
return success;
|
// If the old file exists, delete it and replace
|
||||||
|
if (File.Exists(archiveFileName))
|
||||||
|
{
|
||||||
|
File.Delete(archiveFileName);
|
||||||
|
}
|
||||||
|
File.Move(tempFile, archiveFileName);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user