Files
sharpcompress/SharpCompress/Archive/AbstractWritableArchive.Extensions.cs

85 lines
3.8 KiB
C#
Raw Permalink Normal View History

2013-04-07 10:58:58 +01:00
using System.IO;
using SharpCompress.Common;
namespace SharpCompress.Archive
{
public static class AbstractWritableArchiveExtensions
{
2013-04-28 12:32:55 +01:00
public static void SaveTo<TEntry, TVolume>(this AbstractWritableArchive<TEntry, TVolume> writableArchive,
Stream stream, CompressionType compressionType)
where TEntry : IArchiveEntry
where TVolume : IVolume
{
writableArchive.SaveTo(stream, new CompressionInfo {Type = compressionType});
}
2013-04-07 10:58:58 +01:00
2013-04-28 13:01:29 +01:00
#if !PORTABLE && !NETFX_CORE
2013-04-07 10:58:58 +01:00
public static void AddEntry<TEntry, TVolume>(this AbstractWritableArchive<TEntry, TVolume> writableArchive,
2013-04-28 12:32:55 +01:00
string entryPath, string filePath)
2013-04-07 10:58:58 +01:00
where TEntry : IArchiveEntry
where TVolume : IVolume
2013-04-28 12:32:55 +01:00
{
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);
2013-04-07 10:58:58 +01:00
}
public static void SaveTo<TEntry, TVolume>(this AbstractWritableArchive<TEntry, TVolume> writableArchive,
2013-04-28 12:32:55 +01:00
string filePath, CompressionType compressionType)
2013-04-07 10:58:58 +01:00
where TEntry : IArchiveEntry
where TVolume : IVolume
{
2013-04-28 12:32:55 +01:00
writableArchive.SaveTo(new FileInfo(filePath), new CompressionInfo {Type = compressionType});
2013-04-07 10:58:58 +01:00
}
public static void SaveTo<TEntry, TVolume>(this AbstractWritableArchive<TEntry, TVolume> writableArchive,
2013-04-28 12:32:55 +01:00
FileInfo fileInfo, CompressionType compressionType)
2013-04-07 10:58:58 +01:00
where TEntry : IArchiveEntry
where TVolume : IVolume
{
using (var stream = fileInfo.Open(FileMode.Create, FileAccess.Write))
{
2013-04-28 12:32:55 +01:00
writableArchive.SaveTo(stream, new CompressionInfo {Type = compressionType});
2013-04-07 10:58:58 +01:00
}
}
public static void SaveTo<TEntry, TVolume>(this AbstractWritableArchive<TEntry, TVolume> writableArchive,
2013-04-28 12:32:55 +01:00
string filePath, CompressionInfo compressionInfo)
where TEntry : IArchiveEntry
where TVolume : IVolume
2013-04-07 10:58:58 +01:00
{
2013-04-28 12:32:55 +01:00
writableArchive.SaveTo(new FileInfo(filePath), compressionInfo);
2013-04-07 10:58:58 +01:00
}
public static void SaveTo<TEntry, TVolume>(this AbstractWritableArchive<TEntry, TVolume> writableArchive,
2013-04-28 12:32:55 +01:00
FileInfo fileInfo, CompressionInfo compressionInfo)
where TEntry : IArchiveEntry
where TVolume : IVolume
2013-04-07 10:58:58 +01:00
{
2013-04-28 12:32:55 +01:00
using (var stream = fileInfo.Open(FileMode.Create, FileAccess.Write))
{
writableArchive.SaveTo(stream, compressionInfo);
}
2013-04-07 10:58:58 +01:00
}
2013-04-28 12:32:55 +01:00
public static void AddAllFromDirectory<TEntry, TVolume>(
this AbstractWritableArchive<TEntry, TVolume> writableArchive,
2013-04-07 10:58:58 +01:00
string filePath, string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
where TEntry : IArchiveEntry
where TVolume : IVolume
{
foreach (var path in Directory.EnumerateFiles(filePath, searchPattern, searchOption))
{
2013-04-28 12:32:55 +01:00
var fileInfo = new FileInfo(path);
writableArchive.AddEntry(path.Substring(filePath.Length), fileInfo.OpenRead(), true, fileInfo.Length,
fileInfo.LastWriteTime);
2013-04-07 10:58:58 +01:00
}
}
#endif
}
2013-04-28 12:32:55 +01:00
}