e.g. Calendar Search Help
You must enter a value before pressing Search
ant

Class: org.apache.tools.zip.ZipEntry   ©

 OK to copy?
001 /*
002  * Copyright  2001-2004 The Apache Software Foundation
003  *
004  *  Licensed under the Apache License, Version 2.0 (the "License");
005  *  you may not use this file except in compliance with the License.
006  *  You may obtain a copy of the License at
007  *
008  *      http://www.apache.org/licenses/LICENSE-2.0
009  *
010  *  Unless required by applicable law or agreed to in writing, software
011  *  distributed under the License is distributed on an "AS IS" BASIS,
012  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  *  See the License for the specific language governing permissions and
014  *  limitations under the License.
015  *
016  */
017 
018 package org.apache.tools.zip;
019 
020 import java.lang.reflect.InvocationTargetException;
021 import java.lang.reflect.Method;
022 import java.util.Vector;
023 import java.util.zip.ZipException;
024 
025 /**
026  * Extension that adds better handling of extra fields and provides
027  * access to the internal and external file attributes.
028  *
029  * @version $Revision: 1.12.2.4 $
030  */
031 public class ZipEntry extends java.util.zip.ZipEntry implements Cloneable {
032 
033     private static final int PLATFORM_UNIX = 3;
034     private static final int PLATFORM_FAT  = 0;
035 
036     private int internalAttributes = 0;
037     private int platform = PLATFORM_FAT;
038     private long externalAttributes = 0;
039     private Vector extraFields = new Vector();
040     private String name = null;
041 
042     /**
043      * Creates a new zip entry with the specified name.
044      *
045      * @since 1.1
046      */
047     public ZipEntry(String name) {
048         super(name);
049     }
050 
051     /**
052      * Creates a new zip entry with fields taken from the specified zip entry.
053      *
054      * @since 1.1
055      */
056     public ZipEntry(java.util.zip.ZipEntry entry) throws ZipException {
057         /*
058          * REVISIT: call super(entry) instead of this stuff in Ant2,
059          *          "copy constructor" has not been available in JDK 1.1
060          */
061         super(entry.getName());
062 
063         setComment(entry.getComment());
064         setMethod(entry.getMethod());
065         setTime(entry.getTime());
066 
067         long size = entry.getSize();
068         if (size > 0) {
069             setSize(size);
070         }
071         long cSize = entry.getCompressedSize();
072         if (cSize > 0) {
073             setComprSize(cSize);
074         }
075         long crc = entry.getCrc();
076         if (crc > 0) {
077             setCrc(crc);
078         }
079 
080         byte[] extra = entry.getExtra();
081         if (extra != null) {
082             setExtraFields(ExtraFieldUtils.parse(extra));
083         } else {
084             // initializes extra data to an empty byte array
085             setExtra();
086         }
087     }
088 
089     /**
090      * Creates a new zip entry with fields taken from the specified zip entry.
091      *
092      * @since 1.1
093      */
094     public ZipEntry(ZipEntry entry) throws ZipException {
095         this((java.util.zip.ZipEntry) entry);
096         setInternalAttributes(entry.getInternalAttributes());
097         setExternalAttributes(entry.getExternalAttributes());
098         setExtraFields(entry.getExtraFields());
099     }
100 
101     /**
102      * @since 1.9
103      */
104     protected ZipEntry() {
105         super("");
106     }
107 
108     /**
109      * Overwrite clone
110      *
111      * @since 1.1
112      */
113     public Object clone() {
114         try {
115             ZipEntry e = (ZipEntry) super.clone();
116 
117             e.setName(getName());
118             e.setComment(getComment());
119             e.setMethod(getMethod());
120             e.setTime(getTime());
121             long size = getSize();
122             if (size > 0) {
123                 e.setSize(size);
124             }
125             long cSize = getCompressedSize();
126             if (cSize > 0) {
127                 e.setComprSize(cSize);
128             }
129             long crc = getCrc();
130             if (crc > 0) {
131                 e.setCrc(crc);
132             }
133 
Rate134             e.extraFields = (Vector) extraFields.clone();
135             e.setInternalAttributes(getInternalAttributes());
136             e.setExternalAttributes(getExternalAttributes());
137             e.setExtraFields(getExtraFields());
138             return e;
139         } catch (Throwable t) {
140             // in JDK 1.1 ZipEntry is not Cloneable, so super.clone declares
141             // to throw CloneNotSupported - since JDK 1.2 it is overridden to
142             // not throw that exception
143             return null;
144         }
145     }
146 
147     /**
148      * Retrieves the internal file attributes.
149      *
150      * @since 1.1
151      */
152     public int getInternalAttributes() {
153         return internalAttributes;
154     }
155 
156     /**
157      * Sets the internal file attributes.
158      *
159      * @since 1.1
160      */
161     public void setInternalAttributes(int value) {
162         internalAttributes = value;
163     }
164 
165     /**
166      * Retrieves the external file attributes.
167      *
168      * @since 1.1
169      */
170     public long getExternalAttributes() {
171         return externalAttributes;
172     }
173 
174     /**
175      * Sets the external file attributes.
176      *
177      * @since 1.1
178      */
179     public void setExternalAttributes(long value) {
180         externalAttributes = value;
181     }
182 
183     /**
184      * Sets Unix permissions in a way that is understood by Info-Zip's
185      * unzip command.
186      *
187      * @since Ant 1.5.2
188      */
189     public void setUnixMode(int mode) {
190         setExternalAttributes((mode << 16)
191                               // MS-DOS read-only attribute
192                               | ((mode & 0200) == 0 ? 1 : 0)
193                               // MS-DOS directory flag
194                               | (isDirectory() ? 0x10 : 0));
195         platform = PLATFORM_UNIX;
196     }
197 
198     /**
199      * Unix permission.
200      *
201      * @since Ant 1.6
202      */
203     public int getUnixMode() {
204         return (int) ((getExternalAttributes() >> 16) & 0xFFFF);
205     }
206 
207     /**
208      * Platform specification to put into the &quot;version made
209      * by&quot; part of the central file header.
210      *
211      * @return 0 (MS-DOS FAT) unless {@link #setUnixMode setUnixMode}
212      * has been called, in which case 3 (Unix) will be returned.
213      *
214      * @since Ant 1.5.2
215      */
216     public int getPlatform() {
217         return platform;
218     }
219 
220     /**
221      * @since 1.9
222      */
223     protected void setPlatform(int platform) {
224         this.platform = platform;
225     }
226 
227     /**
228      * Replaces all currently attached extra fields with the new array.
229      *
230      * @since 1.1
231      */
232     public void setExtraFields(ZipExtraField[] fields) {
233         extraFields.removeAllElements();
234         for (int i = 0; i < fields.length; i++) {
235             extraFields.addElement(fields[i]);
236         }
237         setExtra();
238     }
239 
240     /**
241      * Retrieves extra fields.
242      *
243      * @since 1.1
244      */
245     public ZipExtraField[] getExtraFields() {
246         ZipExtraField[] result = new ZipExtraField[extraFields.size()];
247         extraFields.copyInto(result);
248         return result;
249     }
250 
251     /**
252      * Adds an extra fields - replacing an already present extra field
253      * of the same type.
254      *
255      * @since 1.1
256      */
257     public void addExtraField(ZipExtraField ze) {
258         ZipShort type = ze.getHeaderId();
259         boolean done = false;
260         for (int i = 0; !done && i < extraFields.size(); i++) {
261             if (((ZipExtraField) extraFields.elementAt(i)).getHeaderId().equals(type)) {
262                 extraFields.setElementAt(ze, i);
263                 done = true;
264             }
265         }
266         if (!done) {
267             extraFields.addElement(ze);
268         }
269         setExtra();
270     }
271 
272     /**
273      * Remove an extra fields.
274      *
275      * @since 1.1
276      */
277     public void removeExtraField(ZipShort type) {
278         boolean done = false;
279         for (int i = 0; !done && i < extraFields.size(); i++) {
280             if (((ZipExtraField) extraFields.elementAt(i)).getHeaderId().equals(type)) {
281                 extraFields.removeElementAt(i);
282                 done = true;
283             }
284         }
285         if (!done) {
286             throw new java.util.NoSuchElementException();
287         }
288         setExtra();
289     }
290 
291     /**
292      * Throws an Exception if extra data cannot be parsed into extra fields.
293      *
294      * @since 1.1
295      */
296     public void setExtra(byte[] extra) throws RuntimeException {
297         try {
298             setExtraFields(ExtraFieldUtils.parse(extra));
299         } catch (Exception e) {
300             throw new RuntimeException(e.getMessage());
301         }
302     }
303 
304     /**
305      * Unfortunately {@link java.util.zip.ZipOutputStream
306      * java.util.zip.ZipOutputStream} seems to access the extra data
307      * directly, so overriding getExtra doesn't help - we need to
308      * modify super's data directly.
309      *
310      * @since 1.1
311      */
312     protected void setExtra() {
313         super.setExtra(ExtraFieldUtils.mergeLocalFileDataData(getExtraFields()));
314     }
315 
316     /**
317      * Retrieves the extra data for the local file data.
318      *
319      * @since 1.1
320      */
321     public byte[] getLocalFileDataExtra() {
322         byte[] extra = getExtra();
323         return extra != null ? extra : new byte[0];
324     }
325 
326     /**
327      * Retrieves the extra data for the central directory.
328      *
329      * @since 1.1
330      */
331     public byte[] getCentralDirectoryExtra() {
332         return ExtraFieldUtils.mergeCentralDirectoryData(getExtraFields());
333     }
334 
335     /**
336      * Helper for JDK 1.1 <-> 1.2 incompatibility.
337      *
338      * @since 1.2
339      */
340     private Long compressedSize = null;
341 
342     /**
343      * Make this class work in JDK 1.1 like a 1.2 class.
344      *
345      * <p>This either stores the size for later usage or invokes
346      * setCompressedSize via reflection.</p>
347      *
348      * @since 1.2
349      */
350     public void setComprSize(long size) {
351         if (haveSetCompressedSize()) {
352             performSetCompressedSize(this, size);
353         } else {
354             compressedSize = new Long(size);
355         }
356     }
357 
358     /**
359      * Override to make this class work in JDK 1.1 like a 1.2 class.
360      *
361      * @since 1.2
362      */
363     public long getCompressedSize() {
364         if (compressedSize != null) {
365             // has been set explicitly and we are running in a 1.1 VM
366             return compressedSize.longValue();
367         }
368         return super.getCompressedSize();
369     }
370 
371     /**
372      * @since 1.9
373      */
374     public String getName() {
375         return name == null ? super.getName() : name;
376     }
377 
378     /**
379      * @since 1.10
380      */
381     public boolean isDirectory() {
382         return getName().endsWith("/");
383     }
384 
385     protected void setName(String name) {
386         this.name = name;
387     }
388 
389     /**
390      * Helper for JDK 1.1
391      *
392      * @since 1.2
393      */
394     private static Method setCompressedSizeMethod = null;
395     /**
396      * Helper for JDK 1.1
397      *
398      * @since 1.2
399      */
400     private static Object lockReflection = new Object();
401     /**
402      * Helper for JDK 1.1
403      *
404      * @since 1.2
405      */
406     private static boolean triedToGetMethod = false;
407 
408     /**
409      * Are we running JDK 1.2 or higher?
410      *
411      * @since 1.2
412      */
413     private static boolean haveSetCompressedSize() {
414         checkSCS();
415         return setCompressedSizeMethod != null;
416     }
417 
418     /**
419      * Invoke setCompressedSize via reflection.
420      *
421      * @since 1.2
422      */
423     private static void performSetCompressedSize(ZipEntry ze, long size) {
424         Long[] s = {new Long(size)};
425         try {
426             setCompressedSizeMethod.invoke(ze, s);
427         } catch (InvocationTargetException ite) {
428             Throwable nested = ite.getTargetException();
429             throw new RuntimeException("Exception setting the compressed size "
430                                        + "of " + ze + ": "
431                                        + nested.getMessage());
432         } catch (Throwable other) {
433             throw new RuntimeException("Exception setting the compressed size "
434                                        + "of " + ze + ": "
435                                        + other.getMessage());
436         }
437     }
438 
439     /**
440      * Try to get a handle to the setCompressedSize method.
441      *
442      * @since 1.2
443      */
444     private static void checkSCS() {
445         if (!triedToGetMethod) {
446             synchronized (lockReflection) {
447                 triedToGetMethod = true;
448                 try {
449                     setCompressedSizeMethod =
450                         java.util.zip.ZipEntry.class.getMethod("setCompressedSize",
451                                                                new Class[] {Long.TYPE});
452                 } catch (NoSuchMethodException nse) {
453                 }
454             }
455         }
456     }
457 
458 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
134 0% of 0