Skip to content

Commit

Permalink
Merge pull request #402 from a764578566/master
Browse files Browse the repository at this point in the history
file search support linq Pattern
  • Loading branch information
adamhathcock committed Jul 10, 2018
2 parents 4640ca4 + 4ebc1f8 commit 09c2768
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/SharpCompress/Writers/IWriterExtensions.cs
Expand Up @@ -2,6 +2,8 @@
using System;
#endif
using System.IO;
using System.Linq;
using System.Linq.Expressions;

namespace SharpCompress.Writers
{
Expand Down Expand Up @@ -30,22 +32,33 @@ public static void Write(this IWriter writer, string entryPath, string source)
writer.Write(entryPath, new FileInfo(source));
}

public static void WriteAll(this IWriter writer, string directory, string searchPattern = "*",
public static void WriteAll(this IWriter writer, string directory, string searchPattern = "*", SearchOption option = SearchOption.TopDirectoryOnly)
{
writer.WriteAll(directory, searchPattern, null, option);
}

public static void WriteAll(this IWriter writer, string directory, string searchPattern = "*", Expression<Func<string, bool>> fileSearchFunc = null,
SearchOption option = SearchOption.TopDirectoryOnly)
{
if (!Directory.Exists(directory))
{
throw new ArgumentException("Directory does not exist: " + directory);
}

if (fileSearchFunc == null)
{
fileSearchFunc = n => true;
}
#if NET35
foreach (var file in Directory.GetDirectories(directory, searchPattern, option))
foreach (var file in Directory.GetDirectories(directory, searchPattern, option).Where(fileSearchFunc.Compile()))
#else
foreach (var file in Directory.EnumerateFiles(directory, searchPattern, option))
foreach (var file in Directory.EnumerateFiles(directory, searchPattern, option).Where(fileSearchFunc.Compile()))
#endif
{
writer.Write(file.Substring(directory.Length), file);
}
}

#endif
}
}

0 comments on commit 09c2768

Please sign in to comment.