[ArchiveTools] Add TAR output

This commit is contained in:
Matt Nadareski
2016-10-25 21:20:43 -07:00
parent bb50df0499
commit 305a8db75a
4 changed files with 73 additions and 3 deletions

View File

@@ -210,7 +210,7 @@ namespace SabreTools.Helper.Data
helptext.Add(" -qs, --quick Enable quick scanning of archives"); helptext.Add(" -qs, --quick Enable quick scanning of archives");
helptext.Add(" -ad, --add-date Add original dates from DAT, if possible"); helptext.Add(" -ad, --add-date Add original dates from DAT, if possible");
//helptext.Add(" -t7z Enable Torrent7z output"); //helptext.Add(" -t7z Enable Torrent7z output");
//helptext.Add(" -tar Enable TAR output"); helptext.Add(" -tar Enable TAR output");
helptext.Add(" -tgz Enable TorrentGZ output"); helptext.Add(" -tgz Enable TorrentGZ output");
helptext.Add(" -r, --romba Enable Romba depot dir output"); helptext.Add(" -r, --romba Enable Romba depot dir output");
//helptext.Add(" -tlrz Enable TorrentLRZ output"); //helptext.Add(" -tlrz Enable TorrentLRZ output");

View File

@@ -4752,6 +4752,7 @@ namespace SabreTools.Helper.Dats
} }
break; break;
case OutputFormat.TapeArchive: case OutputFormat.TapeArchive:
ArchiveTools.WriteTAR(pathsToFiles, outDir, romsInGame, logger);
break; break;
case OutputFormat.Torrent7Zip: case OutputFormat.Torrent7Zip:
break; break;

View File

@@ -384,7 +384,7 @@ Options:
files. This format is based on the LZMA container format 7zip, but with custom header files. This format is based on the LZMA container format 7zip, but with custom header
information. This is currently unused by any major application. information. This is currently unused by any major application.
-tar Enable Tape ARchive output [NOT IMPLEMENTED] -tar Enable Tape ARchive output
Instead of outputting the fiels to folder, files will be rebuilt to Tape ARchive (TAR) Instead of outputting the fiels to folder, files will be rebuilt to Tape ARchive (TAR)
files. This format is a standardized storage archive without any compression, usually files. This format is a standardized storage archive without any compression, usually
used with other compression formats around it. It is widely used in backup applications used with other compression formats around it. It is widely used in backup applications

View File

@@ -11,6 +11,7 @@ using Ionic.Zlib;
using ROMVault2.SupportedFiles.Zip; using ROMVault2.SupportedFiles.Zip;
using SharpCompress.Archive; using SharpCompress.Archive;
using SharpCompress.Archive.SevenZip; using SharpCompress.Archive.SevenZip;
using SharpCompress.Archive.Tar;
using SharpCompress.Common; using SharpCompress.Common;
using SharpCompress.Reader; using SharpCompress.Reader;
@@ -540,6 +541,7 @@ namespace SabreTools.Helper.Tools
/// <returns>ArchiveType of inputted file (null on error)</returns> /// <returns>ArchiveType of inputted file (null on error)</returns>
public static ArchiveType? GetCurrentArchiveType(string input, Logger logger) public static ArchiveType? GetCurrentArchiveType(string input, Logger logger)
{ {
ArchiveType? outtype = null; ArchiveType? outtype = null;
// First line of defense is going to be the extension, for better or worse // First line of defense is going to be the extension, for better or worse
@@ -768,7 +770,74 @@ namespace SabreTools.Helper.Tools
/// <returns>True if the archive was written properly, false otherwise</returns> /// <returns>True if the archive was written properly, false otherwise</returns>
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)
{ {
return false; bool success = false;
// If the number of inputs is less than the number of available roms, return
if (inputFiles.Count < roms.Count)
{
return success;
}
// If one of the files doesn't exist, return
foreach (string file in inputFiles)
{
if (!File.Exists(file))
{
return success;
}
}
// Get the output archive name from the first rebuild rom
string archiveFileName = Path.Combine(outDir, roms[0].Machine.Name + (roms[0].Machine.Name.EndsWith(".tar") ? "" : ".tar"));
// Set internal variables
Stream tarstream = new MemoryStream();
TarArchive tarchive = TarArchive.Create();
try
{
// If the full output path doesn't exist, create it
if (!Directory.Exists(Path.GetDirectoryName(archiveFileName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName));
}
// We can only overwrite at this point, sorry
tarstream = File.Open(archiveFileName, FileMode.Create, FileAccess.Write);
// Map all inputs to index
Dictionary<string, int> inputIndexMap = new Dictionary<string, int>();
for (int i = 0; i < inputFiles.Count; i++)
{
inputIndexMap.Add(roms[i].Name.Replace('\\', '/'), i);
}
// Sort the keys in TZIP order
List<string> keys = inputIndexMap.Keys.ToList();
keys.Sort(ZipFile.TorrentZipStringCompare);
// Now add each of the files
foreach (string key in keys)
{
int index = inputIndexMap[key];
tarchive.AddEntry(key, inputFiles[index]);
}
tarchive.SaveTo(tarstream, new CompressionInfo { Type = CompressionType.None });
success = true;
}
catch (Exception ex)
{
success = false;
logger.Error(ex.ToString());
}
finally
{
tarstream?.Dispose();
tarchive?.Dispose();
}
return success;
} }
/// <summary> /// <summary>