Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddAllFromDirectoryContentOnly extension method for IWritableArchive #450

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions src/SharpCompress/Archives/IWritableArchiveExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
#endif
using System.IO;
using System.Linq;
using SharpCompress.Writers;

namespace SharpCompress.Archives
Expand All @@ -10,16 +11,20 @@ public static class IWritableArchiveExtensions
{
#if !NO_FILE

public static void AddEntry(this IWritableArchive writableArchive,
string entryPath, string filePath)
public static void AddEntry(
this IWritableArchive writableArchive,
string entryPath,
string filePath,
bool contentOnly = false)
{
var fileInfo = new FileInfo(filePath);
if (!fileInfo.Exists)
{
throw new FileNotFoundException("Could not AddEntry: " + filePath);
}
writableArchive.AddEntry(entryPath, new FileInfo(filePath).OpenRead(), true, fileInfo.Length,
fileInfo.LastWriteTime);

var modified = contentOnly ? (DateTime?)null : fileInfo.LastWriteTime;
writableArchive.AddEntry(entryPath, new FileInfo(filePath).OpenRead(), true, fileInfo.Length, modified);
}

public static void SaveTo(this IWritableArchive writableArchive, string filePath, WriterOptions options)
Expand All @@ -37,27 +42,45 @@ public static void SaveTo(this IWritableArchive writableArchive, FileInfo fileIn

public static void AddAllFromDirectory(
this IWritableArchive writableArchive,
string filePath, string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
string filePath,
string searchPattern = "*.*",
SearchOption searchOption = SearchOption.AllDirectories,
bool contentOnly = false)
{
#if NET35
foreach (var path in Directory.GetFiles(filePath, searchPattern, searchOption))
var paths = Directory.GetFiles(filePath, searchPattern, searchOption);
#else
var paths = Directory.EnumerateFiles(filePath, searchPattern, searchOption);
#endif
if (contentOnly)
{
#if NET35
Array.Sort(paths);
#else
foreach (var path in Directory.EnumerateFiles(filePath, searchPattern, searchOption))
var pathList = paths.ToList();
pathList.Sort();
paths = pathList;
#endif
}

foreach (var path in paths)
{
var fileInfo = new FileInfo(path);
writableArchive.AddEntry(path.Substring(filePath.Length), fileInfo.OpenRead(), true, fileInfo.Length,
fileInfo.LastWriteTime);
var modified = contentOnly ? (DateTime?)null : fileInfo.LastWriteTime;
writableArchive.AddEntry(path.Substring(filePath.Length), fileInfo.OpenRead(), true, fileInfo.Length, modified);
}
}
public static IArchiveEntry AddEntry(this IWritableArchive writableArchive, string key, FileInfo fileInfo)
public static IArchiveEntry AddEntry(this IWritableArchive writableArchive, string key, FileInfo fileInfo, bool contentOnly = false)
{
if (!fileInfo.Exists)
{
throw new ArgumentException("FileInfo does not exist.");
}
return writableArchive.AddEntry(key, fileInfo.OpenRead(), true, fileInfo.Length, fileInfo.LastWriteTime);

var modified = contentOnly ? (DateTime?)null : fileInfo.LastWriteTime;
return writableArchive.AddEntry(key, fileInfo.OpenRead(), true, fileInfo.Length, modified);
}

#endif
}
}
44 changes: 33 additions & 11 deletions src/SharpCompress/Writers/IWriterExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if !NO_FILE
using System;
#endif
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
Expand All @@ -15,30 +16,41 @@ public static void Write(this IWriter writer, string entryPath, Stream source)
}

#if !NO_FILE
public static void Write(this IWriter writer, string entryPath, FileInfo source)
public static void Write(this IWriter writer, string entryPath, FileInfo source, bool contentOnly = false)
{
if (!source.Exists)
{
throw new ArgumentException("Source does not exist: " + source.FullName);
}
using (var stream = source.OpenRead())
{
writer.Write(entryPath, stream, source.LastWriteTime);
var modified = contentOnly ? (DateTime?)null : source.LastWriteTime;
writer.Write(entryPath, stream, modified);
}
}

public static void Write(this IWriter writer, string entryPath, string source)
public static void Write(this IWriter writer, string entryPath, string source, bool contentOnly = false)
{
writer.Write(entryPath, new FileInfo(source));
writer.Write(entryPath, new FileInfo(source), contentOnly);
}

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

public static void WriteAll(this IWriter writer, string directory, string searchPattern = "*", Expression<Func<string, bool>> fileSearchFunc = null,
SearchOption option = SearchOption.TopDirectoryOnly)
public static void WriteAll(
this IWriter writer,
string directory,
string searchPattern = "*",
Expression<Func<string, bool>> fileSearchFunc = null,
SearchOption option = SearchOption.TopDirectoryOnly,
bool contentOnly = false)
{
if (!Directory.Exists(directory))
{
Expand All @@ -50,12 +62,22 @@ public static void WriteAll(this IWriter writer, string directory, string search
fileSearchFunc = n => true;
}
#if NET35
foreach (var file in Directory.GetDirectories(directory, searchPattern, option).Where(fileSearchFunc.Compile()))
IEnumerable<string> paths = Directory.GetFiles(directory, searchPattern, option);
#else
foreach (var file in Directory.EnumerateFiles(directory, searchPattern, option).Where(fileSearchFunc.Compile()))
IEnumerable<string> paths = Directory.EnumerateFiles(directory, searchPattern, option);
#endif
paths = paths.Where(fileSearchFunc.Compile());

if (contentOnly)
{
var pathList = paths.ToList();
pathList.Sort();
paths = pathList;
}

foreach (var path in paths)
{
writer.Write(file.Substring(directory.Length), file);
writer.Write(path.Substring(directory.Length), path, contentOnly);
}
}

Expand Down