Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame^] | 1 | package aQute.bnd.osgi; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 2 | |
| 3 | import java.io.*; |
| 4 | import java.util.*; |
| 5 | import java.util.regex.*; |
| 6 | import java.util.zip.*; |
| 7 | |
| 8 | public class ZipResource implements Resource { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 9 | ZipFile zip; |
| 10 | ZipEntry entry; |
| 11 | long lastModified; |
| 12 | String extra; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 13 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 14 | ZipResource(ZipFile zip, ZipEntry entry, long lastModified) throws UnsupportedEncodingException { |
| 15 | this.zip = zip; |
| 16 | this.entry = entry; |
| 17 | this.lastModified = lastModified; |
| 18 | byte[] data = entry.getExtra(); |
| 19 | if (data != null) |
| 20 | this.extra = new String(data, "UTF-8"); |
| 21 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 22 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 23 | public InputStream openInputStream() throws IOException { |
| 24 | return zip.getInputStream(entry); |
| 25 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 26 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 27 | public String toString() { |
| 28 | return ":" + zip.getName() + "(" + entry.getName() + "):"; |
| 29 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 30 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 31 | public static ZipFile build(Jar jar, File file) throws ZipException, IOException { |
| 32 | return build(jar, file, null); |
| 33 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 34 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 35 | public static ZipFile build(Jar jar, File file, Pattern pattern) throws ZipException, IOException { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 36 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 37 | try { |
| 38 | ZipFile zip = new ZipFile(file); |
| 39 | nextEntry: for (Enumeration< ? extends ZipEntry> e = zip.entries(); e.hasMoreElements();) { |
| 40 | ZipEntry entry = e.nextElement(); |
| 41 | if (pattern != null) { |
| 42 | Matcher m = pattern.matcher(entry.getName()); |
| 43 | if (!m.matches()) |
| 44 | continue nextEntry; |
| 45 | } |
| 46 | if (!entry.isDirectory()) { |
| 47 | long time = entry.getTime(); |
| 48 | if (time <= 0) |
| 49 | time = file.lastModified(); |
| 50 | jar.putResource(entry.getName(), new ZipResource(zip, entry, time), true); |
| 51 | } |
| 52 | } |
| 53 | return zip; |
| 54 | } |
| 55 | catch (ZipException ze) { |
| 56 | throw new ZipException("The JAR/ZIP file (" + file.getAbsolutePath() + ") seems corrupted, error: " |
| 57 | + ze.getMessage()); |
| 58 | } |
| 59 | catch (FileNotFoundException e) { |
| 60 | throw new IllegalArgumentException("Problem opening JAR: " + file.getAbsolutePath()); |
| 61 | } |
| 62 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 63 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 64 | public void write(OutputStream out) throws Exception { |
| 65 | FileResource.copy(this, out); |
| 66 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 67 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 68 | public long lastModified() { |
| 69 | return lastModified; |
| 70 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 71 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 72 | public String getExtra() { |
| 73 | return extra; |
| 74 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 75 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 76 | public void setExtra(String extra) { |
| 77 | this.extra = extra; |
| 78 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 79 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 80 | public long size() { |
| 81 | return entry.getSize(); |
| 82 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 83 | } |