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