| | | 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 readonly 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); |
| | 0 | 60 | | memory.WriteAll(reader); |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | this.fileStream = File.OpenWrite(root); |
| | 0 | 65 | | this.archive = WriterFactory.Open(this.fileStream, archiveType, new WriterOptions(compressionType)); |
| | | 66 | | |
| | 0 | 67 | | if (!memory.IsEmpty) |
| | | 68 | | { |
| | 0 | 69 | | this.WriteAll(memory); |
| | | 70 | | } |
| | 0 | 71 | | } |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Initializes a new instance of the <see cref="ArchiveWritableFileLocator"/> class. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="root">A stream.</param> |
| | | 78 | | /// <param name="archiveType">The archive type.</param> |
| | | 79 | | /// <param name="compressionType">The compression type.</param> |
| | 0 | 80 | | public ArchiveWritableFileLocator(Stream root, ArchiveType archiveType, CompressionType compressionType) |
| | | 81 | | { |
| | 0 | 82 | | this.keys = new SortedSet<string>(); |
| | | 83 | | |
| | 0 | 84 | | this.archive = WriterFactory.Open(root, archiveType, new WriterOptions(compressionType)); |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Immediately releases the unmanaged resources used by the <see cref="ArchiveWritableFileLocator"/> object. |
| | | 89 | | /// </summary> |
| | | 90 | | public void Dispose() |
| | | 91 | | { |
| | 0 | 92 | | if (this.archive != null) |
| | | 93 | | { |
| | 0 | 94 | | this.archive.Dispose(); |
| | 0 | 95 | | this.archive = null; |
| | | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | if (this.fileStream != null) |
| | | 99 | | { |
| | 0 | 100 | | this.fileStream.Dispose(); |
| | 0 | 101 | | this.fileStream = null; |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | GC.SuppressFinalize(this); |
| | 0 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Create a file. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <param name="path">A path.</param> |
| | | 111 | | public void Create(string path) |
| | | 112 | | { |
| | 0 | 113 | | if (string.IsNullOrEmpty(path)) |
| | | 114 | | { |
| | 0 | 115 | | throw new ArgumentNullException(nameof(path)); |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | path = Utilities.PathNormalize(path); |
| | | 119 | | |
| | 0 | 120 | | if (this.keys.Contains(path)) |
| | | 121 | | { |
| | 0 | 122 | | throw new ArgumentOutOfRangeException(nameof(path)); |
| | | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | using (var buffer = new MemoryStream()) |
| | | 126 | | { |
| | 0 | 127 | | buffer.WriteByte(0); |
| | 0 | 128 | | buffer.Seek(0, SeekOrigin.Begin); |
| | | 129 | | |
| | 0 | 130 | | this.archive!.Write(path, buffer); |
| | 0 | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | this.keys.Add(path); |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Write a file. |
| | | 138 | | /// </summary> |
| | | 139 | | /// <param name="path">A path.</param> |
| | | 140 | | /// <param name="data">The data.</param> |
| | | 141 | | public void Write(string path, Stream? data) |
| | | 142 | | { |
| | 0 | 143 | | if (string.IsNullOrEmpty(path)) |
| | | 144 | | { |
| | 0 | 145 | | throw new ArgumentNullException(nameof(path)); |
| | | 146 | | } |
| | | 147 | | |
| | 0 | 148 | | if (data == null) |
| | | 149 | | { |
| | 0 | 150 | | throw new ArgumentNullException(nameof(data)); |
| | | 151 | | } |
| | | 152 | | |
| | 0 | 153 | | path = Utilities.PathNormalize(path); |
| | | 154 | | |
| | 0 | 155 | | if (this.keys.Contains(path)) |
| | | 156 | | { |
| | 0 | 157 | | throw new ArgumentOutOfRangeException(nameof(path)); |
| | | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | this.archive!.Write(path, data); |
| | 0 | 161 | | this.keys.Add(path); |
| | 0 | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Write the files from a file locator. |
| | | 166 | | /// </summary> |
| | | 167 | | /// <param name="locator">A file locator.</param> |
| | | 168 | | public void WriteAll(IFileLocator? locator) |
| | | 169 | | { |
| | 0 | 170 | | this.WriteAll(locator, string.Empty); |
| | 0 | 171 | | } |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// Write the files from a file locator. |
| | | 175 | | /// </summary> |
| | | 176 | | /// <param name="locator">A file locator.</param> |
| | | 177 | | /// <param name="root">The root path.</param> |
| | | 178 | | public void WriteAll(IFileLocator? locator, string root) |
| | | 179 | | { |
| | 0 | 180 | | if (locator == null) |
| | | 181 | | { |
| | 0 | 182 | | throw new ArgumentNullException(nameof(locator)); |
| | | 183 | | } |
| | | 184 | | |
| | 0 | 185 | | foreach (var file in locator.EnumerateFiles(root)) |
| | | 186 | | { |
| | 0 | 187 | | using var stream = locator.Open(file); |
| | 0 | 188 | | this.Write(file, stream); |
| | | 189 | | } |
| | 0 | 190 | | } |
| | | 191 | | } |
| | | 192 | | } |