2 // Copyright (C) 2001 Mike Krueger
4 // This file was translated from java, it was part of the GNU Classpath
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 namespace ICSharpCode
.SharpZipLib
.Zip
43 public enum CompressionMethod
50 /// This class represents a member of a zip archive. ZipFile and
51 /// ZipInputStream will give you instances of this class as information
52 /// about the members in an archive. On the other hand ZipOutputStream
53 /// needs an instance of this class to create a new member.
55 /// author of the original java version : Jochen Hoenicke
57 public class ZipEntry
: ICloneable
59 static int KNOWN_SIZE
= 1;
60 static int KNOWN_CSIZE
= 2;
61 static int KNOWN_CRC
= 4;
62 static int KNOWN_TIME
= 8;
72 CompressionMethod method
= CompressionMethod
.Deflated
;
74 string comment
= null;
77 int zipFileIndex
= -1; /* used by ZipFile */
78 int flags
; /* used by ZipOutputStream */
79 int offset
; /* used by ZipFile and ZipOutputStream */
81 public bool IsEncrypted
{
83 return (flags
& 1) != 0;
94 public int ZipFileIndex
{
112 public int Flags
{ // Stops having two things represent same concept in class (flag isCrypted removed)
123 /// Creates a zip entry with the given name.
125 /// <param name="name">
126 /// the name. May include directory components separated by '/'.
128 public ZipEntry(string name
)
131 throw new System
.ArgumentNullException("name");
133 this.DateTime
= System
.DateTime
.Now
;
138 /// Creates a copy of the given zip entry.
141 /// the entry to copy.
143 public ZipEntry(ZipEntry e
)
148 compressedSize
= e
.compressedSize
;
161 version
= (ushort)value;
165 public long DosTime
{
167 if ((known
& KNOWN_TIME
) == 0) {
174 this.dosTime
= (uint)value;
175 known
|= (ushort)KNOWN_TIME
;
181 /// Gets/Sets the time of last modification of the entry.
183 public DateTime DateTime
{
185 uint sec
= 2 * (dosTime
& 0x1f);
186 uint min
= (dosTime
>> 5) & 0x3f;
187 uint hrs
= (dosTime
>> 11) & 0x1f;
188 uint day
= (dosTime
>> 16) & 0x1f;
189 uint mon
= ((dosTime
>> 21) & 0xf);
190 uint year
= ((dosTime
>> 25) & 0x7f) + 1980; /* since 1900 */
191 return new System
.DateTime((int)year
, (int)mon
, (int)day
, (int)hrs
, (int)min
, (int)sec
);
194 DosTime
= ((uint)value.Year
- 1980 & 0x7f) << 25 |
195 ((uint)value.Month
) << 21 |
196 ((uint)value.Day
) << 16 |
197 ((uint)value.Hour
) << 11 |
198 ((uint)value.Minute
) << 5 |
199 ((uint)value.Second
) >> 1;
204 /// Returns the entry name. The path components in the entry are
205 /// always separated by slashes ('/').
214 // /// Gets/Sets the time of last modification of the entry.
217 // /// the time of last modification of the entry, or -1 if unknown.
219 // public long Time {
221 // return (known & KNOWN_TIME) != 0 ? time * 1000L : -1;
224 // this.time = (int) (value / 1000L);
225 // this.known |= (ushort)KNOWN_TIME;
230 /// Gets/Sets the size of the uncompressed data.
232 /// <exception cref="System.ArgumentOutOfRangeException">
233 /// if size is not in 0..0xffffffffL
236 /// the size or -1 if unknown.
240 return (known
& KNOWN_SIZE
) != 0 ? (long)size
: -1L;
243 if (((ulong)value & 0xFFFFFFFF00000000L
) != 0) {
244 throw new ArgumentOutOfRangeException("size");
246 this.size
= (uint)value;
247 this.known
|= (ushort)KNOWN_SIZE
;
252 /// Gets/Sets the size of the compressed data.
254 /// <exception cref="System.ArgumentOutOfRangeException">
255 /// if csize is not in 0..0xffffffffL
258 /// the size or -1 if unknown.
260 public long CompressedSize
{
262 return (known
& KNOWN_CSIZE
) != 0 ? (long)compressedSize
: -1L;
265 if (((ulong)value & 0xffffffff00000000L
) != 0) {
266 throw new ArgumentOutOfRangeException();
268 this.compressedSize
= (uint)value;
269 this.known
|= (ushort)KNOWN_CSIZE
;
274 /// Gets/Sets the crc of the uncompressed data.
276 /// <exception cref="System.ArgumentOutOfRangeException">
277 /// if crc is not in 0..0xffffffffL
280 /// the crc or -1 if unknown.
284 return (known
& KNOWN_CRC
) != 0 ? crc
& 0xffffffffL
: -1L;
287 if (((ulong)crc
& 0xffffffff00000000L
) != 0)
289 throw new Exception();
291 this.crc
= (uint)value;
292 this.known
|= (ushort)KNOWN_CRC
;
297 /// Gets/Sets the compression method. Only DEFLATED and STORED are supported.
299 /// <exception cref="System.ArgumentOutOfRangeException">
300 /// if method is not supported.
303 /// the compression method or -1 if unknown.
305 /// <see cref="ZipOutputStream.DEFLATED"/>
306 /// <see cref="ZipOutputStream.STORED"/>
307 public CompressionMethod CompressionMethod
{
317 /// Gets/Sets the extra data.
319 /// <exception cref="System.ArgumentOutOfRangeException">
320 /// if extra is longer than 0xffff bytes.
323 /// the extra data or null if not set.
325 public byte[] ExtraData
{
335 if (value.Length
> 0xffff) {
336 throw new System
.ArgumentOutOfRangeException();
341 while (pos
< extra
.Length
) {
342 int sig
= (extra
[pos
++] & 0xff) | (extra
[pos
++] & 0xff) << 8;
343 int len
= (extra
[pos
++] & 0xff) | (extra
[pos
++] & 0xff) << 8;
345 /* extended time stamp, unix format by Rainer Prem <Rainer@Prem.de> */
346 int flags
= extra
[pos
];
347 if ((flags
& 1) != 0) {
348 int iTime
= ((extra
[pos
+1] & 0xff) |
349 (extra
[pos
+2] & 0xff) << 8 |
350 (extra
[pos
+3] & 0xff) << 16 |
351 (extra
[pos
+4] & 0xff) << 24);
353 DateTime
= (new DateTime ( 1970, 1, 1, 0, 0, 0 ) + new TimeSpan ( 0, 0, 0, iTime
, 0 )).ToLocalTime ();
354 known
|= (ushort)KNOWN_TIME
;
359 } catch (Exception
) {
367 /// Gets/Sets the entry comment.
369 /// <exception cref="System.ArgumentOutOfRangeException">
370 /// if comment is longer than 0xffff.
373 /// the comment or null if not set.
375 public string Comment
{
380 if (value.Length
> 0xffff)
382 throw new ArgumentOutOfRangeException();
384 this.comment
= value;
389 /// Gets true, if the entry is a directory. This is solely
390 /// determined by the name, a trailing slash '/' marks a directory.
392 public bool IsDirectory
{
394 int nlen
= name
.Length
;
395 return nlen
> 0 && name
[nlen
- 1] == '/';
400 /// True, if the entry is encrypted.
402 public bool IsCrypted
{
412 /// Creates a copy of this zip entry.
414 public object Clone()
416 return this.MemberwiseClone();
420 /// Gets the string representation of this ZipEntry. This is just
421 /// the name as returned by getName().
423 public override string ToString()