diff options
Diffstat (limited to 'NET/worlds/core/Archive.java')
| -rw-r--r-- | NET/worlds/core/Archive.java | 327 |
1 files changed, 327 insertions, 0 deletions
diff --git a/NET/worlds/core/Archive.java b/NET/worlds/core/Archive.java new file mode 100644 index 0000000..051a340 --- /dev/null +++ b/NET/worlds/core/Archive.java @@ -0,0 +1,327 @@ +/* */ package NET.worlds.core; +/* */ +/* */ import NET.worlds.network.URL; +/* */ import java.io.BufferedInputStream; +/* */ import java.io.File; +/* */ import java.io.FileInputStream; +/* */ import java.io.IOException; +/* */ import java.io.InputStream; +/* */ import java.util.Enumeration; +/* */ import java.util.Hashtable; +/* */ import java.util.zip.ZipEntry; +/* */ import java.util.zip.ZipFile; +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ public class Archive +/* */ { +/* */ public static final String archiveName = "content.zip"; +/* 66 */ private static Object mutex = new Object(); +/* */ +/* */ +/* 69 */ private static Object noArchive = new Object(); +/* */ +/* */ +/* 72 */ private static Hashtable<String, Object> archives = new Hashtable(); +/* */ +/* */ +/* 75 */ private static String homePrefix = URL.getHome().unalias(); +/* */ +/* */ +/* */ private ZipFile zip; +/* */ +/* */ +/* */ +/* */ public static boolean exists(String name) +/* */ { +/* 84 */ synchronized (mutex) +/* */ { +/* */ +/* 87 */ String nname = name.toLowerCase().replace('\\', '/'); +/* */ +/* */ +/* 90 */ if (nname.startsWith("./")) { +/* 91 */ nname = homePrefix + nname.substring(2); +/* */ } +/* 93 */ if (nname.startsWith(homePrefix)) { +/* 94 */ nname = nname.substring(homePrefix.length()); +/* */ +/* */ +/* 97 */ Archive arch = getOrMake(""); +/* */ +/* 99 */ if ((arch != null) && (arch.lookup(nname) != null)) { +/* 100 */ return true; +/* */ } +/* */ +/* */ +/* 104 */ int start = -1; +/* */ int end; +/* 106 */ while ((end = nname.indexOf('/', start + 1)) != -1) { int end; +/* 107 */ String path = nname.substring(0, end + 1); +/* 108 */ arch = getOrMake(path); +/* 109 */ if (arch != null) { +/* 110 */ String file = nname.substring(end + 1); +/* 111 */ if (arch.lookup(file) != null) +/* 112 */ return true; +/* */ } +/* 114 */ start = end; +/* */ } +/* */ } +/* */ +/* */ +/* 119 */ File f = new File(name); +/* 120 */ int length = (int)f.length(); +/* 121 */ if (length != 0) { +/* 122 */ return true; +/* */ } +/* */ +/* */ +/* 126 */ return false; +/* */ } +/* */ } +/* */ +/* */ private static void reallyRead(InputStream s, byte[] data) throws IOException +/* */ { +/* 132 */ int bytesRemaining = data.length; +/* 133 */ int totalBytesRead = 0; +/* */ +/* 135 */ while (bytesRemaining > 0) { +/* 136 */ int bytesRead = s.read(data, totalBytesRead, bytesRemaining); +/* 137 */ totalBytesRead += bytesRead; +/* 138 */ if (bytesRead == -1) +/* 139 */ return; +/* 140 */ bytesRemaining -= bytesRead; +/* */ } +/* */ } +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ public static byte[] readBinaryFile(String name) +/* */ { +/* 150 */ synchronized (mutex) +/* */ { +/* */ try +/* */ { +/* 154 */ String nname = name.toLowerCase().replace('\\', '/'); +/* */ +/* */ +/* 157 */ if (nname.startsWith("./")) { +/* 158 */ nname = homePrefix + nname.substring(2); +/* */ } +/* 160 */ if (nname.startsWith(homePrefix)) { +/* 161 */ nname = nname.substring(homePrefix.length()); +/* */ +/* */ +/* 164 */ Archive arch = getOrMake(""); +/* */ ZipEntry entry; +/* 166 */ if ((arch != null) && ((entry = arch.lookup(nname)) != null)) { +/* 167 */ return arch.load(entry); +/* */ } +/* */ +/* */ +/* 171 */ int start = -1; +/* */ int end; +/* 173 */ while ((end = nname.indexOf('/', start + 1)) != -1) { int end; +/* 174 */ String path = nname.substring(0, end + 1); +/* 175 */ arch = getOrMake(path); +/* 176 */ if (arch != null) { +/* 177 */ String file = nname.substring(end + 1); +/* 178 */ ZipEntry entry; if ((entry = arch.lookup(file)) != null) +/* 179 */ return arch.load(entry); +/* */ } +/* 181 */ start = end; +/* */ } +/* */ } +/* */ +/* */ +/* 186 */ File f = new File(name); +/* 187 */ int length = (int)f.length(); +/* 188 */ if (length != 0) { +/* 189 */ FileInputStream fis = new FileInputStream(f); +/* 190 */ byte[] data = new byte[length]; +/* 191 */ reallyRead(fis, data); +/* 192 */ fis.close(); +/* 193 */ return data; +/* */ } +/* */ +/* */ } +/* */ catch (IOException localIOException) +/* */ { +/* 199 */ return null; +/* */ } +/* */ } +/* */ } +/* */ +/* */ public static byte[] readTextFile(String name) { +/* 205 */ byte[] data = readBinaryFile(name); +/* 206 */ if (data != null) { +/* 207 */ int count = 0; +/* 208 */ for (int i = 0; i < data.length; i++) +/* 209 */ if ((data[i] == 13) && (data[(i + 1)] == 10)) { +/* 210 */ i++; +/* 211 */ count++; +/* */ } +/* 213 */ byte[] ret = new byte[data.length - count]; +/* 214 */ int j = 0; +/* 215 */ for (int i = 0; i < data.length; i++) +/* 216 */ if ((data[i] != 13) || (data[(i + 1)] != 10)) +/* 217 */ ret[(j++)] = data[i]; +/* 218 */ data = ret; +/* */ } +/* 220 */ return data; +/* */ } +/* */ +/* */ +/* */ +/* */ +/* */ public static void flushAll() +/* */ { +/* 228 */ synchronized (mutex) { +/* 229 */ Enumeration<Object> e = archives.elements(); +/* 230 */ while (e.hasMoreElements()) { +/* 231 */ Object o = e.nextElement(); +/* 232 */ if ((o instanceof Archive)) +/* 233 */ ((Archive)o).close(); +/* */ } +/* 235 */ archives.clear(); +/* */ } +/* */ } +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ private static Archive getOrMake(String relPath) +/* */ { +/* 249 */ String archName = relPath + "content.zip"; +/* 250 */ Object obj = archives.get(archName); +/* */ +/* 252 */ if (obj == null) +/* */ { +/* */ try +/* */ { +/* 256 */ Archive arch = new Archive(archName); +/* 257 */ archives.put(archName, arch); +/* 258 */ return arch; +/* */ } +/* */ catch (IOException e) +/* */ { +/* 262 */ archives.put(archName, noArchive); +/* */ } +/* 264 */ } else if (obj != noArchive) +/* */ { +/* 266 */ return (Archive)obj; +/* */ } +/* */ +/* */ +/* 270 */ return null; +/* */ } +/* */ +/* */ private Archive(String path) throws IOException +/* */ { +/* 275 */ this.zip = new ZipFile(homePrefix + path); +/* */ } +/* */ +/* */ private void close() +/* */ { +/* */ try { +/* 281 */ this.zip.close(); +/* */ } +/* */ catch (IOException localIOException) {} +/* */ } +/* */ +/* */ +/* */ +/* */ +/* */ +/* */ private ZipEntry lookup(String name) +/* */ { +/* 292 */ return this.zip.getEntry(name); +/* */ } +/* */ +/* */ +/* */ +/* */ private byte[] load(ZipEntry ent) +/* */ throws IOException +/* */ { +/* 300 */ return load(this.zip, ent); +/* */ } +/* */ +/* */ static byte[] load(ZipFile zip, ZipEntry ent) throws IOException { +/* 304 */ int size = (int)ent.getSize(); +/* 305 */ byte[] data = new byte[size]; +/* 306 */ InputStream is = zip.getInputStream(ent); +/* 307 */ if (ent.getMethod() == 0) { +/* 308 */ reallyRead(is, data); +/* */ } else { +/* 310 */ BufferedInputStream bis = new BufferedInputStream(is, 4096); +/* 311 */ for (int amt = 0; amt < size;) +/* 312 */ amt += bis.read(data, amt, size - amt); +/* 313 */ bis.close(); +/* */ } +/* 315 */ return data; +/* */ } +/* */ +/* */ static Object getMutex() { +/* 319 */ return mutex; +/* */ } +/* */ } + + +/* Location: C:\Program Files (x86)\Worlds Inc\WorldsPlayer - Win7\lib\worlds.jar!\NET\worlds\core\Archive.class + * Java compiler version: 6 (50.0) + * JD-Core Version: 0.7.1 + */
\ No newline at end of file |