2017-11-02 00:29:20 -07:00
using System ;
using System.Collections.Generic ;
2020-06-10 22:37:19 -07:00
using System.IO ;
2017-11-02 00:29:20 -07:00
using System.Linq ;
2020-12-08 16:37:08 -08:00
using SabreTools.Core.Tools ;
2020-12-10 22:31:23 -08:00
using SabreTools.FileTypes.Archives ;
2020-12-07 15:08:57 -08:00
using SabreTools.IO ;
2024-04-24 13:45:38 -04:00
using SabreTools.IO.Extensions ;
2020-12-07 14:29:45 -08:00
using SabreTools.Logging ;
2017-11-02 00:29:20 -07:00
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
/// <summary>
/// Represents a folder for reading and writing
/// </summary>
public class Folder : BaseFile
{
#region Protected instance variables
2024-02-28 19:19:50 -05:00
protected List < BaseFile > ? _children ;
2020-10-07 15:42:30 -07:00
/// <summary>
/// Logging object
/// </summary>
2020-10-07 16:37:10 -07:00
protected Logger logger ;
/// <summary>
/// Static logger for static methods
/// </summary>
2023-04-19 16:39:58 -04:00
protected static Logger staticLogger = new ( ) ;
2020-10-07 15:42:30 -07:00
2020-08-28 20:46:12 -07:00
/// <summary>
/// Flag specific to Folder to omit Machine name from output path
/// </summary>
2020-12-14 16:01:28 -08:00
private readonly bool writeToParent = false ;
2019-02-08 20:51:44 -08:00
#endregion
#region Constructors
/// <summary>
/// Create a new folder with no base file
/// </summary>
2020-08-28 20:46:12 -07:00
/// <param name="writeToParent">True to write directly to parent, false otherwise</param>
public Folder ( bool writeToParent = false )
2019-02-08 20:51:44 -08:00
: base ( )
{
this . Type = FileType . Folder ;
2020-08-28 20:46:12 -07:00
this . writeToParent = writeToParent ;
2020-10-07 16:37:10 -07:00
logger = new Logger ( this ) ;
2019-02-08 20:51:44 -08:00
}
/// <summary>
/// Create a new folder from the given file
/// </summary>
/// <param name="filename">Name of the file to use as an archive</param>
/// <param name="read">True for opening file as read, false for opening file as write</param>
/// <param name="getHashes">True if hashes for this file should be calculated, false otherwise (default)</param>
public Folder ( string filename , bool getHashes = false )
: base ( filename , getHashes )
{
this . Type = FileType . Folder ;
2020-10-07 16:37:10 -07:00
logger = new Logger ( this ) ;
2019-02-08 20:51:44 -08:00
}
2020-07-15 09:41:59 -07:00
/// <summary>
/// Create an folder object of the specified type, if possible
/// </summary>
2020-12-08 16:37:08 -08:00
/// <param name="outputFormat">OutputFormat representing the archive to create</param>
2020-07-15 09:41:59 -07:00
/// <returns>Archive object representing the inputs</returns>
2024-02-28 19:19:50 -05:00
public static Folder ? Create ( OutputFormat outputFormat )
2020-07-15 09:41:59 -07:00
{
2023-04-19 16:39:58 -04:00
return outputFormat switch
2020-07-15 09:41:59 -07:00
{
2023-04-19 16:39:58 -04:00
OutputFormat . Folder = > new Folder ( false ) ,
OutputFormat . ParentFolder = > new Folder ( true ) ,
OutputFormat . TapeArchive = > new TapeArchive ( ) ,
OutputFormat . Torrent7Zip = > new SevenZipArchive ( ) ,
OutputFormat . TorrentGzip = > new GZipArchive ( ) ,
OutputFormat . TorrentGzipRomba = > new GZipArchive ( ) ,
OutputFormat . TorrentLRZip = > new LRZipArchive ( ) ,
OutputFormat . TorrentLZ4 = > new LZ4Archive ( ) ,
OutputFormat . TorrentRar = > new RarArchive ( ) ,
OutputFormat . TorrentXZ = > new XZArchive ( ) ,
OutputFormat . TorrentXZRomba = > new XZArchive ( ) ,
OutputFormat . TorrentZip = > new ZipArchive ( ) ,
OutputFormat . TorrentZPAQ = > new ZPAQArchive ( ) ,
OutputFormat . TorrentZstd = > new ZstdArchive ( ) ,
_ = > null ,
} ;
2020-07-15 09:41:59 -07:00
}
2019-02-08 20:51:44 -08:00
#endregion
#region Extraction
/// <summary>
/// Attempt to extract a file as an archive
/// </summary>
/// <param name="outDir">Output directory for archive extraction</param>
/// <returns>True if the extraction was a success, false otherwise</returns>
public virtual bool CopyAll ( string outDir )
{
2024-02-28 19:19:50 -05:00
// If we have an invalid filename
if ( this . Filename = = null )
return false ;
2019-02-08 20:51:44 -08:00
// Copy all files from the current folder to the output directory recursively
try
{
// Make sure the folders exist
Directory . CreateDirectory ( this . Filename ) ;
Directory . CreateDirectory ( outDir ) ;
2020-06-10 22:37:19 -07:00
DirectoryCopy ( this . Filename , outDir , true ) ;
2019-02-08 20:51:44 -08:00
}
catch ( Exception ex )
{
2020-10-07 15:42:30 -07:00
logger . Error ( ex ) ;
2019-02-08 20:51:44 -08:00
return false ;
}
return true ;
}
2020-06-10 22:37:19 -07:00
// https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-copy-directories
private static void DirectoryCopy ( string sourceDirName , string destDirName , bool copySubDirs )
{
// Get the subdirectories for the specified directory.
2023-04-19 16:39:58 -04:00
DirectoryInfo dir = new ( sourceDirName ) ;
2020-06-10 22:37:19 -07:00
if ( ! dir . Exists )
{
throw new DirectoryNotFoundException (
"Source directory does not exist or could not be found: "
+ sourceDirName ) ;
}
DirectoryInfo [ ] dirs = dir . GetDirectories ( ) ;
// If the destination directory doesn't exist, create it.
if ( ! Directory . Exists ( destDirName ) )
{
Directory . CreateDirectory ( destDirName ) ;
}
// Get the files in the directory and copy them to the new location.
FileInfo [ ] files = dir . GetFiles ( ) ;
foreach ( FileInfo file in files )
{
string temppath = Path . Combine ( destDirName , file . Name ) ;
file . CopyTo ( temppath , false ) ;
}
// If copying subdirectories, copy them and their contents to new location.
if ( copySubDirs )
{
foreach ( DirectoryInfo subdir in dirs )
{
string temppath = Path . Combine ( destDirName , subdir . Name ) ;
DirectoryCopy ( subdir . FullName , temppath , copySubDirs ) ;
}
}
}
2019-02-08 20:51:44 -08:00
/// <summary>
/// Attempt to extract a file from an archive
/// </summary>
/// <param name="entryName">Name of the entry to be extracted</param>
/// <param name="outDir">Output directory for archive extraction</param>
/// <returns>Name of the extracted file, null on error</returns>
2024-02-28 19:19:50 -05:00
public virtual string? CopyToFile ( string entryName , string outDir )
2019-02-08 20:51:44 -08:00
{
2024-02-28 19:19:50 -05:00
string? realentry = null ;
2024-02-28 21:59:13 -05:00
2024-02-28 19:19:50 -05:00
// If we have an invalid filename
if ( this . Filename = = null )
return null ;
2019-02-08 20:51:44 -08:00
// Copy single file from the current folder to the output directory, if exists
try
{
// Make sure the folders exist
Directory . CreateDirectory ( this . Filename ) ;
Directory . CreateDirectory ( outDir ) ;
// Get all files from the input directory
2020-12-10 22:16:53 -08:00
List < string > files = PathTool . GetFilesOrdered ( this . Filename ) ;
2019-02-08 20:51:44 -08:00
// Now sort through to find the first file that matches
2024-02-28 19:19:50 -05:00
string? match = files . Where ( s = > s . EndsWith ( entryName ) ) . FirstOrDefault ( ) ;
2019-02-08 20:51:44 -08:00
// If we had a file, copy that over to the new name
2024-02-28 21:59:13 -05:00
if ( ! string . IsNullOrEmpty ( match ) )
2019-02-08 20:51:44 -08:00
{
realentry = match ;
File . Copy ( match , Path . Combine ( outDir , entryName ) ) ;
}
}
catch ( Exception ex )
{
2020-10-07 15:42:30 -07:00
logger . Error ( ex ) ;
2019-02-08 20:51:44 -08:00
return realentry ;
}
return realentry ;
}
/// <summary>
/// Attempt to extract a stream from an archive
/// </summary>
/// <param name="entryName">Name of the entry to be extracted</param>
/// <returns>MemoryStream representing the entry, null on error</returns>
2024-02-28 19:19:50 -05:00
public virtual ( MemoryStream ? , string? ) CopyToStream ( string entryName )
2019-02-08 20:51:44 -08:00
{
2023-04-19 16:39:58 -04:00
MemoryStream ms = new ( ) ;
2024-02-28 19:19:50 -05:00
string? realentry = null ;
// If we have an invalid filename
if ( this . Filename = = null )
return ( null , null ) ;
2019-02-08 20:51:44 -08:00
// Copy single file from the current folder to the output directory, if exists
try
{
// Make sure the folders exist
Directory . CreateDirectory ( this . Filename ) ;
// Get all files from the input directory
2020-12-10 22:16:53 -08:00
List < string > files = PathTool . GetFilesOrdered ( this . Filename ) ;
2019-02-08 20:51:44 -08:00
// Now sort through to find the first file that matches
2024-02-28 19:19:50 -05:00
string? match = files . Where ( s = > s . EndsWith ( entryName ) ) . FirstOrDefault ( ) ;
2019-02-08 20:51:44 -08:00
// If we had a file, copy that over to the new name
2024-02-28 21:59:13 -05:00
if ( ! string . IsNullOrEmpty ( match ) )
2019-02-08 20:51:44 -08:00
{
2024-02-28 21:59:13 -05:00
#if NET20 | | NET35
var tempStream = File . OpenRead ( match ) ;
byte [ ] buffer = new byte [ 32768 ] ;
int read ;
while ( ( read = tempStream . Read ( buffer , 0 , buffer . Length ) ) > 0 )
{
ms . Write ( buffer , 0 , read ) ;
}
#else
2020-12-08 00:13:22 -08:00
File . OpenRead ( match ) . CopyTo ( ms ) ;
2024-02-28 21:59:13 -05:00
#endif
2019-02-08 20:51:44 -08:00
realentry = match ;
}
}
catch ( Exception ex )
{
2020-10-07 15:42:30 -07:00
logger . Error ( ex ) ;
2019-02-08 20:51:44 -08:00
return ( ms , realentry ) ;
}
return ( ms , realentry ) ;
}
#endregion
#region Information
/// <summary>
/// Generate a list of immediate children from the current folder
/// </summary>
/// <returns>List of BaseFile objects representing the found data</returns>
2024-02-28 19:19:50 -05:00
public virtual List < BaseFile > ? GetChildren ( )
2019-02-08 20:51:44 -08:00
{
2024-02-28 19:19:50 -05:00
// If we have an invalid filename
if ( this . Filename = = null )
return null ;
2019-02-08 20:51:44 -08:00
if ( _children = = null | | _children . Count = = 0 )
{
2024-02-28 19:19:50 -05:00
_children = [ ] ;
2024-02-29 00:14:16 -05:00
#if NET20 | | NET35
2024-02-28 21:59:13 -05:00
foreach ( string file in Directory . GetFiles ( this . Filename , "*" ) )
#else
2019-02-08 20:51:44 -08:00
foreach ( string file in Directory . EnumerateFiles ( this . Filename , "*" , SearchOption . TopDirectoryOnly ) )
2024-02-28 21:59:13 -05:00
#endif
2019-02-08 20:51:44 -08:00
{
2024-03-04 23:56:05 -05:00
BaseFile ? nf = GetInfo ( file , hashes : this . AvailableHashTypes ) ;
2024-02-28 19:19:50 -05:00
if ( nf ! = null )
_children . Add ( nf ) ;
2019-02-08 20:51:44 -08:00
}
2020-06-10 22:37:19 -07:00
2024-02-29 00:14:16 -05:00
#if NET20 | | NET35
2024-02-28 21:59:13 -05:00
foreach ( string dir in Directory . GetDirectories ( this . Filename , "*" ) )
#else
2019-02-08 20:51:44 -08:00
foreach ( string dir in Directory . EnumerateDirectories ( this . Filename , "*" , SearchOption . TopDirectoryOnly ) )
2024-02-28 21:59:13 -05:00
#endif
2019-02-08 20:51:44 -08:00
{
2023-04-19 16:39:58 -04:00
Folder fl = new ( dir ) ;
2019-02-08 20:51:44 -08:00
_children . Add ( fl ) ;
}
}
return _children ;
}
/// <summary>
/// Generate a list of empty folders in an archive
/// </summary>
/// <param name="input">Input file to get data from</param>
/// <returns>List of empty folders in the folder</returns>
2024-02-28 19:19:50 -05:00
public virtual List < string > ? GetEmptyFolders ( )
2019-02-08 20:51:44 -08:00
{
2020-12-10 22:16:53 -08:00
return this . Filename . ListEmpty ( ) ;
2019-02-08 20:51:44 -08:00
}
#endregion
#region Writing
/// <summary>
/// Write an input file to an output folder
/// </summary>
/// <param name="inputFile">Input filename to be moved</param>
/// <param name="outDir">Output directory to build to</param>
2020-12-08 11:09:05 -08:00
/// <param name="baseFile">BaseFile representing the new information</param>
2019-02-08 20:51:44 -08:00
/// <returns>True if the write was a success, false otherwise</returns>
/// <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>
2024-02-28 19:19:50 -05:00
public virtual bool Write ( string inputFile , string outDir , BaseFile ? baseFile )
2019-02-08 20:51:44 -08:00
{
2020-12-08 00:13:22 -08:00
FileStream fs = File . OpenRead ( inputFile ) ;
2020-12-08 11:09:05 -08:00
return Write ( fs , outDir , baseFile ) ;
2019-02-08 20:51:44 -08:00
}
/// <summary>
/// Write an input stream to an output folder
/// </summary>
/// <param name="inputStream">Input stream to be moved</param>
/// <param name="outDir">Output directory to build to</param>
2020-12-08 11:09:05 -08:00
/// <param name="baseFile">BaseFile representing the new information</param>
2019-02-08 20:51:44 -08:00
/// <returns>True if the write was a success, false otherwise</returns>
/// <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>
2024-02-28 19:19:50 -05:00
public virtual bool Write ( Stream ? inputStream , string outDir , BaseFile ? baseFile )
2019-02-08 20:51:44 -08:00
{
bool success = false ;
// If either input is null or empty, return
2020-12-08 11:09:05 -08:00
if ( inputStream = = null | | baseFile = = null | | baseFile . Filename = = null )
2019-02-08 20:51:44 -08:00
return success ;
// If the stream is not readable, return
if ( ! inputStream . CanRead )
return success ;
// Set internal variables
2024-02-28 19:19:50 -05:00
FileStream ? outputStream = null ;
2019-02-08 20:51:44 -08:00
// Get the output folder name from the first rebuild rom
2020-08-28 20:46:12 -07:00
string fileName ;
if ( writeToParent )
2024-02-28 19:19:50 -05:00
fileName = Path . Combine ( outDir , TextHelper . RemovePathUnsafeCharacters ( baseFile . Filename ) ? ? string . Empty ) ;
2020-08-28 20:46:12 -07:00
else
2024-02-28 21:59:13 -05:00
#if NET20 | | NET35
fileName = Path . Combine ( Path . Combine ( outDir , TextHelper . RemovePathUnsafeCharacters ( baseFile . Parent ) ? ? string . Empty ) , TextHelper . RemovePathUnsafeCharacters ( baseFile . Filename ) ? ? string . Empty ) ;
#else
2024-02-28 19:19:50 -05:00
fileName = Path . Combine ( outDir , TextHelper . RemovePathUnsafeCharacters ( baseFile . Parent ) ? ? string . Empty , TextHelper . RemovePathUnsafeCharacters ( baseFile . Filename ) ? ? string . Empty ) ;
2024-02-28 21:59:13 -05:00
#endif
2019-02-08 20:51:44 -08:00
try
{
// If the full output path doesn't exist, create it
2024-02-28 19:19:50 -05:00
string? dir = Path . GetDirectoryName ( fileName ) ;
if ( dir ! = null & & ! Directory . Exists ( dir ) )
Directory . CreateDirectory ( dir ) ;
2019-02-08 20:51:44 -08:00
// Overwrite output files by default
2020-12-08 00:13:22 -08:00
outputStream = File . Create ( fileName ) ;
2019-02-08 20:51:44 -08:00
// If the output stream isn't null
if ( outputStream ! = null )
{
// Copy the input stream to the output
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 ( ) ;
}
2020-08-28 21:38:27 -07:00
2019-02-08 20:51:44 -08:00
outputStream . Dispose ( ) ;
2024-02-28 21:59:13 -05:00
if ( ! string . IsNullOrEmpty ( baseFile . Date ) )
2020-12-08 11:09:05 -08:00
File . SetCreationTime ( fileName , DateTime . Parse ( baseFile . Date ) ) ;
2019-02-08 20:51:44 -08:00
success = true ;
}
}
catch ( Exception ex )
{
2020-10-07 15:42:30 -07:00
logger . Error ( ex ) ;
2019-02-08 20:51:44 -08:00
success = false ;
}
finally
{
outputStream ? . Dispose ( ) ;
}
return success ;
}
/// <summary>
/// Write a set of input files to an output folder (assuming the same output archive name)
/// </summary>
/// <param name="inputFiles">Input files to be moved</param>
/// <param name="outDir">Output directory to build to</param>
2020-12-08 11:09:05 -08:00
/// <param name="baseFiles">BaseFiles representing the new information</param>
/// <returns>True if the inputs were written properly, false otherwise</returns>
2024-02-28 19:19:50 -05:00
public virtual bool Write ( List < string > inputFiles , string outDir , List < BaseFile > ? baseFiles )
2019-02-08 20:51:44 -08:00
{
throw new NotImplementedException ( ) ;
}
#endregion
}
2017-11-02 00:29:20 -07:00
}