1+ /*BEGIN_COPYRIGHT_BLOCK
2+ *
3+ * This file is part of DrJava. Download the current version of this project:
4+ * http://sourceforge.net/projects/drjava/ or http://www.drjava.org/
5+ *
6+ * DrJava Open Source License
7+ *
8+ * Copyright (C) 2001-2003 JavaPLT group at Rice University (javaplt@rice.edu)
9+ * All rights reserved.
10+ *
11+ * Developed by: Java Programming Languages Team
12+ * Rice University
13+ * http://www.cs.rice.edu/~javaplt/
14+ *
15+ * Permission is hereby granted, free of charge, to any person obtaining a
16+ * copy of this software and associated documentation files (the "Software"),
17+ * to deal with the Software without restriction, including without
18+ * limitation the rights to use, copy, modify, merge, publish, distribute,
19+ * sublicense, and/or sell copies of the Software, and to permit persons to
20+ * whom the Software is furnished to do so, subject to the following
21+ * conditions:
22+ *
23+ * - Redistributions of source code must retain the above copyright
24+ * notice, this list of conditions and the following disclaimers.
25+ * - Redistributions in binary form must reproduce the above copyright
26+ * notice, this list of conditions and the following disclaimers in the
27+ * documentation and/or other materials provided with the distribution.
28+ * - Neither the names of DrJava, the JavaPLT, Rice University, nor the
29+ * names of its contributors may be used to endorse or promote products
30+ * derived from this Software without specific prior written permission.
31+ * - Products derived from this software may not be called "DrJava" nor
32+ * use the term "DrJava" as part of their names without prior written
33+ * permission from the JavaPLT group. For permission, write to
34+ * javaplt@rice.edu.
35+ *
36+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
39+ * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
40+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
41+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
42+ * OTHER DEALINGS WITH THE SOFTWARE.
43+ *
44+ END_COPYRIGHT_BLOCK*/
45+
46+ package edu .rice .cs .util .jar ;
47+
48+ import java .io .*;
49+ import java .util .jar .JarEntry ;
50+ import java .util .jar .JarOutputStream ;
51+ import java .util .jar .Manifest ;
52+
53+ public class JarBuilder {
54+ private JarOutputStream _output ;
55+
56+ /**
57+ * Creates a file file without a manifest
58+ *
59+ * @param file the file to write the jar to
60+ * @throws IOException thrown if the file cannot be opened for writing
61+ */
62+ public JarBuilder (File file ) throws IOException {
63+ _output = new JarOutputStream (new BufferedOutputStream (new FileOutputStream (file )), ManifestWriter .DEFAULT );
64+ }
65+
66+ /**
67+ * Creates an empty jar file with the given manifest
68+ *
69+ * @param jar the file to write the jar to
70+ * @param manifest the file that is the manifest for the archive
71+ * @throws IOException thrown if either file cannot be opened for reading
72+ */
73+ public JarBuilder (File jar , File manifest ) throws IOException {
74+ _output = new JarOutputStream (new BufferedOutputStream (new FileOutputStream (jar )), new Manifest (new FileInputStream (manifest )));
75+ }
76+
77+ /**
78+ * Creates an empty jar file with the given manifest
79+ *
80+ * @param jar the file to write the jar to
81+ * @param manifest the manifest file for the jar
82+ * @see ManifestWriter
83+ */
84+ public JarBuilder (File jar , Manifest manifest ) {
85+ try {
86+ _output = new JarOutputStream (new BufferedOutputStream (new FileOutputStream (jar )), manifest );
87+ }
88+ catch (IOException e ) {
89+ e .printStackTrace ();
90+ }
91+ }
92+
93+ /**
94+ * Takes a parent name and a field name and returns the concatenation of them correctly
95+ *
96+ * @param parent The parent directory
97+ * @param name The name of the file or directory
98+ * @return the string concatenation of the parent and the name
99+ */
100+ private String makeName (String parent , String name ) {
101+ if ( parent .equals ("" ) )
102+ return name ;
103+ if (parent .endsWith ("/" ))
104+ return parent + name ;
105+ return parent + "/" + name ;
106+ }
107+
108+ /**
109+ * Adds the file to the given path and name
110+ *
111+ * @param file the file to be added
112+ * @param parent the directory to the path in which the file is to be added
113+ * @param fileName the name of the file in the archive
114+ */
115+ public void addFile (File file , String parent , String fileName ) throws IOException {
116+ byte data [] = new byte [2048 ];
117+
118+ FileInputStream fi = new FileInputStream (file .getAbsolutePath ());
119+ BufferedInputStream origin = new BufferedInputStream (fi , 2048 );
120+
121+ JarEntry entry = new JarEntry (makeName (parent , fileName ));
122+ _output .putNextEntry (entry );
123+
124+ int count = origin .read (data , 0 , 2048 );
125+ while (count != -1 ) {
126+ _output .write (data , 0 , count );
127+ count = origin .read (data , 0 , 2048 );
128+ }
129+
130+ origin .close ();
131+ }
132+
133+ /**
134+ * Add the directory into the directory specified by parent
135+ * @param dir the directory to add
136+ * @param parent the path inside the jar that the directory should be added to
137+ */
138+ public void addDirectoryRecursive (File dir , String parent ) {
139+ addDirectoryRecursiveHelper (dir , parent , new byte [2048 ], new FileFilter () {
140+ public boolean accept (File pathname ) {
141+ return true ;
142+ }
143+ });
144+ }
145+
146+ /**
147+ * Add the directory into the directory specified by parent
148+ * @param dir the directory to add
149+ * @param parent the path inside the jar that the directory should be added to
150+ * @param filter the filter used to filter the files
151+ */
152+ public void addDirectoryRecursive (File dir , String parent , FileFilter filter ) {
153+ addDirectoryRecursiveHelper (dir , parent , new byte [2048 ], filter );
154+ }
155+
156+ /**
157+ * Add the contents of a directory that match a filter to the archive
158+ * @param dir the directory to add
159+ * @param parent the directory to add into
160+ * @param buffer a buffer that is 2048 bytes
161+ * @param filter the FileFilter to filter the files by
162+ * @return true on success, false on failure
163+ */
164+ private boolean addDirectoryRecursiveHelper (File dir , String parent , byte [] buffer , FileFilter filter ) {
165+ try {
166+ File [] files = dir .listFiles (filter );
167+ BufferedInputStream origin = null ;
168+
169+ if ( files == null )
170+ return true ;
171+ for (int i = 0 ; i < files .length ; i ++) {
172+ if ( files [i ].isFile () ) {
173+ origin = new BufferedInputStream (new FileInputStream (files [i ]), 2048 );
174+
175+ JarEntry entry = new JarEntry (makeName (parent , files [i ].getName ()));
176+ _output .putNextEntry (entry );
177+
178+ int count ;
179+ while ((count = origin .read (buffer , 0 , 2048 )) != -1 ) {
180+ _output .write (buffer , 0 , count );
181+ }
182+ origin .close ();
183+ }
184+ else if ( files [i ].isDirectory () ) {
185+ addDirectoryRecursiveHelper (files [i ], makeName (parent , files [i ].getName ()),buffer ,filter );
186+ }
187+ }
188+ } catch (Exception e ) {
189+ e .printStackTrace ();
190+ }
191+ return true ;
192+ }
193+
194+ /**
195+ * Makes a directory in the jar file
196+ *
197+ * @param parent The name of the parent that the directory is to be created in
198+ * @param dirName The name of the directory to be created
199+ * @return Returns true on success, false on failure
200+ */
201+ public boolean makeDirectory (String parent , String dirName ) {
202+ JarEntry entry = new JarEntry (makeName (parent , dirName ));
203+ try {
204+ _output .putNextEntry (entry );
205+ }
206+ catch (IOException e ) {
207+ return false ;
208+ }
209+ return true ;
210+ }
211+
212+ /**
213+ * Close writing on the jar file
214+ */
215+ public void close () throws IOException {
216+ _output .flush ();
217+ _output .close ();
218+ }
219+ }
0 commit comments