| | 1 | | // <copyright file="ArchiveFileLocator.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.Collections.Generic; |
| | 12 | | using System.IO; |
| | 13 | | using SharpCompress.Archives; |
| | 14 | |
|
| | 15 | | /// <summary> |
| | 16 | | /// An archive file locator. |
| | 17 | | /// </summary> |
| | 18 | | internal sealed class ArchiveFileLocator : IFileLocator |
| | 19 | | { |
| | 20 | | /// <summary> |
| | 21 | | /// A file stream. |
| | 22 | | /// </summary> |
| | 23 | | private Stream? fileStream; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// an archive. |
| | 27 | | /// </summary> |
| | 28 | | private IArchive? archive; |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Initializes a new instance of the <see cref="ArchiveFileLocator"/> class. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="root">The root path.</param> |
| 0 | 34 | | public ArchiveFileLocator(string root) |
| | 35 | | { |
| 0 | 36 | | this.archive = ArchiveFactory.Open(root); |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Initializes a new instance of the <see cref="ArchiveFileLocator"/> class. |
| | 41 | | /// </summary> |
| | 42 | | /// <param name="locator">A file locator.</param> |
| | 43 | | /// <param name="root">The root path.</param> |
| 0 | 44 | | public ArchiveFileLocator(IFileLocator? locator, string root) |
| | 45 | | { |
| 0 | 46 | | if (locator != null) |
| | 47 | | { |
| 0 | 48 | | this.fileStream = locator.Open(root); |
| 0 | 49 | | this.archive = ArchiveFactory.Open(this.fileStream); |
| | 50 | | } |
| | 51 | | else |
| | 52 | | { |
| 0 | 53 | | this.archive = ArchiveFactory.Open(root); |
| | 54 | | } |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Initializes a new instance of the <see cref="ArchiveFileLocator"/> class. |
| | 59 | | /// </summary> |
| | 60 | | /// <param name="root">A stream.</param> |
| 0 | 61 | | public ArchiveFileLocator(Stream root) |
| | 62 | | { |
| 0 | 63 | | this.archive = ArchiveFactory.Open(root); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// Immediately releases the unmanaged resources used by the <see cref="ArchiveFileLocator"/> object. |
| | 68 | | /// </summary> |
| | 69 | | public void Dispose() |
| | 70 | | { |
| 0 | 71 | | if (this.fileStream != null) |
| | 72 | | { |
| 0 | 73 | | this.fileStream.Dispose(); |
| 0 | 74 | | this.fileStream = null; |
| | 75 | | } |
| | 76 | |
|
| 0 | 77 | | if (this.archive != null) |
| | 78 | | { |
| 0 | 79 | | this.archive.Dispose(); |
| 0 | 80 | | this.archive = null; |
| | 81 | | } |
| | 82 | |
|
| 0 | 83 | | GC.SuppressFinalize(this); |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// Indicates whether the specified path exists. |
| | 88 | | /// </summary> |
| | 89 | | /// <param name="path">A path.</param> |
| | 90 | | /// <returns>A boolean.</returns> |
| | 91 | | public bool Exists(string path) |
| | 92 | | { |
| 0 | 93 | | if (string.IsNullOrEmpty(path)) |
| | 94 | | { |
| 0 | 95 | | return false; |
| | 96 | | } |
| | 97 | |
|
| 0 | 98 | | foreach (var entry in this.archive!.Entries) |
| | 99 | | { |
| 0 | 100 | | if (entry.IsDirectory) |
| | 101 | | { |
| | 102 | | continue; |
| | 103 | | } |
| | 104 | |
|
| 0 | 105 | | if (Utilities.PathEquals(entry.Key, path)) |
| | 106 | | { |
| 0 | 107 | | return true; |
| | 108 | | } |
| | 109 | | } |
| | 110 | |
|
| 0 | 111 | | return false; |
| 0 | 112 | | } |
| | 113 | |
|
| | 114 | | /// <summary> |
| | 115 | | /// Open a file. |
| | 116 | | /// </summary> |
| | 117 | | /// <param name="path">A path.</param> |
| | 118 | | /// <returns>A stream.</returns> |
| | 119 | | public Stream Open(string path) |
| | 120 | | { |
| 0 | 121 | | if (string.IsNullOrEmpty(path)) |
| | 122 | | { |
| 0 | 123 | | throw new ArgumentNullException(nameof(path)); |
| | 124 | | } |
| | 125 | |
|
| 0 | 126 | | foreach (var entry in this.archive!.Entries) |
| | 127 | | { |
| 0 | 128 | | if (entry.IsDirectory) |
| | 129 | | { |
| | 130 | | continue; |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | if (Utilities.PathEquals(entry.Key, path)) |
| | 134 | | { |
| 0 | 135 | | return entry.OpenEntryStream(); |
| | 136 | | } |
| | 137 | | } |
| | 138 | |
|
| 0 | 139 | | throw new FileNotFoundException(); |
| 0 | 140 | | } |
| | 141 | |
|
| | 142 | | /// <summary> |
| | 143 | | /// Enumerate the files. |
| | 144 | | /// </summary> |
| | 145 | | /// <returns>An enumeration.</returns> |
| | 146 | | public IEnumerable<string> EnumerateFiles() |
| | 147 | | { |
| 0 | 148 | | return this.EnumerateFiles(string.Empty); |
| | 149 | | } |
| | 150 | |
|
| | 151 | | /// <summary> |
| | 152 | | /// Enumerate the files. |
| | 153 | | /// </summary> |
| | 154 | | /// <param name="root">The root path.</param> |
| | 155 | | /// <returns>An enumeration.</returns> |
| | 156 | | public IEnumerable<string> EnumerateFiles(string root) |
| | 157 | | { |
| 0 | 158 | | foreach (var entry in this.archive!.Entries) |
| | 159 | | { |
| 0 | 160 | | if (entry.IsDirectory) |
| | 161 | | { |
| | 162 | | continue; |
| | 163 | | } |
| | 164 | |
|
| 0 | 165 | | if (Utilities.PathStartsWith(entry.Key, root)) |
| | 166 | | { |
| 0 | 167 | | yield return Utilities.PathNormalize(entry.Key); |
| | 168 | | } |
| | 169 | | } |
| 0 | 170 | | } |
| | 171 | | } |
| | 172 | | } |