Files
SabreTools/SabreTools.FileTypes/Archives/RarArchive.cs

316 lines
9.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
2024-10-19 11:43:11 -04:00
#if NET462_OR_GREATER || NETCOREAPP
using System.Linq;
2024-03-04 23:56:05 -05:00
using SabreTools.Hashing;
2024-10-19 11:43:11 -04:00
using SabreTools.Matching.Compare;
using SharpCompress.Archives;
using SharpCompress.Archives.Rar;
using SharpCompress.Readers;
2024-02-28 21:59:13 -05:00
#endif
2020-12-10 22:31:23 -08:00
namespace SabreTools.FileTypes.Archives
{
2020-08-18 23:39:13 -07:00
/// <summary>
/// Represents a TorrentRAR archive for reading and writing
/// </summary>
public class RarArchive : BaseArchive
{
#region Constructors
/// <summary>
/// Create a new TorrentRARArchive with no base file
/// </summary>
public RarArchive()
: base()
{
}
/// <summary>
/// Create a new TorrentRARArchive from the given file
/// </summary>
/// <param name="filename">Name of the file to use as an archive</param>
2025-01-04 20:24:56 -05:00
public RarArchive(string filename)
: base(filename)
2020-08-18 23:39:13 -07:00
{
}
#endregion
#region Extraction
2020-12-08 11:09:05 -08:00
/// <inheritdoc/>
2020-08-18 23:39:13 -07:00
public override bool CopyAll(string outDir)
{
2024-02-28 21:59:13 -05:00
#if NET462_OR_GREATER || NETCOREAPP
2020-08-18 23:39:13 -07:00
bool encounteredErrors = true;
2024-02-28 19:19:50 -05:00
// If we have an invalid file
2024-12-06 13:23:53 -05:00
if (Filename == null)
2024-02-28 19:19:50 -05:00
return true;
2020-08-18 23:39:13 -07:00
try
{
// Create the temp directory
Directory.CreateDirectory(outDir);
// Extract all files to the temp directory
2024-12-06 13:23:53 -05:00
SharpCompress.Archives.Rar.RarArchive ra = SharpCompress.Archives.Rar.RarArchive.Open(Filename);
2020-08-18 23:39:13 -07:00
foreach (RarArchiveEntry entry in ra.Entries)
{
entry.WriteToDirectory(outDir, new SharpCompress.Common.ExtractionOptions { PreserveFileTime = true, ExtractFullPath = true, Overwrite = true });
}
encounteredErrors = false;
ra.Dispose();
}
2020-09-15 12:12:13 -07:00
catch (EndOfStreamException ex)
2020-08-18 23:39:13 -07:00
{
// Catch this but don't count it as an error because SharpCompress is unsafe
_logger.Verbose(ex);
2020-08-18 23:39:13 -07:00
}
2020-09-15 12:12:13 -07:00
catch (InvalidOperationException ex)
2020-08-18 23:39:13 -07:00
{
_logger.Warning(ex);
2020-08-18 23:39:13 -07:00
encounteredErrors = true;
}
catch (Exception ex)
{
_logger.Error(ex);
2020-08-18 23:39:13 -07:00
encounteredErrors = true;
}
return encounteredErrors;
2024-02-28 21:59:13 -05:00
#else
// TODO: Support RAR archives in old .NET
return true;
#endif
2020-08-18 23:39:13 -07:00
}
2020-12-08 11:09:05 -08:00
/// <inheritdoc/>
2024-02-28 19:19:50 -05:00
public override string? CopyToFile(string entryName, string outDir)
2020-08-18 23:39:13 -07:00
{
// Try to extract a stream using the given information
2024-07-17 14:46:14 -04:00
(Stream? stream, string? realEntry) = GetEntryStream(entryName);
2024-07-17 15:19:15 -04:00
if (stream == null || realEntry == null)
return null;
2020-08-18 23:39:13 -07:00
2024-07-15 21:34:17 -04:00
// If the stream and the entry name are both non-null, we write to file
2024-07-17 15:19:15 -04:00
realEntry = Path.Combine(outDir, realEntry);
2020-08-18 23:39:13 -07:00
2024-07-17 15:19:15 -04:00
// Create the output subfolder now
Directory.CreateDirectory(Path.GetDirectoryName(realEntry) ?? string.Empty);
2020-08-18 23:39:13 -07:00
2024-07-17 15:19:15 -04:00
// Now open and write the file if possible
FileStream fs = File.Create(realEntry);
if (fs != null)
{
if (stream.CanSeek)
stream.Seek(0, SeekOrigin.Begin);
byte[] zbuffer = new byte[_bufferSize];
int zlen;
while ((zlen = stream.Read(zbuffer, 0, _bufferSize)) > 0)
2020-08-18 23:39:13 -07:00
{
2024-07-17 15:19:15 -04:00
fs.Write(zbuffer, 0, zlen);
fs.Flush();
2020-08-18 23:39:13 -07:00
}
2024-07-17 15:19:15 -04:00
stream?.Dispose();
fs?.Dispose();
}
else
{
stream?.Dispose();
fs?.Dispose();
realEntry = null;
2020-08-18 23:39:13 -07:00
}
return realEntry;
}
2020-12-08 11:09:05 -08:00
/// <inheritdoc/>
2024-07-15 21:37:38 -04:00
public override (Stream?, string?) GetEntryStream(string entryName)
2020-08-18 23:39:13 -07:00
{
2024-02-28 21:59:13 -05:00
#if NET462_OR_GREATER || NETCOREAPP
2024-02-28 19:19:50 -05:00
// If we have an invalid file
2024-12-06 13:23:53 -05:00
if (Filename == null)
2024-02-28 19:19:50 -05:00
return (null, null);
2020-08-18 23:39:13 -07:00
try
{
2024-07-16 14:58:04 -04:00
Stream? stream = null;
string? realEntry = null;
2024-12-06 13:23:53 -05:00
var ra = SharpCompress.Archives.Rar.RarArchive.Open(Filename, new ReaderOptions { LeaveStreamOpen = false, });
2020-08-18 23:39:13 -07:00
foreach (RarArchiveEntry entry in ra.Entries)
{
2024-07-16 14:58:04 -04:00
// Skip invalid entries
if (entry?.Key == null || !entry.IsComplete)
continue;
// Skip directory entries
if (entry.IsDirectory)
continue;
// Skip non-matching keys
if (!entry.Key.Contains(entryName))
continue;
// Open the entry stream
realEntry = entry.Key;
stream = entry.OpenEntryStream();
break;
2020-08-18 23:39:13 -07:00
}
2024-07-16 14:58:04 -04:00
2020-08-18 23:39:13 -07:00
ra.Dispose();
2024-07-16 14:58:04 -04:00
return (stream, realEntry);
2020-08-18 23:39:13 -07:00
}
catch (Exception ex)
{
_logger.Error(ex);
2024-07-16 14:58:04 -04:00
return (null, null);
2020-08-18 23:39:13 -07:00
}
2024-02-28 21:59:13 -05:00
#else
// TODO: Support RAR archives in old .NET
return (null, null);
#endif
2020-08-18 23:39:13 -07:00
}
#endregion
#region Information
2020-12-08 11:09:05 -08:00
/// <inheritdoc/>
2024-02-28 19:19:50 -05:00
public override List<BaseFile>? GetChildren()
2020-08-18 23:39:13 -07:00
{
2024-02-28 21:59:13 -05:00
#if NET462_OR_GREATER || NETCOREAPP
2024-02-28 19:19:50 -05:00
// If we have an invalid file
2024-12-06 13:23:53 -05:00
if (Filename == null)
2024-02-28 19:19:50 -05:00
return null;
List<BaseFile> found = [];
2024-12-06 13:23:53 -05:00
string? gamename = Path.GetFileNameWithoutExtension(Filename);
2020-08-18 23:39:13 -07:00
try
{
2024-12-06 13:23:53 -05:00
SharpCompress.Archives.Rar.RarArchive ra = SharpCompress.Archives.Rar.RarArchive.Open(File.OpenRead(Filename));
2020-08-18 23:39:13 -07:00
foreach (RarArchiveEntry entry in ra.Entries.Where(e => e != null && !e.IsDirectory))
{
2020-10-04 14:14:57 -07:00
// Create a blank item for the entry
BaseFile rarEntryRom = new();
2020-10-04 14:14:57 -07:00
2020-09-18 01:50:44 -07:00
// Perform a quickscan, if flagged to
if (_hashTypes.Length == 1 && _hashTypes[0] == HashType.CRC32)
2020-08-18 23:39:13 -07:00
{
2020-10-04 14:14:57 -07:00
rarEntryRom.Size = entry.Size;
rarEntryRom.CRC = BitConverter.GetBytes(entry.Crc);
2020-08-18 23:39:13 -07:00
}
// Otherwise, use the stream directly
else
{
using Stream entryStream = entry.OpenEntryStream();
rarEntryRom = GetInfo(entryStream, size: entry.Size, hashes: _hashTypes);
2020-08-18 23:39:13 -07:00
}
2020-10-04 14:14:57 -07:00
2024-07-19 15:14:30 -04:00
// Fill in common details and add to the list
2020-10-04 14:14:57 -07:00
rarEntryRom.Filename = entry.Key;
rarEntryRom.Parent = gamename;
rarEntryRom.Date = entry.LastModifiedTime?.ToString("yyyy/MM/dd HH:mm:ss");
2020-10-04 14:14:57 -07:00
found.Add(rarEntryRom);
2020-08-18 23:39:13 -07:00
}
// Dispose of the archive
ra.Dispose();
}
catch (Exception ex)
{
_logger.Error(ex);
2020-08-18 23:39:13 -07:00
return null;
}
return found;
2024-02-28 21:59:13 -05:00
#else
// TODO: Support RAR archives in old .NET
return null;
#endif
2020-08-18 23:39:13 -07:00
}
2020-12-08 11:09:05 -08:00
/// <inheritdoc/>
2020-08-18 23:39:13 -07:00
public override List<string> GetEmptyFolders()
{
2024-02-28 21:59:13 -05:00
#if NET462_OR_GREATER || NETCOREAPP
2024-02-28 19:19:50 -05:00
List<string> empties = [];
// If we have an invalid file
2024-12-06 13:23:53 -05:00
if (Filename == null)
2024-02-28 19:19:50 -05:00
return empties;
2020-08-18 23:39:13 -07:00
try
{
2024-12-06 13:23:53 -05:00
SharpCompress.Archives.Rar.RarArchive ra = SharpCompress.Archives.Rar.RarArchive.Open(Filename, new ReaderOptions { LeaveStreamOpen = false });
List<RarArchiveEntry> rarEntries = [.. ra.Entries.OrderBy(e => e.Key ?? string.Empty, new NaturalReversedComparer())];
2024-02-28 19:19:50 -05:00
string? lastRarEntry = null;
2020-08-18 23:39:13 -07:00
foreach (RarArchiveEntry entry in rarEntries)
{
2024-04-24 13:45:38 -04:00
if (entry?.Key == null)
continue;
// If the current is a superset of last, we skip it
if (lastRarEntry != null && lastRarEntry.StartsWith(entry.Key))
{
// No-op
}
// If the entry is a directory, we add it
else if (entry.IsDirectory)
2020-08-18 23:39:13 -07:00
{
2024-04-24 13:45:38 -04:00
empties.Add(entry.Key);
lastRarEntry = entry.Key;
2020-08-18 23:39:13 -07:00
}
}
}
catch (Exception ex)
{
_logger.Error(ex);
2020-08-18 23:39:13 -07:00
}
return empties;
2024-02-28 21:59:13 -05:00
#else
// TODO: Support RAR archives in old .NET
return [];
#endif
2020-08-18 23:39:13 -07:00
}
2020-12-08 11:09:05 -08:00
/// <inheritdoc/>
2020-08-18 23:39:13 -07:00
public override bool IsTorrent()
{
throw new NotImplementedException();
}
#endregion
#region Writing
2020-12-08 11:09:05 -08:00
/// <inheritdoc/>
2024-02-28 19:19:50 -05:00
public override bool Write(string inputFile, string outDir, BaseFile? baseFile)
2020-08-18 23:39:13 -07:00
{
// Get the file stream for the file and write out
2020-12-08 11:09:05 -08:00
return Write(File.OpenRead(inputFile), outDir, baseFile);
2020-08-18 23:39:13 -07:00
}
2020-12-08 11:09:05 -08:00
/// <inheritdoc/>
2024-02-28 19:19:50 -05:00
public override bool Write(Stream? inputStream, string outDir, BaseFile? baseFile)
2020-08-18 23:39:13 -07:00
{
throw new NotImplementedException();
}
2020-12-08 11:09:05 -08:00
/// <inheritdoc/>
2024-02-28 19:19:50 -05:00
public override bool Write(List<string> inputFiles, string outDir, List<BaseFile>? roms)
2020-08-18 23:39:13 -07:00
{
throw new NotImplementedException();
}
#endregion
}
}