| | 1 | | // <copyright file="WritableFileLocatorFactory.cs" company="Jérémy Ansel"> |
| | 2 | | // Copyright (c) 2015, 2019 Jérémy Ansel |
| | 3 | | // </copyright> |
| | 4 | | // <license> |
| | 5 | | // Licensed under the MIT license. See LICENSE.txt |
| | 6 | | // </license> |
| | 7 | |
|
| | 8 | | namespace JeremyAnsel.IO.Locator |
| | 9 | | { |
| | 10 | | using System; |
| | 11 | | using System.Diagnostics.CodeAnalysis; |
| | 12 | | using System.IO; |
| | 13 | | using SharpCompress.Common; |
| | 14 | |
|
| | 15 | | /// <summary> |
| | 16 | | /// A factory to create a writable file locator. |
| | 17 | | /// <para> |
| | 18 | | /// The supported formats are: file system, Zip, Tar, GZip. |
| | 19 | | /// </para> |
| | 20 | | /// </summary> |
| | 21 | | public static class WritableFileLocatorFactory |
| | 22 | | { |
| | 23 | | /// <summary> |
| | 24 | | /// Creates a writable file locator. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="root">The root path.</param> |
| | 27 | | /// <returns>A writable file locator.</returns> |
| | 28 | | [SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase", Justification = "Reviewed")] |
| | 29 | | public static IWritableFileLocator Create(string root) |
| | 30 | | { |
| 0 | 31 | | if (string.IsNullOrEmpty(root)) |
| | 32 | | { |
| 0 | 33 | | throw new ArgumentNullException(nameof(root)); |
| | 34 | | } |
| | 35 | |
|
| 0 | 36 | | string ext = Path.GetExtension(root); |
| | 37 | |
|
| 0 | 38 | | if (!string.IsNullOrEmpty(ext)) |
| | 39 | | { |
| 0 | 40 | | switch (ext.ToLowerInvariant()) |
| | 41 | | { |
| | 42 | | case ".zip": |
| 0 | 43 | | return new ArchiveWritableFileLocator(root, ArchiveType.Zip, CompressionType.Deflate); |
| | 44 | |
|
| | 45 | | case ".gz": |
| 0 | 46 | | return new ArchiveWritableFileLocator(root, ArchiveType.GZip, CompressionType.GZip); |
| | 47 | |
|
| | 48 | | default: |
| 0 | 49 | | throw new NotSupportedException(); |
| | 50 | | } |
| | 51 | | } |
| | 52 | |
|
| 0 | 53 | | return new SystemWritableFileLocator(root); |
| | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Creates a writable file locator for an archive. |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="root">The root path.</param> |
| | 60 | | /// <param name="archiveType">The archive type.</param> |
| | 61 | | /// <param name="compressionType">The compression type.</param> |
| | 62 | | /// <returns>A writable file locator.</returns> |
| | 63 | | public static IWritableFileLocator CreateArchive(string root, ArchiveType archiveType, CompressionType compressi |
| | 64 | | { |
| 0 | 65 | | if (string.IsNullOrEmpty(root)) |
| | 66 | | { |
| 0 | 67 | | throw new ArgumentNullException(nameof(root)); |
| | 68 | | } |
| | 69 | |
|
| 0 | 70 | | return new ArchiveWritableFileLocator(root, archiveType, compressionType); |
| | 71 | | } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Creates a writable file locator for an archive. |
| | 75 | | /// </summary> |
| | 76 | | /// <param name="root">The root path.</param> |
| | 77 | | /// <param name="archiveType">The archive type.</param> |
| | 78 | | /// <returns>A writable file locator.</returns> |
| | 79 | | public static IWritableFileLocator CreateArchive(string root, ArchiveType archiveType) |
| | 80 | | { |
| 0 | 81 | | if (string.IsNullOrEmpty(root)) |
| | 82 | | { |
| 0 | 83 | | throw new ArgumentNullException(nameof(root)); |
| | 84 | | } |
| | 85 | |
|
| 0 | 86 | | return new ArchiveWritableFileLocator(root, archiveType, CompressionType.Deflate); |
| | 87 | | } |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// Creates a writable file locator for an archive from a stream. |
| | 91 | | /// </summary> |
| | 92 | | /// <param name="root">A stream</param> |
| | 93 | | /// <param name="archiveType">The archive type.</param> |
| | 94 | | /// <param name="compressionType">The compression type.</param> |
| | 95 | | /// <returns>A writable file locator.</returns> |
| | 96 | | public static IWritableFileLocator CreateArchive(Stream root, ArchiveType archiveType, CompressionType compressi |
| | 97 | | { |
| 0 | 98 | | if (root == null) |
| | 99 | | { |
| 0 | 100 | | throw new ArgumentNullException(nameof(root)); |
| | 101 | | } |
| | 102 | |
|
| 0 | 103 | | return new ArchiveWritableFileLocator(root, archiveType, compressionType); |
| | 104 | | } |
| | 105 | |
|
| | 106 | | /// <summary> |
| | 107 | | /// Creates a writable file locator for an archive from a stream. |
| | 108 | | /// </summary> |
| | 109 | | /// <param name="root">A stream.</param> |
| | 110 | | /// <param name="archiveType">The archive type.</param> |
| | 111 | | /// <returns>A writable file locator.</returns> |
| | 112 | | public static IWritableFileLocator CreateArchive(Stream root, ArchiveType archiveType) |
| | 113 | | { |
| 0 | 114 | | if (root == null) |
| | 115 | | { |
| 0 | 116 | | throw new ArgumentNullException(nameof(root)); |
| | 117 | | } |
| | 118 | |
|
| 0 | 119 | | return new ArchiveWritableFileLocator(root, archiveType, CompressionType.Deflate); |
| | 120 | | } |
| | 121 | | } |
| | 122 | | } |