KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > SyspathArchive


1
2 package org.python.core;
3 import java.io.*;
4 import java.util.zip.*;
5
6 public class SyspathArchive extends PyString {
7     private ZipFile zipFile;
8
9     public SyspathArchive(String JavaDoc archiveName) throws IOException {
10         super(archiveName);
11         archiveName = getArchiveName(archiveName);
12         if(archiveName == null) {
13             throw new IOException("path '" + archiveName + "' not an archive");
14         }
15         zipFile = new ZipFile(new File(archiveName));
16         PySystemState.packageManager.addJar(archiveName, false);
17     }
18
19     SyspathArchive(ZipFile zipFile, String JavaDoc archiveName) {
20         super(archiveName);
21         this.zipFile = zipFile;
22     }
23
24     static String JavaDoc getArchiveName(String JavaDoc dir) {
25         String JavaDoc lowerName = dir.toLowerCase();
26         int idx = lowerName.indexOf(".zip");
27         if (idx < 0) {
28             idx = lowerName.indexOf(".jar");
29         }
30         if (idx < 0) {
31             return null;
32         }
33
34         if (idx == dir.length() - 4) {
35             return dir;
36         }
37         char ch = dir.charAt(idx+4);
38         if (ch == File.separatorChar || ch == '/') {
39             return dir.substring(0, idx+4);
40         }
41         return null;
42     }
43
44     public SyspathArchive makeSubfolder(String JavaDoc folder) {
45         return new SyspathArchive(zipFile, super.toString() + "/" + folder);
46     }
47
48     private String JavaDoc makeEntry(String JavaDoc entry) {
49         String JavaDoc archive = super.toString();
50         String JavaDoc folder = getArchiveName(super.toString());
51         if (archive.length() == folder.length()) {
52             return entry;
53         } else {
54             return archive.substring(folder.length()+1) + "/" + entry;
55         }
56     }
57
58     ZipEntry getEntry(String JavaDoc entryName) {
59         return zipFile.getEntry(makeEntry(entryName));
60     }
61
62     InputStream getInputStream(ZipEntry entry) throws IOException {
63         InputStream istream = zipFile.getInputStream(entry);
64
65         // Some jdk1.1 VMs have problems with detecting the end of a zip
66
// stream correctly. If you read beyond the end, you get a
67
// EOFException("Unexpected end of ZLIB input stream"), not a
68
// -1 return value.
69
// As a workaround we read the file fully here, but only getSize()
70
// bytes.
71
int len = (int) entry.getSize();
72         byte[] buffer = new byte[len];
73         int off = 0;
74         while (len > 0) {
75             int l = istream.read(buffer, off, buffer.length - off);
76             if (l < 0)
77                 return null;
78             off += l;
79             len -= l;
80         }
81         istream.close();
82         return new ByteArrayInputStream(buffer);
83     }
84
85 /*
86     protected void finalize() {
87         System.out.println("closing zip file " + toString());
88         try {
89             zipFile.close();
90         } catch (IOException e) {
91             Py.writeDebug("import", "closing zipEntry failed");
92         }
93     }
94 */

95 }
96
Popular Tags