| 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.Cab |
| 4 | { |
| 5 | using System; |
| 6 | using System.IO; |
| 7 | using System.Collections.Generic; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Engine capable of packing and unpacking archives in the cabinet format. |
| 11 | /// </summary> |
| 12 | public class CabEngine : CompressionEngine |
| 13 | { |
| 14 | private CabPacker packer; |
| 15 | private CabUnpacker unpacker; |
| 16 | |
| 17 | /// <summary> |
| 18 | /// Creates a new instance of the cabinet engine. |
| 19 | /// </summary> |
| 20 | public CabEngine() |
| 21 | : base() |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | /// <summary> |
| 26 | /// Disposes of resources allocated by the cabinet engine. |
| 27 | /// </summary> |
| 28 | /// <param name="disposing">If true, the method has been called directly |
| 29 | /// or indirectly by a user's code, so managed and unmanaged resources |
| 30 | /// will be disposed. If false, the method has been called by the runtime |
| 31 | /// from inside the finalizer, and only unmanaged resources will be |
| 32 | /// disposed.</param> |
| 33 | protected override void Dispose(bool disposing) |
| 34 | { |
| 35 | if (disposing) |
| 36 | { |
| 37 | if (packer != null) |
| 38 | { |
| 39 | packer.Dispose(); |
| 40 | packer = null; |
| 41 | } |
| 42 | if (unpacker != null) |
| 43 | { |
| 44 | unpacker.Dispose(); |
| 45 | unpacker = null; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | base.Dispose(disposing); |
| 50 | } |
| 51 | |
| 52 | private CabPacker Packer |
| 53 | { |
| 54 | get |
| 55 | { |
| 56 | if (this.packer == null) |
| 57 | { |
| 58 | this.packer = new CabPacker(this); |
| 59 | } |
| 60 | |
| 61 | return this.packer; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private CabUnpacker Unpacker |
| 66 | { |
| 67 | get |
| 68 | { |
| 69 | if (this.unpacker == null) |
| 70 | { |
| 71 | this.unpacker = new CabUnpacker(this); |
| 72 | } |
| 73 | |
| 74 | return this.unpacker; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// <summary> |
| 79 | /// Creates a cabinet or chain of cabinets. |
| 80 | /// </summary> |
| 81 | /// <param name="streamContext">A context interface to handle opening |
| 82 | /// and closing of cabinet and file streams.</param> |
| 83 | /// <param name="files">The paths of the files in the archive (not |
| 84 | /// external file paths).</param> |
| 85 | /// <param name="maxArchiveSize">The maximum number of bytes for one |
| 86 | /// cabinet before the contents are chained to the next cabinet, or zero |
| 87 | /// for unlimited cabinet size.</param> |
| 88 | /// <exception cref="ArchiveException">The cabinet could not be |
| 89 | /// created.</exception> |
| 90 | /// <remarks> |
| 91 | /// The stream context implementation may provide a mapping from the |
| 92 | /// file paths within the cabinet to the external file paths. |
| 93 | /// <para>Smaller folder sizes can make it more efficient to extract |
| 94 | /// individual files out of large cabinet packages.</para> |
| 95 | /// </remarks> |
| 96 | public override void Pack( |
| 97 | IPackStreamContext streamContext, |
| 98 | IEnumerable<string> files, |
| 99 | long maxArchiveSize) |
| 100 | { |
| 101 | this.Packer.CompressionLevel = this.CompressionLevel; |
| 102 | this.Packer.UseTempFiles = this.UseTempFiles; |
| 103 | this.Packer.Pack(streamContext, files, maxArchiveSize); |
| 104 | } |
| 105 | |
| 106 | /// <summary> |
| 107 | /// Checks whether a Stream begins with a header that indicates |
| 108 | /// it is a valid cabinet file. |
| 109 | /// </summary> |
| 110 | /// <param name="stream">Stream for reading the cabinet file.</param> |
| 111 | /// <returns>True if the stream is a valid cabinet file |
| 112 | /// (with no offset); false otherwise.</returns> |
| 113 | public override bool IsArchive(Stream stream) |
| 114 | { |
| 115 | return this.Unpacker.IsArchive(stream); |
| 116 | } |
| 117 | |
| 118 | /// <summary> |
| 119 | /// Gets information about files in a cabinet or cabinet chain. |
| 120 | /// </summary> |
| 121 | /// <param name="streamContext">A context interface to handle opening |
| 122 | /// and closing of cabinet and file streams.</param> |
| 123 | /// <param name="fileFilter">A predicate that can determine |
| 124 | /// which files to process, optional.</param> |
| 125 | /// <returns>Information about files in the cabinet stream.</returns> |
| 126 | /// <exception cref="ArchiveException">The cabinet provided |
| 127 | /// by the stream context is not valid.</exception> |
| 128 | /// <remarks> |
| 129 | /// The <paramref name="fileFilter"/> predicate takes an internal file |
| 130 | /// path and returns true to include the file or false to exclude it. |
| 131 | /// </remarks> |
| 132 | public override IList<ArchiveFileInfo> GetFileInfo( |
| 133 | IUnpackStreamContext streamContext, |
| 134 | Predicate<string> fileFilter) |
| 135 | { |
| 136 | return this.Unpacker.GetFileInfo(streamContext, fileFilter); |
| 137 | } |
| 138 | |
| 139 | /// <summary> |
| 140 | /// Extracts files from a cabinet or cabinet chain. |
| 141 | /// </summary> |
| 142 | /// <param name="streamContext">A context interface to handle opening |
| 143 | /// and closing of cabinet and file streams.</param> |
| 144 | /// <param name="fileFilter">An optional predicate that can determine |
| 145 | /// which files to process.</param> |
| 146 | /// <exception cref="ArchiveException">The cabinet provided |
| 147 | /// by the stream context is not valid.</exception> |
| 148 | /// <remarks> |
| 149 | /// The <paramref name="fileFilter"/> predicate takes an internal file |
| 150 | /// path and returns true to include the file or false to exclude it. |
| 151 | /// </remarks> |
| 152 | public override void Unpack( |
| 153 | IUnpackStreamContext streamContext, |
| 154 | Predicate<string> fileFilter) |
| 155 | { |
| 156 | this.Unpacker.Unpack(streamContext, fileFilter); |
| 157 | } |
| 158 | |
| 159 | internal void ReportProgress(ArchiveProgressEventArgs e) |
| 160 | { |
| 161 | base.OnProgress(e); |
| 162 | } |
| 163 | } |
| 164 | } |