using System.IO;
#if NET452_OR_GREATER || NETCOREAPP
using System.IO.Compression;
#endif
using System.Linq;
using System.Text.RegularExpressions;
namespace MPF.Processors
{
///
/// Represents a single output file with a Regex-matched name
///
internal class RegexOutputFile : OutputFile
{
///
/// Create an OutputFile with a single filename
///
public RegexOutputFile(string filename, OutputFileFlags flags)
: base([filename], flags)
{
}
///
/// Create an OutputFile with a single filename
///
public RegexOutputFile(string filename, OutputFileFlags flags, string artifactKey)
: base([filename], flags, artifactKey)
{
}
///
/// Create an OutputFile with set of filenames
///
public RegexOutputFile(string[] filenames, OutputFileFlags flags)
: base(filenames, flags)
{
}
///
/// Create an OutputFile with set of filenames
///
public RegexOutputFile(string[] filenames, OutputFileFlags flags, string artifactKey)
: base(filenames, flags, artifactKey)
{
}
///
public override bool Exists(string baseDirectory)
{
// Ensure the directory exists
if (!Directory.Exists(baseDirectory))
return false;
// Get list of all files in directory
var directoryFiles = Directory.GetFiles(baseDirectory);
foreach (string file in directoryFiles)
{
if (Filenames.Any(pattern => Regex.IsMatch(file, pattern)))
return true;
}
return false;
}
#if NET452_OR_GREATER || NETCOREAPP
///
/// Indicates if an output file exists in an archive
///
/// Zip archive to check in
public override bool Exists(ZipArchive? archive)
{
// If the archive is invalid
if (archive == null)
return false;
// Get list of all files in archive
var archiveFiles = archive.Entries.Select(e => e.Name).ToList();
foreach (string file in archiveFiles)
{
if (Filenames.Any(pattern => Regex.IsMatch(file, pattern)))
return true;
}
return false;
}
#endif
}
}