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,41 +131,40 @@ 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
|
||||
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
|
||||
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)
|
||||
byte[] zbuffer = new byte[_bufferSize];
|
||||
int zlen;
|
||||
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
|
||||
{
|
||||
if (stream.CanSeek)
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
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;
|
||||
fs.Write(zbuffer, 0, zlen);
|
||||
fs.Flush();
|
||||
}
|
||||
|
||||
stream?.Dispose();
|
||||
fs?.Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
stream?.Dispose();
|
||||
fs?.Dispose();
|
||||
realEntry = null;
|
||||
}
|
||||
|
||||
return realEntry;
|
||||
@@ -207,59 +206,59 @@ 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);
|
||||
|
||||
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();
|
||||
|
||||
// If it was, then add it to the outputs and continue
|
||||
if (possibleTgz != null && possibleTgz.Filename != null)
|
||||
// Perform a quickscan, if flagged to
|
||||
if (this.AvailableHashTypes.Length == 1 && this.AvailableHashTypes[0] == HashType.CRC32)
|
||||
{
|
||||
_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
|
||||
{
|
||||
try
|
||||
{
|
||||
// Create a blank item for the entry
|
||||
BaseFile gzipEntryRom = new();
|
||||
|
||||
// Perform a quickscan, if flagged to
|
||||
if (this.AvailableHashTypes.Length == 1 && this.AvailableHashTypes[0] == HashType.CRC32)
|
||||
{
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
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,39 +96,38 @@ 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
|
||||
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
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(realEntry) ?? string.Empty);
|
||||
|
||||
// Now open and write the file if possible
|
||||
FileStream fs = File.Create(realEntry);
|
||||
if (fs != null)
|
||||
byte[] zbuffer = new byte[_bufferSize];
|
||||
int zlen;
|
||||
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
|
||||
{
|
||||
if (stream.CanSeek)
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
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;
|
||||
fs.Write(zbuffer, 0, zlen);
|
||||
fs.Flush();
|
||||
}
|
||||
|
||||
stream?.Dispose();
|
||||
fs?.Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
stream?.Dispose();
|
||||
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,39 +182,38 @@ 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
|
||||
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
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(realEntry)!);
|
||||
|
||||
// Now open and write the file if possible
|
||||
FileStream fs = File.Create(realEntry);
|
||||
if (fs != null)
|
||||
byte[] zbuffer = new byte[_bufferSize];
|
||||
int zlen;
|
||||
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
|
||||
{
|
||||
if (stream.CanSeek)
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
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;
|
||||
fs.Write(zbuffer, 0, zlen);
|
||||
fs.Flush();
|
||||
}
|
||||
|
||||
stream?.Dispose();
|
||||
fs?.Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
stream?.Dispose();
|
||||
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,39 +97,38 @@ 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
|
||||
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
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(realEntry)!);
|
||||
|
||||
// Now open and write the file if possible
|
||||
FileStream fs = File.Create(realEntry);
|
||||
if (fs != null)
|
||||
byte[] zbuffer = new byte[_bufferSize];
|
||||
int zlen;
|
||||
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
|
||||
{
|
||||
if (stream.CanSeek)
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
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;
|
||||
fs.Write(zbuffer, 0, zlen);
|
||||
fs.Flush();
|
||||
}
|
||||
|
||||
stream?.Dispose();
|
||||
fs?.Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
stream?.Dispose();
|
||||
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,39 +115,38 @@ 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
|
||||
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
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(realEntry)!);
|
||||
|
||||
// Now open and write the file if possible
|
||||
FileStream fs = File.Create(realEntry);
|
||||
if (fs != null)
|
||||
byte[] zbuffer = new byte[_bufferSize];
|
||||
int zlen;
|
||||
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
|
||||
{
|
||||
if (stream.CanSeek)
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
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;
|
||||
fs.Write(zbuffer, 0, zlen);
|
||||
fs.Flush();
|
||||
}
|
||||
|
||||
stream?.Dispose();
|
||||
fs?.Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
stream?.Dispose();
|
||||
fs?.Dispose();
|
||||
realEntry = null;
|
||||
}
|
||||
|
||||
return realEntry;
|
||||
@@ -188,56 +187,55 @@ 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 = [];
|
||||
_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();
|
||||
|
||||
// If it was, then add it to the outputs and continue
|
||||
if (possibleTxz != null && possibleTxz.Filename != null)
|
||||
// Perform a quickscan, if flagged to
|
||||
if (this.AvailableHashTypes.Length == 1 && this.AvailableHashTypes[0] == HashType.CRC32)
|
||||
{
|
||||
_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
|
||||
{
|
||||
try
|
||||
{
|
||||
// Create a blank item for the entry
|
||||
BaseFile xzEntryRom = new();
|
||||
|
||||
// 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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
return _children;
|
||||
|
||||
@@ -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,39 +155,38 @@ 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
|
||||
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
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(realEntry)!);
|
||||
|
||||
// Now open and write the file if possible
|
||||
FileStream fs = File.Create(realEntry);
|
||||
if (fs != null)
|
||||
byte[] zbuffer = new byte[_bufferSize];
|
||||
int zlen;
|
||||
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
|
||||
{
|
||||
if (stream.CanSeek)
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
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;
|
||||
fs.Write(zbuffer, 0, zlen);
|
||||
fs.Flush();
|
||||
}
|
||||
|
||||
stream?.Dispose();
|
||||
fs?.Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
stream?.Dispose();
|
||||
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,42 +361,38 @@ 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)
|
||||
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;
|
||||
while ((ilen = inputStream.Read(ibuffer, 0, bufferSize)) > 0)
|
||||
{
|
||||
// Copy the input stream to the output
|
||||
if (inputStream.CanSeek)
|
||||
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.Write(ibuffer, 0, ilen);
|
||||
outputStream.Flush();
|
||||
}
|
||||
|
||||
outputStream.Dispose();
|
||||
|
||||
if (!string.IsNullOrEmpty(baseFile.Date))
|
||||
File.SetCreationTime(fileName, DateTime.Parse(baseFile.Date));
|
||||
|
||||
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