From b9817c9af594702c312634fcd9bac6de1d1ab13c Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 16 Jul 2024 15:14:59 -0400 Subject: [PATCH] Always write to temp file if compressing after --- SabreTools.DatTools/Rebuilder.cs | 4 ++-- SabreTools.FileTypes/Archives/TapeArchive.cs | 8 ++++++-- SabreTools.FileTypes/Archives/ZipArchive.cs | 4 +++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/SabreTools.DatTools/Rebuilder.cs b/SabreTools.DatTools/Rebuilder.cs index a0b7a492..b9c9c67c 100644 --- a/SabreTools.DatTools/Rebuilder.cs +++ b/SabreTools.DatTools/Rebuilder.cs @@ -425,9 +425,9 @@ namespace SabreTools.DatTools if (RebuildTorrentXz(datFile, datItem, file, outDir, outputFormat, isZip)) return true; - // If there is more than one dupe, write to a temporary file and use that stream instead + // Create a temp file if we're compressing the data after string? tempFile = null; - if (dupes.Count > 1) + if (outputFormat != OutputFormat.Folder) { tempFile = Path.Combine(outDir, $"tmp{System.Guid.NewGuid()}"); Stream tempStream = System.IO.File.Open(tempFile, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); diff --git a/SabreTools.FileTypes/Archives/TapeArchive.cs b/SabreTools.FileTypes/Archives/TapeArchive.cs index 015cd0b1..6838e570 100644 --- a/SabreTools.FileTypes/Archives/TapeArchive.cs +++ b/SabreTools.FileTypes/Archives/TapeArchive.cs @@ -328,7 +328,9 @@ namespace SabreTools.FileTypes.Archives usableDate = dt; // Copy the input stream to the output - inputStream.Seek(0, SeekOrigin.Begin); + if (inputStream.CanSeek) + inputStream.Seek(0, SeekOrigin.Begin); + tarFile.AddEntry(baseFile.Filename, inputStream, size: baseFile.Size ?? 0, modified: usableDate); } @@ -384,7 +386,9 @@ namespace SabreTools.FileTypes.Archives if (index < 0) { // Copy the input file to the output - inputStream.Seek(0, SeekOrigin.Begin); + if (inputStream.CanSeek) + inputStream.Seek(0, SeekOrigin.Begin); + tarFile.AddEntry(baseFile.Filename, inputStream, size: baseFile.Size ?? 0, modified: usableDate); } diff --git a/SabreTools.FileTypes/Archives/ZipArchive.cs b/SabreTools.FileTypes/Archives/ZipArchive.cs index 65a89d68..149db892 100644 --- a/SabreTools.FileTypes/Archives/ZipArchive.cs +++ b/SabreTools.FileTypes/Archives/ZipArchive.cs @@ -428,7 +428,9 @@ namespace SabreTools.FileTypes.Archives // If the archive doesn't exist, create it and put the single file if (!File.Exists(archiveFileName)) { - inputStream.Seek(0, SeekOrigin.Begin); + if (inputStream.CanSeek) + inputStream.Seek(0, SeekOrigin.Begin); + zipReturn = zipFile.ZipFileCreate(tempFile); // Open the input file for reading