1 /* 2 * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Sun designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Sun in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 22 * CA 95054 USA or visit www.sun.com if you need additional information or 23 * have any questions. 24 */ 25 26 package com.sun.tools.javac.zip; 27 28 import java.io.File; 29 import java.util.Calendar; 30 31 public final class ZipFileIndexEntry implements Comparable<ZipFileIndexEntry> { 32 public static final ZipFileIndexEntry[] EMPTY_ARRAY = {}; 33 34 // Directory related 35 String dir; 36 boolean isDir; 37 38 // File related 39 String name; 40 41 int offset; 42 int size; 43 int compressedSize; 44 long javatime; 45 46 private int nativetime; 47 48 public ZipFileIndexEntry(String path) { 49 int separator = path.lastIndexOf(File.separatorChar); 50 if (separator == -1) { 51 dir = "".intern(); 52 name = path; 53 } else { 54 dir = path.substring(0, separator).intern(); 55 name = path.substring(separator + 1); 56 } 57 } 58 59 public ZipFileIndexEntry(String directory, String name) { 60 this.dir = directory.intern(); 61 this.name = name; 62 } 63 64 public String getName() { 65 if (dir == null || dir.length() == 0) { 66 return name; 67 } 68 69 StringBuilder sb = new StringBuilder(); 70 sb.append(dir); 71 sb.append(File.separatorChar); 72 sb.append(name); 73 return sb.toString(); 74 } 75 76 public String getFileName() { 77 return name; 78 } 79 80 public long getLastModified() { 81 if (javatime == 0) { 82 javatime = dosToJavaTime(nativetime); 83 } 84 return javatime; 85 } 86 87 // based on dosToJavaTime in java.util.Zip, but avoiding the 88 // use of deprecated Date constructor 89 private static long dosToJavaTime(int dtime) { 90 Calendar c = Calendar.getInstance(); 91 c.set(Calendar.YEAR, ((dtime >> 25) & 0x7f) + 1980); 92 c.set(Calendar.MONTH, ((dtime >> 21) & 0x0f) - 1); 93 c.set(Calendar.DATE, ((dtime >> 16) & 0x1f)); 94 c.set(Calendar.HOUR_OF_DAY, ((dtime >> 11) & 0x1f)); 95 c.set(Calendar.MINUTE, ((dtime >> 5) & 0x3f)); 96 c.set(Calendar.SECOND, ((dtime << 1) & 0x3e)); 97 c.set(Calendar.MILLISECOND, 0); 98 return c.getTimeInMillis(); 99 } 100 101 void setNativeTime(int natTime) { 102 nativetime = natTime; 103 } 104 105 public boolean isDirectory() { 106 return isDir; 107 } 108 109 public int compareTo(ZipFileIndexEntry other) { 110 String otherD = other.dir; 111 if (dir != otherD) { 112 int c = dir.compareTo(otherD); 113 if (c != 0) 114 return c; 115 } 116 return name.compareTo(other.name); 117 } 118 119 120 public String toString() { 121 return isDir ? ("Dir:" + dir + " : " + name) : 122 (dir + ":" + name); 123 } 124 }