mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-07-21 08:25:22 +00:00
* Remove NET35, NET45 and NET Standard 1.0 * Update README and memset * Remove NETCORE build flag * NET 46 too? * Update packages and usage
54 lines
2.2 KiB
C#
54 lines
2.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using SharpCompress.Writers;
|
|
|
|
namespace SharpCompress.Archives
|
|
{
|
|
public static class IWritableArchiveExtensions
|
|
{
|
|
public static void AddEntry(this IWritableArchive writableArchive,
|
|
string entryPath, string filePath)
|
|
{
|
|
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);
|
|
}
|
|
|
|
public static void SaveTo(this IWritableArchive writableArchive, string filePath, WriterOptions options)
|
|
{
|
|
writableArchive.SaveTo(new FileInfo(filePath), options);
|
|
}
|
|
|
|
public static void SaveTo(this IWritableArchive writableArchive, FileInfo fileInfo, WriterOptions options)
|
|
{
|
|
using (var stream = fileInfo.Open(FileMode.Create, FileAccess.Write))
|
|
{
|
|
writableArchive.SaveTo(stream, options);
|
|
}
|
|
}
|
|
|
|
public static void AddAllFromDirectory(
|
|
this IWritableArchive writableArchive,
|
|
string filePath, string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
|
|
{
|
|
foreach (var path in Directory.EnumerateFiles(filePath, searchPattern, searchOption))
|
|
{
|
|
var fileInfo = new FileInfo(path);
|
|
writableArchive.AddEntry(path.Substring(filePath.Length), fileInfo.OpenRead(), true, fileInfo.Length,
|
|
fileInfo.LastWriteTime);
|
|
}
|
|
}
|
|
public static IArchiveEntry AddEntry(this IWritableArchive writableArchive, string key, FileInfo fileInfo)
|
|
{
|
|
if (!fileInfo.Exists)
|
|
{
|
|
throw new ArgumentException("FileInfo does not exist.");
|
|
}
|
|
return writableArchive.AddEntry(key, fileInfo.OpenRead(), true, fileInfo.Length, fileInfo.LastWriteTime);
|
|
}
|
|
}
|
|
} |