Flags were a better idea when I was younger. It's not clear though.

This commit is contained in:
Adam Hathcock
2016-09-27 10:50:36 +01:00
parent 5dafcb02d4
commit 177fc2a12c
14 changed files with 186 additions and 59 deletions

View File

@@ -142,7 +142,7 @@ namespace SharpCompress.Archives
/// Extract to specific directory, retaining filename
/// </summary>
public static void WriteToDirectory(string sourceArchive, string destinationDirectory,
ExtractOptions options = ExtractOptions.Overwrite)
ExtractionOptions options = null)
{
using (IArchive archive = Open(sourceArchive))
{

View File

@@ -1,6 +1,7 @@
using System.IO;
using SharpCompress.Common;
using SharpCompress.IO;
using SharpCompress.Readers;
namespace SharpCompress.Archives
{
@@ -43,13 +44,18 @@ namespace SharpCompress.Archives
/// Extract to specific directory, retaining filename
/// </summary>
public static void WriteToDirectory(this IArchiveEntry entry, string destinationDirectory,
ExtractOptions options = ExtractOptions.Overwrite)
ExtractionOptions options = null)
{
string destinationFileName;
string file = Path.GetFileName(entry.Key);
options = options ?? new ExtractionOptions()
{
Overwrite = true
};
if (options.HasFlag(ExtractOptions.ExtractFullPath))
if (options.ExtractFullPath)
{
string folder = Path.GetDirectoryName(entry.Key);
string destdir = Path.Combine(destinationDirectory, folder);
@@ -73,11 +79,16 @@ namespace SharpCompress.Archives
/// Extract to specific file
/// </summary>
public static void WriteToFile(this IArchiveEntry entry, string destinationFileName,
ExtractOptions options = ExtractOptions.Overwrite)
ExtractionOptions options = null)
{
FileMode fm = FileMode.Create;
options = options ?? new ExtractionOptions()
{
Overwrite = true
};
if (!options.HasFlag(ExtractOptions.Overwrite))
if (!options.Overwrite)
{
fm = FileMode.CreateNew;
}

View File

@@ -1,6 +1,7 @@
#if !NO_FILE
using System.Linq;
using SharpCompress.Common;
using SharpCompress.Readers;
#endif
namespace SharpCompress.Archives
@@ -13,7 +14,7 @@ namespace SharpCompress.Archives
/// Extract to specific directory, retaining filename
/// </summary>
public static void WriteToDirectory(this IArchive archive, string destinationDirectory,
ExtractOptions options = ExtractOptions.Overwrite)
ExtractionOptions options = null)
{
foreach (IArchiveEntry entry in archive.Entries.Where(x => !x.IsDirectory))
{

View File

@@ -1,15 +1,16 @@
#if !NO_FILE
using System.IO;
using SharpCompress.Readers;
namespace SharpCompress.Common
{
internal static class IEntryExtensions
{
internal static void PreserveExtractionOptions(this IEntry entry, string destinationFileName,
ExtractOptions options)
ExtractionOptions options)
{
if (options.HasFlag(ExtractOptions.PreserveFileTime) || options.HasFlag(ExtractOptions.PreserveAttributes))
if (options.PreserveFileTime || options.PreserveAttributes)
{
FileInfo nf = new FileInfo(destinationFileName);
if (!nf.Exists)
@@ -18,7 +19,7 @@ namespace SharpCompress.Common
}
// update file time to original packed time
if (options.HasFlag(ExtractOptions.PreserveFileTime))
if (options.PreserveFileTime)
{
if (entry.CreatedTime.HasValue)
{
@@ -36,7 +37,7 @@ namespace SharpCompress.Common
}
}
if (options.HasFlag(ExtractOptions.PreserveAttributes))
if (options.PreserveAttributes)
{
if (entry.Attrib.HasValue)
{

View File

@@ -1,30 +1,25 @@
using System;
namespace SharpCompress.Common
namespace SharpCompress.Readers
{
[Flags]
public enum ExtractOptions
public class ExtractionOptions
{
None = 0,
/// <summary>
/// overwrite target if it exists
/// </summary>
Overwrite = 1 << 0,
public bool Overwrite {get; set; }
/// <summary>
/// extract with internal directory structure
/// </summary>
ExtractFullPath = 1 << 1,
public bool ExtractFullPath { get; set; }
/// <summary>
/// preserve file time
/// </summary>
PreserveFileTime = 1 << 2,
public bool PreserveFileTime { get; set; }
/// <summary>
/// preserve windows file attributes
/// </summary>
PreserveAttributes = 1 << 3
public bool PreserveAttributes { get; set; }
}
}

View File

@@ -28,7 +28,7 @@ namespace SharpCompress.Readers
/// Extract all remaining unread entries to specific directory, retaining filename
/// </summary>
public static void WriteAllToDirectory(this IReader reader, string destinationDirectory,
ExtractOptions options = ExtractOptions.Overwrite)
ExtractionOptions options = null)
{
while (reader.MoveToNextEntry())
{
@@ -40,13 +40,16 @@ namespace SharpCompress.Readers
/// Extract to specific directory, retaining filename
/// </summary>
public static void WriteEntryToDirectory(this IReader reader, string destinationDirectory,
ExtractOptions options = ExtractOptions.Overwrite)
ExtractionOptions options = null)
{
string destinationFileName = string.Empty;
string file = Path.GetFileName(reader.Entry.Key);
options = options ?? new ExtractionOptions()
{
Overwrite = true
};
if (options.HasFlag(ExtractOptions.ExtractFullPath))
if (options.ExtractFullPath)
{
string folder = Path.GetDirectoryName(reader.Entry.Key);
string destdir = Path.Combine(destinationDirectory, folder);
@@ -65,7 +68,7 @@ namespace SharpCompress.Readers
{
reader.WriteEntryToFile(destinationFileName, options);
}
else if (options.HasFlag(ExtractOptions.ExtractFullPath) && !Directory.Exists(destinationFileName))
else if (options.ExtractFullPath && !Directory.Exists(destinationFileName))
{
Directory.CreateDirectory(destinationFileName);
}
@@ -75,11 +78,15 @@ namespace SharpCompress.Readers
/// Extract to specific file
/// </summary>
public static void WriteEntryToFile(this IReader reader, string destinationFileName,
ExtractOptions options = ExtractOptions.Overwrite)
ExtractionOptions options = null)
{
FileMode fm = FileMode.Create;
options = options ?? new ExtractionOptions()
{
Overwrite = true
};
if (!options.HasFlag(ExtractOptions.Overwrite))
if (!options.Overwrite)
{
fm = FileMode.CreateNew;
}

View File

@@ -1,5 +1,4 @@
using SharpCompress.Common;
using SharpCompress.Compressors.Deflate;
namespace SharpCompress.Writers
{

View File

@@ -4,6 +4,7 @@ using System.IO;
using System.Linq;
using SharpCompress.Archives;
using SharpCompress.Common;
using SharpCompress.Readers;
using Xunit;
namespace SharpCompress.Test
@@ -39,7 +40,11 @@ namespace SharpCompress.Test
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
{
entry.WriteToDirectory(SCRATCH_FILES_PATH,
ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
VerifyFiles();
@@ -67,7 +72,11 @@ namespace SharpCompress.Test
{
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
{
entry.WriteToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
VerifyFiles();
@@ -93,7 +102,11 @@ namespace SharpCompress.Test
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
{
entry.WriteToDirectory(SCRATCH_FILES_PATH,
ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
VerifyFiles();
@@ -151,7 +164,13 @@ namespace SharpCompress.Test
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
{
entry.WriteToDirectory(SCRATCH_FILES_PATH,
ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite | ExtractOptions.PreserveFileTime | ExtractOptions.PreserveAttributes);
new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true,
PreserveAttributes = true,
PreserveFileTime = true
});
}
}
VerifyFilesEx();

View File

@@ -45,7 +45,11 @@ namespace SharpCompress.Test
if (!entry.IsDirectory)
{
Assert.Equal(entry.CompressionType, CompressionType.Rar);
entry.WriteToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
}
@@ -70,7 +74,11 @@ namespace SharpCompress.Test
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
{
entry.WriteToDirectory(SCRATCH_FILES_PATH,
ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
VerifyFiles();
@@ -98,7 +106,11 @@ namespace SharpCompress.Test
{
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
{
entry.WriteToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
}
@@ -118,7 +130,11 @@ namespace SharpCompress.Test
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
{
entry.WriteToDirectory(SCRATCH_FILES_PATH,
ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
VerifyFiles();
@@ -137,7 +153,11 @@ namespace SharpCompress.Test
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
{
entry.WriteToDirectory(SCRATCH_FILES_PATH,
ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
}
@@ -173,7 +193,11 @@ namespace SharpCompress.Test
{
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
{
entry.WriteToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
VerifyFiles();
@@ -216,7 +240,11 @@ namespace SharpCompress.Test
{
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
{
entry.WriteToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
VerifyFiles();

View File

@@ -26,7 +26,11 @@ namespace SharpCompress.Test
{
while (reader.MoveToNextEntry())
{
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
VerifyFiles();
@@ -49,7 +53,11 @@ namespace SharpCompress.Test
{
while (reader.MoveToNextEntry())
{
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
VerifyFiles();
@@ -77,7 +85,11 @@ namespace SharpCompress.Test
{
while (reader.MoveToNextEntry())
{
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
foreach (var stream in streams)
@@ -138,7 +150,11 @@ namespace SharpCompress.Test
if (!reader.Entry.IsDirectory)
{
Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
}
@@ -192,7 +208,11 @@ namespace SharpCompress.Test
while (reader.MoveToNextEntry())
{
Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
CompareFilesByPath(Path.Combine(SCRATCH_FILES_PATH, "test.dat"),
@@ -212,7 +232,11 @@ namespace SharpCompress.Test
while (reader.MoveToNextEntry())
{
Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
VerifyFiles();
@@ -239,7 +263,11 @@ namespace SharpCompress.Test
if (reader.Entry.Key.Contains("jpg"))
{
Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
}
@@ -260,7 +288,11 @@ namespace SharpCompress.Test
if (reader.Entry.Key.Contains("jpg"))
{
Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
}

View File

@@ -34,7 +34,11 @@ namespace SharpCompress.Test
if (!reader.Entry.IsDirectory)
{
Assert.Equal(reader.Entry.CompressionType, expectedCompression);
reader.WriteEntryToDirectory(test.SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
reader.WriteEntryToDirectory(test.SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
test.VerifyFiles();

View File

@@ -39,7 +39,10 @@ namespace SharpCompress.Test
using (Stream stream = File.OpenRead(Path.Combine(SCRATCH2_FILES_PATH, archive)))
using (var reader = ReaderFactory.Open(new NonDisposingStream(stream)))
{
reader.WriteAllToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath);
reader.WriteAllToDirectory(SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true
});
}
VerifyFiles();
}

View File

@@ -303,7 +303,11 @@ namespace SharpCompress.Test
{
foreach (var entry in reader.Entries.Where(x => !x.IsDirectory))
{
entry.WriteToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
VerifyFiles();
@@ -321,7 +325,11 @@ namespace SharpCompress.Test
{
foreach (var entry in reader.Entries.Where(x => !x.IsDirectory))
{
entry.WriteToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
entry.WriteToDirectory(SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
VerifyFiles();

View File

@@ -94,7 +94,11 @@ namespace SharpCompress.Test
if (!reader.Entry.IsDirectory)
{
Assert.Equal(reader.Entry.CompressionType, CompressionType.BZip2);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
}
@@ -114,7 +118,11 @@ namespace SharpCompress.Test
if (!reader.Entry.IsDirectory)
{
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH,
ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
}
@@ -134,7 +142,11 @@ namespace SharpCompress.Test
if (!reader.Entry.IsDirectory)
{
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH,
ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
Assert.False(stream.IsDisposed);
@@ -163,8 +175,11 @@ namespace SharpCompress.Test
Assert.Equal(reader.Entry.CompressionType,
CompressionType.Unknown);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH,
ExtractOptions.ExtractFullPath
| ExtractOptions.Overwrite);
new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
}
@@ -188,7 +203,11 @@ namespace SharpCompress.Test
{
Assert.Equal(reader.Entry.CompressionType, CompressionType.Unknown);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH,
ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
new ExtractOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
}