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 ;
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 ;
2024-10-24 00:36:44 -04:00
using SabreTools.IO.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>
2024-10-19 23:17:37 -04: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 ( )
{
2024-10-19 23:17:37 -04:00
_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 )
{
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 . TorrentRar = > new RarArchive ( ) ,
OutputFormat . TorrentXZ = > new XZArchive ( ) ,
OutputFormat . TorrentXZRomba = > new XZArchive ( ) ,
OutputFormat . TorrentZip = > new ZipArchive ( ) ,
_ = > 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
2024-10-19 23:17:37 -04:00
if ( Filename = = null )
2024-02-28 19:19:50 -05:00
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
2024-10-19 23:17:37 -04:00
Directory . CreateDirectory ( Filename ) ;
2019-02-08 20:51:44 -08:00
Directory . CreateDirectory ( outDir ) ;
2024-10-19 23:17:37 -04:00
DirectoryCopy ( 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
2024-10-19 23:17:37 -04:00
if ( Filename = = null )
2024-02-28 19:19:50 -05:00
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
2024-10-19 23:17:37 -04:00
Directory . CreateDirectory ( Filename ) ;
2019-02-08 20:51:44 -08:00
Directory . CreateDirectory ( outDir ) ;
// Get all files from the input directory
2024-10-19 23:17:37 -04:00
List < string > files = PathTool . GetFilesOrdered ( Filename ) ;
2019-02-08 20:51:44 -08:00
// Now sort through to find the first file that matches
2024-11-12 21:12:06 -05:00
string? match = files . Find ( s = > s . EndsWith ( entryName ) ) ;
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>
2024-07-15 21:37:38 -04:00
/// <returns>Stream representing the entry, null on error</returns>
public virtual ( Stream ? , string? ) GetEntryStream ( string entryName )
2019-02-08 20:51:44 -08:00
{
2024-02-28 19:19:50 -05:00
// If we have an invalid filename
2024-10-19 23:17:37 -04:00
if ( Filename = = null )
2024-02-28 19:19:50 -05:00
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
2024-10-19 23:17:37 -04:00
Directory . CreateDirectory ( Filename ) ;
2019-02-08 20:51:44 -08:00
// Get all files from the input directory
2024-10-19 23:17:37 -04:00
List < string > files = PathTool . GetFilesOrdered ( Filename ) ;
2019-02-08 20:51:44 -08:00
// Now sort through to find the first file that matches
2024-11-12 21:12:06 -05:00
string? match = files . Find ( s = > s . EndsWith ( entryName ) ) ;
2019-02-08 20:51:44 -08:00
2024-07-16 14:58:04 -04:00
// If we had a file, open and return the stream
2024-02-28 21:59:13 -05:00
if ( ! string . IsNullOrEmpty ( match ) )
2019-02-08 20:51:44 -08:00
{
2024-07-16 14:58:04 -04:00
var stream = File . OpenRead ( match ) ;
return ( stream , match ) ;
2019-02-08 20:51:44 -08:00
}
2024-07-16 14:58:04 -04:00
return ( null , null ) ;
2019-02-08 20:51:44 -08:00
}
catch ( Exception ex )
{
2020-10-07 15:42:30 -07:00
logger . Error ( ex ) ;
2024-07-16 14:58:04 -04:00
return ( null , null ) ;
2019-02-08 20:51:44 -08:00
}
}
#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
2024-10-19 23:17:37 -04:00
if ( Filename = = null )
2024-02-28 19:19:50 -05:00
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-10-19 23:17:37 -04:00
foreach ( string file in Directory . GetFiles ( Filename , "*" ) )
2024-02-28 21:59:13 -05:00
#else
2024-10-19 23:17:37 -04:00
foreach ( string file in Directory . EnumerateFiles ( Filename , "*" , SearchOption . TopDirectoryOnly ) )
2024-02-28 21:59:13 -05:00
#endif
2019-02-08 20:51:44 -08:00
{
2024-10-19 23:17:37 -04:00
BaseFile ? nf = GetInfo ( file , hashes : 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-10-19 23:17:37 -04:00
foreach ( string dir in Directory . GetDirectories ( Filename , "*" ) )
2024-02-28 21:59:13 -05:00
#else
2024-10-19 23:17:37 -04:00
foreach ( string dir in Directory . EnumerateDirectories ( 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
{
2024-10-19 23:17:37 -04:00
return 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
{
// If either input is null or empty, return
2020-12-08 11:09:05 -08:00
if ( inputStream = = null | | baseFile = = null | | baseFile . Filename = = null )
2024-07-17 15:19:15 -04:00
return false ;
2019-02-08 20:51:44 -08:00
// If the stream is not readable, return
if ( ! inputStream . CanRead )
2024-07-17 15:19:15 -04:00
return false ;
2019-02-08 20:51:44 -08:00
// 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 ;
2024-10-19 23:17:37 -04:00
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
2024-07-15 13:42:49 -04:00
// Replace any incorrect directory characters
if ( Path . DirectorySeparatorChar = = '\\' )
fileName = fileName . Replace ( '/' , '\\' ) ;
else if ( Path . DirectorySeparatorChar = = '/' )
fileName = fileName . Replace ( '\\' , '/' ) ;
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 ) ;
2024-07-17 15:19:15 -04:00
if ( outputStream = = null )
return false ;
2019-02-08 20:51:44 -08:00
2024-07-17 15:19:15 -04:00
if ( inputStream . CanSeek )
inputStream . Seek ( 0 , SeekOrigin . Begin ) ;
2024-07-16 15:09:51 -04:00
2024-07-17 15:19:15 -04:00
// Copy the input stream to the output
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
2024-07-17 15:19:15 -04:00
outputStream . Dispose ( ) ;
2019-02-08 20:51:44 -08:00
2024-07-18 00:00:23 -04:00
// Try to set the creation time
try
{
if ( ! string . IsNullOrEmpty ( baseFile . Date ) )
File . SetCreationTime ( fileName , DateTime . Parse ( baseFile . Date ) ) ;
}
catch { }
2019-02-08 20:51:44 -08:00
2024-07-17 15:19:15 -04:00
return true ;
2019-02-08 20:51:44 -08:00
}
catch ( Exception ex )
{
2020-10-07 15:42:30 -07:00
logger . Error ( ex ) ;
2024-07-17 15:19:15 -04:00
return false ;
2019-02-08 20:51:44 -08:00
}
finally
{
outputStream ? . Dispose ( ) ;
}
}
/// <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
}