| | 1 | | // <copyright file="ArchiveWritableFileLocator.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.Common; |
| | 14 | | using SharpCompress.Writers; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// A writable archive file locator. |
| | 18 | | /// </summary> |
| | 19 | | internal sealed class ArchiveWritableFileLocator : IWritableFileLocator |
| | 20 | | { |
| | 21 | | /// <summary> |
| | 22 | | /// A file stream. |
| | 23 | | /// </summary> |
| | 24 | | private Stream fileStream; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// An archive. |
| | 28 | | /// </summary> |
| | 29 | | private IWriter archive; |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// The file keys. |
| | 33 | | /// </summary> |
| | 34 | | private SortedSet<string> keys; |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Initializes a new instance of the <see cref="ArchiveWritableFileLocator"/> class. |
| | 38 | | /// </summary> |
| | 39 | | /// <param name="root">The root path.</param> |
| | 40 | | /// <param name="archiveType">The archive type.</param> |
| | 41 | | /// <param name="compressionType">The compression type.</param> |
| 0 | 42 | | public ArchiveWritableFileLocator(string root, ArchiveType archiveType, CompressionType compressionType) |
| | 43 | | { |
| 0 | 44 | | this.keys = new SortedSet<string>(); |
| | 45 | |
|
| 0 | 46 | | using (var memory = new MemoryFileLocator()) |
| | 47 | | { |
| 0 | 48 | | if (File.Exists(root)) |
| | 49 | | { |
| 0 | 50 | | long length = 0; |
| | 51 | |
|
| 0 | 52 | | using (var file = File.OpenRead(root)) |
| | 53 | | { |
| 0 | 54 | | length = file.Length; |
| 0 | 55 | | } |
| | 56 | |
|
| 0 | 57 | | if (length != 0) |
| | 58 | | { |
| 0 | 59 | | using (var reader = FileLocatorFactory.Create(root)) |
| | 60 | | { |
| 0 | 61 | | memory.WriteAll(reader); |
| 0 | 62 | | } |
| | 63 | | } |
| | 64 | | } |
| | 65 | |
|
| 0 | 66 | | this.fileStream = File.OpenWrite(root); |
| 0 | 67 | | this.archive = WriterFactory.Open(this.fileStream, archiveType, new WriterOptions(compressionType)); |
| | 68 | |
|
| 0 | 69 | | if (!memory.IsEmpty) |
| | 70 | | { |
| 0 | 71 | | this.WriteAll(memory); |
| | 72 | | } |
| 0 | 73 | | } |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Initializes a new instance of the <see cref="ArchiveWritableFileLocator"/> class. |
| | 78 | | /// </summary> |
| | 79 | | /// <param name="root">A stream.</param> |
| | 80 | | /// <param name="archiveType">The archive type.</param> |
| | 81 | | /// <param name="compressionType">The compression type.</param> |
| 0 | 82 | | public ArchiveWritableFileLocator(Stream root, ArchiveType archiveType, CompressionType compressionType) |
| | 83 | | { |
| 0 | 84 | | this.keys = new SortedSet<string>(); |
| | 85 | |
|
| 0 | 86 | | this.archive = WriterFactory.Open(root, archiveType, new WriterOptions(compressionType)); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// Immediately releases the unmanaged resources used by the <see cref="ArchiveWritableFileLocator"/> object. |
| | 91 | | /// </summary> |
| | 92 | | public void Dispose() |
| | 93 | | { |
| 0 | 94 | | if (this.archive != null) |
| | 95 | | { |
| 0 | 96 | | this.archive.Dispose(); |
| 0 | 97 | | this.archive = null; |
| | 98 | | } |
| | 99 | |
|
| 0 | 100 | | if (this.fileStream != null) |
| | 101 | | { |
| 0 | 102 | | this.fileStream.Dispose(); |
| 0 | 103 | | this.fileStream = null; |
| | 104 | | } |
| | 105 | |
|
| 0 | 106 | | GC.SuppressFinalize(this); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Create a file. |
| | 111 | | /// </summary> |
| | 112 | | /// <param name="path">A path.</param> |
| | 113 | | public void Create(string path) |
| | 114 | | { |
| 0 | 115 | | if (string.IsNullOrEmpty(path)) |
| | 116 | | { |
| 0 | 117 | | throw new ArgumentNullException(nameof(path)); |
| | 118 | | } |
| | 119 | |
|
| 0 | 120 | | path = Utilities.PathNormalize(path); |
| | 121 | |
|
| 0 | 122 | | if (this.keys.Contains(path)) |
| | 123 | | { |
| 0 | 124 | | throw new ArgumentOutOfRangeException(nameof(path)); |
| | 125 | | } |
| | 126 | |
|
| 0 | 127 | | using (var buffer = new MemoryStream()) |
| | 128 | | { |
| 0 | 129 | | buffer.WriteByte(0); |
| 0 | 130 | | buffer.Seek(0, SeekOrigin.Begin); |
| | 131 | |
|
| 0 | 132 | | this.archive.Write(path, buffer); |
| 0 | 133 | | } |
| | 134 | |
|
| 0 | 135 | | this.keys.Add(path); |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | /// <summary> |
| | 139 | | /// Write a file. |
| | 140 | | /// </summary> |
| | 141 | | /// <param name="path">A path.</param> |
| | 142 | | /// <param name="data">The data.</param> |
| | 143 | | public void Write(string path, Stream data) |
| | 144 | | { |
| 0 | 145 | | if (string.IsNullOrEmpty(path)) |
| | 146 | | { |
| 0 | 147 | | throw new ArgumentNullException(nameof(path)); |
| | 148 | | } |
| | 149 | |
|
| 0 | 150 | | if (data == null) |
| | 151 | | { |
| 0 | 152 | | throw new ArgumentNullException(nameof(data)); |
| | 153 | | } |
| | 154 | |
|
| 0 | 155 | | path = Utilities.PathNormalize(path); |
| | 156 | |
|
| 0 | 157 | | if (this.keys.Contains(path)) |
| | 158 | | { |
| 0 | 159 | | throw new ArgumentOutOfRangeException(nameof(path)); |
| | 160 | | } |
| | 161 | |
|
| 0 | 162 | | this.archive.Write(path, data); |
| 0 | 163 | | this.keys.Add(path); |
| 0 | 164 | | } |
| | 165 | |
|
| | 166 | | /// <summary> |
| | 167 | | /// Write the files from a file locator. |
| | 168 | | /// </summary> |
| | 169 | | /// <param name="locator">A file locator.</param> |
| | 170 | | public void WriteAll(IFileLocator locator) |
| | 171 | | { |
| 0 | 172 | | this.WriteAll(locator, string.Empty); |
| 0 | 173 | | } |
| | 174 | |
|
| | 175 | | /// <summary> |
| | 176 | | /// Write the files from a file locator. |
| | 177 | | /// </summary> |
| | 178 | | /// <param name="locator">A file locator.</param> |
| | 179 | | /// <param name="root">The root path.</param> |
| | 180 | | public void WriteAll(IFileLocator locator, string root) |
| | 181 | | { |
| 0 | 182 | | if (locator == null) |
| | 183 | | { |
| 0 | 184 | | throw new ArgumentNullException(nameof(locator)); |
| | 185 | | } |
| | 186 | |
|
| 0 | 187 | | foreach (var file in locator.EnumerateFiles(root)) |
| | 188 | | { |
| 0 | 189 | | using (var stream = locator.Open(file)) |
| | 190 | | { |
| 0 | 191 | | this.Write(file, stream); |
| 0 | 192 | | } |
| | 193 | | } |
| 0 | 194 | | } |
| | 195 | | } |
| | 196 | | } |