blob: 1101945357723abe4be48e196e00d1ade565dce0 [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 McCulloch55d4dfe2012-08-07 10:57:21 +000027 @Override
Stuart McCulloch2286f232012-06-15 13:27:53 +000028 public String toString() {
29 return ":" + zip.getName() + "(" + entry.getName() + "):";
30 }
Stuart McCullochbb014372012-06-07 21:57:32 +000031
Stuart McCulloch2286f232012-06-15 13:27:53 +000032 public static ZipFile build(Jar jar, File file) throws ZipException, IOException {
33 return build(jar, file, null);
34 }
Stuart McCullochbb014372012-06-07 21:57:32 +000035
Stuart McCulloch2286f232012-06-15 13:27:53 +000036 public static ZipFile build(Jar jar, File file, Pattern pattern) throws ZipException, IOException {
Stuart McCullochbb014372012-06-07 21:57:32 +000037
Stuart McCulloch2286f232012-06-15 13:27:53 +000038 try {
39 ZipFile zip = new ZipFile(file);
40 nextEntry: for (Enumeration< ? extends ZipEntry> e = zip.entries(); e.hasMoreElements();) {
41 ZipEntry entry = e.nextElement();
42 if (pattern != null) {
43 Matcher m = pattern.matcher(entry.getName());
44 if (!m.matches())
45 continue nextEntry;
46 }
47 if (!entry.isDirectory()) {
48 long time = entry.getTime();
49 if (time <= 0)
50 time = file.lastModified();
51 jar.putResource(entry.getName(), new ZipResource(zip, entry, time), true);
52 }
53 }
54 return zip;
55 }
56 catch (ZipException ze) {
57 throw new ZipException("The JAR/ZIP file (" + file.getAbsolutePath() + ") seems corrupted, error: "
58 + ze.getMessage());
59 }
60 catch (FileNotFoundException e) {
61 throw new IllegalArgumentException("Problem opening JAR: " + file.getAbsolutePath());
62 }
63 }
Stuart McCullochbb014372012-06-07 21:57:32 +000064
Stuart McCulloch2286f232012-06-15 13:27:53 +000065 public void write(OutputStream out) throws Exception {
66 FileResource.copy(this, out);
67 }
Stuart McCullochbb014372012-06-07 21:57:32 +000068
Stuart McCulloch2286f232012-06-15 13:27:53 +000069 public long lastModified() {
70 return lastModified;
71 }
Stuart McCullochbb014372012-06-07 21:57:32 +000072
Stuart McCulloch2286f232012-06-15 13:27:53 +000073 public String getExtra() {
74 return extra;
75 }
Stuart McCullochbb014372012-06-07 21:57:32 +000076
Stuart McCulloch2286f232012-06-15 13:27:53 +000077 public void setExtra(String extra) {
78 this.extra = extra;
79 }
Stuart McCullochbb014372012-06-07 21:57:32 +000080
Stuart McCulloch2286f232012-06-15 13:27:53 +000081 public long size() {
82 return entry.getSize();
83 }
Stuart McCullochbb014372012-06-07 21:57:32 +000084}