2017-11-02 00:29:20 -07:00
|
|
|
|
using System.Collections.Generic;
|
2020-06-11 11:44:46 -07:00
|
|
|
|
using System.IO;
|
2020-12-10 22:31:23 -08:00
|
|
|
|
using SabreTools.FileTypes.Archives;
|
|
|
|
|
|
|
2020-12-08 14:53:49 -08:00
|
|
|
|
namespace SabreTools.FileTypes
|
2017-11-02 00:29:20 -07:00
|
|
|
|
{
|
2019-02-08 20:51:44 -08:00
|
|
|
|
public abstract class BaseArchive : Folder
|
|
|
|
|
|
{
|
2020-09-18 15:01:03 -07:00
|
|
|
|
#region Fields
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Determines if dates are read or written
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool UseDates { get; set; } = false;
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
2020-09-18 15:01:03 -07:00
|
|
|
|
#region Protected instance variables
|
2020-09-18 01:50:44 -07:00
|
|
|
|
|
2020-09-18 15:01:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Buffer size used by archives
|
|
|
|
|
|
/// </summary>
|
2019-02-08 20:51:44 -08:00
|
|
|
|
protected const int _bufferSize = 4096 * 128;
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Construtors
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a new Archive with no base file
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public BaseArchive()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a new Archive from the given file
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filename">Name of the file to use as an archive</param>
|
|
|
|
|
|
/// <param name="getHashes">True if hashes for this file should be calculated, false otherwise (default)</param>
|
|
|
|
|
|
public BaseArchive(string filename, bool getHashes = false)
|
|
|
|
|
|
: base(filename, getHashes)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create an archive object from a filename, if possible
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input">Name of the file to create the archive from</param>
|
|
|
|
|
|
/// <returns>Archive object representing the inputs</returns>
|
2024-02-28 19:19:50 -05:00
|
|
|
|
public static BaseArchive? Create(string input)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
2024-02-28 19:19:50 -05:00
|
|
|
|
BaseArchive? archive = null;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
|
|
|
|
|
// First get the archive type
|
2020-12-08 00:13:22 -08:00
|
|
|
|
FileType? at = GetFileType(input);
|
2020-07-15 09:41:59 -07:00
|
|
|
|
|
|
|
|
|
|
// If we got back null, then it's not an archive, so we we return
|
|
|
|
|
|
if (at == null)
|
|
|
|
|
|
return archive;
|
|
|
|
|
|
|
|
|
|
|
|
// Create the archive based on the type
|
2020-10-07 16:37:10 -07:00
|
|
|
|
staticLogger.Verbose($"Found archive of type: {at}");
|
2020-07-15 09:41:59 -07:00
|
|
|
|
switch (at)
|
|
|
|
|
|
{
|
|
|
|
|
|
case FileType.GZipArchive:
|
|
|
|
|
|
archive = new GZipArchive(input);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case FileType.RarArchive:
|
|
|
|
|
|
archive = new RarArchive(input);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case FileType.SevenZipArchive:
|
|
|
|
|
|
archive = new SevenZipArchive(input);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case FileType.TapeArchive:
|
|
|
|
|
|
archive = new TapeArchive(input);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case FileType.ZipArchive:
|
|
|
|
|
|
archive = new ZipArchive(input);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
// We ignore all other types for now
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return archive;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create an archive object of the specified type, if possible
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="archiveType">SharpCompress.Common.ArchiveType representing the archive to create</param>
|
|
|
|
|
|
/// <returns>Archive object representing the inputs</returns>
|
2024-02-28 19:19:50 -05:00
|
|
|
|
public static BaseArchive? Create(FileType archiveType)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
2020-12-14 16:01:28 -08:00
|
|
|
|
return archiveType switch
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
2020-12-14 16:01:28 -08:00
|
|
|
|
FileType.GZipArchive => new GZipArchive(),
|
|
|
|
|
|
FileType.RarArchive => new RarArchive(),
|
|
|
|
|
|
FileType.SevenZipArchive => new SevenZipArchive(),
|
|
|
|
|
|
FileType.TapeArchive => new TapeArchive(),
|
|
|
|
|
|
FileType.ZipArchive => new ZipArchive(),
|
|
|
|
|
|
_ => null,
|
|
|
|
|
|
};
|
2020-07-15 09:41:59 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-02-08 20:51:44 -08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Extraction
|
|
|
|
|
|
|
2020-12-08 11:09:05 -08:00
|
|
|
|
/// <inheritdoc/>
|
2019-02-08 20:51:44 -08:00
|
|
|
|
public override abstract bool CopyAll(string outDir);
|
|
|
|
|
|
|
2020-12-08 11:09:05 -08:00
|
|
|
|
/// <inheritdoc/>
|
2024-02-28 19:19:50 -05:00
|
|
|
|
public override abstract string? CopyToFile(string entryName, string outDir);
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
2020-12-08 11:09:05 -08:00
|
|
|
|
/// <inheritdoc/>
|
2024-07-15 21:37:38 -04:00
|
|
|
|
public override abstract (Stream?, string?) GetEntryStream(string entryName);
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Information
|
|
|
|
|
|
|
2020-12-08 11:09:05 -08:00
|
|
|
|
/// <inheritdoc/>
|
2024-02-28 19:19:50 -05:00
|
|
|
|
public override abstract List<BaseFile>? GetChildren();
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
2020-12-08 11:09:05 -08:00
|
|
|
|
/// <inheritdoc/>
|
2019-02-08 20:51:44 -08:00
|
|
|
|
public override abstract List<string> GetEmptyFolders();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Check whether the input file is a standardized format
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public abstract bool IsTorrent();
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Writing
|
|
|
|
|
|
|
2020-12-08 11:09:05 -08:00
|
|
|
|
/// <inheritdoc/>
|
2024-02-28 19:19:50 -05:00
|
|
|
|
public override abstract bool Write(string inputFile, string outDir, BaseFile? baseFile);
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
2020-12-08 11:09:05 -08:00
|
|
|
|
/// <inheritdoc/>
|
2024-02-28 19:19:50 -05:00
|
|
|
|
public override abstract bool Write(Stream? inputStream, string outDir, BaseFile? baseFile);
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
2020-12-08 11:09:05 -08:00
|
|
|
|
/// <inheritdoc/>
|
2024-02-28 19:19:50 -05:00
|
|
|
|
public override abstract bool Write(List<string> inputFiles, string outDir, List<BaseFile>? baseFiles);
|
2019-02-08 20:51:44 -08:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
2017-11-02 00:29:20 -07:00
|
|
|
|
}
|