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 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 archiveName) { 20 super(archiveName); 21 this.zipFile = zipFile; 22 } 23 24 static String getArchiveName(String dir) { 25 String 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 folder) { 45 return new SyspathArchive(zipFile, super.toString() + "/" + folder); 46 } 47 48 private String makeEntry(String entry) { 49 String archive = super.toString(); 50 String 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 entryName) { 59 return zipFile.getEntry(makeEntry(entryName)); 60 } 61 62 InputStream getInputStream(ZipEntry entry) throws IOException { 63 InputStream istream = zipFile.getInputStream(entry); 64 65 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 95 } 96 | Popular Tags |