1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.compress.archivers.sevenz;
18
19 import java.nio.file.attribute.FileTime;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.Objects;
26
27 import org.apache.commons.compress.archivers.ArchiveEntry;
28 import org.apache.commons.io.file.attribute.FileTimes;
29
30
31
32
33
34
35
36 public class SevenZArchiveEntry implements ArchiveEntry {
37
38 static final SevenZArchiveEntry[] EMPTY_SEVEN_Z_ARCHIVE_ENTRY_ARRAY = {};
39
40
41
42
43
44
45
46
47
48 @Deprecated
49 public static long javaTimeToNtfsTime(final Date date) {
50 return FileTimes.toNtfsTime(date);
51 }
52
53
54
55
56
57
58
59
60
61 @Deprecated
62 public static Date ntfsTimeToJavaTime(final long ntfsTime) {
63 return FileTimes.ntfsTimeToDate(ntfsTime);
64 }
65
66 private String name;
67 private boolean hasStream;
68 private boolean isDirectory;
69 private boolean isAntiItem;
70 private boolean hasCreationDate;
71 private boolean hasLastModifiedDate;
72 private boolean hasAccessDate;
73 private FileTime creationDate;
74 private FileTime lastModifiedDate;
75 private FileTime accessDate;
76 private boolean hasWindowsAttributes;
77 private int windowsAttributes;
78 private boolean hasCrc;
79 private long crc, compressedCrc;
80 private long size, compressedSize;
81 private Iterable<? extends SevenZMethodConfiguration> contentMethods;
82
83
84
85
86 public SevenZArchiveEntry() {
87 }
88
89 @Override
90 public boolean equals(final Object obj) {
91 if (this == obj) {
92 return true;
93 }
94 if (obj == null || getClass() != obj.getClass()) {
95 return false;
96 }
97 final SevenZArchiveEntry other = (SevenZArchiveEntry) obj;
98 return Objects.equals(name, other.name) && hasStream == other.hasStream && isDirectory == other.isDirectory && isAntiItem == other.isAntiItem
99 && hasCreationDate == other.hasCreationDate && hasLastModifiedDate == other.hasLastModifiedDate && hasAccessDate == other.hasAccessDate
100 && Objects.equals(creationDate, other.creationDate) && Objects.equals(lastModifiedDate, other.lastModifiedDate)
101 && Objects.equals(accessDate, other.accessDate) && hasWindowsAttributes == other.hasWindowsAttributes
102 && windowsAttributes == other.windowsAttributes && hasCrc == other.hasCrc && crc == other.crc && compressedCrc == other.compressedCrc
103 && size == other.size && compressedSize == other.compressedSize && equalSevenZMethods(contentMethods, other.contentMethods);
104 }
105
106 private boolean equalSevenZMethods(final Iterable<? extends SevenZMethodConfiguration> c1, final Iterable<? extends SevenZMethodConfiguration> c2) {
107 if (c1 == null) {
108 return c2 == null;
109 }
110 if (c2 == null) {
111 return false;
112 }
113 final Iterator<? extends SevenZMethodConfiguration> i2 = c2.iterator();
114 for (final SevenZMethodConfiguration element : c1) {
115 if (!i2.hasNext()) {
116 return false;
117 }
118 if (!element.equals(i2.next())) {
119 return false;
120 }
121 }
122 return !i2.hasNext();
123 }
124
125
126
127
128
129
130
131
132 public Date getAccessDate() {
133 return FileTimes.toDate(getAccessTime());
134 }
135
136
137
138
139
140
141
142
143 public FileTime getAccessTime() {
144 if (hasAccessDate) {
145 return accessDate;
146 }
147 throw new UnsupportedOperationException("The entry doesn't have this timestamp");
148 }
149
150
151
152
153
154
155
156 @Deprecated
157 int getCompressedCrc() {
158 return (int) compressedCrc;
159 }
160
161
162
163
164
165
166
167 long getCompressedCrcValue() {
168 return compressedCrc;
169 }
170
171
172
173
174
175
176 long getCompressedSize() {
177 return compressedSize;
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public Iterable<? extends SevenZMethodConfiguration> getContentMethods() {
196 return contentMethods;
197 }
198
199
200
201
202
203
204
205 @Deprecated
206 public int getCrc() {
207 return (int) crc;
208 }
209
210
211
212
213
214
215
216 public long getCrcValue() {
217 return crc;
218 }
219
220
221
222
223
224
225
226
227 public Date getCreationDate() {
228 return FileTimes.toDate(getCreationTime());
229 }
230
231
232
233
234
235
236
237
238 public FileTime getCreationTime() {
239 if (hasCreationDate) {
240 return creationDate;
241 }
242 throw new UnsupportedOperationException("The entry doesn't have this timestamp");
243 }
244
245
246
247
248
249
250 public boolean getHasAccessDate() {
251 return hasAccessDate;
252 }
253
254
255
256
257
258
259
260
261
262
263 public boolean getHasCrc() {
264 return hasCrc;
265 }
266
267
268
269
270
271
272 public boolean getHasCreationDate() {
273 return hasCreationDate;
274 }
275
276
277
278
279
280
281 public boolean getHasLastModifiedDate() {
282 return hasLastModifiedDate;
283 }
284
285
286
287
288
289
290 public boolean getHasWindowsAttributes() {
291 return hasWindowsAttributes;
292 }
293
294
295
296
297
298
299
300
301 @Override
302 public Date getLastModifiedDate() {
303 return FileTimes.toDate(getLastModifiedTime());
304 }
305
306
307
308
309
310
311
312
313 public FileTime getLastModifiedTime() {
314 if (hasLastModifiedDate) {
315 return lastModifiedDate;
316 }
317 throw new UnsupportedOperationException("The entry doesn't have this timestamp");
318 }
319
320
321
322
323
324
325
326
327
328
329 @Override
330 public String getName() {
331 return name;
332 }
333
334
335
336
337
338
339 @Override
340 public long getSize() {
341 return size;
342 }
343
344
345
346
347
348
349 public int getWindowsAttributes() {
350 return windowsAttributes;
351 }
352
353 @Override
354 public int hashCode() {
355 final String n = getName();
356 return n == null ? 0 : n.hashCode();
357 }
358
359
360
361
362
363
364 public boolean hasStream() {
365 return hasStream;
366 }
367
368
369
370
371
372
373 public boolean isAntiItem() {
374 return isAntiItem;
375 }
376
377
378
379
380
381
382 @Override
383 public boolean isDirectory() {
384 return isDirectory;
385 }
386
387
388
389
390
391
392
393 public void setAccessDate(final Date accessDate) {
394 setAccessTime(FileTimes.toFileTime(accessDate));
395 }
396
397
398
399
400
401
402 public void setAccessDate(final long ntfsAccessDate) {
403 this.accessDate = FileTimes.ntfsTimeToFileTime(ntfsAccessDate);
404 }
405
406
407
408
409
410
411
412 public void setAccessTime(final FileTime time) {
413 hasAccessDate = time != null;
414 if (hasAccessDate) {
415 this.accessDate = time;
416 }
417 }
418
419
420
421
422
423
424 public void setAntiItem(final boolean isAntiItem) {
425 this.isAntiItem = isAntiItem;
426 }
427
428
429
430
431
432
433
434 @Deprecated
435 void setCompressedCrc(final int crc) {
436 this.compressedCrc = crc;
437 }
438
439
440
441
442
443
444
445 void setCompressedCrcValue(final long crc) {
446 this.compressedCrc = crc;
447 }
448
449
450
451
452
453
454 void setCompressedSize(final long size) {
455 this.compressedSize = size;
456 }
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473 public void setContentMethods(final Iterable<? extends SevenZMethodConfiguration> methods) {
474 if (methods != null) {
475 final LinkedList<SevenZMethodConfiguration> l = new LinkedList<>();
476 methods.forEach(l::addLast);
477 contentMethods = Collections.unmodifiableList(l);
478 } else {
479 contentMethods = null;
480 }
481 }
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498 public void setContentMethods(final SevenZMethodConfiguration... methods) {
499 setContentMethods(Arrays.asList(methods));
500 }
501
502
503
504
505
506
507
508 @Deprecated
509 public void setCrc(final int crc) {
510 this.crc = crc;
511 }
512
513
514
515
516
517
518
519 public void setCrcValue(final long crc) {
520 this.crc = crc;
521 }
522
523
524
525
526
527
528
529 public void setCreationDate(final Date creationDate) {
530 setCreationTime(FileTimes.toFileTime(creationDate));
531 }
532
533
534
535
536
537
538 public void setCreationDate(final long ntfsCreationDate) {
539 this.creationDate = FileTimes.ntfsTimeToFileTime(ntfsCreationDate);
540 }
541
542
543
544
545
546
547
548 public void setCreationTime(final FileTime time) {
549 hasCreationDate = time != null;
550 if (hasCreationDate) {
551 this.creationDate = time;
552 }
553 }
554
555
556
557
558
559
560 public void setDirectory(final boolean isDirectory) {
561 this.isDirectory = isDirectory;
562 }
563
564
565
566
567
568
569 public void setHasAccessDate(final boolean hasAcessDate) {
570 this.hasAccessDate = hasAcessDate;
571 }
572
573
574
575
576
577
578 public void setHasCrc(final boolean hasCrc) {
579 this.hasCrc = hasCrc;
580 }
581
582
583
584
585
586
587 public void setHasCreationDate(final boolean hasCreationDate) {
588 this.hasCreationDate = hasCreationDate;
589 }
590
591
592
593
594
595
596 public void setHasLastModifiedDate(final boolean hasLastModifiedDate) {
597 this.hasLastModifiedDate = hasLastModifiedDate;
598 }
599
600
601
602
603
604
605 public void setHasStream(final boolean hasStream) {
606 this.hasStream = hasStream;
607 }
608
609
610
611
612
613
614 public void setHasWindowsAttributes(final boolean hasWindowsAttributes) {
615 this.hasWindowsAttributes = hasWindowsAttributes;
616 }
617
618
619
620
621
622
623
624 public void setLastModifiedDate(final Date lastModifiedDate) {
625 setLastModifiedTime(FileTimes.toFileTime(lastModifiedDate));
626 }
627
628
629
630
631
632
633 public void setLastModifiedDate(final long ntfsLastModifiedDate) {
634 this.lastModifiedDate = FileTimes.ntfsTimeToFileTime(ntfsLastModifiedDate);
635 }
636
637
638
639
640
641
642
643 public void setLastModifiedTime(final FileTime time) {
644 hasLastModifiedDate = time != null;
645 if (hasLastModifiedDate) {
646 this.lastModifiedDate = time;
647 }
648 }
649
650
651
652
653
654
655 public void setName(final String name) {
656 this.name = name;
657 }
658
659
660
661
662
663
664 public void setSize(final long size) {
665 this.size = size;
666 }
667
668
669
670
671
672
673 public void setWindowsAttributes(final int windowsAttributes) {
674 this.windowsAttributes = windowsAttributes;
675 }
676 }