Make archives more consistent

This commit is contained in:
Matt Nadareski
2024-07-17 15:19:15 -04:00
parent 11fa1544f1
commit 5c179bbf8b
7 changed files with 289 additions and 354 deletions

View File

@@ -131,10 +131,10 @@ namespace SabreTools.FileTypes.Archives
{ {
// Try to extract a stream using the given information // Try to extract a stream using the given information
(Stream? stream, string? realEntry) = GetEntryStream(entryName); (Stream? stream, string? realEntry) = GetEntryStream(entryName);
if (stream == null || realEntry == null)
return null;
// If the stream and the entry name are both non-null, we write to file // If the stream and the entry name are both non-null, we write to file
if (stream != null && realEntry != null)
{
realEntry = Path.Combine(outDir, realEntry); realEntry = Path.Combine(outDir, realEntry);
// Create the output subfolder now // Create the output subfolder now
@@ -166,7 +166,6 @@ namespace SabreTools.FileTypes.Archives
fs?.Dispose(); fs?.Dispose();
realEntry = null; realEntry = null;
} }
}
return realEntry; return realEntry;
} }
@@ -207,8 +206,10 @@ namespace SabreTools.FileTypes.Archives
if (this.Filename == null) if (this.Filename == null)
return null; return null;
if (_children == null || _children.Count == 0) // If we have children cached already
{ if (_children != null && _children.Count > 0)
return _children;
_children = []; _children = [];
string gamename = Path.GetFileNameWithoutExtension(this.Filename); string gamename = Path.GetFileNameWithoutExtension(this.Filename);
@@ -219,9 +220,9 @@ namespace SabreTools.FileTypes.Archives
if (possibleTgz != null && possibleTgz.Filename != null) if (possibleTgz != null && possibleTgz.Filename != null)
{ {
_children.Add(possibleTgz); _children.Add(possibleTgz);
return _children;
} }
else
{
try try
{ {
// Create a blank item for the entry // Create a blank item for the entry
@@ -259,8 +260,6 @@ namespace SabreTools.FileTypes.Archives
logger.Error(ex); logger.Error(ex);
return null; return null;
} }
}
}
return _children; return _children;
} }

View File

@@ -2,7 +2,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using SabreTools.Core;
using SabreTools.Hashing; using SabreTools.Hashing;
using SabreTools.Matching; using SabreTools.Matching;
#if NET462_OR_GREATER || NETCOREAPP #if NET462_OR_GREATER || NETCOREAPP
@@ -97,10 +96,10 @@ namespace SabreTools.FileTypes.Archives
{ {
// Try to extract a stream using the given information // Try to extract a stream using the given information
(Stream? stream, string? realEntry) = GetEntryStream(entryName); (Stream? stream, string? realEntry) = GetEntryStream(entryName);
if (stream == null || realEntry == null)
return null;
// If the stream and the entry name are both non-null, we write to file // If the stream and the entry name are both non-null, we write to file
if (stream != null && realEntry != null)
{
realEntry = Path.Combine(outDir, realEntry); realEntry = Path.Combine(outDir, realEntry);
// Create the output subfolder now // Create the output subfolder now
@@ -130,7 +129,6 @@ namespace SabreTools.FileTypes.Archives
fs?.Dispose(); fs?.Dispose();
realEntry = null; realEntry = null;
} }
}
return realEntry; return realEntry;
} }

View File

@@ -105,12 +105,10 @@ namespace SabreTools.FileTypes.Archives
Directory.CreateDirectory(outDir); Directory.CreateDirectory(outDir);
// Extract all files to the temp directory // Extract all files to the temp directory
SevenZ zf = new(); var zf = new SevenZ();
ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true); ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true);
if (zr != ZipReturn.ZipGood) if (zr != ZipReturn.ZipGood)
{
throw new Exception(CompressUtils.ZipErrorMessageText(zr)); throw new Exception(CompressUtils.ZipErrorMessageText(zr));
}
for (int i = 0; i < zf.LocalFilesCount() && zr == ZipReturn.ZipGood; i++) for (int i = 0; i < zf.LocalFilesCount() && zr == ZipReturn.ZipGood; i++)
{ {
@@ -184,10 +182,10 @@ namespace SabreTools.FileTypes.Archives
{ {
// Try to extract a stream using the given information // Try to extract a stream using the given information
(Stream? stream, string? realEntry) = GetEntryStream(entryName); (Stream? stream, string? realEntry) = GetEntryStream(entryName);
if (stream == null || realEntry == null)
return null;
// If the stream and the entry name are both non-null, we write to file // If the stream and the entry name are both non-null, we write to file
if (stream != null && realEntry != null)
{
realEntry = Path.Combine(outDir, realEntry); realEntry = Path.Combine(outDir, realEntry);
// Create the output subfolder now // Create the output subfolder now
@@ -217,7 +215,6 @@ namespace SabreTools.FileTypes.Archives
fs?.Dispose(); fs?.Dispose();
realEntry = null; realEntry = null;
} }
}
return realEntry; return realEntry;
} }
@@ -289,12 +286,10 @@ namespace SabreTools.FileTypes.Archives
try try
{ {
SevenZ zf = new(); var zf = new SevenZ();
ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true); ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true);
if (zr != ZipReturn.ZipGood) if (zr != ZipReturn.ZipGood)
{
throw new Exception(CompressUtils.ZipErrorMessageText(zr)); throw new Exception(CompressUtils.ZipErrorMessageText(zr));
}
for (int i = 0; i < zf.LocalFilesCount(); i++) for (int i = 0; i < zf.LocalFilesCount(); i++)
{ {
@@ -363,12 +358,10 @@ namespace SabreTools.FileTypes.Archives
try try
{ {
SevenZ zf = new(); var zf = new SevenZ();
ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true); ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true);
if (zr != ZipReturn.ZipGood) if (zr != ZipReturn.ZipGood)
{
throw new Exception(CompressUtils.ZipErrorMessageText(zr)); throw new Exception(CompressUtils.ZipErrorMessageText(zr));
}
List<(string, bool)> zipEntries = []; List<(string, bool)> zipEntries = [];
for (int i = 0; i < zf.LocalFilesCount(); i++) for (int i = 0; i < zf.LocalFilesCount(); i++)
@@ -389,9 +382,8 @@ namespace SabreTools.FileTypes.Archives
else else
{ {
if (entry.Item2) if (entry.Item2)
{
empties.Add(entry.Item1); empties.Add(entry.Item1);
}
lastZipEntry = entry.Item1; lastZipEntry = entry.Item1;
} }
} }
@@ -414,9 +406,7 @@ namespace SabreTools.FileTypes.Archives
SevenZ zf = new(); SevenZ zf = new();
ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true); ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true);
if (zr != ZipReturn.ZipGood) if (zr != ZipReturn.ZipGood)
{
throw new Exception(CompressUtils.ZipErrorMessageText(zr)); throw new Exception(CompressUtils.ZipErrorMessageText(zr));
}
return zf.ZipStatus == ZipStatus.Trrnt7Zip; return zf.ZipStatus == ZipStatus.Trrnt7Zip;
} }
@@ -519,9 +509,7 @@ namespace SabreTools.FileTypes.Archives
// If the old one doesn't contain the new file, then add it // If the old one doesn't contain the new file, then add it
if (!oldZipFileContents.Contains(baseFile.Filename.Replace('\\', '/'))) if (!oldZipFileContents.Contains(baseFile.Filename.Replace('\\', '/')))
{
inputIndexMap.Add(baseFile.Filename.Replace('\\', '/'), -1); inputIndexMap.Add(baseFile.Filename.Replace('\\', '/'), -1);
}
// Then add all of the old entries to it too // Then add all of the old entries to it too
for (int i = 0; i < oldZipFile.LocalFilesCount(); i++) for (int i = 0; i < oldZipFile.LocalFilesCount(); i++)
@@ -639,23 +627,17 @@ namespace SabreTools.FileTypes.Archives
// If either list of roms is null or empty, return // If either list of roms is null or empty, return
if (inputFiles == null || baseFiles == null || inputFiles.Count == 0 || baseFiles.Count == 0) if (inputFiles == null || baseFiles == null || inputFiles.Count == 0 || baseFiles.Count == 0)
{ return false;
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 < baseFiles.Count) if (inputFiles.Count < baseFiles.Count)
{ return false;
return success;
}
// If one of the files doesn't exist, return // If one of the files doesn't exist, return
foreach (string file in inputFiles) foreach (string file in inputFiles)
{ {
if (!File.Exists(file)) if (!File.Exists(file))
{ return false;
return success;
}
} }
// Get the output archive name from the first rebuild rom // Get the output archive name from the first rebuild rom
@@ -743,9 +725,7 @@ namespace SabreTools.FileTypes.Archives
// If the old one contains the new file, then just skip out // If the old one contains the new file, then just skip out
if (oldZipFileContents.Contains(baseFiles[i].Filename!.Replace('\\', '/'))) if (oldZipFileContents.Contains(baseFiles[i].Filename!.Replace('\\', '/')))
{
continue; continue;
}
inputIndexMap.Add(baseFiles[i].Filename!.Replace('\\', '/'), -(i + 1)); inputIndexMap.Add(baseFiles[i].Filename!.Replace('\\', '/'), -(i + 1));
} }
@@ -803,6 +783,7 @@ namespace SabreTools.FileTypes.Archives
writeStream!.Write(ibuffer, 0, ilen); writeStream!.Write(ibuffer, 0, ilen);
writeStream.Flush(); writeStream.Flush();
} }
freadStream.Dispose(); freadStream.Dispose();
zipFile.ZipFileCloseWriteStream(baseFiles[-index - 1].CRC!); zipFile.ZipFileCloseWriteStream(baseFiles[-index - 1].CRC!);
} }
@@ -842,9 +823,8 @@ namespace SabreTools.FileTypes.Archives
// If the old file exists, delete it and replace // If the old file exists, delete it and replace
if (File.Exists(archiveFileName)) if (File.Exists(archiveFileName))
{
File.Delete(archiveFileName); File.Delete(archiveFileName);
}
File.Move(tempFile, archiveFileName); File.Move(tempFile, archiveFileName);
return true; return true;

View File

@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Compress; using Compress;
using SabreTools.Core;
using SabreTools.Core.Tools; using SabreTools.Core.Tools;
using SabreTools.Hashing; using SabreTools.Hashing;
using SabreTools.Matching; using SabreTools.Matching;
@@ -98,10 +97,10 @@ namespace SabreTools.FileTypes.Archives
{ {
// Try to extract a stream using the given information // Try to extract a stream using the given information
(Stream? stream, string? realEntry) = GetEntryStream(entryName); (Stream? stream, string? realEntry) = GetEntryStream(entryName);
if (stream == null || realEntry == null)
return null;
// If the stream and the entry name are both non-null, we write to file // If the stream and the entry name are both non-null, we write to file
if (stream != null && realEntry != null)
{
realEntry = Path.Combine(outDir, realEntry); realEntry = Path.Combine(outDir, realEntry);
// Create the output subfolder now // Create the output subfolder now
@@ -131,7 +130,6 @@ namespace SabreTools.FileTypes.Archives
fs?.Dispose(); fs?.Dispose();
realEntry = null; realEntry = null;
} }
}
return realEntry; return realEntry;
} }
@@ -446,23 +444,17 @@ namespace SabreTools.FileTypes.Archives
// If either list of roms is null or empty, return // If either list of roms is null or empty, return
if (inputFiles == null || baseFiles == null || inputFiles.Count == 0 || baseFiles.Count == 0) if (inputFiles == null || baseFiles == null || inputFiles.Count == 0 || baseFiles.Count == 0)
{ return false;
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 < baseFiles.Count) if (inputFiles.Count < baseFiles.Count)
{ return false;
return success;
}
// If one of the files doesn't exist, return // If one of the files doesn't exist, return
foreach (string file in inputFiles) foreach (string file in inputFiles)
{ {
if (!File.Exists(file)) if (!File.Exists(file))
{ return false;
return success;
}
} }
// Get the output archive name from the first rebuild rom // Get the output archive name from the first rebuild rom
@@ -476,9 +468,7 @@ namespace SabreTools.FileTypes.Archives
{ {
// If the full output path doesn't exist, create it // If the full output path doesn't exist, create it
if (!Directory.Exists(Path.GetDirectoryName(archiveFileName))) if (!Directory.Exists(Path.GetDirectoryName(archiveFileName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName)!); Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName)!);
}
// If the archive doesn't exist, create it and put the single file // If the archive doesn't exist, create it and put the single file
if (!File.Exists(archiveFileName)) if (!File.Exists(archiveFileName))
@@ -603,9 +593,8 @@ namespace SabreTools.FileTypes.Archives
// If the old file exists, delete it and replace // If the old file exists, delete it and replace
if (File.Exists(archiveFileName)) if (File.Exists(archiveFileName))
{
File.Delete(archiveFileName); File.Delete(archiveFileName);
}
File.Move(tempFile, archiveFileName); File.Move(tempFile, archiveFileName);
return true; return true;

View File

@@ -115,10 +115,10 @@ namespace SabreTools.FileTypes.Archives
{ {
// Try to extract a stream using the given information // Try to extract a stream using the given information
(Stream? stream, string? realEntry) = GetEntryStream(entryName); (Stream? stream, string? realEntry) = GetEntryStream(entryName);
if (stream == null || realEntry == null)
return null;
// If the stream and the entry name are both non-null, we write to file // If the stream and the entry name are both non-null, we write to file
if (stream != null && realEntry != null)
{
realEntry = Path.Combine(outDir, realEntry); realEntry = Path.Combine(outDir, realEntry);
// Create the output subfolder now // Create the output subfolder now
@@ -148,7 +148,6 @@ namespace SabreTools.FileTypes.Archives
fs?.Dispose(); fs?.Dispose();
realEntry = null; realEntry = null;
} }
}
return realEntry; return realEntry;
} }
@@ -188,22 +187,23 @@ namespace SabreTools.FileTypes.Archives
/// <inheritdoc/> /// <inheritdoc/>
public override List<BaseFile>? GetChildren() public override List<BaseFile>? GetChildren()
{ {
// If we have children cached already
if (_children != null && _children.Count > 0)
return _children;
#if NET462_OR_GREATER || NETCOREAPP #if NET462_OR_GREATER || NETCOREAPP
if (_children == null || _children.Count == 0)
{
_children = []; _children = [];
string? gamename = Path.GetFileNameWithoutExtension(this.Filename); string? gamename = Path.GetFileNameWithoutExtension(this.Filename);
BaseFile? possibleTxz = GetTorrentXZFileInfo(); BaseFile? possibleTxz = GetTorrentXZFileInfo();
// If it was, then add it to the outputs and continue // If it was, then add it to the outputs and continue
if (possibleTxz != null && possibleTxz.Filename != null) if (possibleTxz != null && possibleTxz.Filename != null)
{ {
_children.Add(possibleTxz); _children.Add(possibleTxz);
return _children;
} }
else
{
try try
{ {
// Create a blank item for the entry // Create a blank item for the entry
@@ -237,8 +237,6 @@ namespace SabreTools.FileTypes.Archives
logger.Error(ex); logger.Error(ex);
return null; return null;
} }
}
}
return _children; return _children;
#else #else

View File

@@ -78,12 +78,10 @@ namespace SabreTools.FileTypes.Archives
Directory.CreateDirectory(outDir); Directory.CreateDirectory(outDir);
// Extract all files to the temp directory // Extract all files to the temp directory
Zip zf = new(); var zf = new Zip();
ZipReturn zr = zf.ZipFileOpen(this.Filename!, -1, true); ZipReturn zr = zf.ZipFileOpen(this.Filename!, -1, true);
if (zr != ZipReturn.ZipGood) if (zr != ZipReturn.ZipGood)
{
throw new Exception(CompressUtils.ZipErrorMessageText(zr)); throw new Exception(CompressUtils.ZipErrorMessageText(zr));
}
for (int i = 0; i < zf.LocalFilesCount() && zr == ZipReturn.ZipGood; i++) for (int i = 0; i < zf.LocalFilesCount() && zr == ZipReturn.ZipGood; i++)
{ {
@@ -92,9 +90,7 @@ namespace SabreTools.FileTypes.Archives
// Create the rest of the path, if needed // Create the rest of the path, if needed
if (!string.IsNullOrEmpty(Path.GetDirectoryName(zf.GetLocalFile(i).Filename))) if (!string.IsNullOrEmpty(Path.GetDirectoryName(zf.GetLocalFile(i).Filename)))
{
Directory.CreateDirectory(Path.Combine(outDir, Path.GetDirectoryName(zf.GetLocalFile(i).Filename)!)); Directory.CreateDirectory(Path.Combine(outDir, Path.GetDirectoryName(zf.GetLocalFile(i).Filename)!));
}
// If the entry ends with a directory separator, continue to the next item, if any // If the entry ends with a directory separator, continue to the next item, if any
if (zf.GetLocalFile(i).Filename!.EndsWith(Path.DirectorySeparatorChar.ToString()) if (zf.GetLocalFile(i).Filename!.EndsWith(Path.DirectorySeparatorChar.ToString())
@@ -159,10 +155,10 @@ namespace SabreTools.FileTypes.Archives
{ {
// Try to extract a stream using the given information // Try to extract a stream using the given information
(Stream? stream, string? realEntry) = GetEntryStream(entryName); (Stream? stream, string? realEntry) = GetEntryStream(entryName);
if (stream == null || realEntry == null)
return null;
// If the stream and the entry name are both non-null, we write to file // If the stream and the entry name are both non-null, we write to file
if (stream != null && realEntry != null)
{
realEntry = Path.Combine(outDir, realEntry); realEntry = Path.Combine(outDir, realEntry);
// Create the output subfolder now // Create the output subfolder now
@@ -192,7 +188,6 @@ namespace SabreTools.FileTypes.Archives
fs?.Dispose(); fs?.Dispose();
realEntry = null; realEntry = null;
} }
}
return realEntry; return realEntry;
} }
@@ -254,17 +249,15 @@ namespace SabreTools.FileTypes.Archives
/// <inheritdoc/> /// <inheritdoc/>
public override List<BaseFile>? GetChildren() public override List<BaseFile>? GetChildren()
{ {
List<BaseFile> found = new(); var found = new List<BaseFile>();
string? gamename = Path.GetFileNameWithoutExtension(this.Filename); string? gamename = Path.GetFileNameWithoutExtension(this.Filename);
try try
{ {
Zip zf = new(); var zf = new Zip();
ZipReturn zr = zf.ZipFileOpen(this.Filename!, -1, true); ZipReturn zr = zf.ZipFileOpen(this.Filename!, -1, true);
if (zr != ZipReturn.ZipGood) if (zr != ZipReturn.ZipGood)
{
throw new Exception(CompressUtils.ZipErrorMessageText(zr)); throw new Exception(CompressUtils.ZipErrorMessageText(zr));
}
for (int i = 0; i < zf.LocalFilesCount(); i++) for (int i = 0; i < zf.LocalFilesCount(); i++)
{ {
@@ -330,12 +323,10 @@ namespace SabreTools.FileTypes.Archives
try try
{ {
Zip zf = new(); var zf = new Zip();
ZipReturn zr = zf.ZipFileOpen(this.Filename!, -1, true); ZipReturn zr = zf.ZipFileOpen(this.Filename!, -1, true);
if (zr != ZipReturn.ZipGood) if (zr != ZipReturn.ZipGood)
{
throw new Exception(CompressUtils.ZipErrorMessageText(zr)); throw new Exception(CompressUtils.ZipErrorMessageText(zr));
}
List<(string, bool)> zipEntries = new(); List<(string, bool)> zipEntries = new();
for (int i = 0; i < zf.LocalFilesCount(); i++) for (int i = 0; i < zf.LocalFilesCount(); i++)
@@ -356,9 +347,8 @@ namespace SabreTools.FileTypes.Archives
else else
{ {
if (entry.Item2) if (entry.Item2)
{
empties.Add(entry.Item1); empties.Add(entry.Item1);
}
lastZipEntry = entry.Item1; lastZipEntry = entry.Item1;
} }
} }
@@ -377,9 +367,7 @@ namespace SabreTools.FileTypes.Archives
Zip zf = new(); Zip zf = new();
ZipReturn zr = zf.ZipFileOpen(this.Filename!, -1, true); ZipReturn zr = zf.ZipFileOpen(this.Filename!, -1, true);
if (zr != ZipReturn.ZipGood) if (zr != ZipReturn.ZipGood)
{
throw new Exception(CompressUtils.ZipErrorMessageText(zr)); throw new Exception(CompressUtils.ZipErrorMessageText(zr));
}
return zf.ZipStatus == ZipStatus.TrrntZip; return zf.ZipStatus == ZipStatus.TrrntZip;
} }
@@ -593,23 +581,17 @@ namespace SabreTools.FileTypes.Archives
// If either list of roms is null or empty, return // If either list of roms is null or empty, return
if (inputFiles == null || baseFiles == null || inputFiles.Count == 0 || baseFiles.Count == 0) if (inputFiles == null || baseFiles == null || inputFiles.Count == 0 || baseFiles.Count == 0)
{ return false;
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 < baseFiles.Count) if (inputFiles.Count < baseFiles.Count)
{ return false;
return success;
}
// If one of the files doesn't exist, return // If one of the files doesn't exist, return
foreach (string file in inputFiles) foreach (string file in inputFiles)
{ {
if (!File.Exists(file)) if (!File.Exists(file))
{ return false;
return success;
}
} }
// Get the output archive name from the first rebuild rom // Get the output archive name from the first rebuild rom
@@ -617,17 +599,15 @@ namespace SabreTools.FileTypes.Archives
// Set internal variables // Set internal variables
Stream? writeStream = null; Stream? writeStream = null;
Zip oldZipFile = new(); var oldZipFile = new Zip();
Zip zipFile = new(); var zipFile = new Zip();
ZipReturn zipReturn = ZipReturn.ZipGood; ZipReturn zipReturn = ZipReturn.ZipGood;
try try
{ {
// If the full output path doesn't exist, create it // If the full output path doesn't exist, create it
if (!Directory.Exists(Path.GetDirectoryName(archiveFileName))) if (!Directory.Exists(Path.GetDirectoryName(archiveFileName)))
{
Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName)!); Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName)!);
}
// If the archive doesn't exist, create it and put the single file // If the archive doesn't exist, create it and put the single file
if (!File.Exists(archiveFileName)) if (!File.Exists(archiveFileName))
@@ -699,9 +679,7 @@ namespace SabreTools.FileTypes.Archives
// If the old one contains the new file, then just skip out // If the old one contains the new file, then just skip out
if (oldZipFileContents.Contains(baseFiles[i].Filename!.Replace('\\', '/'))) if (oldZipFileContents.Contains(baseFiles[i].Filename!.Replace('\\', '/')))
{
continue; continue;
}
inputIndexMap.Add(baseFiles[i].Filename!.Replace('\\', '/'), -(i + 1)); inputIndexMap.Add(baseFiles[i].Filename!.Replace('\\', '/'), -(i + 1));
} }
@@ -800,9 +778,8 @@ namespace SabreTools.FileTypes.Archives
// If the old file exists, delete it and replace // If the old file exists, delete it and replace
if (File.Exists(archiveFileName)) if (File.Exists(archiveFileName))
{
File.Delete(archiveFileName); File.Delete(archiveFileName);
}
File.Move(tempFile, archiveFileName); File.Move(tempFile, archiveFileName);
return true; return true;

View File

@@ -324,15 +324,13 @@ namespace SabreTools.FileTypes
/// <remarks>This works for now, but it can be sped up by using Ionic.Zip or another zlib wrapper that allows for header values built-in. See edc's code.</remarks> /// <remarks>This works for now, but it can be sped up by using Ionic.Zip or another zlib wrapper that allows for header values built-in. See edc's code.</remarks>
public virtual bool Write(Stream? inputStream, string outDir, BaseFile? baseFile) public virtual bool Write(Stream? inputStream, string outDir, BaseFile? baseFile)
{ {
bool success = false;
// If either input is null or empty, return // If either input is null or empty, return
if (inputStream == null || baseFile == null || baseFile.Filename == null) if (inputStream == null || baseFile == null || baseFile.Filename == null)
return success; return false;
// If the stream is not readable, return // If the stream is not readable, return
if (!inputStream.CanRead) if (!inputStream.CanRead)
return success; return false;
// Set internal variables // Set internal variables
FileStream? outputStream = null; FileStream? outputStream = null;
@@ -363,14 +361,13 @@ namespace SabreTools.FileTypes
// Overwrite output files by default // Overwrite output files by default
outputStream = File.Create(fileName); outputStream = File.Create(fileName);
if (outputStream == null)
return false;
// If the output stream isn't null
if (outputStream != null)
{
// Copy the input stream to the output
if (inputStream.CanSeek) if (inputStream.CanSeek)
inputStream.Seek(0, SeekOrigin.Begin); inputStream.Seek(0, SeekOrigin.Begin);
// Copy the input stream to the output
int bufferSize = 4096 * 128; int bufferSize = 4096 * 128;
byte[] ibuffer = new byte[bufferSize]; byte[] ibuffer = new byte[bufferSize];
int ilen; int ilen;
@@ -385,20 +382,17 @@ namespace SabreTools.FileTypes
if (!string.IsNullOrEmpty(baseFile.Date)) if (!string.IsNullOrEmpty(baseFile.Date))
File.SetCreationTime(fileName, DateTime.Parse(baseFile.Date)); File.SetCreationTime(fileName, DateTime.Parse(baseFile.Date));
success = true; return true;
}
} }
catch (Exception ex) catch (Exception ex)
{ {
logger.Error(ex); logger.Error(ex);
success = false; return false;
} }
finally finally
{ {
outputStream?.Dispose(); outputStream?.Dispose();
} }
return success;
} }
/// <summary> /// <summary>