| 1 | |
| 2 | /*BEGIN_COPYRIGHT_BLOCK |
| 3 | * |
| 4 | * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu) |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are met: |
| 9 | * * Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * * Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the |
| 15 | * names of its contributors may be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 22 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 25 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 26 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | * |
| 30 | * This software is Open Source Initiative approved Open Source Software. |
| 31 | * Open Source Initative Approved is a trademark of the Open Source Initiative. |
| 32 | * |
| 33 | * This file is part of DrJava. Download the current version of this project |
| 34 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ |
| 35 | * |
| 36 | * END_COPYRIGHT_BLOCK*/ |
| 37 | |
| 38 | package edu.rice.cs.util.jar; |
| 39 | |
| 40 | import java.io.BufferedInputStream; |
| 41 | import java.io.BufferedOutputStream; |
| 42 | import java.io.File; |
| 43 | import java.io.FileFilter; |
| 44 | import java.io.FileInputStream; |
| 45 | import java.io.FileOutputStream; |
| 46 | import java.io.IOException; |
| 47 | import java.util.jar.JarEntry; |
| 48 | import java.util.jar.JarOutputStream; |
| 49 | import java.util.jar.Manifest; |
| 50 | |
| 51 | import org.apache.log4j.Logger; |
| 52 | |
| 53 | public class JarBuilder { |
| 54 | |
| 55 | /** Logger for this class.*/ |
| 56 | private static final Logger logger = Logger.getLogger(JarBuilder.class); |
| 57 | private JarOutputStream _output; |
| 58 | |
| 59 | /** |
| 60 | * Creates a file file without a manifest |
| 61 | * |
| 62 | * @param file the file to write the jar to |
| 63 | * @throws IOException thrown if the file cannot be opened for writing |
| 64 | */ |
| 65 | public JarBuilder(File file) throws IOException { |
| 66 | _output = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(file))); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Creates an empty jar file with the given manifest |
| 71 | * |
| 72 | * @param jar the file to write the jar to |
| 73 | * @param manifest the file that is the manifest for the archive |
| 74 | * @throws IOException thrown if either file cannot be opened for reading |
| 75 | */ |
| 76 | public JarBuilder(File jar, File manifest) throws IOException { |
| 77 | _output = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(jar)), new Manifest(new FileInputStream(manifest))); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Creates an empty jar file with the given manifest |
| 82 | * |
| 83 | * @param jar the file to write the jar to |
| 84 | * @param manifest the manifest file for the jar |
| 85 | * @see ManifestWriter |
| 86 | */ |
| 87 | public JarBuilder(File jar, Manifest manifest) { |
| 88 | try { |
| 89 | _output = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(jar)), manifest); |
| 90 | } |
| 91 | catch (IOException e) { |
| 92 | logger.error("Could not create an empty JAR file.", e); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Takes a parent name and a field name and returns the concatenation of them correctly |
| 98 | * |
| 99 | * @param parent The parent directory |
| 100 | * @param name The name of the file or directory |
| 101 | * @return the string concatenation of the parent and the name |
| 102 | */ |
| 103 | private String makeName(String parent, String name) { |
| 104 | String sep = "/"; // NOTE: This can be a '/' since it is a path in the jar file itself |
| 105 | if( parent.equals("") ) |
| 106 | return name; |
| 107 | if (parent.endsWith(sep)) |
| 108 | return parent + name; |
| 109 | return parent + sep + name; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Adds the file to the given path and name |
| 114 | * |
| 115 | * @param file the file to be added |
| 116 | * @param parent the directory to the path in which the file is to be added |
| 117 | * @param fileName the name of the file in the archive |
| 118 | */ |
| 119 | public void addFile(File file, String parent, String fileName) throws IOException { |
| 120 | byte data[] = new byte[2048]; |
| 121 | |
| 122 | FileInputStream fi = new FileInputStream(file.getAbsolutePath()); |
| 123 | BufferedInputStream origin = new BufferedInputStream(fi, 2048); |
| 124 | |
| 125 | JarEntry entry = new JarEntry(makeName(parent, fileName)); |
| 126 | _output.putNextEntry(entry); |
| 127 | |
| 128 | int count = origin.read(data, 0, 2048); |
| 129 | while (count != -1) { |
| 130 | _output.write(data, 0, count); |
| 131 | count = origin.read(data, 0, 2048); |
| 132 | } |
| 133 | |
| 134 | origin.close(); |
| 135 | } |
| 136 | |
| 137 | /** Add the directory into the directory specified by parent |
| 138 | * @param dir the directory to add |
| 139 | * @param parent the path inside the jar that the directory should be added to |
| 140 | */ |
| 141 | public void addDirectoryRecursive(File dir, String parent) { |
| 142 | addDirectoryRecursiveHelper(dir, parent, new byte[2048], null); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Add the directory into the directory specified by parent |
| 147 | * @param dir the directory to add |
| 148 | * @param parent the path inside the jar that the directory should be added to |
| 149 | * @param filter the filter used to filter the files |
| 150 | */ |
| 151 | public void addDirectoryRecursive(File dir, String parent, FileFilter filter) { |
| 152 | addDirectoryRecursiveHelper(dir, parent, new byte[2048], filter); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Add the contents of a directory that match a filter to the archive |
| 157 | * @param dir the directory to add |
| 158 | * @param parent the directory to add into |
| 159 | * @param buffer a buffer that is 2048 bytes |
| 160 | * @param filter the FileFilter to filter the files by |
| 161 | * @return true on success, false on failure |
| 162 | */ |
| 163 | private boolean addDirectoryRecursiveHelper(File dir, String parent, byte[] buffer, FileFilter filter) { |
| 164 | try { |
| 165 | File[] files = dir.listFiles(filter); |
| 166 | BufferedInputStream origin = null; |
| 167 | |
| 168 | if( files == null ) // listFiles may return null if there's an IO error |
| 169 | return true; |
| 170 | for (int i = 0; i < files.length; i++) { |
| 171 | if( files[i].isFile() ) { |
| 172 | origin = new BufferedInputStream(new FileInputStream(files[i]), 2048); |
| 173 | |
| 174 | JarEntry entry = new JarEntry(makeName(parent, files[i].getName())); |
| 175 | _output.putNextEntry(entry); |
| 176 | |
| 177 | int count; |
| 178 | while((count = origin.read(buffer, 0, 2048)) != -1) { |
| 179 | _output.write(buffer, 0, count); |
| 180 | } |
| 181 | origin.close(); |
| 182 | } |
| 183 | else if( files[i].isDirectory() ) { |
| 184 | addDirectoryRecursiveHelper(files[i], makeName(parent, files[i].getName()),buffer,filter); |
| 185 | } |
| 186 | } |
| 187 | } catch(Exception e) { |
| 188 | logger.error("Could not add directory contents to archive.", e); |
| 189 | } |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Makes a directory in the jar file |
| 195 | * |
| 196 | * @param parent The name of the parent that the directory is to be created in |
| 197 | * @param dirName The name of the directory to be created |
| 198 | * @return Returns true on success, false on failure |
| 199 | */ |
| 200 | public boolean makeDirectory(String parent, String dirName) { |
| 201 | JarEntry entry = new JarEntry(makeName(parent, dirName)); |
| 202 | try { |
| 203 | _output.putNextEntry(entry); |
| 204 | } |
| 205 | catch (IOException e) { |
| 206 | return false; |
| 207 | } |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Close writing on the jar file |
| 213 | */ |
| 214 | public void close() throws IOException { |
| 215 | _output.flush(); |
| 216 | _output.close(); |
| 217 | } |
| 218 | } |