blob: 38b744f00aadad2ea7c4c38ad43fddf92e2a828f [file] [log] [blame]
Stuart McCulloch5ec302d2008-12-04 07:58:07 +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 }
21
22 public InputStream openInputStream() throws IOException {
23 return zip.getInputStream(entry);
24 }
25
26 public String toString() {
27 return ":" + entry.getName() + ":";
28 }
29
30 public static ZipFile build(Jar jar, File file) throws ZipException,
31 IOException {
32 return build(jar, file, null);
33 }
34
35 public static ZipFile build(Jar jar, File file, Pattern pattern)
36 throws ZipException, IOException {
37
38 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,
52 entry, time), true);
53 }
54 }
55 return zip;
56 } catch (FileNotFoundException e) {
57 throw new IllegalArgumentException("Problem opening JAR: "
58 + file.getAbsolutePath());
59 }
60 }
61
62 public void write(OutputStream out) throws IOException {
63 FileResource.copy(this, out);
64 }
65
66 public long lastModified() {
67 return lastModified;
68 }
69
70 public String getExtra() {
71 return extra;
72 }
73
74 public void setExtra(String extra) {
75 this.extra = extra;
76 }
77
78}