blob: edf6a686b7c685a56871d987d6222a1a8be9a401 [file] [log] [blame]
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +00001package aQute.bnd.osgi;
Stuart McCullochbb014372012-06-07 21:57:32 +00002
3import java.io.*;
4import java.util.*;
5import java.util.regex.*;
6import java.util.zip.*;
7
8public class ZipResource implements Resource {
Stuart McCulloch2286f232012-06-15 13:27:53 +00009 ZipFile zip;
10 ZipEntry entry;
11 long lastModified;
12 String extra;
Stuart McCullochbb014372012-06-07 21:57:32 +000013
Stuart McCulloch2286f232012-06-15 13:27:53 +000014 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 McCullochbb014372012-06-07 21:57:32 +000022
Stuart McCulloch2286f232012-06-15 13:27:53 +000023 public InputStream openInputStream() throws IOException {
24 return zip.getInputStream(entry);
25 }
Stuart McCullochbb014372012-06-07 21:57:32 +000026
Stuart McCulloch2286f232012-06-15 13:27:53 +000027 public String toString() {
28 return ":" + zip.getName() + "(" + entry.getName() + "):";
29 }
Stuart McCullochbb014372012-06-07 21:57:32 +000030
Stuart McCulloch2286f232012-06-15 13:27:53 +000031 public static ZipFile build(Jar jar, File file) throws ZipException, IOException {
32 return build(jar, file, null);
33 }
Stuart McCullochbb014372012-06-07 21:57:32 +000034
Stuart McCulloch2286f232012-06-15 13:27:53 +000035 public static ZipFile build(Jar jar, File file, Pattern pattern) throws ZipException, IOException {
Stuart McCullochbb014372012-06-07 21:57:32 +000036
Stuart McCulloch2286f232012-06-15 13:27:53 +000037 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 McCullochbb014372012-06-07 21:57:32 +000063
Stuart McCulloch2286f232012-06-15 13:27:53 +000064 public void write(OutputStream out) throws Exception {
65 FileResource.copy(this, out);
66 }
Stuart McCullochbb014372012-06-07 21:57:32 +000067
Stuart McCulloch2286f232012-06-15 13:27:53 +000068 public long lastModified() {
69 return lastModified;
70 }
Stuart McCullochbb014372012-06-07 21:57:32 +000071
Stuart McCulloch2286f232012-06-15 13:27:53 +000072 public String getExtra() {
73 return extra;
74 }
Stuart McCullochbb014372012-06-07 21:57:32 +000075
Stuart McCulloch2286f232012-06-15 13:27:53 +000076 public void setExtra(String extra) {
77 this.extra = extra;
78 }
Stuart McCullochbb014372012-06-07 21:57:32 +000079
Stuart McCulloch2286f232012-06-15 13:27:53 +000080 public long size() {
81 return entry.getSize();
82 }
Stuart McCullochbb014372012-06-07 21:57:32 +000083}