1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.commons.compress.compressors.gzip;
21
22 import java.io.OutputStream;
23 import java.nio.charset.Charset;
24 import java.time.Instant;
25 import java.util.Objects;
26 import java.util.zip.Deflater;
27
28 import org.apache.commons.io.Charsets;
29 import org.apache.commons.lang3.ArrayUtils;
30 import org.apache.commons.lang3.StringUtils;
31
32
33
34
35
36
37
38
39
40 public class GzipParameters {
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 public enum OS {
66
67
68
69
70 ACORN_RISCOS(OS_ACORN_RISCOS),
71
72
73
74
75 AMIGA(OS_AMIGA),
76
77
78
79
80 ATARI_TOS(OS_ATARI_TOS),
81
82
83
84
85 CPM(OS_CPM),
86
87
88
89
90
91 FAT(OS_FAT),
92
93
94
95
96 HPFS(OS_HPFS),
97
98
99
100
101 MACINTOSH(OS_MACINTOSH),
102
103
104
105
106 NTFS(OS_NTFS),
107
108
109
110
111 QDOS(OS_QDOS),
112
113
114
115
116 TOPS_20(OS_TOPS_20),
117
118
119
120
121 UNIX(OS_UNIX),
122
123
124
125
126 UNKNOWN(OS_UNKNOWN),
127
128
129
130
131 VM_CMS(OS_VM_CMS),
132
133
134
135
136 VMS(OS_VMS),
137
138
139
140
141 Z_SYSTEM(OS_Z_SYSTEM);
142
143
144
145
146
147
148
149
150 public static OS from(final int code) {
151 switch (code) {
152 case OS_ACORN_RISCOS:
153 return ACORN_RISCOS;
154 case OS_AMIGA:
155 return AMIGA;
156 case OS_ATARI_TOS:
157 return ATARI_TOS;
158 case OS_CPM:
159 return CPM;
160 case OS_FAT:
161 return FAT;
162 case OS_HPFS:
163 return HPFS;
164 case OS_MACINTOSH:
165 return MACINTOSH;
166 case OS_NTFS:
167 return NTFS;
168 case OS_QDOS:
169 return QDOS;
170 case OS_TOPS_20:
171 return TOPS_20;
172 case OS_UNIX:
173 return UNIX;
174 case OS_UNKNOWN:
175 return UNKNOWN;
176 case OS_VM_CMS:
177 return VM_CMS;
178 case OS_VMS:
179 return VMS;
180 case OS_Z_SYSTEM:
181 return Z_SYSTEM;
182 default:
183 return UNKNOWN;
184 }
185 }
186
187 private final int type;
188
189
190
191
192
193
194 OS(final int type) {
195 this.type = type;
196 }
197
198
199
200
201
202
203 public int type() {
204 return type;
205 }
206
207 }
208
209 private static final int BUFFER_SIZE = 512;
210
211
212
213
214 private static final int OS_ACORN_RISCOS = 13;
215
216
217
218
219 private static final int OS_AMIGA = 1;
220
221
222
223
224 private static final int OS_ATARI_TOS = 5;
225
226
227
228
229 private static final int OS_CPM = 9;
230
231
232
233
234 private static final int OS_FAT = 0;
235
236
237
238
239 private static final int OS_HPFS = 6;
240
241
242
243
244 private static final int OS_MACINTOSH = 7;
245
246
247
248
249 private static final int OS_NTFS = 11;
250
251
252
253
254 private static final int OS_QDOS = 12;
255
256
257
258
259 private static final int OS_TOPS_20 = 10;
260
261
262
263
264 private static final int OS_UNIX = 3;
265
266
267
268
269 private static final int OS_UNKNOWN = 255;
270
271
272
273
274 private static final int OS_VM_CMS = 4;
275
276
277
278
279 private static final int OS_VMS = 2;
280
281
282
283
284 private static final int OS_Z_SYSTEM = 8;
285
286 private int bufferSize = BUFFER_SIZE;
287
288 private String comment;
289 private int compressionLevel = Deflater.DEFAULT_COMPRESSION;
290 private int deflateStrategy = Deflater.DEFAULT_STRATEGY;
291 private ExtraField extraField;
292 private String fileName;
293 private Charset fileNameCharset = GzipUtils.GZIP_ENCODING;
294 private boolean headerCrc;
295
296
297
298
299
300
301
302
303 private Instant modificationInstant = Instant.EPOCH;
304 private OS operatingSystem = OS.UNKNOWN;
305 private long trailerCrc;
306 private long trailerISize;
307
308 @Override
309 public boolean equals(final Object obj) {
310 if (this == obj) {
311 return true;
312 }
313 if (!(obj instanceof GzipParameters)) {
314 return false;
315 }
316 final GzipParameters other = (GzipParameters) obj;
317 return bufferSize == other.bufferSize && Objects.equals(comment, other.comment) && compressionLevel == other.compressionLevel
318 && deflateStrategy == other.deflateStrategy && Objects.equals(extraField, other.extraField) && Objects.equals(fileName, other.fileName)
319 && Objects.equals(fileNameCharset, other.fileNameCharset) && headerCrc == other.headerCrc
320 && Objects.equals(modificationInstant, other.modificationInstant) && operatingSystem == other.operatingSystem && trailerCrc == other.trailerCrc
321 && trailerISize == other.trailerISize;
322 }
323
324
325
326
327
328
329
330
331 public int getBufferSize() {
332 return this.bufferSize;
333 }
334
335
336
337
338
339
340 public String getComment() {
341 return comment;
342 }
343
344
345
346
347
348
349
350
351
352
353 public int getCompressionLevel() {
354 return compressionLevel;
355 }
356
357
358
359
360
361
362
363
364
365 public int getDeflateStrategy() {
366 return deflateStrategy;
367 }
368
369
370
371
372
373
374
375 public ExtraField getExtraField() {
376 return extraField;
377 }
378
379
380
381
382
383
384
385 @Deprecated
386 public String getFilename() {
387 return fileName;
388 }
389
390
391
392
393
394
395
396 public String getFileName() {
397 return fileName;
398 }
399
400
401
402
403
404
405
406
407
408
409 public Charset getFileNameCharset() {
410 return fileNameCharset;
411 }
412
413
414
415
416
417
418
419 public boolean getHeaderCRC() {
420 return headerCrc;
421 }
422
423
424
425
426
427
428
429 public Instant getModificationInstant() {
430 return modificationInstant;
431 }
432
433
434
435
436
437
438
439
440
441
442
443 public long getModificationTime() {
444 return modificationInstant.getEpochSecond();
445 }
446
447
448
449
450
451
452 public int getOperatingSystem() {
453 return operatingSystem.type;
454 }
455
456
457
458
459
460
461
462 public OS getOS() {
463 return operatingSystem;
464 }
465
466
467
468
469
470
471
472 public long getTrailerCrc() {
473 return trailerCrc;
474 }
475
476
477
478
479
480
481
482 public long getTrailerISize() {
483 return trailerISize;
484 }
485
486 @Override
487 public int hashCode() {
488 return Objects.hash(bufferSize, comment, compressionLevel, deflateStrategy, extraField, fileName, fileNameCharset, headerCrc, modificationInstant,
489 operatingSystem, trailerCrc, trailerISize);
490 }
491
492 private String requireNonNulByte(final String text) {
493 if (StringUtils.isNotEmpty(text) && ArrayUtils.contains(text.getBytes(fileNameCharset), (byte) 0)) {
494 throw new IllegalArgumentException("String encoded in Charset '" + fileNameCharset + "' contains the nul byte 0 which is not supported in gzip.");
495 }
496 return text;
497 }
498
499
500
501
502
503
504
505 public void setBufferSize(final int bufferSize) {
506 if (bufferSize <= 0) {
507 throw new IllegalArgumentException("invalid buffer size: " + bufferSize);
508 }
509 this.bufferSize = bufferSize;
510 }
511
512
513
514
515
516
517
518 public void setComment(final String comment) {
519 this.comment = requireNonNulByte(comment);
520 }
521
522
523
524
525
526
527
528
529
530
531 public void setCompressionLevel(final int compressionLevel) {
532 if (compressionLevel < -1 || compressionLevel > 9) {
533 throw new IllegalArgumentException("Invalid gzip compression level: " + compressionLevel);
534 }
535 this.compressionLevel = compressionLevel;
536 }
537
538
539
540
541
542
543
544
545 public void setDeflateStrategy(final int deflateStrategy) {
546 this.deflateStrategy = deflateStrategy;
547 }
548
549
550
551
552
553
554
555
556 public void setExtraField(final ExtraField extra) {
557 this.extraField = extra;
558 }
559
560
561
562
563
564
565
566
567 @Deprecated
568 public void setFilename(final String fileName) {
569 setFileName(fileName);
570 }
571
572
573
574
575
576
577
578 public void setFileName(final String fileName) {
579 this.fileName = requireNonNulByte(fileName);
580 }
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595 public void setFileNameCharset(final Charset charset) {
596 this.fileNameCharset = Charsets.toCharset(charset, GzipUtils.GZIP_ENCODING);
597 }
598
599
600
601
602
603
604
605
606 public void setHeaderCRC(final boolean headerCRC) {
607 this.headerCrc = headerCRC;
608 }
609
610
611
612
613
614
615
616 public void setModificationInstant(final Instant modificationTime) {
617 this.modificationInstant = modificationTime != null ? modificationTime : Instant.EPOCH;
618 }
619
620
621
622
623
624
625
626
627
628
629
630 public void setModificationTime(final long modificationTimeSeconds) {
631 this.modificationInstant = Instant.ofEpochSecond(modificationTimeSeconds);
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 public void setOperatingSystem(final int operatingSystem) {
657 this.operatingSystem = OS.from(operatingSystem);
658 }
659
660
661
662
663
664
665
666 public void setOS(final OS os) {
667 this.operatingSystem = os != null ? os : OS.UNKNOWN;
668 }
669
670 void setTrailerCrc(final long trailerCrc) {
671 this.trailerCrc = trailerCrc;
672 }
673
674 void setTrailerISize(final long trailerISize) {
675 this.trailerISize = trailerISize;
676 }
677
678 @Override
679 public String toString() {
680 final StringBuilder builder = new StringBuilder();
681 builder.append("GzipParameters [bufferSize=").append(bufferSize).append(", comment=").append(comment).append(", compressionLevel=")
682 .append(compressionLevel).append(", deflateStrategy=").append(deflateStrategy).append(", extraField=").append(extraField).append(", fileName=")
683 .append(fileName).append(", fileNameCharset=").append(fileNameCharset).append(", headerCrc=").append(headerCrc).append(", modificationInstant=")
684 .append(modificationInstant).append(", operatingSystem=").append(operatingSystem).append(", trailerCrc=").append(trailerCrc)
685 .append(", trailerISize=").append(trailerISize).append("]");
686 return builder.toString();
687 }
688 }