3 // Copyright 2006 John Reilly
5 // Copyright (C) 2001 Free Software Foundation, Inc.
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.
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.
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.
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
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.
40 using ICSharpCode.SharpZipLib.Silverlight.Core;
41 using IEntryFactory=ICSharpCode.SharpZipLib.Silverlight.Zip.IEntryFactory;
42 using ZipEntry=ICSharpCode.SharpZipLib.Silverlight.Zip.ZipEntry;
43 using ZipException=ICSharpCode.SharpZipLib.Silverlight.Zip.ZipException;
44 using ZipNameTransform=ICSharpCode.SharpZipLib.Zip.ZipNameTransform;
46 namespace ICSharpCode.SharpZipLib.Silverlight.Zip
49 /// Basic implementation of <see cref="IEntryFactory"></see>
51 public class ZipEntryFactory : IEntryFactory
55 /// Defines the possible values to be used for the <see cref="ZipEntry.DateTime"/>.
57 public enum TimeSetting
60 /// Use the recorded LastWriteTime value for the file.
64 /// Use the recorded LastWriteTimeUtc value for the file
68 /// Use the recorded CreateTime value for the file.
72 /// Use the recorded CreateTimeUtc value for the file.
76 /// Use the recorded LastAccessTime value for the file.
80 /// Use the recorded LastAccessTimeUtc value for the file.
84 /// Use a fixed value.
86 /// <remarks>The actual <see cref="DateTime"/> value used can be
87 /// specified via the <see cref="ZipEntryFactory(DateTime)"/> constructor or
88 /// using the <see cref="ZipEntryFactory(TimeSetting)"/> with the setting set
89 /// to <see cref="TimeSetting.Fixed"/> which will use the <see cref="DateTime"/> when this class was constructed.
90 /// The <see cref="FixedDateTime"/> property can also be used to set this value.</remarks>
97 /// Initialise a new instance of the <see cref="ZipEntryFactory"/> class.
99 /// <remarks>A default <see cref="INameTransform"/>, and the LastWriteTime for files is used.</remarks>
100 public ZipEntryFactory()
102 nameTransform_ = new ZipNameTransform();
106 /// Initialise a new instance of <see cref="ZipEntryFactory"/> using the specified <see cref="TimeSetting"/>
108 /// <param name="timeSetting">The <see cref="TimeSetting">time setting</see> to use when creating <see cref="ZipEntry">Zip entries</see>.</param>
109 public ZipEntryFactory(TimeSetting timeSetting)
111 timeSetting_ = timeSetting;
112 nameTransform_ = new ZipNameTransform();
116 /// Initialise a new instance of <see cref="ZipEntryFactory"/> using the specified <see cref="DateTime"/>
118 /// <param name="time">The time to set all <see cref="ZipEntry.DateTime"/> values to.</param>
119 public ZipEntryFactory(DateTime time)
121 timeSetting_ = TimeSetting.Fixed;
122 FixedDateTime = time;
123 nameTransform_ = new ZipNameTransform();
130 /// Get / set the <see cref="INameTransform"/> to be used when creating new <see cref="ZipEntry"/> values.
133 /// Setting this property to null will cause a default <see cref="ZipNameTransform">name transform</see> to be used.
135 public INameTransform NameTransform
137 get { return nameTransform_; }
140 nameTransform_ = value ?? new ZipNameTransform();
145 /// Get / set the <see cref="TimeSetting"/> in use.
147 public TimeSetting Setting
149 get { return timeSetting_; }
150 set { timeSetting_ = value; }
154 /// Get / set the <see cref="DateTime"/> value to use when <see cref="Setting"/> is set to <see cref="TimeSetting.Fixed"/>
156 public DateTime FixedDateTime
158 get { return fixedDateTime_; }
161 if (value.Year < 1970) {
162 throw new ArgumentException("Value is too old to be valid", "value");
164 fixedDateTime_ = value;
169 /// A bitmask defining the attributes to be retrieved from the actual file.
171 /// <remarks>The default is to get all possible attributes from the actual file.</remarks>
172 public int GetAttributes
174 get { return getAttributes_; }
175 set { getAttributes_ = value; }
179 /// A bitmask defining which attributes are to be set on.
181 /// <remarks>By default no attributes are set on.</remarks>
182 public int SetAttributes
184 get { return setAttributes_; }
185 set { setAttributes_ = value; }
189 /// Get set a value indicating wether unidoce text should be set on.
191 public bool IsUnicodeText
193 get { return isUnicodeText_; }
194 set { isUnicodeText_ = value; }
199 #region IEntryFactory Members
202 /// Make a new <see cref="ZipEntry"/> for a file.
204 /// <param name="fileName">The name of the file to create a new entry for.</param>
205 /// <returns>Returns a new <see cref="ZipEntry"/> based on the <paramref name="fileName"/>.</returns>
206 public ZipEntry MakeFileEntry(string fileName)
208 return MakeFileEntry(fileName, true);
212 /// Make a new <see cref="ZipEntry"/> from a name.
214 /// <param name="fileName">The name of the file to create a new entry for.</param>
215 /// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param>
216 /// <returns>Returns a new <see cref="ZipEntry"/> based on the <paramref name="fileName"/>.</returns>
217 public ZipEntry MakeFileEntry(string fileName, bool useFileSystem)
219 var result = new ZipEntry(nameTransform_.TransformFile(fileName)){IsUnicodeText = isUnicodeText_};
221 var externalAttributes = 0;
222 var useAttributes = (setAttributes_ != 0);
227 fi = new FileInfo(fileName);
230 if ((fi != null) && fi.Exists)
232 switch (timeSetting_)
234 case TimeSetting.CreateTime:
235 result.DateTime = fi.CreationTime;
238 //case TimeSetting.CreateTimeUtc:
239 //result.DateTime = fi.CreationTimeUtc;
242 case TimeSetting.LastAccessTime:
243 result.DateTime = fi.LastAccessTime;
246 //case TimeSetting.LastAccessTimeUtc:
247 //result.DateTime = fi.LastAccessTimeUtc;
250 case TimeSetting.LastWriteTime:
251 result.DateTime = fi.LastWriteTime;
254 //case TimeSetting.LastWriteTimeUtc:
255 //result.DateTime = fi.LastWriteTimeUtc;
258 case TimeSetting.Fixed:
259 result.DateTime = fixedDateTime_;
263 throw new ZipException("Unhandled time setting in MakeFileEntry");
266 result.Size = fi.Length;
268 useAttributes = true;
269 externalAttributes = ((int)fi.Attributes & getAttributes_);
273 if (timeSetting_ == TimeSetting.Fixed)
275 result.DateTime = fixedDateTime_;
281 externalAttributes |= setAttributes_;
282 result.ExternalFileAttributes = externalAttributes;
289 /// Make a new <see cref="ZipEntry"></see> for a directory.
291 /// <param name="directoryName">The raw untransformed name for the new directory</param>
292 /// <returns>Returns a new <see cref="ZipEntry"></see> representing a directory.</returns>
293 public ZipEntry MakeDirectoryEntry(string directoryName)
295 return MakeDirectoryEntry(directoryName, true);
299 /// Make a new <see cref="ZipEntry"></see> for a directory.
301 /// <param name="directoryName">The raw untransformed name for the new directory</param>
302 /// <param name="useFileSystem">If true entry detail is retrieved from the file system if the file exists.</param>
303 /// <returns>Returns a new <see cref="ZipEntry"></see> representing a directory.</returns>
304 public ZipEntry MakeDirectoryEntry(string directoryName, bool useFileSystem)
307 var result = new ZipEntry(nameTransform_.TransformDirectory(directoryName)){Size = 0};
309 var externalAttributes = 0;
311 DirectoryInfo di = null;
315 di = new DirectoryInfo(directoryName);
319 if ((di != null) && di.Exists)
321 switch (timeSetting_)
323 case TimeSetting.CreateTime:
324 result.DateTime = di.CreationTime;
327 //case TimeSetting.CreateTimeUtc:
328 //result.DateTime = di.CreationTimeUtc;
331 case TimeSetting.LastAccessTime:
332 result.DateTime = di.LastAccessTime;
335 //case TimeSetting.LastAccessTimeUtc:
336 //result.DateTime = di.LastAccessTimeUtc;
339 case TimeSetting.LastWriteTime:
340 result.DateTime = di.LastWriteTime;
343 //case TimeSetting.LastWriteTimeUtc:
344 //result.DateTime = di.LastWriteTimeUtc;
347 case TimeSetting.Fixed:
348 result.DateTime = fixedDateTime_;
352 throw new ZipException("Unhandled time setting in MakeDirectoryEntry");
355 externalAttributes = ((int)di.Attributes & getAttributes_);
359 if (timeSetting_ == TimeSetting.Fixed)
361 result.DateTime = fixedDateTime_;
365 // Always set directory attribute on.
366 externalAttributes |= (setAttributes_ | 16);
367 result.ExternalFileAttributes = externalAttributes;
374 #region Instance Fields
375 INameTransform nameTransform_;
376 DateTime fixedDateTime_ = DateTime.Now;
377 TimeSetting timeSetting_;
380 int getAttributes_ = -1;