| 1 | // ZipEntryFactory.cs
|
|---|
| 2 | //
|
|---|
| 3 | // Copyright 2006 John Reilly
|
|---|
| 4 | //
|
|---|
| 5 | // Copyright (C) 2001 Free Software Foundation, Inc.
|
|---|
| 6 | //
|
|---|
| 7 | // This program is free software; you can redistribute it and/or
|
|---|
| 8 | // modify it under the terms of the GNU General Public License
|
|---|
| 9 | // as published by the Free Software Foundation; either version 2
|
|---|
| 10 | // of the License, or (at your option) any later version.
|
|---|
| 11 | //
|
|---|
| 12 | // This program is distributed in the hope that it will be useful,
|
|---|
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 15 | // GNU General Public License for more details.
|
|---|
| 16 | //
|
|---|
| 17 | // You should have received a copy of the GNU General Public License
|
|---|
| 18 | // along with this program; if not, write to the Free Software
|
|---|
| 19 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|---|
| 20 | //
|
|---|
| 21 | // Linking this library statically or dynamically with other modules is
|
|---|
| 22 | // making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | // conditions of the GNU General Public License cover the whole
|
|---|
| 24 | // combination.
|
|---|
| 25 | //
|
|---|
| 26 | // As a special exception, the copyright holders of this library give you
|
|---|
| 27 | // permission to link this library with independent modules to produce an
|
|---|
| 28 | // executable, regardless of the license terms of these independent
|
|---|
| 29 | // modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | // terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | // independent module, the terms and conditions of the license of that
|
|---|
| 32 | // module. An independent module is a module which is not derived from
|
|---|
| 33 | // or based on this library. If you modify this library, you may extend
|
|---|
| 34 | // this exception to your version of the library, but you are not
|
|---|
| 35 | // obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | // exception statement from your version.
|
|---|
| 37 |
|
|---|
| 38 | using System;
|
|---|
| 39 | using System.IO;
|
|---|
| 40 | using System.Text;
|
|---|
| 41 |
|
|---|
| 42 | using ICSharpCode.SharpZipLib.Core;
|
|---|
| 43 |
|
|---|
| 44 | namespace ICSharpCode.SharpZipLib.Zip
|
|---|
| 45 | {
|
|---|
| 46 | /// <summary>
|
|---|
| 47 | /// Basic implementation of <see cref="IEntryFactory"></see>
|
|---|
| 48 | /// </summary>
|
|---|
| 49 | public class ZipEntryFactory : IEntryFactory
|
|---|
| 50 | {
|
|---|
| 51 | #region Enumerations
|
|---|
| 52 | /// <summary>
|
|---|
| 53 | /// Defines the possible values to be used for the <see cref="ZipEntry.DateTime"/>.
|
|---|
| 54 | /// </summary>
|
|---|
| 55 | public enum TimeSetting
|
|---|
| 56 | {
|
|---|
| 57 | /// <summary>
|
|---|
| 58 | /// Use the recorded LastWriteTime value for the file.
|
|---|
| 59 | /// </summary>
|
|---|
| 60 | LastWriteTime,
|
|---|
| 61 | /// <summary>
|
|---|
| 62 | /// Use the recorded LastWriteTimeUtc value for the file
|
|---|
| 63 | /// </summary>
|
|---|
| 64 | LastWriteTimeUtc,
|
|---|
| 65 | /// <summary>
|
|---|
| 66 | /// Use the recorded CreateTime value for the file.
|
|---|
| 67 | /// </summary>
|
|---|
| 68 | CreateTime,
|
|---|
| 69 | /// <summary>
|
|---|
| 70 | /// Use the recorded CreateTimeUtc value for the file.
|
|---|
| 71 | /// </summary>
|
|---|
| 72 | CreateTimeUtc,
|
|---|
| 73 | /// <summary>
|
|---|
| 74 | /// Use the recorded LastAccessTime value for the file.
|
|---|
| 75 | /// </summary>
|
|---|
| 76 | LastAccessTime,
|
|---|
| 77 | /// <summary>
|
|---|
| 78 | /// Use the recorded LastAccessTimeUtc value for the file.
|
|---|
| 79 | /// </summary>
|
|---|
| 80 | LastAccessTimeUtc,
|
|---|
| 81 | /// <summary>
|
|---|
| 82 | /// Use a fixed value.
|
|---|
| 83 | /// </summary>
|
|---|
| 84 | /// <remarks>The actual <see cref="DateTime"/> value used can be
|
|---|
| 85 | /// specified via the <see cref="ZipEntryFactory(DateTime)"/> constructor or
|
|---|
| 86 | /// using the <see cref="ZipEntryFactory(TimeSetting)"/> with the setting set
|
|---|
| 87 | /// to <see cref="TimeSetting.Fixed"/> which will use the <see cref="DateTime"/> when this class was constructed.
|
|---|
| 88 | /// The <see cref="FixedDateTime"/> property can also be used to set this value.</remarks>
|
|---|
| 89 | Fixed,
|
|---|
| 90 | }
|
|---|
| 91 | #endregion
|
|---|
| 92 |
|
|---|
| 93 | #region Constructors
|
|---|
| 94 | /// <summary>
|
|---|
| 95 | /// Initialise a new instance of the <see cref="ZipEntryFactory"/> class.
|
|---|
| 96 | /// </summary>
|
|---|
| 97 | /// <remarks>A default <see cref="INameTransform"/>, and the LastWriteTime for files is used.</remarks>
|
|---|
| 98 | public ZipEntryFactory()
|
|---|
| 99 | {
|
|---|
| 100 | nameTransform_ = new ZipNameTransform();
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /// <summary>
|
|---|
| 104 | /// Initialise a new instance of <see cref="ZipEntryFactory"/> using the specified <see cref="TimeSetting"/>
|
|---|
| 105 | /// </summary>
|
|---|
| 106 | /// <param name="timeSetting">The <see cref="TimeSetting">time setting</see> to use when creating <see cref="ZipEntry">Zip entries</see>.</param>
|
|---|
| 107 | public ZipEntryFactory(TimeSetting timeSetting)
|
|---|
| 108 | {
|
|---|
| 109 | timeSetting_ = timeSetting;
|
|---|
| 110 | nameTransform_ = new ZipNameTransform();
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /// <summary>
|
|---|
| 114 | /// Initialise a new instance of <see cref="ZipEntryFactory"/> using the specified <see cref="DateTime"/>
|
|---|
| 115 | /// </summary>
|
|---|
| 116 | /// <param name="time">The time to set all <see cref="ZipEntry.DateTime"/> values to.</param>
|
|---|
| 117 | public ZipEntryFactory(DateTime time)
|
|---|
| 118 | {
|
|---|
| 119 | timeSetting_ = TimeSetting.Fixed;
|
|---|
| 120 | FixedDateTime = time;
|
|---|
| 121 | nameTransform_ = new ZipNameTransform();
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | #endregion
|
|---|
| 125 |
|
|---|
| 126 | #region Properties
|
|---|
| 127 | /// <summary>
|
|---|
| 128 | /// Get / set the <see cref="INameTransform"/> to be used when creating new <see cref="ZipEntry"/> values.
|
|---|
| 129 | /// </summary>
|
|---|
| 130 | /// <remarks>
|
|---|
| 131 | /// Setting this property to null will cause a default <see cref="ZipNameTransform">name transform</see> to be used.
|
|---|
| 132 | /// </remarks>
|
|---|
| 133 | public INameTransform NameTransform
|
|---|
| 134 | {
|
|---|
| 135 | get { return nameTransform_; }
|
|---|
| 136 | set
|
|---|
| 137 | {
|
|---|
| 138 | if (value == null) {
|
|---|
| 139 | nameTransform_ = new ZipNameTransform();
|
|---|
| 140 | }
|
|---|
| 141 | else {
|
|---|
| 142 | nameTransform_ = value;
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /// <summary>
|
|---|
| 148 | /// Get / set the <see cref="TimeSetting"/> in use.
|
|---|
| 149 | /// </summary>
|
|---|
| 150 | public TimeSetting Setting
|
|---|
| 151 | {
|
|---|
| 152 | get { return timeSetting_; }
|
|---|
| 153 | set { timeSetting_ = value; }
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /// <summary>
|
|---|
| 157 | /// Get / set the <see cref="DateTime"/> value to use when <see cref="Setting"/> is set to <see cref="TimeSetting.Fixed"/>
|
|---|
| 158 | /// </summary>
|
|---|
| 159 | public DateTime FixedDateTime
|
|---|
| 160 | {
|
|---|
| 161 | get { return fixedDateTime_; }
|
|---|
| 162 | set
|
|---|
| 163 | {
|
|---|
| 164 | if (value.Year < 1970) {
|
|---|
| 165 | throw new ArgumentException("Value is too old to be valid", "value");
|
|---|
| 166 | }
|
|---|
| 167 | fixedDateTime_ = value;
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /// <summary>
|
|---|
| 172 | /// A bitmask defining the attributes to be retrieved from the actual file.
|
|---|
| 173 | /// </summary>
|
|---|
| 174 | /// <remarks>The default is to get all possible attributes from the actual file.</remarks>
|
|---|
| 175 | public int GetAttributes
|
|---|
| 176 | {
|
|---|
| 177 | get { return getAttributes_; }
|
|---|
| 178 | set { getAttributes_ = value; }
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /// <summary>
|
|---|
| 182 | /// A bitmask defining which attributes are to be set on.
|
|---|
| 183 | /// </summary>
|
|---|
| 184 | /// <remarks>By default no attributes are set on.</remarks>
|
|---|
| 185 | public int SetAttributes
|
|---|
| 186 | {
|
|---|
| 187 | get { return setAttributes_; }
|
|---|
| 188 | set { setAttributes_ = value; }
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /// <summary>
|
|---|
| 192 | /// Get set a value indicating wether unidoce text should be set on.
|
|---|
| 193 | /// </summary>
|
|---|
| 194 | public bool IsUnicodeText
|
|---|
| 195 | {
|
|---|
| 196 | get { return isUnicodeText_; }
|
|---|
| 197 | set { isUnicodeText_ = value; }
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | #endregion
|
|---|
| 201 |
|
|---|
| 202 | #region IEntryFactory Members
|
|---|
| 203 |
|
|---|
| 204 | /// <summary>
|
|---|
| 205 | /// Make a new <see cref="ZipEntry"/> for a file.
|
|---|
| 206 | /// </summary>
|
|---|
| 207 | /// <param name="fileName">The name of the file to create a new entry for.</param>
|
|---|
| 208 | /// <returns>Returns a new <see cref="ZipEntry"/> based on the <paramref name="fileName"/>.</returns>
|
|---|
| 209 | public ZipEntry MakeFileEntry(string fileName)
|
|---|
| 210 | {
|
|---|
| 211 | return MakeFileEntry(fileName, true);
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /// <summary>
|
|---|
| 215 | /// Make a new <see cref="ZipEntry"/> from a name.
|
|---|
| 216 | /// </summary>
|
|---|
| 217 | /// <param name="fileName">The name of the file to create a new entry for.</param>
|
|---|
| 218 | /// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param>
|
|---|
| 219 | /// <returns>Returns a new <see cref="ZipEntry"/> based on the <paramref name="fileName"/>.</returns>
|
|---|
| 220 | public ZipEntry MakeFileEntry(string fileName, bool useFileSystem)
|
|---|
| 221 | {
|
|---|
| 222 | ZipEntry result = new ZipEntry(nameTransform_.TransformFile(fileName));
|
|---|
| 223 | result.IsUnicodeText = isUnicodeText_;
|
|---|
| 224 |
|
|---|
| 225 | int externalAttributes = 0;
|
|---|
| 226 | bool useAttributes = (setAttributes_ != 0);
|
|---|
| 227 |
|
|---|
| 228 | FileInfo fi = null;
|
|---|
| 229 | if (useFileSystem)
|
|---|
| 230 | {
|
|---|
| 231 | fi = new FileInfo(fileName);
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | if ((fi != null) && fi.Exists)
|
|---|
| 235 | {
|
|---|
| 236 | switch (timeSetting_)
|
|---|
| 237 | {
|
|---|
| 238 | case TimeSetting.CreateTime:
|
|---|
| 239 | result.DateTime = fi.CreationTime;
|
|---|
| 240 | break;
|
|---|
| 241 |
|
|---|
| 242 | case TimeSetting.CreateTimeUtc:
|
|---|
| 243 | #if NETCF_1_0 || NETCF_2_0
|
|---|
| 244 | result.DateTime = fi.CreationTime.ToUniversalTime();
|
|---|
| 245 | #else
|
|---|
| 246 | result.DateTime = fi.CreationTimeUtc;
|
|---|
| 247 | #endif
|
|---|
| 248 | break;
|
|---|
| 249 |
|
|---|
| 250 | case TimeSetting.LastAccessTime:
|
|---|
| 251 | result.DateTime = fi.LastAccessTime;
|
|---|
| 252 | break;
|
|---|
| 253 |
|
|---|
| 254 | case TimeSetting.LastAccessTimeUtc:
|
|---|
| 255 | #if NETCF_1_0 || NETCF_2_0
|
|---|
| 256 | result.DateTime = fi.LastAccessTime.ToUniversalTime();
|
|---|
| 257 | #else
|
|---|
| 258 | result.DateTime = fi.LastAccessTimeUtc;
|
|---|
| 259 | #endif
|
|---|
| 260 | break;
|
|---|
| 261 |
|
|---|
| 262 | case TimeSetting.LastWriteTime:
|
|---|
| 263 | result.DateTime = fi.LastWriteTime;
|
|---|
| 264 | break;
|
|---|
| 265 |
|
|---|
| 266 | case TimeSetting.LastWriteTimeUtc:
|
|---|
| 267 | #if NETCF_1_0 || NETCF_2_0
|
|---|
| 268 | result.DateTime = fi.LastWriteTime.ToUniversalTime();
|
|---|
| 269 | #else
|
|---|
| 270 | result.DateTime = fi.LastWriteTimeUtc;
|
|---|
| 271 | #endif
|
|---|
| 272 | break;
|
|---|
| 273 |
|
|---|
| 274 | case TimeSetting.Fixed:
|
|---|
| 275 | result.DateTime = fixedDateTime_;
|
|---|
| 276 | break;
|
|---|
| 277 |
|
|---|
| 278 | default:
|
|---|
| 279 | throw new ZipException("Unhandled time setting in MakeFileEntry");
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | result.Size = fi.Length;
|
|---|
| 283 |
|
|---|
| 284 | useAttributes = true;
|
|---|
| 285 | externalAttributes = ((int)fi.Attributes & getAttributes_);
|
|---|
| 286 | }
|
|---|
| 287 | else
|
|---|
| 288 | {
|
|---|
| 289 | if (timeSetting_ == TimeSetting.Fixed)
|
|---|
| 290 | {
|
|---|
| 291 | result.DateTime = fixedDateTime_;
|
|---|
| 292 | }
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | if (useAttributes)
|
|---|
| 296 | {
|
|---|
| 297 | externalAttributes |= setAttributes_;
|
|---|
| 298 | result.ExternalFileAttributes = externalAttributes;
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | return result;
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | /// <summary>
|
|---|
| 305 | /// Make a new <see cref="ZipEntry"></see> for a directory.
|
|---|
| 306 | /// </summary>
|
|---|
| 307 | /// <param name="directoryName">The raw untransformed name for the new directory</param>
|
|---|
| 308 | /// <returns>Returns a new <see cref="ZipEntry"></see> representing a directory.</returns>
|
|---|
| 309 | public ZipEntry MakeDirectoryEntry(string directoryName)
|
|---|
| 310 | {
|
|---|
| 311 | return MakeDirectoryEntry(directoryName, true);
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | /// <summary>
|
|---|
| 315 | /// Make a new <see cref="ZipEntry"></see> for a directory.
|
|---|
| 316 | /// </summary>
|
|---|
| 317 | /// <param name="directoryName">The raw untransformed name for the new directory</param>
|
|---|
| 318 | /// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param>
|
|---|
| 319 | /// <returns>Returns a new <see cref="ZipEntry"></see> representing a directory.</returns>
|
|---|
| 320 | public ZipEntry MakeDirectoryEntry(string directoryName, bool useFileSystem)
|
|---|
| 321 | {
|
|---|
| 322 |
|
|---|
| 323 | ZipEntry result = new ZipEntry(nameTransform_.TransformDirectory(directoryName));
|
|---|
| 324 | result.Size=0;
|
|---|
| 325 |
|
|---|
| 326 | int externalAttributes = 0;
|
|---|
| 327 |
|
|---|
| 328 | DirectoryInfo di = null;
|
|---|
| 329 |
|
|---|
| 330 | if (useFileSystem)
|
|---|
| 331 | {
|
|---|
| 332 | di = new DirectoryInfo(directoryName);
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 | if ((di != null) && di.Exists)
|
|---|
| 337 | {
|
|---|
| 338 | switch (timeSetting_)
|
|---|
| 339 | {
|
|---|
| 340 | case TimeSetting.CreateTime:
|
|---|
| 341 | result.DateTime = di.CreationTime;
|
|---|
| 342 | break;
|
|---|
| 343 |
|
|---|
| 344 | case TimeSetting.CreateTimeUtc:
|
|---|
| 345 | #if NETCF_1_0 || NETCF_2_0
|
|---|
| 346 | result.DateTime = di.CreationTime.ToUniversalTime();
|
|---|
| 347 | #else
|
|---|
| 348 | result.DateTime = di.CreationTimeUtc;
|
|---|
| 349 | #endif
|
|---|
| 350 | break;
|
|---|
| 351 |
|
|---|
| 352 | case TimeSetting.LastAccessTime:
|
|---|
| 353 | result.DateTime = di.LastAccessTime;
|
|---|
| 354 | break;
|
|---|
| 355 |
|
|---|
| 356 | case TimeSetting.LastAccessTimeUtc:
|
|---|
| 357 | #if NETCF_1_0 || NETCF_2_0
|
|---|
| 358 | result.DateTime = di.LastAccessTime.ToUniversalTime();
|
|---|
| 359 | #else
|
|---|
| 360 | result.DateTime = di.LastAccessTimeUtc;
|
|---|
| 361 | #endif
|
|---|
| 362 | break;
|
|---|
| 363 |
|
|---|
| 364 | case TimeSetting.LastWriteTime:
|
|---|
| 365 | result.DateTime = di.LastWriteTime;
|
|---|
| 366 | break;
|
|---|
| 367 |
|
|---|
| 368 | case TimeSetting.LastWriteTimeUtc:
|
|---|
| 369 | #if NETCF_1_0 || NETCF_2_0
|
|---|
| 370 | result.DateTime = di.LastWriteTime.ToUniversalTime();
|
|---|
| 371 | #else
|
|---|
| 372 | result.DateTime = di.LastWriteTimeUtc;
|
|---|
| 373 | #endif
|
|---|
| 374 | break;
|
|---|
| 375 |
|
|---|
| 376 | case TimeSetting.Fixed:
|
|---|
| 377 | result.DateTime = fixedDateTime_;
|
|---|
| 378 | break;
|
|---|
| 379 |
|
|---|
| 380 | default:
|
|---|
| 381 | throw new ZipException("Unhandled time setting in MakeDirectoryEntry");
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | externalAttributes = ((int)di.Attributes & getAttributes_);
|
|---|
| 385 | }
|
|---|
| 386 | else
|
|---|
| 387 | {
|
|---|
| 388 | if (timeSetting_ == TimeSetting.Fixed)
|
|---|
| 389 | {
|
|---|
| 390 | result.DateTime = fixedDateTime_;
|
|---|
| 391 | }
|
|---|
| 392 | }
|
|---|
| 393 |
|
|---|
| 394 | // Always set directory attribute on.
|
|---|
| 395 | externalAttributes |= (setAttributes_ | 16);
|
|---|
| 396 | result.ExternalFileAttributes = externalAttributes;
|
|---|
| 397 |
|
|---|
| 398 | return result;
|
|---|
| 399 | }
|
|---|
| 400 |
|
|---|
| 401 | #endregion
|
|---|
| 402 |
|
|---|
| 403 | #region Instance Fields
|
|---|
| 404 | INameTransform nameTransform_;
|
|---|
| 405 | DateTime fixedDateTime_ = DateTime.Now;
|
|---|
| 406 | TimeSetting timeSetting_;
|
|---|
| 407 | bool isUnicodeText_;
|
|---|
| 408 |
|
|---|
| 409 | int getAttributes_ = -1;
|
|---|
| 410 | int setAttributes_;
|
|---|
| 411 | #endregion
|
|---|
| 412 | }
|
|---|
| 413 | }
|
|---|