2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util.resource;
22 import java.io.FileOutputStream;
23 import java.io.FilterInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.net.JarURLConnection;
29 import java.util.jar.JarEntry;
30 import java.util.jar.JarInputStream;
31 import java.util.jar.Manifest;
33 import org.eclipse.jetty.util.IO;
34 import org.eclipse.jetty.util.URIUtil;
35 import org.eclipse.jetty.util.log.Log;
36 import org.eclipse.jetty.util.log.Logger;
39 /* ------------------------------------------------------------ */
40 public class JarResource extends URLResource
42 private static final Logger LOG = Log.getLogger(JarResource.class);
43 protected JarURLConnection _jarConnection;
45 /* -------------------------------------------------------- */
46 protected JarResource(URL url)
51 /* ------------------------------------------------------------ */
52 protected JarResource(URL url, boolean useCaches)
54 super(url, null, useCaches);
57 /* ------------------------------------------------------------ */
59 public synchronized void close()
65 /* ------------------------------------------------------------ */
67 protected synchronized boolean checkConnection()
69 super.checkConnection();
72 if (_jarConnection!=_connection)
81 return _jarConnection!=null;
84 /* ------------------------------------------------------------ */
86 * @throws IOException Sub-classes of <code>JarResource</code> may throw an IOException (or subclass)
88 protected void newConnection() throws IOException
90 _jarConnection=(JarURLConnection)_connection;
93 /* ------------------------------------------------------------ */
95 * Returns true if the represented resource exists.
98 public boolean exists()
100 if (_urlString.endsWith("!/"))
101 return checkConnection();
103 return super.exists();
106 /* ------------------------------------------------------------ */
108 public File getFile()
114 /* ------------------------------------------------------------ */
116 public InputStream getInputStream()
117 throws java.io.IOException
120 if (!_urlString.endsWith("!/"))
121 return new FilterInputStream(getInputStream(false))
124 public void close() throws IOException {this.in=IO.getClosedStream();}
127 URL url = new URL(_urlString.substring(4,_urlString.length()-2));
128 InputStream is = url.openStream();
135 /* ------------------------------------------------------------ */
137 public void copyTo(File directory)
143 if(LOG.isDebugEnabled())
144 LOG.debug("Extract "+this+" to "+directory);
146 String urlString = this.getURL().toExternalForm().trim();
147 int endOfJarUrl = urlString.indexOf("!/");
148 int startOfJarUrl = (endOfJarUrl >= 0?4:0);
151 throw new IOException("Not a valid jar url: "+urlString);
153 URL jarFileURL = new URL(urlString.substring(startOfJarUrl, endOfJarUrl));
154 String subEntryName = (endOfJarUrl+2 < urlString.length() ? urlString.substring(endOfJarUrl + 2) : null);
155 boolean subEntryIsDir = (subEntryName != null && subEntryName.endsWith("/")?true:false);
157 if (LOG.isDebugEnabled())
158 LOG.debug("Extracting entry = "+subEntryName+" from jar "+jarFileURL);
160 try (InputStream is = jarFileURL.openConnection().getInputStream();
161 JarInputStream jin = new JarInputStream(is))
164 boolean shouldExtract;
165 while((entry=jin.getNextJarEntry())!=null)
167 String entryName = entry.getName();
168 if ((subEntryName != null) && (entryName.startsWith(subEntryName)))
170 // is the subentry really a dir?
171 if (!subEntryIsDir && subEntryName.length()+1==entryName.length() && entryName.endsWith("/"))
174 //if there is a particular subEntry that we are looking for, only
178 //if it is a subdirectory we are looking for, then we
179 //are looking to extract its contents into the target
180 //directory. Remove the name of the subdirectory so
181 //that we don't wind up creating it too.
182 entryName = entryName.substring(subEntryName.length());
183 if (!entryName.equals(""))
186 shouldExtract = true;
189 shouldExtract = false;
192 shouldExtract = true;
194 else if ((subEntryName != null) && (!entryName.startsWith(subEntryName)))
196 //there is a particular entry we are looking for, and this one
198 shouldExtract = false;
202 //we are extracting everything
203 shouldExtract = true;
208 if (LOG.isDebugEnabled())
209 LOG.debug("Skipping entry: "+entryName);
213 String dotCheck = entryName.replace('\\', '/');
214 dotCheck = URIUtil.canonicalPath(dotCheck);
215 if (dotCheck == null)
217 if (LOG.isDebugEnabled())
218 LOG.debug("Invalid entry: "+entryName);
222 File file=new File(directory,entryName);
224 if (entry.isDirectory())
232 // make directory (some jars don't list dirs)
233 File dir = new File(file.getParent());
238 try (OutputStream fout = new FileOutputStream(file))
244 if (entry.getTime()>=0)
245 file.setLastModified(entry.getTime());
249 if ((subEntryName == null) || (subEntryName != null && subEntryName.equalsIgnoreCase("META-INF/MANIFEST.MF")))
251 Manifest manifest = jin.getManifest();
252 if (manifest != null)
254 File metaInf = new File (directory, "META-INF");
256 File f = new File(metaInf, "MANIFEST.MF");
257 try (OutputStream fout = new FileOutputStream(f))
259 manifest.write(fout);
266 public static Resource newJarResource(Resource resource) throws IOException
268 if (resource instanceof JarResource)
270 return Resource.newResource("jar:" + resource + "!/");