From 7a08f8444b572329a398f0a5e2796166a374f4d8 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 13 Sep 2016 17:05:54 -0700 Subject: [PATCH] [SimpleSort, FileTools] Add undocumented TZIP WIP --- SabreTools.Helper/Tools/FileTools.cs | 80 ++++++++++++++++++++++++++++ SimpleSort/SimpleSortApp.cs | 33 +++++++++++- 2 files changed, 111 insertions(+), 2 deletions(-) diff --git a/SabreTools.Helper/Tools/FileTools.cs b/SabreTools.Helper/Tools/FileTools.cs index ea422ec9..7661e1ef 100644 --- a/SabreTools.Helper/Tools/FileTools.cs +++ b/SabreTools.Helper/Tools/FileTools.cs @@ -403,6 +403,86 @@ namespace SabreTools.Helper return WriteToArchive(tempfile, outputDirectory, destEntry); } + /// + /// Reorder all of the files in the archive based on lowercase filename + /// + /// Source archive name + /// Logger object for file and console output + /// True if the operation succeeded, false otherwise + public static bool TorrentZipArchive(string inputArchive, Logger logger) + { + bool success = false; + + // If the input file doesn't exist, return + if (!File.Exists(inputArchive)) + { + return success; + } + + // Make sure the file is a zip file to begin with + if (GetCurrentArchiveType(inputArchive, logger) != ArchiveType.Zip) + { + return success; + } + + ZipArchive outarchive = null; + try + { + // If the archive doesn't exist, create it + if (!File.Exists(inputArchive)) + { + outarchive = ZipFile.Open(inputArchive, ZipArchiveMode.Create); + outarchive.Dispose(); + } + + // Open the archive for sorting + Dictionary entries = new Dictionary(); + using (outarchive = ZipFile.Open(inputArchive, ZipArchiveMode.Update)) + { + // Get and sort the entries + foreach (ZipArchiveEntry entry in outarchive.Entries) + { + entries.Add(entry.Name.ToLowerInvariant(), entry.Name); + } + } + + // Now write out the entries by name + List keys = entries.Keys.ToList(); + keys.Sort(Style.CompareNumeric); + + foreach (string key in keys) + { + Rom temp = new Rom + { + Machine = new Machine + { + Name = Path.GetFileNameWithoutExtension(inputArchive) + ".new", + }, + Name = entries[key], + Date = "12/24/1996 11:32 PM", + }; + CopyFileBetweenArchives(inputArchive, Path.GetDirectoryName(inputArchive), entries[key], temp, logger); + } + + string newfile = Path.Combine(Path.GetDirectoryName(inputArchive), Path.GetFileNameWithoutExtension(inputArchive) + ".new.zip"); + File.Delete(inputArchive); + File.Move(newfile, inputArchive); + + success = true; + } + catch (Exception ex) + { + Console.WriteLine(ex); + success = false; + } + finally + { + outarchive?.Dispose(); + } + + return success; + } + #endregion #region File Information diff --git a/SimpleSort/SimpleSortApp.cs b/SimpleSort/SimpleSortApp.cs index 155d96b6..233f689f 100644 --- a/SimpleSort/SimpleSortApp.cs +++ b/SimpleSort/SimpleSortApp.cs @@ -50,6 +50,7 @@ namespace SabreTools simpleSort = true, tgz = false, toFolder = false, + tzip = false, updateDat = false, verify = false; int sevenzip = 0, @@ -95,6 +96,10 @@ namespace SabreTools case "--tgz": tgz = true; break; + case "-tzip": + case "--tzip": + tzip = true; + break; case "-ud": case "--updated-dat": updateDat = true; @@ -182,7 +187,7 @@ namespace SabreTools } // If a switch that requires a filename is set and no file is, show the help screen - if (inputs.Count == 0 && ((simpleSort && !verify) || convert)) + if (inputs.Count == 0 && ((simpleSort && !verify) || convert || tzip)) { logger.Error("This feature requires at least one input"); Build.Help(); @@ -190,8 +195,14 @@ namespace SabreTools return; } + // TorrentZip a folder + if (tzip) + { + InitTorrentZip(inputs, outdir, tempdir, logger); + } + // If we are converting the folder to TGZ - if (convert) + else if (convert) { InitConvertFolderTGZ(inputs, outdir, tempdir, delete, romba, sevenzip, gz, rar, zip, logger); } @@ -223,6 +234,24 @@ namespace SabreTools return; } + private static void InitTorrentZip(List inputs, string outdir, string tempdir, Logger logger) + { + foreach (string input in inputs) + { + if (File.Exists(input)) + { + FileTools.TorrentZipArchive(input, logger); + } + else if (Directory.Exists(input)) + { + foreach (string file in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories)) + { + FileTools.TorrentZipArchive(file, logger); + } + } + } + } + /// /// Wrap sorting files using an input DAT ///