mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Make archives more consistent
This commit is contained in:
@@ -131,10 +131,10 @@ namespace SabreTools.FileTypes.Archives
|
||||
{
|
||||
// Try to extract a stream using the given information
|
||||
(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 (stream != null && realEntry != null)
|
||||
{
|
||||
realEntry = Path.Combine(outDir, realEntry);
|
||||
|
||||
// Create the output subfolder now
|
||||
@@ -166,7 +166,6 @@ namespace SabreTools.FileTypes.Archives
|
||||
fs?.Dispose();
|
||||
realEntry = null;
|
||||
}
|
||||
}
|
||||
|
||||
return realEntry;
|
||||
}
|
||||
@@ -207,8 +206,10 @@ namespace SabreTools.FileTypes.Archives
|
||||
if (this.Filename == null)
|
||||
return null;
|
||||
|
||||
if (_children == null || _children.Count == 0)
|
||||
{
|
||||
// If we have children cached already
|
||||
if (_children != null && _children.Count > 0)
|
||||
return _children;
|
||||
|
||||
_children = [];
|
||||
|
||||
string gamename = Path.GetFileNameWithoutExtension(this.Filename);
|
||||
@@ -219,9 +220,9 @@ namespace SabreTools.FileTypes.Archives
|
||||
if (possibleTgz != null && possibleTgz.Filename != null)
|
||||
{
|
||||
_children.Add(possibleTgz);
|
||||
return _children;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
// Create a blank item for the entry
|
||||
@@ -259,8 +260,6 @@ namespace SabreTools.FileTypes.Archives
|
||||
logger.Error(ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return _children;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Hashing;
|
||||
using SabreTools.Matching;
|
||||
#if NET462_OR_GREATER || NETCOREAPP
|
||||
@@ -97,10 +96,10 @@ namespace SabreTools.FileTypes.Archives
|
||||
{
|
||||
// Try to extract a stream using the given information
|
||||
(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 (stream != null && realEntry != null)
|
||||
{
|
||||
realEntry = Path.Combine(outDir, realEntry);
|
||||
|
||||
// Create the output subfolder now
|
||||
@@ -130,7 +129,6 @@ namespace SabreTools.FileTypes.Archives
|
||||
fs?.Dispose();
|
||||
realEntry = null;
|
||||
}
|
||||
}
|
||||
|
||||
return realEntry;
|
||||
}
|
||||
|
||||
@@ -105,12 +105,10 @@ namespace SabreTools.FileTypes.Archives
|
||||
Directory.CreateDirectory(outDir);
|
||||
|
||||
// Extract all files to the temp directory
|
||||
SevenZ zf = new();
|
||||
var zf = new SevenZ();
|
||||
ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true);
|
||||
if (zr != ZipReturn.ZipGood)
|
||||
{
|
||||
throw new Exception(CompressUtils.ZipErrorMessageText(zr));
|
||||
}
|
||||
|
||||
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
|
||||
(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 (stream != null && realEntry != null)
|
||||
{
|
||||
realEntry = Path.Combine(outDir, realEntry);
|
||||
|
||||
// Create the output subfolder now
|
||||
@@ -217,7 +215,6 @@ namespace SabreTools.FileTypes.Archives
|
||||
fs?.Dispose();
|
||||
realEntry = null;
|
||||
}
|
||||
}
|
||||
|
||||
return realEntry;
|
||||
}
|
||||
@@ -289,12 +286,10 @@ namespace SabreTools.FileTypes.Archives
|
||||
|
||||
try
|
||||
{
|
||||
SevenZ zf = new();
|
||||
var zf = new SevenZ();
|
||||
ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true);
|
||||
if (zr != ZipReturn.ZipGood)
|
||||
{
|
||||
throw new Exception(CompressUtils.ZipErrorMessageText(zr));
|
||||
}
|
||||
|
||||
for (int i = 0; i < zf.LocalFilesCount(); i++)
|
||||
{
|
||||
@@ -363,12 +358,10 @@ namespace SabreTools.FileTypes.Archives
|
||||
|
||||
try
|
||||
{
|
||||
SevenZ zf = new();
|
||||
var zf = new SevenZ();
|
||||
ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true);
|
||||
if (zr != ZipReturn.ZipGood)
|
||||
{
|
||||
throw new Exception(CompressUtils.ZipErrorMessageText(zr));
|
||||
}
|
||||
|
||||
List<(string, bool)> zipEntries = [];
|
||||
for (int i = 0; i < zf.LocalFilesCount(); i++)
|
||||
@@ -389,9 +382,8 @@ namespace SabreTools.FileTypes.Archives
|
||||
else
|
||||
{
|
||||
if (entry.Item2)
|
||||
{
|
||||
empties.Add(entry.Item1);
|
||||
}
|
||||
|
||||
lastZipEntry = entry.Item1;
|
||||
}
|
||||
}
|
||||
@@ -414,9 +406,7 @@ namespace SabreTools.FileTypes.Archives
|
||||
SevenZ zf = new();
|
||||
ZipReturn zr = zf.ZipFileOpen(this.Filename, -1, true);
|
||||
if (zr != ZipReturn.ZipGood)
|
||||
{
|
||||
throw new Exception(CompressUtils.ZipErrorMessageText(zr));
|
||||
}
|
||||
|
||||
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 (!oldZipFileContents.Contains(baseFile.Filename.Replace('\\', '/')))
|
||||
{
|
||||
inputIndexMap.Add(baseFile.Filename.Replace('\\', '/'), -1);
|
||||
}
|
||||
|
||||
// Then add all of the old entries to it too
|
||||
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 (inputFiles == null || baseFiles == null || inputFiles.Count == 0 || baseFiles.Count == 0)
|
||||
{
|
||||
return success;
|
||||
}
|
||||
return false;
|
||||
|
||||
// If the number of inputs is less than the number of available roms, return
|
||||
if (inputFiles.Count < baseFiles.Count)
|
||||
{
|
||||
return success;
|
||||
}
|
||||
return false;
|
||||
|
||||
// If one of the files doesn't exist, return
|
||||
foreach (string file in inputFiles)
|
||||
{
|
||||
if (!File.Exists(file))
|
||||
{
|
||||
return success;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 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 (oldZipFileContents.Contains(baseFiles[i].Filename!.Replace('\\', '/')))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
inputIndexMap.Add(baseFiles[i].Filename!.Replace('\\', '/'), -(i + 1));
|
||||
}
|
||||
@@ -803,6 +783,7 @@ namespace SabreTools.FileTypes.Archives
|
||||
writeStream!.Write(ibuffer, 0, ilen);
|
||||
writeStream.Flush();
|
||||
}
|
||||
|
||||
freadStream.Dispose();
|
||||
zipFile.ZipFileCloseWriteStream(baseFiles[-index - 1].CRC!);
|
||||
}
|
||||
@@ -842,9 +823,8 @@ namespace SabreTools.FileTypes.Archives
|
||||
|
||||
// If the old file exists, delete it and replace
|
||||
if (File.Exists(archiveFileName))
|
||||
{
|
||||
File.Delete(archiveFileName);
|
||||
}
|
||||
|
||||
File.Move(tempFile, archiveFileName);
|
||||
|
||||
return true;
|
||||
|
||||
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Compress;
|
||||
using SabreTools.Core;
|
||||
using SabreTools.Core.Tools;
|
||||
using SabreTools.Hashing;
|
||||
using SabreTools.Matching;
|
||||
@@ -98,10 +97,10 @@ namespace SabreTools.FileTypes.Archives
|
||||
{
|
||||
// Try to extract a stream using the given information
|
||||
(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 (stream != null && realEntry != null)
|
||||
{
|
||||
realEntry = Path.Combine(outDir, realEntry);
|
||||
|
||||
// Create the output subfolder now
|
||||
@@ -131,7 +130,6 @@ namespace SabreTools.FileTypes.Archives
|
||||
fs?.Dispose();
|
||||
realEntry = null;
|
||||
}
|
||||
}
|
||||
|
||||
return realEntry;
|
||||
}
|
||||
@@ -446,23 +444,17 @@ namespace SabreTools.FileTypes.Archives
|
||||
|
||||
// If either list of roms is null or empty, return
|
||||
if (inputFiles == null || baseFiles == null || inputFiles.Count == 0 || baseFiles.Count == 0)
|
||||
{
|
||||
return success;
|
||||
}
|
||||
return false;
|
||||
|
||||
// If the number of inputs is less than the number of available roms, return
|
||||
if (inputFiles.Count < baseFiles.Count)
|
||||
{
|
||||
return success;
|
||||
}
|
||||
return false;
|
||||
|
||||
// If one of the files doesn't exist, return
|
||||
foreach (string file in inputFiles)
|
||||
{
|
||||
if (!File.Exists(file))
|
||||
{
|
||||
return success;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 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 (!Directory.Exists(Path.GetDirectoryName(archiveFileName)))
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName)!);
|
||||
}
|
||||
|
||||
// If the archive doesn't exist, create it and put the single file
|
||||
if (!File.Exists(archiveFileName))
|
||||
@@ -603,9 +593,8 @@ namespace SabreTools.FileTypes.Archives
|
||||
|
||||
// If the old file exists, delete it and replace
|
||||
if (File.Exists(archiveFileName))
|
||||
{
|
||||
File.Delete(archiveFileName);
|
||||
}
|
||||
|
||||
File.Move(tempFile, archiveFileName);
|
||||
|
||||
return true;
|
||||
|
||||
@@ -115,10 +115,10 @@ namespace SabreTools.FileTypes.Archives
|
||||
{
|
||||
// Try to extract a stream using the given information
|
||||
(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 (stream != null && realEntry != null)
|
||||
{
|
||||
realEntry = Path.Combine(outDir, realEntry);
|
||||
|
||||
// Create the output subfolder now
|
||||
@@ -148,7 +148,6 @@ namespace SabreTools.FileTypes.Archives
|
||||
fs?.Dispose();
|
||||
realEntry = null;
|
||||
}
|
||||
}
|
||||
|
||||
return realEntry;
|
||||
}
|
||||
@@ -188,22 +187,23 @@ namespace SabreTools.FileTypes.Archives
|
||||
/// <inheritdoc/>
|
||||
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 (_children == null || _children.Count == 0)
|
||||
{
|
||||
_children = [];
|
||||
|
||||
string? gamename = Path.GetFileNameWithoutExtension(this.Filename);
|
||||
|
||||
BaseFile? possibleTxz = GetTorrentXZFileInfo();
|
||||
|
||||
// If it was, then add it to the outputs and continue
|
||||
if (possibleTxz != null && possibleTxz.Filename != null)
|
||||
{
|
||||
_children.Add(possibleTxz);
|
||||
return _children;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
// Create a blank item for the entry
|
||||
@@ -237,8 +237,6 @@ namespace SabreTools.FileTypes.Archives
|
||||
logger.Error(ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return _children;
|
||||
#else
|
||||
|
||||
@@ -78,12 +78,10 @@ namespace SabreTools.FileTypes.Archives
|
||||
Directory.CreateDirectory(outDir);
|
||||
|
||||
// Extract all files to the temp directory
|
||||
Zip zf = new();
|
||||
var zf = new Zip();
|
||||
ZipReturn zr = zf.ZipFileOpen(this.Filename!, -1, true);
|
||||
if (zr != ZipReturn.ZipGood)
|
||||
{
|
||||
throw new Exception(CompressUtils.ZipErrorMessageText(zr));
|
||||
}
|
||||
|
||||
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
|
||||
if (!string.IsNullOrEmpty(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 (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
|
||||
(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 (stream != null && realEntry != null)
|
||||
{
|
||||
realEntry = Path.Combine(outDir, realEntry);
|
||||
|
||||
// Create the output subfolder now
|
||||
@@ -192,7 +188,6 @@ namespace SabreTools.FileTypes.Archives
|
||||
fs?.Dispose();
|
||||
realEntry = null;
|
||||
}
|
||||
}
|
||||
|
||||
return realEntry;
|
||||
}
|
||||
@@ -254,17 +249,15 @@ namespace SabreTools.FileTypes.Archives
|
||||
/// <inheritdoc/>
|
||||
public override List<BaseFile>? GetChildren()
|
||||
{
|
||||
List<BaseFile> found = new();
|
||||
var found = new List<BaseFile>();
|
||||
string? gamename = Path.GetFileNameWithoutExtension(this.Filename);
|
||||
|
||||
try
|
||||
{
|
||||
Zip zf = new();
|
||||
var zf = new Zip();
|
||||
ZipReturn zr = zf.ZipFileOpen(this.Filename!, -1, true);
|
||||
if (zr != ZipReturn.ZipGood)
|
||||
{
|
||||
throw new Exception(CompressUtils.ZipErrorMessageText(zr));
|
||||
}
|
||||
|
||||
for (int i = 0; i < zf.LocalFilesCount(); i++)
|
||||
{
|
||||
@@ -330,12 +323,10 @@ namespace SabreTools.FileTypes.Archives
|
||||
|
||||
try
|
||||
{
|
||||
Zip zf = new();
|
||||
var zf = new Zip();
|
||||
ZipReturn zr = zf.ZipFileOpen(this.Filename!, -1, true);
|
||||
if (zr != ZipReturn.ZipGood)
|
||||
{
|
||||
throw new Exception(CompressUtils.ZipErrorMessageText(zr));
|
||||
}
|
||||
|
||||
List<(string, bool)> zipEntries = new();
|
||||
for (int i = 0; i < zf.LocalFilesCount(); i++)
|
||||
@@ -356,9 +347,8 @@ namespace SabreTools.FileTypes.Archives
|
||||
else
|
||||
{
|
||||
if (entry.Item2)
|
||||
{
|
||||
empties.Add(entry.Item1);
|
||||
}
|
||||
|
||||
lastZipEntry = entry.Item1;
|
||||
}
|
||||
}
|
||||
@@ -377,9 +367,7 @@ namespace SabreTools.FileTypes.Archives
|
||||
Zip zf = new();
|
||||
ZipReturn zr = zf.ZipFileOpen(this.Filename!, -1, true);
|
||||
if (zr != ZipReturn.ZipGood)
|
||||
{
|
||||
throw new Exception(CompressUtils.ZipErrorMessageText(zr));
|
||||
}
|
||||
|
||||
return zf.ZipStatus == ZipStatus.TrrntZip;
|
||||
}
|
||||
@@ -593,23 +581,17 @@ namespace SabreTools.FileTypes.Archives
|
||||
|
||||
// If either list of roms is null or empty, return
|
||||
if (inputFiles == null || baseFiles == null || inputFiles.Count == 0 || baseFiles.Count == 0)
|
||||
{
|
||||
return success;
|
||||
}
|
||||
return false;
|
||||
|
||||
// If the number of inputs is less than the number of available roms, return
|
||||
if (inputFiles.Count < baseFiles.Count)
|
||||
{
|
||||
return success;
|
||||
}
|
||||
return false;
|
||||
|
||||
// If one of the files doesn't exist, return
|
||||
foreach (string file in inputFiles)
|
||||
{
|
||||
if (!File.Exists(file))
|
||||
{
|
||||
return success;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get the output archive name from the first rebuild rom
|
||||
@@ -617,17 +599,15 @@ namespace SabreTools.FileTypes.Archives
|
||||
|
||||
// Set internal variables
|
||||
Stream? writeStream = null;
|
||||
Zip oldZipFile = new();
|
||||
Zip zipFile = new();
|
||||
var oldZipFile = new Zip();
|
||||
var zipFile = new Zip();
|
||||
ZipReturn zipReturn = ZipReturn.ZipGood;
|
||||
|
||||
try
|
||||
{
|
||||
// If the full output path doesn't exist, create it
|
||||
if (!Directory.Exists(Path.GetDirectoryName(archiveFileName)))
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(archiveFileName)!);
|
||||
}
|
||||
|
||||
// If the archive doesn't exist, create it and put the single file
|
||||
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 (oldZipFileContents.Contains(baseFiles[i].Filename!.Replace('\\', '/')))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
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 (File.Exists(archiveFileName))
|
||||
{
|
||||
File.Delete(archiveFileName);
|
||||
}
|
||||
|
||||
File.Move(tempFile, archiveFileName);
|
||||
|
||||
return true;
|
||||
|
||||
@@ -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>
|
||||
public virtual bool Write(Stream? inputStream, string outDir, BaseFile? baseFile)
|
||||
{
|
||||
bool success = false;
|
||||
|
||||
// If either input is null or empty, return
|
||||
if (inputStream == null || baseFile == null || baseFile.Filename == null)
|
||||
return success;
|
||||
return false;
|
||||
|
||||
// If the stream is not readable, return
|
||||
if (!inputStream.CanRead)
|
||||
return success;
|
||||
return false;
|
||||
|
||||
// Set internal variables
|
||||
FileStream? outputStream = null;
|
||||
@@ -363,14 +361,13 @@ namespace SabreTools.FileTypes
|
||||
|
||||
// Overwrite output files by default
|
||||
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)
|
||||
inputStream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
// Copy the input stream to the output
|
||||
int bufferSize = 4096 * 128;
|
||||
byte[] ibuffer = new byte[bufferSize];
|
||||
int ilen;
|
||||
@@ -385,20 +382,17 @@ namespace SabreTools.FileTypes
|
||||
if (!string.IsNullOrEmpty(baseFile.Date))
|
||||
File.SetCreationTime(fileName, DateTime.Parse(baseFile.Date));
|
||||
|
||||
success = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.Error(ex);
|
||||
success = false;
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
outputStream?.Dispose();
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user