forked from icsharpcode/SharpZipLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZipEntry.cs
1368 lines (1239 loc) · 36 KB
/
ZipEntry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.IO;
namespace ICSharpCode.SharpZipLib.Zip
{
/// <summary>
/// Defines known values for the <see cref="HostSystemID"/> property.
/// </summary>
public enum HostSystemID
{
/// <summary>
/// Host system = MSDOS
/// </summary>
Msdos = 0,
/// <summary>
/// Host system = Amiga
/// </summary>
Amiga = 1,
/// <summary>
/// Host system = Open VMS
/// </summary>
OpenVms = 2,
/// <summary>
/// Host system = Unix
/// </summary>
Unix = 3,
/// <summary>
/// Host system = VMCms
/// </summary>
VMCms = 4,
/// <summary>
/// Host system = Atari ST
/// </summary>
AtariST = 5,
/// <summary>
/// Host system = OS2
/// </summary>
OS2 = 6,
/// <summary>
/// Host system = Macintosh
/// </summary>
Macintosh = 7,
/// <summary>
/// Host system = ZSystem
/// </summary>
ZSystem = 8,
/// <summary>
/// Host system = Cpm
/// </summary>
Cpm = 9,
/// <summary>
/// Host system = Windows NT
/// </summary>
WindowsNT = 10,
/// <summary>
/// Host system = MVS
/// </summary>
MVS = 11,
/// <summary>
/// Host system = VSE
/// </summary>
Vse = 12,
/// <summary>
/// Host system = Acorn RISC
/// </summary>
AcornRisc = 13,
/// <summary>
/// Host system = VFAT
/// </summary>
Vfat = 14,
/// <summary>
/// Host system = Alternate MVS
/// </summary>
AlternateMvs = 15,
/// <summary>
/// Host system = BEOS
/// </summary>
BeOS = 16,
/// <summary>
/// Host system = Tandem
/// </summary>
Tandem = 17,
/// <summary>
/// Host system = OS400
/// </summary>
OS400 = 18,
/// <summary>
/// Host system = OSX
/// </summary>
OSX = 19,
/// <summary>
/// Host system = WinZIP AES
/// </summary>
WinZipAES = 99,
}
/// <summary>
/// This class represents an entry in a zip archive. This can be a file
/// or a directory
/// ZipFile and ZipInputStream will give you instances of this class as
/// information about the members in an archive. ZipOutputStream
/// uses an instance of this class when creating an entry in a Zip file.
/// <br/>
/// <br/>Author of the original java version : Jochen Hoenicke
/// </summary>
public class ZipEntry
{
[Flags]
private enum Known : byte
{
None = 0,
Size = 0x01,
CompressedSize = 0x02,
Crc = 0x04,
Time = 0x08,
ExternalAttributes = 0x10,
}
#region Constructors
/// <summary>
/// Creates a zip entry with the given name.
/// </summary>
/// <param name="name">
/// The name for this entry. Can include directory components.
/// The convention for names is 'unix' style paths with relative names only.
/// There are with no device names and path elements are separated by '/' characters.
/// </param>
/// <exception cref="ArgumentNullException">
/// The name passed is null
/// </exception>
public ZipEntry(string name)
: this(name, 0, ZipConstants.VersionMadeBy, CompressionMethod.Deflated)
{
}
/// <summary>
/// Creates a zip entry with the given name and version required to extract
/// </summary>
/// <param name="name">
/// The name for this entry. Can include directory components.
/// The convention for names is 'unix' style paths with no device names and
/// path elements separated by '/' characters. This is not enforced see <see cref="CleanName(string)">CleanName</see>
/// on how to ensure names are valid if this is desired.
/// </param>
/// <param name="versionRequiredToExtract">
/// The minimum 'feature version' required this entry
/// </param>
/// <exception cref="ArgumentNullException">
/// The name passed is null
/// </exception>
internal ZipEntry(string name, int versionRequiredToExtract)
: this(name, versionRequiredToExtract, ZipConstants.VersionMadeBy,
CompressionMethod.Deflated)
{
}
/// <summary>
/// Initializes an entry with the given name and made by information
/// </summary>
/// <param name="name">Name for this entry</param>
/// <param name="madeByInfo">Version and HostSystem Information</param>
/// <param name="versionRequiredToExtract">Minimum required zip feature version required to extract this entry</param>
/// <param name="method">Compression method for this entry.</param>
/// <exception cref="ArgumentNullException">
/// The name passed is null
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// versionRequiredToExtract should be 0 (auto-calculate) or > 10
/// </exception>
/// <remarks>
/// This constructor is used by the ZipFile class when reading from the central header
/// It is not generally useful, use the constructor specifying the name only.
/// </remarks>
internal ZipEntry(string name, int versionRequiredToExtract, int madeByInfo,
CompressionMethod method)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
if (name.Length > 0xffff)
{
throw new ArgumentException("Name is too long", nameof(name));
}
if ((versionRequiredToExtract != 0) && (versionRequiredToExtract < 10))
{
throw new ArgumentOutOfRangeException(nameof(versionRequiredToExtract));
}
this.DateTime = DateTime.Now;
this.name = name;
this.versionMadeBy = (ushort)madeByInfo;
this.versionToExtract = (ushort)versionRequiredToExtract;
this.method = method;
IsUnicodeText = ZipStrings.UseUnicode;
}
/// <summary>
/// Creates a deep copy of the given zip entry.
/// </summary>
/// <param name="entry">
/// The entry to copy.
/// </param>
[Obsolete("Use Clone instead")]
public ZipEntry(ZipEntry entry)
{
if (entry == null)
{
throw new ArgumentNullException(nameof(entry));
}
known = entry.known;
name = entry.name;
size = entry.size;
compressedSize = entry.compressedSize;
crc = entry.crc;
dateTime = entry.DateTime;
method = entry.method;
comment = entry.comment;
versionToExtract = entry.versionToExtract;
versionMadeBy = entry.versionMadeBy;
externalFileAttributes = entry.externalFileAttributes;
flags = entry.flags;
zipFileIndex = entry.zipFileIndex;
offset = entry.offset;
forceZip64_ = entry.forceZip64_;
if (entry.extra != null)
{
extra = new byte[entry.extra.Length];
Array.Copy(entry.extra, 0, extra, 0, entry.extra.Length);
}
}
#endregion Constructors
/// <summary>
/// Get a value indicating whether the entry has a CRC value available.
/// </summary>
public bool HasCrc
{
get
{
return (known & Known.Crc) != 0;
}
}
/// <summary>
/// Get/Set flag indicating if entry is encrypted.
/// A simple helper routine to aid interpretation of <see cref="Flags">flags</see>
/// </summary>
/// <remarks>This is an assistant that interprets the <see cref="Flags">flags</see> property.</remarks>
public bool IsCrypted
{
get
{
return (flags & 1) != 0;
}
set
{
if (value)
{
flags |= 1;
}
else
{
flags &= ~1;
}
}
}
/// <summary>
/// Get / set a flag indicating whether entry name and comment text are
/// encoded in <a href="http://www.unicode.org">unicode UTF8</a>.
/// </summary>
/// <remarks>This is an assistant that interprets the <see cref="Flags">flags</see> property.</remarks>
public bool IsUnicodeText
{
get
{
return (flags & (int)GeneralBitFlags.UnicodeText) != 0;
}
set
{
if (value)
{
flags |= (int)GeneralBitFlags.UnicodeText;
}
else
{
flags &= ~(int)GeneralBitFlags.UnicodeText;
}
}
}
/// <summary>
/// Value used during password checking for PKZIP 2.0 / 'classic' encryption.
/// </summary>
internal byte CryptoCheckValue
{
get
{
return cryptoCheckValue_;
}
set
{
cryptoCheckValue_ = value;
}
}
/// <summary>
/// Get/Set general purpose bit flag for entry
/// </summary>
/// <remarks>
/// General purpose bit flag<br/>
/// <br/>
/// Bit 0: If set, indicates the file is encrypted<br/>
/// Bit 1-2 Only used for compression type 6 Imploding, and 8, 9 deflating<br/>
/// Imploding:<br/>
/// Bit 1 if set indicates an 8K sliding dictionary was used. If clear a 4k dictionary was used<br/>
/// Bit 2 if set indicates 3 Shannon-Fanno trees were used to encode the sliding dictionary, 2 otherwise<br/>
/// <br/>
/// Deflating:<br/>
/// Bit 2 Bit 1<br/>
/// 0 0 Normal compression was used<br/>
/// 0 1 Maximum compression was used<br/>
/// 1 0 Fast compression was used<br/>
/// 1 1 Super fast compression was used<br/>
/// <br/>
/// Bit 3: If set, the fields crc-32, compressed size
/// and uncompressed size are were not able to be written during zip file creation
/// The correct values are held in a data descriptor immediately following the compressed data. <br/>
/// Bit 4: Reserved for use by PKZIP for enhanced deflating<br/>
/// Bit 5: If set indicates the file contains compressed patch data<br/>
/// Bit 6: If set indicates strong encryption was used.<br/>
/// Bit 7-10: Unused or reserved<br/>
/// Bit 11: If set the name and comments for this entry are in <a href="http://www.unicode.org">unicode</a>.<br/>
/// Bit 12-15: Unused or reserved<br/>
/// </remarks>
/// <seealso cref="IsUnicodeText"></seealso>
/// <seealso cref="IsCrypted"></seealso>
public int Flags
{
get
{
return flags;
}
set
{
flags = value;
}
}
/// <summary>
/// Get/Set index of this entry in Zip file
/// </summary>
/// <remarks>This is only valid when the entry is part of a <see cref="ZipFile"></see></remarks>
public long ZipFileIndex
{
get
{
return zipFileIndex;
}
set
{
zipFileIndex = value;
}
}
/// <summary>
/// Get/set offset for use in central header
/// </summary>
public long Offset
{
get
{
return offset;
}
set
{
offset = value;
}
}
/// <summary>
/// Get/Set external file attributes as an integer.
/// The values of this are operating system dependant see
/// <see cref="HostSystem">HostSystem</see> for details
/// </summary>
public int ExternalFileAttributes
{
get
{
if ((known & Known.ExternalAttributes) == 0)
{
return -1;
}
else
{
return externalFileAttributes;
}
}
set
{
externalFileAttributes = value;
known |= Known.ExternalAttributes;
}
}
/// <summary>
/// Get the version made by for this entry or zero if unknown.
/// The value / 10 indicates the major version number, and
/// the value mod 10 is the minor version number
/// </summary>
public int VersionMadeBy
{
get
{
return (versionMadeBy & 0xff);
}
}
/// <summary>
/// Get a value indicating this entry is for a DOS/Windows system.
/// </summary>
public bool IsDOSEntry
{
get
{
return ((HostSystem == (int)HostSystemID.Msdos) ||
(HostSystem == (int)HostSystemID.WindowsNT));
}
}
/// <summary>
/// Test the external attributes for this <see cref="ZipEntry"/> to
/// see if the external attributes are Dos based (including WINNT and variants)
/// and match the values
/// </summary>
/// <param name="attributes">The attributes to test.</param>
/// <returns>Returns true if the external attributes are known to be DOS/Windows
/// based and have the same attributes set as the value passed.</returns>
private bool HasDosAttributes(int attributes)
{
bool result = false;
if ((known & Known.ExternalAttributes) != 0)
{
result |= (((HostSystem == (int)HostSystemID.Msdos) ||
(HostSystem == (int)HostSystemID.WindowsNT)) &&
(ExternalFileAttributes & attributes) == attributes);
}
return result;
}
/// <summary>
/// Gets the compatability information for the <see cref="ExternalFileAttributes">external file attribute</see>
/// If the external file attributes are compatible with MS-DOS and can be read
/// by PKZIP for DOS version 2.04g then this value will be zero. Otherwise the value
/// will be non-zero and identify the host system on which the attributes are compatible.
/// </summary>
///
/// <remarks>
/// The values for this as defined in the Zip File format and by others are shown below. The values are somewhat
/// misleading in some cases as they are not all used as shown. You should consult the relevant documentation
/// to obtain up to date and correct information. The modified appnote by the infozip group is
/// particularly helpful as it documents a lot of peculiarities. The document is however a little dated.
/// <list type="table">
/// <item>0 - MS-DOS and OS/2 (FAT / VFAT / FAT32 file systems)</item>
/// <item>1 - Amiga</item>
/// <item>2 - OpenVMS</item>
/// <item>3 - Unix</item>
/// <item>4 - VM/CMS</item>
/// <item>5 - Atari ST</item>
/// <item>6 - OS/2 HPFS</item>
/// <item>7 - Macintosh</item>
/// <item>8 - Z-System</item>
/// <item>9 - CP/M</item>
/// <item>10 - Windows NTFS</item>
/// <item>11 - MVS (OS/390 - Z/OS)</item>
/// <item>12 - VSE</item>
/// <item>13 - Acorn Risc</item>
/// <item>14 - VFAT</item>
/// <item>15 - Alternate MVS</item>
/// <item>16 - BeOS</item>
/// <item>17 - Tandem</item>
/// <item>18 - OS/400</item>
/// <item>19 - OS/X (Darwin)</item>
/// <item>99 - WinZip AES</item>
/// <item>remainder - unused</item>
/// </list>
/// </remarks>
public int HostSystem
{
get
{
return (versionMadeBy >> 8) & 0xff;
}
set
{
versionMadeBy &= 0x00ff;
versionMadeBy |= (ushort)((value & 0xff) << 8);
}
}
/// <summary>
/// Get minimum Zip feature version required to extract this entry
/// </summary>
/// <remarks>
/// Minimum features are defined as:<br/>
/// 1.0 - Default value<br/>
/// 1.1 - File is a volume label<br/>
/// 2.0 - File is a folder/directory<br/>
/// 2.0 - File is compressed using Deflate compression<br/>
/// 2.0 - File is encrypted using traditional encryption<br/>
/// 2.1 - File is compressed using Deflate64<br/>
/// 2.5 - File is compressed using PKWARE DCL Implode<br/>
/// 2.7 - File is a patch data set<br/>
/// 4.5 - File uses Zip64 format extensions<br/>
/// 4.6 - File is compressed using BZIP2 compression<br/>
/// 5.0 - File is encrypted using DES<br/>
/// 5.0 - File is encrypted using 3DES<br/>
/// 5.0 - File is encrypted using original RC2 encryption<br/>
/// 5.0 - File is encrypted using RC4 encryption<br/>
/// 5.1 - File is encrypted using AES encryption<br/>
/// 5.1 - File is encrypted using corrected RC2 encryption<br/>
/// 5.1 - File is encrypted using corrected RC2-64 encryption<br/>
/// 6.1 - File is encrypted using non-OAEP key wrapping<br/>
/// 6.2 - Central directory encryption (not confirmed yet)<br/>
/// 6.3 - File is compressed using LZMA<br/>
/// 6.3 - File is compressed using PPMD+<br/>
/// 6.3 - File is encrypted using Blowfish<br/>
/// 6.3 - File is encrypted using Twofish<br/>
/// </remarks>
/// <seealso cref="CanDecompress"></seealso>
public int Version
{
get
{
// Return recorded version if known.
if (versionToExtract != 0)
{
return versionToExtract & 0x00ff; // Only lower order byte. High order is O/S file system.
}
else
{
int result = 10;
if (AESKeySize > 0)
{
result = ZipConstants.VERSION_AES; // Ver 5.1 = AES
}
else if (CentralHeaderRequiresZip64)
{
result = ZipConstants.VersionZip64;
}
else if (CompressionMethod.Deflated == method)
{
result = 20;
}
else if (CompressionMethod.BZip2 == method)
{
result = ZipConstants.VersionBZip2;
}
else if (IsDirectory == true)
{
result = 20;
}
else if (IsCrypted == true)
{
result = 20;
}
else if (HasDosAttributes(0x08))
{
result = 11;
}
return result;
}
}
}
/// <summary>
/// Get a value indicating whether this entry can be decompressed by the library.
/// </summary>
/// <remarks>This is based on the <see cref="Version"></see> and
/// whether the <see cref="IsCompressionMethodSupported()">compression method</see> is supported.</remarks>
public bool CanDecompress
{
get
{
return (Version <= ZipConstants.VersionMadeBy) &&
((Version == 10) ||
(Version == 11) ||
(Version == 20) ||
(Version == 45) ||
(Version == 46) ||
(Version == 51)) &&
IsCompressionMethodSupported();
}
}
/// <summary>
/// Force this entry to be recorded using Zip64 extensions.
/// </summary>
public void ForceZip64()
{
forceZip64_ = true;
}
/// <summary>
/// Get a value indicating whether Zip64 extensions were forced.
/// </summary>
/// <returns>A <see cref="bool"/> value of true if Zip64 extensions have been forced on; false if not.</returns>
public bool IsZip64Forced()
{
return forceZip64_;
}
/// <summary>
/// Gets a value indicating if the entry requires Zip64 extensions
/// to store the full entry values.
/// </summary>
/// <value>A <see cref="bool"/> value of true if a local header requires Zip64 extensions; false if not.</value>
public bool LocalHeaderRequiresZip64
{
get
{
bool result = forceZip64_;
if (!result)
{
ulong trueCompressedSize = compressedSize;
if ((versionToExtract == 0) && IsCrypted)
{
trueCompressedSize += (ulong)this.EncryptionOverheadSize;
}
// TODO: A better estimation of the true limit based on compression overhead should be used
// to determine when an entry should use Zip64.
result =
((this.size >= uint.MaxValue) || (trueCompressedSize >= uint.MaxValue)) &&
((versionToExtract == 0) || (versionToExtract >= ZipConstants.VersionZip64));
}
return result;
}
}
/// <summary>
/// Get a value indicating whether the central directory entry requires Zip64 extensions to be stored.
/// </summary>
public bool CentralHeaderRequiresZip64
{
get
{
return LocalHeaderRequiresZip64 || (offset >= uint.MaxValue);
}
}
/// <summary>
/// Get/Set DosTime value.
/// </summary>
/// <remarks>
/// The MS-DOS date format can only represent dates between 1/1/1980 and 12/31/2107.
/// </remarks>
public long DosTime
{
get
{
if ((known & Known.Time) == 0)
{
return 0;
}
else
{
var year = (uint)DateTime.Year;
var month = (uint)DateTime.Month;
var day = (uint)DateTime.Day;
var hour = (uint)DateTime.Hour;
var minute = (uint)DateTime.Minute;
var second = (uint)DateTime.Second;
if (year < 1980)
{
year = 1980;
month = 1;
day = 1;
hour = 0;
minute = 0;
second = 0;
}
else if (year > 2107)
{
year = 2107;
month = 12;
day = 31;
hour = 23;
minute = 59;
second = 59;
}
return ((year - 1980) & 0x7f) << 25 |
(month << 21) |
(day << 16) |
(hour << 11) |
(minute << 5) |
(second >> 1);
}
}
set
{
unchecked
{
var dosTime = (uint)value;
uint sec = Math.Min(59, 2 * (dosTime & 0x1f));
uint min = Math.Min(59, (dosTime >> 5) & 0x3f);
uint hrs = Math.Min(23, (dosTime >> 11) & 0x1f);
uint mon = Math.Max(1, Math.Min(12, ((uint)(value >> 21) & 0xf)));
uint year = ((dosTime >> 25) & 0x7f) + 1980;
int day = Math.Max(1, Math.Min(DateTime.DaysInMonth((int)year, (int)mon), (int)((value >> 16) & 0x1f)));
DateTime = new DateTime((int)year, (int)mon, day, (int)hrs, (int)min, (int)sec, DateTimeKind.Unspecified);
}
}
}
/// <summary>
/// Gets/Sets the time of last modification of the entry.
/// </summary>
/// <remarks>
/// The <see cref="DosTime"></see> property is updated to match this as far as possible.
/// </remarks>
public DateTime DateTime
{
get
{
return dateTime;
}
set
{
dateTime = value;
known |= Known.Time;
}
}
/// <summary>
/// Returns the entry name.
/// </summary>
/// <remarks>
/// The unix naming convention is followed.
/// Path components in the entry should always separated by forward slashes ('/').
/// Dos device names like C: should also be removed.
/// See the <see cref="ZipNameTransform"/> class, or <see cref="CleanName(string)"/>
///</remarks>
public string Name
{
get
{
return name;
}
internal set
{
name = value;
}
}
/// <summary>
/// Gets/Sets the size of the uncompressed data.
/// </summary>
/// <returns>
/// The size or -1 if unknown.
/// </returns>
/// <remarks>Setting the size before adding an entry to an archive can help
/// avoid compatability problems with some archivers which dont understand Zip64 extensions.</remarks>
public long Size
{
get
{
return (known & Known.Size) != 0 ? (long)size : -1L;
}
set
{
this.size = (ulong)value;
this.known |= Known.Size;
}
}
/// <summary>
/// Gets/Sets the size of the compressed data.
/// </summary>
/// <returns>
/// The compressed entry size or -1 if unknown.
/// </returns>
public long CompressedSize
{
get
{
return (known & Known.CompressedSize) != 0 ? (long)compressedSize : -1L;
}
set
{
this.compressedSize = (ulong)value;
this.known |= Known.CompressedSize;
}
}
/// <summary>
/// Gets/Sets the crc of the uncompressed data.
/// </summary>
/// <exception cref="System.ArgumentOutOfRangeException">
/// Crc is not in the range 0..0xffffffffL
/// </exception>
/// <returns>
/// The crc value or -1 if unknown.
/// </returns>
public long Crc
{
get
{
return (known & Known.Crc) != 0 ? crc & 0xffffffffL : -1L;
}
set
{
if (((ulong)crc & 0xffffffff00000000L) != 0)
{
throw new ArgumentOutOfRangeException(nameof(value));
}
this.crc = (uint)value;
this.known |= Known.Crc;
}
}
/// <summary>
/// Gets/Sets the compression method. Only Deflated and Stored are supported.
/// </summary>
/// <returns>
/// The compression method for this entry
/// </returns>
/// <see cref="ICSharpCode.SharpZipLib.Zip.CompressionMethod.Deflated"/>
/// <see cref="ICSharpCode.SharpZipLib.Zip.CompressionMethod.Stored"/>
public CompressionMethod CompressionMethod
{
get
{
return method;
}
set
{
if (!IsCompressionMethodSupported(value))
{
throw new NotSupportedException("Compression method not supported");
}
this.method = value;
}
}
/// <summary>
/// Gets the compression method for outputting to the local or central header.
/// Returns same value as CompressionMethod except when AES encrypting, which
/// places 99 in the method and places the real method in the extra data.
/// </summary>
internal CompressionMethod CompressionMethodForHeader
{
get
{
return (AESKeySize > 0) ? CompressionMethod.WinZipAES : method;
}
}
/// <summary>
/// Gets/Sets the extra data.
/// </summary>
/// <exception cref="System.ArgumentOutOfRangeException">
/// Extra data is longer than 64KB (0xffff) bytes.
/// </exception>
/// <returns>
/// Extra data or null if not set.
/// </returns>
public byte[] ExtraData
{
get
{
// TODO: This is slightly safer but less efficient. Think about whether it should change.
// return (byte[]) extra.Clone();
return extra;
}
set
{
if (value == null)
{
extra = null;
}
else
{
if (value.Length > 0xffff)
{
throw new System.ArgumentOutOfRangeException(nameof(value));
}
extra = new byte[value.Length];
Array.Copy(value, 0, extra, 0, value.Length);
}
}
}
/// <summary>
/// For AES encrypted files returns or sets the number of bits of encryption (128, 192 or 256).
/// When setting, only 0 (off), 128 or 256 is supported.
/// </summary>
public int AESKeySize
{
get
{
// the strength (1 or 3) is in the entry header
switch (_aesEncryptionStrength)
{
case 0:
return 0; // Not AES
case 1:
return 128;
case 2:
return 192; // Not used by WinZip
case 3:
return 256;
default:
throw new ZipException("Invalid AESEncryptionStrength " + _aesEncryptionStrength);
}
}
set
{
switch (value)
{
case 0:
_aesEncryptionStrength = 0;
break;
case 128:
_aesEncryptionStrength = 1;
break;
case 256:
_aesEncryptionStrength = 3;
break;
default:
throw new ZipException("AESKeySize must be 0, 128 or 256: " + value);
}
}
}
/// <summary>
/// AES Encryption strength for storage in extra data in entry header.
/// 1 is 128 bit, 2 is 192 bit, 3 is 256 bit.
/// </summary>
internal byte AESEncryptionStrength
{
get
{
return (byte)_aesEncryptionStrength;
}
}
/// <summary>
/// Returns the length of the salt, in bytes
/// </summary>
internal int AESSaltLen