| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.Dtf.Compression |
| 4 | { |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | using System.Diagnostics.CodeAnalysis; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Stream context used to extract a single file from an archive into a memory stream. |
| 11 | /// </summary> |
| 12 | [SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable")] |
| 13 | public class BasicUnpackStreamContext : IUnpackStreamContext |
| 14 | { |
| 15 | private Stream archiveStream; |
| 16 | private Stream fileStream; |
| 17 | |
| 18 | /// <summary> |
| 19 | /// Creates a new BasicExtractStreamContext that reads from the specified archive stream. |
| 20 | /// </summary> |
| 21 | /// <param name="archiveStream">Archive stream to read from.</param> |
| 22 | public BasicUnpackStreamContext(Stream archiveStream) |
| 23 | { |
| 24 | this.archiveStream = archiveStream; |
| 25 | } |
| 26 | |
| 27 | /// <summary> |
| 28 | /// Gets the stream for the extracted file, or null if no file was extracted. |
| 29 | /// </summary> |
| 30 | public Stream FileStream |
| 31 | { |
| 32 | get |
| 33 | { |
| 34 | return this.fileStream; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /// <summary> |
| 39 | /// Opens the archive stream for reading. Returns a DuplicateStream instance, |
| 40 | /// so the stream may be virtually opened multiple times. |
| 41 | /// </summary> |
| 42 | /// <param name="archiveNumber">The archive number to open (ignored; 0 is assumed).</param> |
| 43 | /// <param name="archiveName">The name of the archive being opened.</param> |
| 44 | /// <param name="compressionEngine">Instance of the compression engine doing the operations.</param> |
| 45 | /// <returns>A stream from which archive bytes are read.</returns> |
| 46 | public Stream OpenArchiveReadStream(int archiveNumber, string archiveName, CompressionEngine compressionEngine) |
| 47 | { |
| 48 | return new DuplicateStream(this.archiveStream); |
| 49 | } |
| 50 | |
| 51 | /// <summary> |
| 52 | /// Does *not* close the stream. The archive stream should be managed by |
| 53 | /// the code that invokes the archive extraction. |
| 54 | /// </summary> |
| 55 | /// <param name="archiveNumber">The archive number of the stream to close.</param> |
| 56 | /// <param name="archiveName">The name of the archive being closed.</param> |
| 57 | /// <param name="stream">The stream being closed.</param> |
| 58 | public void CloseArchiveReadStream(int archiveNumber, string archiveName, Stream stream) |
| 59 | { |
| 60 | // Do nothing. |
| 61 | } |
| 62 | |
| 63 | /// <summary> |
| 64 | /// Opens a stream for writing extracted file bytes. The returned stream is a MemoryStream |
| 65 | /// instance, so the file is extracted straight into memory. |
| 66 | /// </summary> |
| 67 | /// <param name="path">Path of the file within the archive.</param> |
| 68 | /// <param name="fileSize">The uncompressed size of the file to be extracted.</param> |
| 69 | /// <param name="lastWriteTime">The last write time of the file.</param> |
| 70 | /// <returns>A stream where extracted file bytes are to be written.</returns> |
| 71 | public Stream OpenFileWriteStream(string path, long fileSize, DateTime lastWriteTime) |
| 72 | { |
| 73 | this.fileStream = new MemoryStream(new byte[fileSize], 0, (int) fileSize, true, true); |
| 74 | return this.fileStream; |
| 75 | } |
| 76 | |
| 77 | /// <summary> |
| 78 | /// Does *not* close the file stream. The file stream is saved in memory so it can |
| 79 | /// be read later. |
| 80 | /// </summary> |
| 81 | /// <param name="path">Path of the file within the archive.</param> |
| 82 | /// <param name="stream">The file stream to be closed.</param> |
| 83 | /// <param name="attributes">The attributes of the extracted file.</param> |
| 84 | /// <param name="lastWriteTime">The last write time of the file.</param> |
| 85 | public void CloseFileWriteStream(string path, Stream stream, FileAttributes attributes, DateTime lastWriteTime) |
| 86 | { |
| 87 | // Do nothing. |
| 88 | } |
| 89 | } |
| 90 | } |