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
(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;