using System.Collections.Generic;
using System.IO;
using SabreTools.FileTypes.Archives;
namespace SabreTools.FileTypes
{
public abstract class BaseArchive : Folder
{
#region Fields
///
/// Determines if dates are read or written
///
public bool UseDates { get; set; } = false;
#endregion
#region Protected instance variables
///
/// Buffer size used by archives
///
protected const int _bufferSize = 4096 * 128;
#endregion
#region Construtors
///
/// Create a new Archive with no base file
///
public BaseArchive()
{
}
///
/// Create a new Archive from the given file
///
/// Name of the file to use as an archive
/// True if hashes for this file should be calculated, false otherwise (default)
public BaseArchive(string filename, bool getHashes = false)
: base(filename, getHashes)
{
}
///
/// Create an archive object from a filename, if possible
///
/// Name of the file to create the archive from
/// Archive object representing the inputs
public static BaseArchive? Create(string input)
{
BaseArchive? archive = null;
// First get the archive type
FileType? at = GetFileType(input);
// 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
staticLogger.Verbose($"Found archive of type: {at}");
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;
}
///
/// Create an archive object of the specified type, if possible
///
/// SharpCompress.Common.ArchiveType representing the archive to create
/// Archive object representing the inputs
public static BaseArchive? Create(FileType archiveType)
{
return archiveType switch
{
FileType.GZipArchive => new GZipArchive(),
FileType.RarArchive => new RarArchive(),
FileType.SevenZipArchive => new SevenZipArchive(),
FileType.TapeArchive => new TapeArchive(),
FileType.ZipArchive => new ZipArchive(),
_ => null,
};
}
#endregion
#region Extraction
///
public override abstract bool CopyAll(string outDir);
///
public override abstract string? CopyToFile(string entryName, string outDir);
///
public override abstract (Stream?, string?) GetEntryStream(string entryName);
#endregion
#region Information
///
public override abstract List? GetChildren();
///
public override abstract List GetEmptyFolders();
///
/// Check whether the input file is a standardized format
///
public abstract bool IsTorrent();
#endregion
#region Writing
///
public override abstract bool Write(string inputFile, string outDir, BaseFile? baseFile);
///
public override abstract bool Write(Stream? inputStream, string outDir, BaseFile? baseFile);
///
public override abstract bool Write(List inputFiles, string outDir, List? baseFiles);
#endregion
}
}