From d1331c6bb357b71cc6ea14860efcebcf4a36608a Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Fri, 31 Mar 2017 14:43:40 -0700 Subject: [PATCH] [ArchiveTools] Add stream-to-file writing --- SabreTools.Helper/Tools/ArchiveTools.cs | 84 ++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/SabreTools.Helper/Tools/ArchiveTools.cs b/SabreTools.Helper/Tools/ArchiveTools.cs index 751b77de..185b1981 100644 --- a/SabreTools.Helper/Tools/ArchiveTools.cs +++ b/SabreTools.Helper/Tools/ArchiveTools.cs @@ -1436,6 +1436,88 @@ namespace SabreTools.Helper.Tools #region Writing + /// + /// Write an input stream to file + /// + /// Input filename to be moved + /// Output directory to build to + /// RomData representing the new information + /// True if the date from the DAT should be used if available, false otherwise (default) + /// True if we should overwrite the file if it exists, false otherwise + /// True if the file was written properly, false otherwise + public static bool WriteFile(Stream inputStream, string outDir, Rom rom, bool date = false, bool overwrite = false) + { + bool success = false; + + // If either input is null or empty, return + if (inputStream == null || rom == null || rom.Name == null) + { + return success; + } + + // If the stream is not readable, return + if (!inputStream.CanRead) + { + return success; + } + + // Set internal variables + FileStream outputStream = null; + + // Get the output folder name from the first rebuild rom + string fileName = Path.Combine(outDir, Style.RemovePathUnsafeCharacters(rom.Machine.Name), Style.RemovePathUnsafeCharacters(rom.Name)); + + try + { + // If the full output path doesn't exist, create it + if (!Directory.Exists(Path.GetDirectoryName(fileName))) + { + Directory.CreateDirectory(Path.GetDirectoryName(fileName)); + } + + // If the file exists and we're supposed to overwrite or the file doesn't exist at all + if ((File.Exists(fileName) && overwrite) || !File.Exists(fileName)) + { + outputStream = FileTools.TryCreate(fileName); + } + + // If the output stream isn't null + if (outputStream != null) + { + // Copy the input stream to the output + inputStream.Seek(0, SeekOrigin.Begin); + int bufferSize = 4096 * 128; + byte[] ibuffer = new byte[bufferSize]; + int ilen; + while ((ilen = inputStream.Read(ibuffer, 0, bufferSize)) > 0) + { + outputStream.Write(ibuffer, 0, ilen); + outputStream.Flush(); + } + outputStream.Dispose(); + + if (date && !String.IsNullOrEmpty(rom.Date)) + { + File.SetCreationTime(fileName, DateTime.Parse(rom.Date)); + } + + success = true; + } + } + catch (Exception ex) + { + Console.WriteLine(ex); + success = false; + } + finally + { + inputStream.Dispose(); + outputStream?.Dispose(); + } + + return success; + } + /// /// Write an input file to a tape archive /// @@ -1582,7 +1664,7 @@ namespace SabreTools.Helper.Tools } File.Move(tempFile, archiveFileName); - return true; + return success; } ///