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,41 +131,40 @@ 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);
// Create the output subfolder now
string? dir = Path.GetDirectoryName(realEntry);
if (dir != null)
Directory.CreateDirectory(dir);
// Now open and write the file if possible
FileStream fs = File.Create(realEntry);
if (fs != null)
{ {
realEntry = Path.Combine(outDir, realEntry); if (stream.CanSeek)
stream.Seek(0, SeekOrigin.Begin);
// Create the output subfolder now byte[] zbuffer = new byte[_bufferSize];
string? dir = Path.GetDirectoryName(realEntry); int zlen;
if (dir != null) while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
Directory.CreateDirectory(dir);
// Now open and write the file if possible
FileStream fs = File.Create(realEntry);
if (fs != null)
{ {
if (stream.CanSeek) fs.Write(zbuffer, 0, zlen);
stream.Seek(0, SeekOrigin.Begin); fs.Flush();
byte[] zbuffer = new byte[_bufferSize];
int zlen;
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
{
fs.Write(zbuffer, 0, zlen);
fs.Flush();
}
stream?.Dispose();
fs?.Dispose();
}
else
{
stream?.Dispose();
fs?.Dispose();
realEntry = null;
} }
stream?.Dispose();
fs?.Dispose();
}
else
{
stream?.Dispose();
fs?.Dispose();
realEntry = null;
} }
return realEntry; return realEntry;
@@ -207,59 +206,59 @@ 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 = [];
string gamename = Path.GetFileNameWithoutExtension(this.Filename);
BaseFile? possibleTgz = GetTorrentGZFileInfo();
// If it was, then add it to the outputs and continue
if (possibleTgz != null && possibleTgz.Filename != null)
{ {
_children = []; _children.Add(possibleTgz);
return _children;
}
string gamename = Path.GetFileNameWithoutExtension(this.Filename); try
{
// Create a blank item for the entry
BaseFile gzipEntryRom = new();
BaseFile? possibleTgz = GetTorrentGZFileInfo(); // Perform a quickscan, if flagged to
if (this.AvailableHashTypes.Length == 1 && this.AvailableHashTypes[0] == HashType.CRC32)
// If it was, then add it to the outputs and continue
if (possibleTgz != null && possibleTgz.Filename != null)
{ {
_children.Add(possibleTgz); gzipEntryRom.Filename = gamename;
using BinaryReader br = new(File.OpenRead(this.Filename));
br.BaseStream.Seek(-8, SeekOrigin.End);
gzipEntryRom.CRC = br.ReadBytesBigEndian(4);
gzipEntryRom.Size = br.ReadInt32BigEndian();
} }
// Otherwise, use the stream directly
else else
{ {
try var gz = new gZip();
{ ZipReturn ret = gz.ZipFileOpen(this.Filename);
// Create a blank item for the entry ret = gz.ZipFileOpenReadStream(0, out Stream? gzstream, out ulong streamSize);
BaseFile gzipEntryRom = new(); gzipEntryRom = GetInfo(gzstream, hashes: this.AvailableHashTypes);
gzipEntryRom.Filename = gz.GetLocalFile(0).Filename;
// Perform a quickscan, if flagged to gzipEntryRom.Parent = gamename;
if (this.AvailableHashTypes.Length == 1 && this.AvailableHashTypes[0] == HashType.CRC32) gzipEntryRom.Date = (gz.TimeStamp > 0 ? gz.TimeStamp.ToString() : null);
{ gzstream!.Dispose();
gzipEntryRom.Filename = gamename;
using BinaryReader br = new(File.OpenRead(this.Filename));
br.BaseStream.Seek(-8, SeekOrigin.End);
gzipEntryRom.CRC = br.ReadBytesBigEndian(4);
gzipEntryRom.Size = br.ReadInt32BigEndian();
}
// Otherwise, use the stream directly
else
{
var gz = new gZip();
ZipReturn ret = gz.ZipFileOpen(this.Filename);
ret = gz.ZipFileOpenReadStream(0, out Stream? gzstream, out ulong streamSize);
gzipEntryRom = GetInfo(gzstream, hashes: this.AvailableHashTypes);
gzipEntryRom.Filename = gz.GetLocalFile(0).Filename;
gzipEntryRom.Parent = gamename;
gzipEntryRom.Date = (gz.TimeStamp > 0 ? gz.TimeStamp.ToString() : null);
gzstream!.Dispose();
}
// Fill in comon details and add to the list
gzipEntryRom.Parent = gamename;
_children.Add(gzipEntryRom);
}
catch (Exception ex)
{
logger.Error(ex);
return null;
}
} }
// Fill in comon details and add to the list
gzipEntryRom.Parent = gamename;
_children.Add(gzipEntryRom);
}
catch (Exception ex)
{
logger.Error(ex);
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,39 +96,38 @@ 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);
// Create the output subfolder now
Directory.CreateDirectory(Path.GetDirectoryName(realEntry) ?? string.Empty);
// Now open and write the file if possible
FileStream fs = File.Create(realEntry);
if (fs != null)
{ {
realEntry = Path.Combine(outDir, realEntry); if (stream.CanSeek)
stream.Seek(0, SeekOrigin.Begin);
// Create the output subfolder now byte[] zbuffer = new byte[_bufferSize];
Directory.CreateDirectory(Path.GetDirectoryName(realEntry) ?? string.Empty); int zlen;
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
// Now open and write the file if possible
FileStream fs = File.Create(realEntry);
if (fs != null)
{ {
if (stream.CanSeek) fs.Write(zbuffer, 0, zlen);
stream.Seek(0, SeekOrigin.Begin); fs.Flush();
byte[] zbuffer = new byte[_bufferSize];
int zlen;
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
{
fs.Write(zbuffer, 0, zlen);
fs.Flush();
}
stream?.Dispose();
fs?.Dispose();
}
else
{
stream?.Dispose();
fs?.Dispose();
realEntry = null;
} }
stream?.Dispose();
fs?.Dispose();
}
else
{
stream?.Dispose();
fs?.Dispose();
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,39 +182,38 @@ 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);
// Create the output subfolder now
Directory.CreateDirectory(Path.GetDirectoryName(realEntry)!);
// Now open and write the file if possible
FileStream fs = File.Create(realEntry);
if (fs != null)
{ {
realEntry = Path.Combine(outDir, realEntry); if (stream.CanSeek)
stream.Seek(0, SeekOrigin.Begin);
// Create the output subfolder now byte[] zbuffer = new byte[_bufferSize];
Directory.CreateDirectory(Path.GetDirectoryName(realEntry)!); int zlen;
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
// Now open and write the file if possible
FileStream fs = File.Create(realEntry);
if (fs != null)
{ {
if (stream.CanSeek) fs.Write(zbuffer, 0, zlen);
stream.Seek(0, SeekOrigin.Begin); fs.Flush();
byte[] zbuffer = new byte[_bufferSize];
int zlen;
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
{
fs.Write(zbuffer, 0, zlen);
fs.Flush();
}
stream?.Dispose();
fs?.Dispose();
}
else
{
stream?.Dispose();
fs?.Dispose();
realEntry = null;
} }
stream?.Dispose();
fs?.Dispose();
}
else
{
stream?.Dispose();
fs?.Dispose();
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,39 +97,38 @@ 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);
// Create the output subfolder now
Directory.CreateDirectory(Path.GetDirectoryName(realEntry)!);
// Now open and write the file if possible
FileStream fs = File.Create(realEntry);
if (fs != null)
{ {
realEntry = Path.Combine(outDir, realEntry); if (stream.CanSeek)
stream.Seek(0, SeekOrigin.Begin);
// Create the output subfolder now byte[] zbuffer = new byte[_bufferSize];
Directory.CreateDirectory(Path.GetDirectoryName(realEntry)!); int zlen;
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
// Now open and write the file if possible
FileStream fs = File.Create(realEntry);
if (fs != null)
{ {
if (stream.CanSeek) fs.Write(zbuffer, 0, zlen);
stream.Seek(0, SeekOrigin.Begin); fs.Flush();
byte[] zbuffer = new byte[_bufferSize];
int zlen;
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
{
fs.Write(zbuffer, 0, zlen);
fs.Flush();
}
stream?.Dispose();
fs?.Dispose();
}
else
{
stream?.Dispose();
fs?.Dispose();
realEntry = null;
} }
stream?.Dispose();
fs?.Dispose();
}
else
{
stream?.Dispose();
fs?.Dispose();
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,39 +115,38 @@ 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);
// Create the output subfolder now
Directory.CreateDirectory(Path.GetDirectoryName(realEntry)!);
// Now open and write the file if possible
FileStream fs = File.Create(realEntry);
if (fs != null)
{ {
realEntry = Path.Combine(outDir, realEntry); if (stream.CanSeek)
stream.Seek(0, SeekOrigin.Begin);
// Create the output subfolder now byte[] zbuffer = new byte[_bufferSize];
Directory.CreateDirectory(Path.GetDirectoryName(realEntry)!); int zlen;
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
// Now open and write the file if possible
FileStream fs = File.Create(realEntry);
if (fs != null)
{ {
if (stream.CanSeek) fs.Write(zbuffer, 0, zlen);
stream.Seek(0, SeekOrigin.Begin); fs.Flush();
byte[] zbuffer = new byte[_bufferSize];
int zlen;
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
{
fs.Write(zbuffer, 0, zlen);
fs.Flush();
}
stream?.Dispose();
fs?.Dispose();
}
else
{
stream?.Dispose();
fs?.Dispose();
realEntry = null;
} }
stream?.Dispose();
fs?.Dispose();
}
else
{
stream?.Dispose();
fs?.Dispose();
realEntry = null;
} }
return realEntry; return realEntry;
@@ -188,56 +187,55 @@ 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 = [];
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 = []; _children.Add(possibleTxz);
return _children;
}
string? gamename = Path.GetFileNameWithoutExtension(this.Filename); try
{
// Create a blank item for the entry
BaseFile xzEntryRom = new();
BaseFile? possibleTxz = GetTorrentXZFileInfo(); // Perform a quickscan, if flagged to
if (this.AvailableHashTypes.Length == 1 && this.AvailableHashTypes[0] == HashType.CRC32)
// If it was, then add it to the outputs and continue
if (possibleTxz != null && possibleTxz.Filename != null)
{ {
_children.Add(possibleTxz); xzEntryRom.Filename = gamename;
using BinaryReader br = new(File.OpenRead(this.Filename!));
br.BaseStream.Seek(-8, SeekOrigin.End);
xzEntryRom.CRC = br.ReadBytesBigEndian(4);
xzEntryRom.Size = br.ReadInt32BigEndian();
} }
// Otherwise, use the stream directly
else else
{ {
try var xzStream = new XZStream(File.OpenRead(this.Filename!));
{ xzEntryRom = GetInfo(xzStream, hashes: this.AvailableHashTypes);
// Create a blank item for the entry xzEntryRom.Filename = gamename;
BaseFile xzEntryRom = new(); xzStream.Dispose();
// Perform a quickscan, if flagged to
if (this.AvailableHashTypes.Length == 1 && this.AvailableHashTypes[0] == HashType.CRC32)
{
xzEntryRom.Filename = gamename;
using BinaryReader br = new(File.OpenRead(this.Filename!));
br.BaseStream.Seek(-8, SeekOrigin.End);
xzEntryRom.CRC = br.ReadBytesBigEndian(4);
xzEntryRom.Size = br.ReadInt32BigEndian();
}
// Otherwise, use the stream directly
else
{
var xzStream = new XZStream(File.OpenRead(this.Filename!));
xzEntryRom = GetInfo(xzStream, hashes: this.AvailableHashTypes);
xzEntryRom.Filename = gamename;
xzStream.Dispose();
}
// Fill in comon details and add to the list
xzEntryRom.Parent = gamename;
_children.Add(xzEntryRom);
}
catch (Exception ex)
{
logger.Error(ex);
return null;
}
} }
// Fill in comon details and add to the list
xzEntryRom.Parent = gamename;
_children.Add(xzEntryRom);
}
catch (Exception ex)
{
logger.Error(ex);
return null;
} }
return _children; return _children;

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,39 +155,38 @@ 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);
// Create the output subfolder now
Directory.CreateDirectory(Path.GetDirectoryName(realEntry)!);
// Now open and write the file if possible
FileStream fs = File.Create(realEntry);
if (fs != null)
{ {
realEntry = Path.Combine(outDir, realEntry); if (stream.CanSeek)
stream.Seek(0, SeekOrigin.Begin);
// Create the output subfolder now byte[] zbuffer = new byte[_bufferSize];
Directory.CreateDirectory(Path.GetDirectoryName(realEntry)!); int zlen;
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
// Now open and write the file if possible
FileStream fs = File.Create(realEntry);
if (fs != null)
{ {
if (stream.CanSeek) fs.Write(zbuffer, 0, zlen);
stream.Seek(0, SeekOrigin.Begin); fs.Flush();
byte[] zbuffer = new byte[_bufferSize];
int zlen;
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
{
fs.Write(zbuffer, 0, zlen);
fs.Flush();
}
stream?.Dispose();
fs?.Dispose();
}
else
{
stream?.Dispose();
fs?.Dispose();
realEntry = null;
} }
stream?.Dispose();
fs?.Dispose();
}
else
{
stream?.Dispose();
fs?.Dispose();
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,42 +361,38 @@ 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 (inputStream.CanSeek)
if (outputStream != null) inputStream.Seek(0, SeekOrigin.Begin);
// Copy the input stream to the output
int bufferSize = 4096 * 128;
byte[] ibuffer = new byte[bufferSize];
int ilen;
while ((ilen = inputStream.Read(ibuffer, 0, bufferSize)) > 0)
{ {
// Copy the input stream to the output outputStream.Write(ibuffer, 0, ilen);
if (inputStream.CanSeek) outputStream.Flush();
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 (!string.IsNullOrEmpty(baseFile.Date))
File.SetCreationTime(fileName, DateTime.Parse(baseFile.Date));
success = true;
} }
outputStream.Dispose();
if (!string.IsNullOrEmpty(baseFile.Date))
File.SetCreationTime(fileName, DateTime.Parse(baseFile.Date));
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>