Jar file helper to deployment : Jar File « File Input Output « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » File Input Output » Jar FileScreenshots 
Jar file helper to deployment
 
/**
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.security.cert.Certificate;
/**
 @version $Rev: 726699 $ $Date: 2008-12-15 06:30:36 -0800 (Mon, 15 Dec 2008) $
 */
public class UnpackedJarFile extends JarFile {
    private final File baseDir;
    private boolean manifestLoaded = false;
    private Manifest manifest;

    public UnpackedJarFile(File baseDirthrows IOException {
        super(DeploymentUtil.DUMMY_JAR_FILE);
        this.baseDir = baseDir;
        if (!baseDir.isDirectory()) {
            throw new IOException("File must be a directory: file=" + baseDir.getAbsolutePath());
        }
    }

    public File getBaseDir() {
        return baseDir;
    }

    public Manifest getManifest() throws IOException {
        if (!manifestLoaded) {
            File manifestFile = getFile("META-INF/MANIFEST.MF");

            if (manifestFile != null && manifestFile.isFile()) {
                FileInputStream in = null;
                try {
                    in = new FileInputStream(manifestFile);
                    manifest = new Manifest(in);
                finally {
                    if (in != null) {
                        try {
                            in.close();
                        catch (IOException e) {
                            // ignore
                        }
                    }
                }
            }
            manifestLoaded = true;
        }
        return manifest;
    }

    public UnpackedJarEntry getUnpackedJarEntry(String name) {
        File file = getFile(name);
        if (file == null) {
            return null;
        }
        return new UnpackedJarEntry(name, file, getManifestSafe());
    }

    public JarEntry getJarEntry(String name) {
        return getUnpackedJarEntry(name);
    }

    public ZipEntry getEntry(String name) {
        return getUnpackedJarEntry(name);
    }

    public Enumeration entries() {
        Collection files = DeploymentUtil.listRecursiveFiles(baseDir);

        Manifest manifest = getManifestSafe();
        LinkedList entries = new LinkedList();
        URI baseURI = baseDir.getAbsoluteFile().toURI();
        for (Iterator iterator = files.iterator(); iterator.hasNext();) {
            File entryFile = ((Fileiterator.next()).getAbsoluteFile();
            URI entryURI = entryFile.toURI();
            URI relativeURI = baseURI.relativize(entryURI);
            entries.add(new UnpackedJarEntry(relativeURI.getPath(), entryFile, manifest));
        }
        return Collections.enumeration(entries);
    }

    public InputStream getInputStream(ZipEntry zipEntrythrows IOException {
        File file;
        if (zipEntry instanceof UnpackedJarEntry) {
            file = ((UnpackedJarEntry)zipEntry).getFile();
        else {
            file = getFile(zipEntry.getName());
        }

        if (file == null) {
            throw new IOException("Entry not found: name=" + zipEntry.getName());
        else if (file.isDirectory()) {
            return new DeploymentUtil.EmptyInputStream();
        }
        return new FileInputStream(file);
    }

    public String getName() {
        return baseDir.getAbsolutePath();
    }

    /**
     * Always returns -1.
     @return -1
     */
    public int size() {
        return -1;
    }

    public void close() throws IOException {
        try {
            super.close();
        catch(IOException ignored) {
        }
    }

    protected void finalize() throws IOException {
    }

    public File getFile(String name) {
        File file = new File(baseDir, name);
        if (!file.exists()) {
            return null;
        }
        return file;
    }

    private Manifest getManifestSafe() {
        Manifest manifest = null;
        try {
            manifest = getManifest();
        catch (IOException e) {
            // ignore
        }
        return manifest;
    }
}

/**
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */


/**
 @version $Rev: 617659 $ $Date: 2008-02-01 13:29:25 -0800 (Fri, 01 Feb 2008) $
 */
 final class DeploymentUtil {
    private DeploymentUtil() {
    }

    public static final File DUMMY_JAR_FILE;
    private static final boolean jarUrlRewrite;
    static {
        jarUrlRewrite = new Boolean(System.getProperty("org.apache.geronimo.deployment.util.DeploymentUtil.jarUrlRewrite""false"));
        try {
            DUMMY_JAR_FILE = DeploymentUtil.createTempFile();
            new JarOutputStream(new FileOutputStream(DeploymentUtil.DUMMY_JAR_FILE)new Manifest()).close();
        catch (IOException e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    // be careful to clean up the temp directory
    public static File createTempDir() throws IOException {
        File tempDir = File.createTempFile("geronimo-deploymentUtil"".tmpdir");
        tempDir.delete();
        tempDir.mkdirs();
        return tempDir;
    }

    // be careful to clean up the temp file... we tell the vm to delete this on exit
    // but VMs can't be trusted to acutally delete the file
    public static File createTempFile() throws IOException {
        File tempFile = File.createTempFile("geronimo-deploymentUtil"".tmpdir");
        tempFile.deleteOnExit();
        return tempFile;
    }
    
    // be careful to clean up the temp file... we tell the vm to delete this on exit
    // but VMs can't be trusted to acutally delete the file
    private static File createTempFile(String extensionthrows IOException {
        File tempFile = File.createTempFile("geronimo-deploymentUtil", extension == null".tmpdir": extension);
        tempFile.deleteOnExit();
        return tempFile;
    }


    public static void copyFile(File source, File destinationthrows IOException {
        File destinationDir = destination.getParentFile();
        if (!destinationDir.exists() && !destinationDir.mkdirs()) {
            throw new java.io.IOException("Cannot create directory : " + destinationDir);
        }
        
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(source);
            out = new FileOutputStream(destination);
            writeAll(in, out);
        finally {
            close(in);
            close(out);
        }
    }

    private static void writeAll(InputStream in, OutputStream outthrows IOException {
        byte[] buffer = new byte[4096];
        int count;
        while ((count = in.read(buffer)) 0) {
            out.write(buffer, 0, count);
        }
        out.flush();
    }
    public static File toTempFile(JarFile jarFile, String paththrows IOException {
        return toTempFile(createJarURL(jarFile, path));
    }

    public static File toTempFile(URL urlthrows IOException {
        InputStream in = null;
        OutputStream out = null;
        JarFile jarFile = null;
        try {
            if(url.getProtocol().equalsIgnoreCase("jar")) {
                // url.openStream() locks the jar file and does not release the lock even after the stream is closed.
                // This problem is avoided by using JarFile APIs.
                File file = new File(url.getFile().substring(5, url.getFile().indexOf("!/")));
                String path = url.getFile().substring(url.getFile().indexOf("!/")+2);
                jarFile = new JarFile(file);
                JarEntry jarEntry = jarFile.getJarEntry(path);
                if(jarEntry != null) {
                    in = jarFile.getInputStream(jarEntry);
                else {
                    throw new FileNotFoundException("JarEntry "+path+" not found in "+file);
                }
            else {
                in = url.openStream();
            }
            int index = url.getPath().lastIndexOf(".");
            String extension = null;
            if (index > 0) {
                extension = url.getPath().substring(index);
            }
            File tempFile = createTempFile(extension);

            out = new FileOutputStream(tempFile);

            writeAll(in, out);
            return tempFile;
        finally {
            close(out);
            close(in);
            close(jarFile);
        }
    }

    public static String readAll(URL urlthrows IOException {
        Reader reader = null;
        JarFile jarFile = null;
        try {
            if(url.getProtocol().equalsIgnoreCase("jar")) {
                // url.openStream() locks the jar file and does not release the lock even after the stream is closed.
                // This problem is avoided by using JarFile APIs.
                File file = new File(url.getFile().substring(5, url.getFile().indexOf("!/")));
                String path = url.getFile().substring(url.getFile().indexOf("!/")+2);
                jarFile = new JarFile(file);
                JarEntry jarEntry = jarFile.getJarEntry(path);
                if(jarEntry != null) {
                    reader = new InputStreamReader(jarFile.getInputStream(jarEntry));
                else {
                    throw new FileNotFoundException("JarEntry "+path+" not found in "+file);
                }
            else {
                reader = new InputStreamReader(url.openStream());
            }
            char[] buffer = new char[4000];
            StringBuffer out = new StringBuffer();
            for(int count = reader.read(buffer); count >= 0; count = reader.read(buffer)) {
                out.append(buffer, 0, count);
            }
            return out.toString();
        finally {
            close(reader);
            close(jarFile);
        }
    }

    public static File toFile(JarFile jarFilethrows IOException {
        if (jarFile instanceof UnpackedJarFile) {
            return ((UnpackedJarFilejarFile).getBaseDir();
        else {
          throw new IOException("jarFile is not a directory");
        }
    }

    // be careful with this method as it can leave a temp lying around
    public static File toFile(JarFile jarFile, String paththrows IOException {
        if (jarFile instanceof UnpackedJarFile) {
            File baseDir = ((UnpackedJarFilejarFile).getBaseDir();
            File file = new File(baseDir, path);
            if (!file.isFile()) {
                throw new IOException("No such file: " + file.getAbsolutePath());
            }
            return file;
        else {
            String urlString = "jar:" new File(jarFile.getName()).toURL() "!/" + path;
            return toTempFile(new URL(urlString));
        }
    }

    public static URL createJarURL(JarFile jarFile, String paththrows MalformedURLException {
        if (jarFile instanceof NestedJarFile) {
            NestedJarFile nestedJar = (NestedJarFilejarFile;
            if (nestedJar.isUnpacked()) {
                JarFile baseJar = nestedJar.getBaseJar();
                String basePath = nestedJar.getBasePath();
                if (baseJar instanceof UnpackedJarFile) {
                    File baseDir = ((UnpackedJarFilebaseJar).getBaseDir();
                    baseDir = new File(baseDir, basePath);
                    return new File(baseDir, path).toURL();
                }
            }
        }
        
        if (jarFile instanceof UnpackedJarFile) {
            File baseDir = ((UnpackedJarFilejarFile).getBaseDir();
            return new File(baseDir, path).toURL();
        else {
            String urlString = "jar:" new File(jarFile.getName()).toURL() "!/" + path;
            if(jarUrlRewrite) {
                // To prevent the lockout of archive, instead of returning a jar url, write the content to a
                // temp file and return the url of that file.
                File tempFile = null;
                try {
                    tempFile = toTempFile(new URL(urlString));
                catch (IOException e) {
                    // The JarEntry does not exist!
                    // Return url of a file that does not exist.
                    try {
                        tempFile = createTempFile();
                        tempFile.delete();
                    catch (IOException ignored) {
                    }
                 }
                return tempFile.toURL();
            else {
                return new URL(urlString);
            }
        }
    }

    public static JarFile createJarFile(File jarFilethrows IOException {
        if (jarFile.isDirectory()) {
            return new UnpackedJarFile(jarFile);
        else {
            return new JarFile(jarFile);
        }
    }

    public static void copyToPackedJar(JarFile inputJar, File outputFilethrows IOException {
        if (inputJar.getClass() == JarFile.class) {
            // this is a plain old jar... nothign special
            copyFile(new File(inputJar.getName()), outputFile);
        else if (inputJar instanceof NestedJarFile && ((NestedJarFile)inputJar).isPacked()) {
            NestedJarFile nestedJarFile = (NestedJarFile)inputJar;
            JarFile baseJar = nestedJarFile.getBaseJar();
            String basePath = nestedJarFile.getBasePath();
            if (baseJar instanceof UnpackedJarFile) {
                // our target jar is just a file in upacked jar (a plain old directory)... now
                // we just need to find where it is and copy it to the outptu
                copyFile(((UnpackedJarFile)baseJar).getFile(basePath), outputFile);
            else {
                // out target is just a plain old jar file directly accessabel from the file system
                copyFile(new File(baseJar.getName()), outputFile);
            }
        else {
            // copy out the module contents to a standalone jar file (entry by entry)
            JarOutputStream out = null;
            try {
                out = new JarOutputStream(new FileOutputStream(outputFile));
                byte[] buffer = new byte[4096];
                Enumeration entries = inputJar.entries();
                while (entries.hasMoreElements()) {
                    ZipEntry entry = (ZipEntryentries.nextElement();
                    InputStream in = inputJar.getInputStream(entry);
                    try {
                        out.putNextEntry(new ZipEntry(entry.getName()));
                        try {
                            int count;
                            while ((count = in.read(buffer)) 0) {
                                out.write(buffer, 0, count);
                            }
                        finally {
                            out.closeEntry();
                        }
                    finally {
                        close(in);
                    }
                }
            finally {
                close(out);
            }
        }
    }

    public static void jarDirectory(File sourceDirecotry, File destinationFilethrows IOException {
        JarFile inputJar = new UnpackedJarFile(sourceDirecotry);
        try {
            copyToPackedJar(inputJar, destinationFile);
        finally {
            close(inputJar);
        }
    }

    private static void createDirectory(File dirthrows IOException {
        if (dir != null && !dir.exists()) {
            boolean success = dir.mkdirs();
            if (!success) {
                throw new IOException("Cannot create directory " + dir.getAbsolutePath());
            }
        }
    }

    public static void unzipToDirectory(ZipFile zipFile, File destDirthrows IOException {
        Enumeration entries = zipFile.entries();
        try {
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntryentries.nextElement();
                if (entry.isDirectory()) {
                    File dir = new File(destDir, entry.getName());
                    createDirectory(dir);
                else {
                    File file = new File(destDir, entry.getName());
                    createDirectory(file.getParentFile());
                    OutputStream out = null;
                    InputStream in = null;
                    try {
                        out = new BufferedOutputStream(new FileOutputStream(file));
                        in = zipFile.getInputStream(entry);
                        writeAll(in, out);
                    finally {
                        if (null != out) {
                            out.close();
                        }
                        if (null != in) {
                            in.close();
                        }
                    }
                }
            }
        finally {
            zipFile.close();
        }
    }
    
    
    public static boolean recursiveDelete(File root, Collection<String> unableToDeleteCollection) {
        if (root == null) {
            return true;
        }

        if (root.isDirectory()) {
            File[] files = root.listFiles();
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    File file = files[i];
                    if (file.isDirectory()) {
                        recursiveDelete(file, unableToDeleteCollection);
                    else {
                        if (!file.delete() && unableToDeleteCollection != null) {
                            unableToDeleteCollection.add(file.getAbsolutePath());    
                        }
                    }
                    // help out the GC of file handles by nulling the references
                    files[inull;
                }
            }
        }
        boolean rootDeleteStatus;
        if (!(rootDeleteStatus = root.delete()) && unableToDeleteCollection != null
          unableToDeleteCollection.add(root.getAbsolutePath());
        
        return rootDeleteStatus;
    }
    
    public static boolean recursiveDelete(File root) {
        return recursiveDelete(root, null);
    }

    public static Collection<File> listRecursiveFiles(File file) {
        Collection<File> list = new ArrayList<File>();
        listRecursiveFiles(file, list);
        return Collections.unmodifiableCollection(list);
    }

    public static void listRecursiveFiles(File file, Collection<File> collection) {
        File[] files = file.listFiles();
        if null == files ) {
            return;
        }
        for (File file1 : files) {
            collection.add(file1);
            if (file1.isDirectory()) {
                listRecursiveFiles(file1, collection);
            }
        }
    }

    public static void flush(OutputStream thing) {
        if (thing != null) {
            try {
                thing.flush();
            catch(Exception ignored) {
            }
        }
    }

    public static void flush(Writer thing) {
        if (thing != null) {
            try {
                thing.flush();
            catch(Exception ignored) {
            }
        }
    }

    public static void close(JarFile thing) {
        if (thing != null) {
            try {
                thing.close();
            catch(Exception ignored) {
            }
        }
    }

    public static void close(InputStream thing) {
        if (thing != null) {
            try {
                thing.close();
            catch(Exception ignored) {
            }
        }
    }

    public static void close(OutputStream thing) {
        if (thing != null) {
            try {
                thing.close();
            catch(Exception ignored) {
            }
        }
    }

    public static void close(Reader thing) {
        if (thing != null) {
            try {
                thing.close();
            catch(Exception ignored) {
            }
        }
    }

    public static void close(Writer thing) {
        if (thing != null) {
            try {
                thing.close();
            catch(Exception ignored) {
            }
        }
    }

    public static final class EmptyInputStream extends InputStream {
        public int read() {
            return -1;
        }

        public int read(byte b[])  {
            return -1;
        }

        public int read(byte b[]int off, int len) {
            return -1;
        }

        public long skip(long n) {
            return 0;
        }

        public int available() {
            return 0;
        }

        public void close() {
        }

        public synchronized void mark(int readlimit) {
        }

        public synchronized void reset() {
        }

        public boolean markSupported() {
            return false;
        }
    }
}

 /**
  *  Licensed to the Apache Software Foundation (ASF) under one or more
  *  contributor license agreements.  See the NOTICE file distributed with
  *  this work for additional information regarding copyright ownership.
  *  The ASF licenses this file to You under the Apache License, Version 2.0
  *  (the "License"); you may not use this file except in compliance with
  *  the License.  You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */


 /**
  @version $Rev: 726699 $ $Date: 2008-12-15 06:30:36 -0800 (Mon, 15 Dec 2008) $
  */
 class NestedJarFile extends JarFile {
     private JarFile baseJar;
     private String basePath;
     private boolean isClosed = false;
     private boolean manifestLoaded = false;
     private Manifest manifest;
     private File tempFile;

     public NestedJarFile(JarFile jarFile, String paththrows IOException {
         super(DeploymentUtil.DUMMY_JAR_FILE);

         // verify that the jar actually contains that path
         JarEntry targetEntry = jarFile.getJarEntry(path + "/");
         if (targetEntry == null) {
             targetEntry = jarFile.getJarEntry(path);
             if (targetEntry == null) {
                 throw new IOException("Jar entry does not exist: jarFile=" + jarFile.getName() ", path=" + path);
             }
         }

         if (targetEntry.isDirectory()) {
          if(targetEntry instanceof UnpackedJarEntry) {
            //unpacked nested module inside unpacked ear
            File targetFile = ((UnpackedJarEntrytargetEntry).getFile();
            baseJar = new UnpackedJarFile(targetFile);
                 basePath = "";
          else {
            baseJar = jarFile;
            if (!path.endsWith("/")) {
                     path += "/";
                 }
                 basePath = path;
          }
         else {
             if (targetEntry instanceof UnpackedJarEntry) {
                 // for unpacked jars we don't need to copy the jar file
                 // out to a temp directory, since it is already available
                 // as a raw file
                 File targetFile = ((UnpackedJarEntrytargetEntry).getFile();
                 baseJar = new JarFile(targetFile);
                 basePath = "";
             else {
                 tempFile = DeploymentUtil.toFile(jarFile, targetEntry.getName());
                 baseJar = new JarFile(tempFile);
                 basePath = "";
             }
         }
     }

     public boolean isUnpacked() {
         if (isClosed) {
             throw new IllegalStateException("NestedJarFile is closed");
         }

         return basePath.length() ||
                ( ( baseJar != null && baseJar instanceof UnpackedJarFile ) );
     }

     public boolean isPacked() {
         if (isClosed) {
             throw new IllegalStateException("NestedJarFile is closed");
         }

         return basePath.length() == &&
                ( ( baseJar == null || !baseJar instanceof UnpackedJarFile ) );
     }

     public JarFile getBaseJar() {
         if (isClosed) {
             throw new IllegalStateException("NestedJarFile is closed");
         }
         return baseJar;
     }

     public String getBasePath() {
         if (isClosed) {
             throw new IllegalStateException("NestedJarFile is closed");
         }
         return basePath;
     }

     public Manifest getManifest() throws IOException {
         if (isClosed) {
             throw new IllegalStateException("NestedJarFile is closed");
         }

         if (!manifestLoaded) {
             JarEntry manifestEntry = getBaseEntry("META-INF/MANIFEST.MF");

             if (manifestEntry != null && !manifestEntry.isDirectory()) {
                 InputStream in = null;
                 try {
                     in = baseJar.getInputStream(manifestEntry);
                     manifest = new Manifest(in);
                 finally {
                     if (in != null) {
                         try {
                             in.close();
                         catch (IOException e) {
                             // ignore
                         }
                     }
                 }
             }
             manifestLoaded = true;
         }
         return manifest;
     }

     public NestedJarEntry getNestedJarEntry(String name) {
         if (isClosed) {
             throw new IllegalStateException("NestedJarFile is closed");
         }

         JarEntry baseEntry = getBaseEntry(name);
         if (baseEntry == null) {
             return null;
         }
         return new NestedJarEntry(name, baseEntry, getManifestSafe());
     }

     public JarEntry getJarEntry(String name) {
         if (isClosed) {
             throw new IllegalStateException("NestedJarFile is closed");
         }

         return getNestedJarEntry(name);
     }

     public ZipEntry getEntry(String name) {
         if (isClosed) {
             throw new IllegalStateException("NestedJarFile is closed");
         }

         return getNestedJarEntry(name);
     }

     public Enumeration entries() {
         if (isClosed) {
             throw new IllegalStateException("NestedJarFile is closed");
         }

         Collection baseEntries = Collections.list(baseJar.entries());
         Collection entries = new LinkedList();
         for (Iterator iterator = baseEntries.iterator(); iterator.hasNext();) {
             JarEntry baseEntry = (JarEntryiterator.next();
             String path = baseEntry.getName();
             if (path.startsWith(basePath)) {
                 entries.add(new NestedJarEntry(path.substring(basePath.length()), baseEntry, getManifestSafe()));
             }
         }
         return Collections.enumeration(entries);
     }

     public InputStream getInputStream(ZipEntry zipEntrythrows IOException {
         if (isClosed) {
             throw new IllegalStateException("NestedJarFile is closed");
         }

         JarEntry baseEntry;
         if (zipEntry instanceof NestedJarEntry) {
             baseEntry = ((NestedJarEntry)zipEntry).getBaseEntry();
         else {
             baseEntry = getBaseEntry(zipEntry.getName());
         }

         if (baseEntry == null) {
             throw new IOException("Entry not found: name=" + zipEntry.getName());
         else if (baseEntry.isDirectory()) {
             return new DeploymentUtil.EmptyInputStream();
         }
         return baseJar.getInputStream(baseEntry);
     }

     public String getName() {
         return baseJar.getName();
     }

     /**
      * Always returns -1.
      @return -1
      */
     public int size() {
         if (isClosed) {
             throw new IllegalStateException("NestedJarFile is closed");
         }
         return -1;
     }

     public void close() throws IOException {
         if (isClosed) {
             return;
         }

         try {
             try {
                 super.close();
             catch(IOException ignored) {
             }
             if (baseJar != null && basePath.length() == 0) {
                 // baseJar is created by us.  We should be closing it too.
                 baseJar.close();
             }
         finally {
             isClosed = true;
             baseJar = null;
             basePath = null;
             manifestLoaded = false;
             manifest = null;
             if (tempFile != null) {
                 tempFile.delete();
                 tempFile = null;
             }
         }
     }

     protected void finalize() throws IOException {
         close();
     }

     private JarEntry getBaseEntry(String name) {
         return baseJar.getJarEntry(basePath + name);
     }

     private Manifest getManifestSafe() {
         Manifest manifest = null;
         try {
             manifest = getManifest();
         catch (IOException e) {
             // ignore
         }
         return manifest;
     }

 }

 /**
  *  Licensed to the Apache Software Foundation (ASF) under one or more
  *  contributor license agreements.  See the NOTICE file distributed with
  *  this work for additional information regarding copyright ownership.
  *  The ASF licenses this file to You under the Apache License, Version 2.0
  *  (the "License"); you may not use this file except in compliance with
  *  the License.  You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */


 /**
  @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
  */
  class NestedJarEntry extends JarEntry {
     private final JarEntry baseEntry;
     private final Manifest manifest;

     public NestedJarEntry(String name, JarEntry baseEntry, Manifest manifest) {
         super(name);
         this.baseEntry = baseEntry;
         this.manifest = manifest;
     }

     public JarEntry getBaseEntry() {
         return baseEntry;
     }

     public Attributes getAttributes() throws IOException {
         if (manifest == null) {
             return null;
         }
         return manifest.getAttributes(getName());
     }

 

     public long getTime() {
         return baseEntry.getTime();
     }

     public void setTime(long time) {
         baseEntry.setTime(time);
     }

     public long getSize() {
         return baseEntry.getSize();
     }

     public void setSize(long size) {
         baseEntry.setSize(size);
     }

     public long getCompressedSize() {
         return baseEntry.getCompressedSize();
     }

     public void setCompressedSize(long csize) {
         baseEntry.setCompressedSize(csize);
     }

     public long getCrc() {
         return baseEntry.getCrc();
     }

     public void setCrc(long crc) {
         baseEntry.setCrc(crc);
     }

     public int getMethod() {
         return baseEntry.getMethod();
     }

     public void setMethod(int method) {
         baseEntry.setMethod(method);
     }

     public byte[] getExtra() {
         return baseEntry.getExtra();
     }

     public void setExtra(byte[] extra) {
         baseEntry.setExtra(extra);
     }

     public String getComment() {
         return baseEntry.getComment();
     }

     public void setComment(String comment) {
         baseEntry.setComment(comment);
     }

     public boolean isDirectory() {
         return baseEntry.isDirectory();
     }

     public String toString() {
         return baseEntry.toString();
     }

     public int hashCode() {
         return baseEntry.hashCode();
     }

     public Object clone() {
         return new NestedJarEntry(getName(), baseEntry, manifest);
     }
 }

  /**
   *  Licensed to the Apache Software Foundation (ASF) under one or more
   *  contributor license agreements.  See the NOTICE file distributed with
   *  this work for additional information regarding copyright ownership.
   *  The ASF licenses this file to You under the Apache License, Version 2.0
   *  (the "License"); you may not use this file except in compliance with
   *  the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   */


  /**
   @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
   */
  class UnpackedJarEntry extends JarEntry {
      private final File file;
      private final Manifest manifest;

      public UnpackedJarEntry(String name, File file, Manifest manifest) {
          super(name);
          this.file = file;
          this.manifest = manifest;
      }

      public File getFile() {
          return file;
      }

      public Attributes getAttributes() throws IOException {
          if (manifest == null) {
              return null;
          }
          return manifest.getAttributes(getName());
      }



      /**
       * An unpacked jar is read only, so this method always throws an UnsupportedOperationException.
       @param time ignored
       @throws UnsupportedOperationException always
       */
      public void setTime(long timethrows UnsupportedOperationException {
          throw new UnsupportedOperationException("Can not change the time of unpacked jar entry");
      }

      public long getTime() {
          return file.lastModified();
      }

      /**
       * An unpacked jar is read only, so this method always throws an UnsupportedOperationException.
       @param size ignored
       @throws UnsupportedOperationException always
       */
      public void setSize(long sizethrows UnsupportedOperationException {
          throw new UnsupportedOperationException("Can not change the size of unpacked jar entry");
      }

      public long getSize() {
          if (file.isDirectory()) {
              return -1;
          else {
              return file.length();
          }
      }

      /**
       * An unpacked jar is not compressed, so this method returns getSize().
       @return getSize()
       */
      public long getCompressedSize() {
          return getSize();
      }

      /**
       * An unpacked jar is read only, so this method always throws an UnsupportedOperationException.
       @param compressedSize ignored
       @throws UnsupportedOperationException always
       */
      public void setCompressedSize(long compressedSize) {
          throw new UnsupportedOperationException("Can not change the compressed size of unpacked jar entry");
      }

      public long getCrc() {
          return super.getCrc();    //To change body of overridden methods use File | Settings | File Templates.
      }

      /**
       * An unpacked jar is read only, so this method always throws an UnsupportedOperationException.
       @param crc ignored
       @throws UnsupportedOperationException always
       */
      public void setCrc(long crc) {
          throw new UnsupportedOperationException("Can not change the crc of unpacked jar entry");
      }

      public int getMethod() {
          return ZipEntry.STORED;
      }

      /**
       * An unpacked jar is read only, so this method always throws an UnsupportedOperationException.
       @param method ignored
       @throws UnsupportedOperationException always
       */
      public void setMethod(int method) {
          throw new UnsupportedOperationException("Can not change the method of unpacked jar entry");
      }

      /**
       * Always returns null.
       @return null
       */
      public byte[] getExtra() {
          return null;
      }

      /**
       * An unpacked jar is read only, so this method always throws an UnsupportedOperationException.
       @param extra ignored
       @throws UnsupportedOperationException always
       */
      public void setExtra(byte[] extra) {
          throw new UnsupportedOperationException("Can not change the extra data of unpacked jar entry");
      }

      /**
       * Always returns null.
       @return null
       */
      public String getComment() {
          return null;
      }

      /**
       * An unpacked jar is read only, so this method always throws an UnsupportedOperationException.
       @param comment ignored
       @throws UnsupportedOperationException always
       */
      public void setComment(String comment) {
          throw new UnsupportedOperationException("Can not change the comment of unpacked jar entry");
      }

      public boolean isDirectory() {
          return file.isDirectory();
      }

      public Object clone() {
          return new UnpackedJarEntry(getName(), file, manifest);
      }
  }

   
  
Related examples in the same category
1. Load an Image from a JAR file
2. List files in a jar file
3. Reading a text file from a jar file without unzipping
4. Load an Icon from a jar
5. Creating a JAR File
6. Sign jar with the certificate named alias in the keystore
7. JAR Archives: Jar Lister
8. JAR Archives: Packer200
9. JAR Archives: Unpacker200
10. Listing the Entries of a JAR File Manifest
11. Listing the Main Attributes in a JAR File Manifest
12. Retrieves the manifest from a JAR file and writes the manifest contents to a file.
13. Create Jar file
14. Get the jar file from a URL
15. Get the jar file
16. Get the jar entry
17. Getting a Jar File Using a URL
18. When no entry is specified on the URL, the entry name is null
19. Get the entry name; it should be the same as specified on URL
20. Manifest Writer
21. Load resource from Jar file
22. Jar Entry OutputStream
23. Helper Class to manipulate Java Archive File
24. Search all jar and zip files in the current directory for a given class file
25. Some utility classes for manipulating JAR files
26. Retreive Text File From Jar
27. Retreive Binary File From Jar
28. Zip jar Imploder
29. InstallJars - a utility to download and install files, Jars and Zips.
30. Get resource from Jar file
31. class for exploding jar/zip files onto the file system
32. Create a URL that refers to a jar file in the file system
33. Create a URL that refers to an entry in the jar file
34. Unjar a file
35. Make Temp Jar
36. Determine whether a file is a JAR File.
37. Search class in class path and Jar files
38. Add a jar entry to the deployment archive
39. Add jar contents to the deployment archive under the given prefix
40. Jarring and unjarring files and directories.
41. Create a Jar archive containing the src file/directory
42. A class to find resources in the classpath by their mime-type specified in the MANIFEST.
43. Jar builder
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.