Summary

Class:ICSharpCode.SharpZipLib.Zip.ZipEntryFactory
Assembly:ICSharpCode.SharpZipLib
File(s):C:\Users\Neil\Documents\Visual Studio 2015\Projects\icsharpcode\SZL_master\ICSharpCode.SharpZipLib\Zip\ZipEntryFactory.cs
Covered lines:51
Uncovered lines:50
Coverable lines:101
Total lines:341
Line coverage:50.4%
Branch coverage:35%

Metrics

MethodCyclomatic ComplexitySequence CoverageBranch Coverage
.ctor()1100100
.ctor(...)1100100
.ctor(...)1100100
MakeFileEntry(...)1100100
MakeFileEntry(...)1100100
MakeFileEntry(...)1661.7661.90
MakeDirectoryEntry(...)100
MakeDirectoryEntry(...)1300

File(s)

C:\Users\Neil\Documents\Visual Studio 2015\Projects\icsharpcode\SZL_master\ICSharpCode.SharpZipLib\Zip\ZipEntryFactory.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3using ICSharpCode.SharpZipLib.Core;
 4
 5namespace ICSharpCode.SharpZipLib.Zip
 6{
 7  /// <summary>
 8  /// Basic implementation of <see cref="IEntryFactory"></see>
 9  /// </summary>
 10  public class ZipEntryFactory : IEntryFactory
 11  {
 12    #region Enumerations
 13    /// <summary>
 14    /// Defines the possible values to be used for the <see cref="ZipEntry.DateTime"/>.
 15    /// </summary>
 16    public enum TimeSetting
 17    {
 18      /// <summary>
 19      /// Use the recorded LastWriteTime value for the file.
 20      /// </summary>
 21      LastWriteTime,
 22      /// <summary>
 23      /// Use the recorded LastWriteTimeUtc value for the file
 24      /// </summary>
 25      LastWriteTimeUtc,
 26      /// <summary>
 27      /// Use the recorded CreateTime value for the file.
 28      /// </summary>
 29      CreateTime,
 30      /// <summary>
 31      /// Use the recorded CreateTimeUtc value for the file.
 32      /// </summary>
 33      CreateTimeUtc,
 34      /// <summary>
 35      /// Use the recorded LastAccessTime value for the file.
 36      /// </summary>
 37      LastAccessTime,
 38      /// <summary>
 39      /// Use the recorded LastAccessTimeUtc value for the file.
 40      /// </summary>
 41      LastAccessTimeUtc,
 42      /// <summary>
 43      /// Use a fixed value.
 44      /// </summary>
 45      /// <remarks>The actual <see cref="DateTime"/> value used can be
 46      /// specified via the <see cref="ZipEntryFactory(DateTime)"/> constructor or
 47      /// using the <see cref="ZipEntryFactory(TimeSetting)"/> with the setting set
 48      /// to <see cref="TimeSetting.Fixed"/> which will use the <see cref="DateTime"/> when this class was constructed.
 49      /// The <see cref="FixedDateTime"/> property can also be used to set this value.</remarks>
 50      Fixed,
 51    }
 52    #endregion
 53
 54    #region Constructors
 55    /// <summary>
 56    /// Initialise a new instance of the <see cref="ZipEntryFactory"/> class.
 57    /// </summary>
 58    /// <remarks>A default <see cref="INameTransform"/>, and the LastWriteTime for files is used.</remarks>
 9559    public ZipEntryFactory()
 60    {
 9561      nameTransform_ = new ZipNameTransform();
 9562    }
 63
 64    /// <summary>
 65    /// Initialise a new instance of <see cref="ZipEntryFactory"/> using the specified <see cref="TimeSetting"/>
 66    /// </summary>
 67    /// <param name="timeSetting">The <see cref="TimeSetting">time setting</see> to use when creating <see cref="ZipEntr
 168    public ZipEntryFactory(TimeSetting timeSetting)
 69    {
 170      timeSetting_ = timeSetting;
 171      nameTransform_ = new ZipNameTransform();
 172    }
 73
 74    /// <summary>
 75    /// Initialise a new instance of <see cref="ZipEntryFactory"/> using the specified <see cref="DateTime"/>
 76    /// </summary>
 77    /// <param name="time">The time to set all <see cref="ZipEntry.DateTime"/> values to.</param>
 178    public ZipEntryFactory(DateTime time)
 79    {
 180      timeSetting_ = TimeSetting.Fixed;
 181      FixedDateTime = time;
 182      nameTransform_ = new ZipNameTransform();
 183    }
 84
 85    #endregion
 86
 87    #region Properties
 88    /// <summary>
 89    /// Get / set the <see cref="INameTransform"/> to be used when creating new <see cref="ZipEntry"/> values.
 90    /// </summary>
 91    /// <remarks>
 92    /// Setting this property to null will cause a default <see cref="ZipNameTransform">name transform</see> to be used.
 93    /// </remarks>
 94    public INameTransform NameTransform {
 6574395      get { return nameTransform_; }
 96      set {
 497         if (value == null) {
 098          nameTransform_ = new ZipNameTransform();
 099        } else {
 4100          nameTransform_ = value;
 101        }
 4102      }
 103    }
 104
 105    /// <summary>
 106    /// Get / set the <see cref="TimeSetting"/> in use.
 107    /// </summary>
 108    public TimeSetting Setting {
 3109      get { return timeSetting_; }
 2110      set { timeSetting_ = value; }
 111    }
 112
 113    /// <summary>
 114    /// Get / set the <see cref="DateTime"/> value to use when <see cref="Setting"/> is set to <see cref="TimeSetting.Fi
 115    /// </summary>
 116    public DateTime FixedDateTime {
 5117      get { return fixedDateTime_; }
 118      set {
 2119         if (value.Year < 1970) {
 0120          throw new ArgumentException("Value is too old to be valid", nameof(value));
 121        }
 2122        fixedDateTime_ = value;
 2123      }
 124    }
 125
 126    /// <summary>
 127    /// A bitmask defining the attributes to be retrieved from the actual file.
 128    /// </summary>
 129    /// <remarks>The default is to get all possible attributes from the actual file.</remarks>
 130    public int GetAttributes {
 3131      get { return getAttributes_; }
 0132      set { getAttributes_ = value; }
 133    }
 134
 135    /// <summary>
 136    /// A bitmask defining which attributes are to be set on.
 137    /// </summary>
 138    /// <remarks>By default no attributes are set on.</remarks>
 139    public int SetAttributes {
 3140      get { return setAttributes_; }
 2141      set { setAttributes_ = value; }
 142    }
 143
 144    /// <summary>
 145    /// Get set a value indicating wether unidoce text should be set on.
 146    /// </summary>
 147    public bool IsUnicodeText {
 0148      get { return isUnicodeText_; }
 4149      set { isUnicodeText_ = value; }
 150    }
 151
 152    #endregion
 153
 154    #region IEntryFactory Members
 155
 156    /// <summary>
 157    /// Make a new <see cref="ZipEntry"/> for a file.
 158    /// </summary>
 159    /// <param name="fileName">The name of the file to create a new entry for.</param>
 160    /// <returns>Returns a new <see cref="ZipEntry"/> based on the <paramref name="fileName"/>.</returns>
 161    public ZipEntry MakeFileEntry(string fileName)
 162    {
 7163      return MakeFileEntry(fileName, null, true);
 164    }
 165
 166    /// <summary>
 167    /// Make a new <see cref="ZipEntry"/> for a file.
 168    /// </summary>
 169    /// <param name="fileName">The name of the file to create a new entry for.</param>
 170    /// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param>
 171    /// <returns>Returns a new <see cref="ZipEntry"/> based on the <paramref name="fileName"/>.</returns>
 172    public ZipEntry MakeFileEntry(string fileName, bool useFileSystem)
 173    {
 165174      return MakeFileEntry(fileName, null, useFileSystem);
 175    }
 176
 177    /// <summary>
 178    /// Make a new <see cref="ZipEntry"/> from a name.
 179    /// </summary>
 180    /// <param name="fileName">The name of the file to create a new entry for.</param>
 181    /// <param name="entryName">An alternative name to be used for the new entry. Null if not applicable.</param>
 182    /// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param>
 183    /// <returns>Returns a new <see cref="ZipEntry"/> based on the <paramref name="fileName"/>.</returns>
 184    public ZipEntry MakeFileEntry(string fileName, string entryName, bool useFileSystem)
 185    {
 172186       var result = new ZipEntry(nameTransform_.TransformFile(!string.IsNullOrEmpty(entryName) ? entryName : fileName));
 172187      result.IsUnicodeText = isUnicodeText_;
 188
 172189      int externalAttributes = 0;
 172190      bool useAttributes = (setAttributes_ != 0);
 191
 172192      FileInfo fi = null;
 172193       if (useFileSystem) {
 7194        fi = new FileInfo(fileName);
 195      }
 196
 172197       if ((fi != null) && fi.Exists) {
 7198         switch (timeSetting_) {
 199          case TimeSetting.CreateTime:
 0200            result.DateTime = fi.CreationTime;
 0201            break;
 202
 203          case TimeSetting.CreateTimeUtc:
 0204            result.DateTime = fi.CreationTimeUtc;
 0205            break;
 206
 207          case TimeSetting.LastAccessTime:
 0208            result.DateTime = fi.LastAccessTime;
 0209            break;
 210
 211          case TimeSetting.LastAccessTimeUtc:
 0212            result.DateTime = fi.LastAccessTimeUtc;
 0213            break;
 214
 215          case TimeSetting.LastWriteTime:
 7216            result.DateTime = fi.LastWriteTime;
 7217            break;
 218
 219          case TimeSetting.LastWriteTimeUtc:
 0220            result.DateTime = fi.LastWriteTimeUtc;
 0221            break;
 222
 223          case TimeSetting.Fixed:
 0224            result.DateTime = fixedDateTime_;
 0225            break;
 226
 227          default:
 0228            throw new ZipException("Unhandled time setting in MakeFileEntry");
 229        }
 230
 7231        result.Size = fi.Length;
 232
 7233        useAttributes = true;
 7234        externalAttributes = ((int)fi.Attributes & getAttributes_);
 7235      } else {
 165236         if (timeSetting_ == TimeSetting.Fixed) {
 2237          result.DateTime = fixedDateTime_;
 238        }
 239      }
 240
 172241       if (useAttributes) {
 9242        externalAttributes |= setAttributes_;
 9243        result.ExternalFileAttributes = externalAttributes;
 244      }
 245
 172246      return result;
 247    }
 248
 249    /// <summary>
 250    /// Make a new <see cref="ZipEntry"></see> for a directory.
 251    /// </summary>
 252    /// <param name="directoryName">The raw untransformed name for the new directory</param>
 253    /// <returns>Returns a new <see cref="ZipEntry"></see> representing a directory.</returns>
 254    public ZipEntry MakeDirectoryEntry(string directoryName)
 255    {
 0256      return MakeDirectoryEntry(directoryName, true);
 257    }
 258
 259    /// <summary>
 260    /// Make a new <see cref="ZipEntry"></see> for a directory.
 261    /// </summary>
 262    /// <param name="directoryName">The raw untransformed name for the new directory</param>
 263    /// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param>
 264    /// <returns>Returns a new <see cref="ZipEntry"></see> representing a directory.</returns>
 265    public ZipEntry MakeDirectoryEntry(string directoryName, bool useFileSystem)
 266    {
 267
 0268      var result = new ZipEntry(nameTransform_.TransformDirectory(directoryName));
 0269      result.IsUnicodeText = isUnicodeText_;
 0270      result.Size = 0;
 271
 0272      int externalAttributes = 0;
 273
 0274      DirectoryInfo di = null;
 275
 0276       if (useFileSystem) {
 0277        di = new DirectoryInfo(directoryName);
 278      }
 279
 280
 0281       if ((di != null) && di.Exists) {
 0282         switch (timeSetting_) {
 283          case TimeSetting.CreateTime:
 0284            result.DateTime = di.CreationTime;
 0285            break;
 286
 287          case TimeSetting.CreateTimeUtc:
 0288            result.DateTime = di.CreationTimeUtc;
 0289            break;
 290
 291          case TimeSetting.LastAccessTime:
 0292            result.DateTime = di.LastAccessTime;
 0293            break;
 294
 295          case TimeSetting.LastAccessTimeUtc:
 0296            result.DateTime = di.LastAccessTimeUtc;
 0297            break;
 298
 299          case TimeSetting.LastWriteTime:
 0300            result.DateTime = di.LastWriteTime;
 0301            break;
 302
 303          case TimeSetting.LastWriteTimeUtc:
 0304            result.DateTime = di.LastWriteTimeUtc;
 0305            break;
 306
 307          case TimeSetting.Fixed:
 0308            result.DateTime = fixedDateTime_;
 0309            break;
 310
 311          default:
 0312            throw new ZipException("Unhandled time setting in MakeDirectoryEntry");
 313        }
 314
 0315        externalAttributes = ((int)di.Attributes & getAttributes_);
 0316      } else {
 0317         if (timeSetting_ == TimeSetting.Fixed) {
 0318          result.DateTime = fixedDateTime_;
 319        }
 320      }
 321
 322      // Always set directory attribute on.
 0323      externalAttributes |= (setAttributes_ | 16);
 0324      result.ExternalFileAttributes = externalAttributes;
 325
 0326      return result;
 327    }
 328
 329    #endregion
 330
 331    #region Instance Fields
 332    INameTransform nameTransform_;
 97333    DateTime fixedDateTime_ = DateTime.Now;
 334    TimeSetting timeSetting_;
 335    bool isUnicodeText_;
 336
 97337    int getAttributes_ = -1;
 338    int setAttributes_;
 339    #endregion
 340  }
 341}