KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > jar > JarBuilder


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util.jar;
35
36 import java.io.*;
37 import java.util.jar.JarEntry JavaDoc;
38 import java.util.jar.JarOutputStream JavaDoc;
39 import java.util.jar.Manifest JavaDoc;
40
41 public class JarBuilder {
42   private JarOutputStream JavaDoc _output;
43
44   /**
45    * Creates a file file without a manifest
46    *
47    * @param file the file to write the jar to
48    * @throws IOException thrown if the file cannot be opened for writing
49    */

50   public JarBuilder(File file) throws IOException {
51     _output = new JarOutputStream JavaDoc(new BufferedOutputStream(new FileOutputStream(file)), ManifestWriter.DEFAULT);
52   }
53
54   /**
55    * Creates an empty jar file with the given manifest
56    *
57    * @param jar the file to write the jar to
58    * @param manifest the file that is the manifest for the archive
59    * @throws IOException thrown if either file cannot be opened for reading
60    */

61   public JarBuilder(File jar, File manifest) throws IOException {
62     _output = new JarOutputStream JavaDoc(new BufferedOutputStream(new FileOutputStream(jar)), new Manifest JavaDoc(new FileInputStream(manifest)));
63   }
64
65   /**
66    * Creates an empty jar file with the given manifest
67    *
68    * @param jar the file to write the jar to
69    * @param manifest the manifest file for the jar
70    * @see ManifestWriter
71    */

72   public JarBuilder(File jar, Manifest JavaDoc manifest) {
73     try {
74       _output = new JarOutputStream JavaDoc(new BufferedOutputStream(new FileOutputStream(jar)), manifest);
75     }
76     catch (IOException e) {
77       e.printStackTrace();
78     }
79   }
80
81   /**
82    * Takes a parent name and a field name and returns the concatenation of them correctly
83    *
84    * @param parent The parent directory
85    * @param name The name of the file or directory
86    * @return the string concatenation of the parent and the name
87    */

88   private String JavaDoc makeName(String JavaDoc parent, String JavaDoc name) {
89     String JavaDoc sep = "/"; // NOTE: This can be a '/' since it is a path in the jar file itself
90
if( parent.equals("") )
91       return name;
92     if (parent.endsWith(sep))
93       return parent + name;
94     return parent + sep + name;
95   }
96
97   /**
98    * Adds the file to the given path and name
99    *
100    * @param file the file to be added
101    * @param parent the directory to the path in which the file is to be added
102    * @param fileName the name of the file in the archive
103    */

104   public void addFile(File file, String JavaDoc parent, String JavaDoc fileName) throws IOException {
105     byte data[] = new byte[2048];
106
107     FileInputStream fi = new FileInputStream(file.getAbsolutePath());
108     BufferedInputStream origin = new BufferedInputStream(fi, 2048);
109
110     JarEntry JavaDoc entry = new JarEntry JavaDoc(makeName(parent, fileName));
111     _output.putNextEntry(entry);
112
113     int count = origin.read(data, 0, 2048);
114     while (count != -1) {
115       _output.write(data, 0, count);
116       count = origin.read(data, 0, 2048);
117     }
118
119     origin.close();
120   }
121
122   /** Add the directory into the directory specified by parent
123    * @param dir the directory to add
124    * @param parent the path inside the jar that the directory should be added to
125    */

126   public void addDirectoryRecursive(File dir, String JavaDoc parent) {
127     addDirectoryRecursiveHelper(dir, parent, new byte[2048], new FileFilter() {
128       public boolean accept(File pathname) { return true; }
129     });
130   }
131
132   /**
133    * Add the directory into the directory specified by parent
134    * @param dir the directory to add
135    * @param parent the path inside the jar that the directory should be added to
136    * @param filter the filter used to filter the files
137    */

138   public void addDirectoryRecursive(File dir, String JavaDoc parent, FileFilter filter) {
139     addDirectoryRecursiveHelper(dir, parent, new byte[2048], filter);
140   }
141
142   /**
143    * Add the contents of a directory that match a filter to the archive
144    * @param dir the directory to add
145    * @param parent the directory to add into
146    * @param buffer a buffer that is 2048 bytes
147    * @param filter the FileFilter to filter the files by
148    * @return true on success, false on failure
149    */

150   private boolean addDirectoryRecursiveHelper(File dir, String JavaDoc parent, byte[] buffer, FileFilter filter) {
151     try {
152       File[] files = dir.listFiles(filter);
153       BufferedInputStream origin = null;
154
155       if( files == null ) // listFiles may return null if there's an IO error
156
return true;
157       for (int i = 0; i < files.length; i++) {
158         if( files[i].isFile() ) {
159           origin = new BufferedInputStream(new FileInputStream(files[i]), 2048);
160
161           JarEntry JavaDoc entry = new JarEntry JavaDoc(makeName(parent, files[i].getName()));
162           _output.putNextEntry(entry);
163
164           int count;
165           while((count = origin.read(buffer, 0, 2048)) != -1) {
166             _output.write(buffer, 0, count);
167           }
168           origin.close();
169         }
170         else if( files[i].isDirectory() ) {
171           addDirectoryRecursiveHelper(files[i], makeName(parent, files[i].getName()),buffer,filter);
172         }
173       }
174     } catch(Exception JavaDoc e) {
175       e.printStackTrace();
176     }
177     return true;
178   }
179
180   /**
181    * Makes a directory in the jar file
182    *
183    * @param parent The name of the parent that the directory is to be created in
184    * @param dirName The name of the directory to be created
185    * @return Returns true on success, false on failure
186    */

187   public boolean makeDirectory(String JavaDoc parent, String JavaDoc dirName) {
188     JarEntry JavaDoc entry = new JarEntry JavaDoc(makeName(parent, dirName));
189     try {
190       _output.putNextEntry(entry);
191     }
192     catch (IOException e) {
193       return false;
194     }
195     return true;
196   }
197
198   /**
199    * Close writing on the jar file
200    */

201   public void close() throws IOException {
202     _output.flush();
203     _output.close();
204   }
205 }
Popular Tags