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