001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.util.zip;
019:
020: import java.util.Calendar;
021: import java.util.Date;
022: import java.util.GregorianCalendar;
023:
024: /**
025: * ZipEntry represents an entry in a zip file.
026: *
027: * @see ZipFile
028: * @see ZipInputStream
029: */
030: public class ZipEntry implements ZipConstants, Cloneable {
031: String name, comment;
032:
033: long compressedSize = -1, crc = -1, size = -1, dataOffset = -1;
034:
035: int compressionMethod = -1, time = -1, modDate = -1;
036:
037: byte[] extra;
038:
039: /**
040: * Zip entry state: Deflated
041: */
042: public static final int DEFLATED = 8;
043:
044: /**
045: * Zip entry state: Stored
046: */
047: public static final int STORED = 0;
048:
049: /**
050: * Constructs a new ZipEntry with the specified name.
051: *
052: * @param name
053: * the name of the zip entry
054: */
055: public ZipEntry(String name) {
056: if (name == null) {
057: throw new NullPointerException();
058: }
059: if (name.length() > 0xFFFF) {
060: throw new IllegalArgumentException();
061: }
062: this .name = name;
063: }
064:
065: /**
066: * Gets the comment for this ZipEntry.
067: *
068: * @return the comment for this ZipEntry, or null if there is no comment
069: */
070: public String getComment() {
071: return comment;
072: }
073:
074: /**
075: * Gets the compressed size of this ZipEntry.
076: *
077: * @return the compressed size, or -1 if the compressed size has not been
078: * set
079: */
080: public long getCompressedSize() {
081: return compressedSize;
082: }
083:
084: /**
085: * Gets the crc for this ZipEntry.
086: *
087: * @return the crc, or -1 if the crc has not been set
088: */
089: public long getCrc() {
090: return crc;
091: }
092:
093: /**
094: * Gets the extra information for this ZipEntry.
095: *
096: * @return a byte array containing the extra information, or null if there
097: * is none
098: */
099: public byte[] getExtra() {
100: return extra;
101: }
102:
103: /**
104: * Gets the compression method for this ZipEntry.
105: *
106: * @return the compression method, either DEFLATED, STORED or -1 if the
107: * compression method has not been set
108: */
109: public int getMethod() {
110: return compressionMethod;
111: }
112:
113: /**
114: * Gets the name of this ZipEntry.
115: *
116: * @return the entry name
117: */
118: public String getName() {
119: return name;
120: }
121:
122: /**
123: * Gets the uncompressed size of this ZipEntry.
124: *
125: * @return the uncompressed size, or -1 if the size has not been set
126: */
127: public long getSize() {
128: return size;
129: }
130:
131: /**
132: * Gets the last modification time of this ZipEntry.
133: *
134: * @return the last modification time as the number of milliseconds since
135: * Jan. 1, 1970
136: */
137: public long getTime() {
138: if (time != -1) {
139: GregorianCalendar cal = new GregorianCalendar();
140: cal.set(Calendar.MILLISECOND, 0);
141: cal.set(1980 + ((modDate >> 9) & 0x7f),
142: ((modDate >> 5) & 0xf) - 1, modDate & 0x1f,
143: (time >> 11) & 0x1f, (time >> 5) & 0x3f,
144: (time & 0x1f) << 1);
145: return cal.getTime().getTime();
146: }
147: return -1;
148: }
149:
150: /**
151: * Answers if this ZipEntry is a directory.
152: *
153: * @return <code>true</code> when this ZipEntry is a directory,
154: * <code>false<code> otherwise
155: */
156: public boolean isDirectory() {
157: return name.charAt(name.length() - 1) == '/';
158: }
159:
160: /**
161: * Sets the comment for this ZipEntry.
162: *
163: * @param string
164: * the comment
165: */
166: public void setComment(String string) {
167: if (string == null || string.length() <= 0xFFFF) {
168: comment = string;
169: } else {
170: throw new IllegalArgumentException();
171: }
172: }
173:
174: /**
175: * Sets the compressed size for this ZipEntry.
176: *
177: * @param value
178: * the compressed size
179: */
180: public void setCompressedSize(long value) {
181: compressedSize = value;
182: }
183:
184: /**
185: * Sets the crc for this ZipEntry.
186: *
187: * @param value
188: * the crc
189: *
190: * @throws IllegalArgumentException
191: * if value is < 0 or > 0xFFFFFFFFL
192: */
193: public void setCrc(long value) {
194: if (value >= 0 && value <= 0xFFFFFFFFL) {
195: crc = value;
196: } else {
197: throw new IllegalArgumentException();
198: }
199: }
200:
201: /**
202: * Sets the extra information for this ZipEntry.
203: *
204: * @param data
205: * a byte array containing the extra information
206: *
207: * @throws IllegalArgumentException
208: * when the length of data is > 0xFFFF bytes
209: */
210: public void setExtra(byte[] data) {
211: if (data == null || data.length <= 0xFFFF) {
212: extra = data;
213: } else {
214: throw new IllegalArgumentException();
215: }
216: }
217:
218: /**
219: * Sets the compression method for this ZipEntry.
220: *
221: * @param value
222: * the compression method, either DEFLATED or STORED
223: *
224: * @throws IllegalArgumentException
225: * when value is not DEFLATED or STORED
226: */
227: public void setMethod(int value) {
228: if (value != STORED && value != DEFLATED) {
229: throw new IllegalArgumentException();
230: }
231: compressionMethod = value;
232: }
233:
234: /**
235: * Sets the uncompressed size of this ZipEntry.
236: *
237: * @param value
238: * the uncompressed size
239: *
240: * @throws IllegalArgumentException
241: * if value is < 0 or > 0xFFFFFFFFL
242: */
243: public void setSize(long value) {
244: if (value >= 0 && value <= 0xFFFFFFFFL) {
245: size = value;
246: } else {
247: throw new IllegalArgumentException();
248: }
249: }
250:
251: /**
252: * Sets the last modification time of this ZipEntry.
253: *
254: * @param value
255: * the last modification time as the number of milliseconds since
256: * Jan. 1, 1970
257: */
258: public void setTime(long value) {
259: GregorianCalendar cal = new GregorianCalendar();
260: cal.setTime(new Date(value));
261: int year = cal.get(Calendar.YEAR);
262: if (year < 1980) {
263: modDate = 0x21;
264: time = 0;
265: } else {
266: modDate = cal.get(Calendar.DATE);
267: modDate = (cal.get(Calendar.MONTH) + 1 << 5) | modDate;
268: modDate = ((cal.get(Calendar.YEAR) - 1980) << 9) | modDate;
269: time = cal.get(Calendar.SECOND) >> 1;
270: time = (cal.get(Calendar.MINUTE) << 5) | time;
271: time = (cal.get(Calendar.HOUR_OF_DAY) << 11) | time;
272: }
273: }
274:
275: /**
276: * Answers the string representation of this ZipEntry.
277: *
278: * @return the string representation of this ZipEntry
279: */
280: @Override
281: public String toString() {
282: return name;
283: }
284:
285: ZipEntry(String name, String comment, byte[] extra, long modTime,
286: long size, long compressedSize, long crc,
287: int compressionMethod, long modDate, long offset) {
288: this .name = name;
289: this .comment = comment;
290: this .extra = extra;
291: time = (int) modTime;
292: this .size = size;
293: this .compressedSize = compressedSize;
294: this .crc = crc;
295: this .compressionMethod = compressionMethod;
296: this .modDate = (int) modDate;
297: dataOffset = offset;
298: }
299:
300: /**
301: * Constructs a new ZipEntry using the values obtained from ze.
302: *
303: * @param ze
304: * ZipEntry from which to obtain values.
305: */
306: public ZipEntry(ZipEntry ze) {
307: name = ze.name;
308: comment = ze.comment;
309: time = ze.time;
310: size = ze.size;
311: compressedSize = ze.compressedSize;
312: crc = ze.crc;
313: compressionMethod = ze.compressionMethod;
314: modDate = ze.modDate;
315: extra = ze.extra;
316: dataOffset = ze.dataOffset;
317: }
318:
319: /**
320: * Returns a shallow copy of this entry
321: *
322: * @return a copy of this entry
323: */
324: @Override
325: public Object clone() {
326: return new ZipEntry(this );
327: }
328:
329: /**
330: * Returns the hashCode for this ZipEntry.
331: *
332: * @return the hashCode of the entry
333: */
334: @Override
335: public int hashCode() {
336: return name.hashCode();
337: }
338: }
|