View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.commons.compress.compressors.gzip;
21  
22  import java.io.OutputStream;
23  import java.util.zip.Deflater;
24  
25  /**
26   * Parameters for the GZIP compressor.
27   *
28   * @see GzipCompressorInputStream
29   * @see GzipCompressorOutputStream
30   * @since 1.7
31   */
32  public class GzipParameters {
33  
34      private int compressionLevel = Deflater.DEFAULT_COMPRESSION;
35      private long modificationTime;
36      private String fileName;
37      private String comment;
38      private int operatingSystem = 255; // Unknown OS by default
39      private int bufferSize = 512;
40      private int deflateStrategy = Deflater.DEFAULT_STRATEGY;
41  
42      /**
43       * Gets size of the buffer used to retrieve compressed data.
44       *
45       * @return The size of the buffer used to retrieve compressed data.
46       * @since 1.21
47       * @see #setBufferSize(int)
48       */
49      public int getBufferSize() {
50          return this.bufferSize;
51      }
52  
53      public String getComment() {
54          return comment;
55      }
56  
57      public int getCompressionLevel() {
58          return compressionLevel;
59      }
60  
61      /**
62       * Gets the deflater strategy.
63       *
64       * @return the deflater strategy, {@link Deflater#DEFAULT_STRATEGY} by default.
65       * @see #setDeflateStrategy(int)
66       * @see Deflater#setStrategy(int)
67       * @since 1.23
68       */
69      public int getDeflateStrategy() {
70          return deflateStrategy;
71      }
72  
73      /**
74       * Gets the file name.
75       *
76       * @return the file name.
77       * @deprecated Use {@link #getFileName()}.
78       */
79      @Deprecated
80      public String getFilename() {
81          return fileName;
82      }
83  
84      /**
85       * Gets the file name.
86       *
87       * @return the file name.
88       * @since 2.25.0
89       */
90      public String getFileName() {
91          return fileName;
92      }
93  
94      public long getModificationTime() {
95          return modificationTime;
96      }
97  
98      public int getOperatingSystem() {
99          return operatingSystem;
100     }
101 
102     /**
103      * Sets size of the buffer used to retrieve compressed data from {@link Deflater} and write to underlying {@link OutputStream}.
104      *
105      * @param bufferSize the bufferSize to set. Must be a positive value.
106      * @since 1.21
107      */
108     public void setBufferSize(final int bufferSize) {
109         if (bufferSize <= 0) {
110             throw new IllegalArgumentException("invalid buffer size: " + bufferSize);
111         }
112         this.bufferSize = bufferSize;
113     }
114 
115     public void setComment(final String comment) {
116         this.comment = comment;
117     }
118 
119     /**
120      * Sets the compression level.
121      *
122      * @param compressionLevel the compression level (between 0 and 9)
123      * @see Deflater#NO_COMPRESSION
124      * @see Deflater#BEST_SPEED
125      * @see Deflater#DEFAULT_COMPRESSION
126      * @see Deflater#BEST_COMPRESSION
127      */
128     public void setCompressionLevel(final int compressionLevel) {
129         if (compressionLevel < -1 || compressionLevel > 9) {
130             throw new IllegalArgumentException("Invalid gzip compression level: " + compressionLevel);
131         }
132         this.compressionLevel = compressionLevel;
133     }
134 
135     /**
136      * Sets the deflater strategy.
137      *
138      * @param deflateStrategy the new compression strategy
139      * @see Deflater#setStrategy(int)
140      * @since 1.23
141      */
142     public void setDeflateStrategy(final int deflateStrategy) {
143         this.deflateStrategy = deflateStrategy;
144     }
145 
146     /**
147      * Sets the name of the compressed file.
148      *
149      * @param fileName the name of the file without the directory path
150      * @deprecated Use {@link #setFileName(String)}.
151      */
152     @Deprecated
153     public void setFilename(final String fileName) {
154         this.fileName = fileName;
155     }
156 
157     /**
158      * Sets the name of the compressed file.
159      *
160      * @param fileName the name of the file without the directory path
161      */
162     public void setFileName(final String fileName) {
163         this.fileName = fileName;
164     }
165 
166     /**
167      * Sets the modification time of the compressed file.
168      *
169      * @param modificationTime the modification time, in milliseconds
170      */
171     public void setModificationTime(final long modificationTime) {
172         this.modificationTime = modificationTime;
173     }
174 
175     /**
176      * Sets the operating system on which the compression took place. The defined values are:
177      * <ul>
178      * <li>0: FAT file system (MS-DOS, OS/2, NT/Win32)</li>
179      * <li>1: Amiga</li>
180      * <li>2: VMS (or OpenVMS)</li>
181      * <li>3: Unix</li>
182      * <li>4: VM/CMS</li>
183      * <li>5: Atari TOS</li>
184      * <li>6: HPFS file system (OS/2, NT)</li>
185      * <li>7: Macintosh</li>
186      * <li>8: Z-System</li>
187      * <li>9: CP/M</li>
188      * <li>10: TOPS-20</li>
189      * <li>11: NTFS file system (NT)</li>
190      * <li>12: QDOS</li>
191      * <li>13: Acorn RISCOS</li>
192      * <li>255: Unknown</li>
193      * </ul>
194      *
195      * @param operatingSystem the code of the operating system
196      */
197     public void setOperatingSystem(final int operatingSystem) {
198         this.operatingSystem = operatingSystem;
199     }
200 }