Add jar contents to the deployment archive under the given prefix : Jar File « File Input Output « Java






Add jar contents to the deployment archive under the given prefix

  
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;

/**
 * A collection of jar utilities
 * 
 * @author thomas.diesler@jboss.org
 * @version $Revision: 81011 $
 */
public class JarUtils {
  /**
   * Add jar contents to the deployment archive under the given prefix
   */
  public static String[] addJar(JarOutputStream outputStream, String prefix, File jar)
      throws IOException {

    ArrayList tmp = new ArrayList();
    FileInputStream fis = new FileInputStream(jar);
    JarInputStream jis = new JarInputStream(fis);
    JarEntry entry = jis.getNextJarEntry();
    while (entry != null) {
      if (entry.isDirectory() == false) {
        String entryName = prefix + entry.getName();
        tmp.add(entryName);
        addJarEntry(outputStream, entryName, jis);
      }
      entry = jis.getNextJarEntry();
    }
    jis.close();
    String[] names = new String[tmp.size()];
    tmp.toArray(names);
    return names;
  }

  /**
   * Add a jar entry to the deployment archive
   */
  public static void addJarEntry(JarOutputStream outputStream, String entryName,
      InputStream inputStream) throws IOException {
    outputStream.putNextEntry(new JarEntry(entryName));
    copyStream(outputStream, inputStream);
  }

  /**
   * Copies the input stream to the output stream
   */
  public static void copyStream(OutputStream outputStream, InputStream inputStream)
      throws IOException {
    byte[] bytes = new byte[4096];
    int read = inputStream.read(bytes, 0, 4096);
    while (read > 0) {
      outputStream.write(bytes, 0, read);
      read = inputStream.read(bytes, 0, 4096);
    }
  }

}

   
    
  








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.Jar file helper to deployment
36.Make Temp Jar
37.Determine whether a file is a JAR File.
38.Search class in class path and Jar files
39.Add a jar entry to the deployment archive
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
44.Writes all files of the given directory to the specified jar-file