KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > util > zip > ZipWriter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /* Byron Nevins, April 2000
25  * ZipFile -- A utility class for exploding jar files that contain EJB(s). Used *only* in this package by the EJBImporter class
26  */

27
28 package com.sun.enterprise.tools.common.util.zip;
29
30 import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.FileOutputStream JavaDoc;
34 import java.io.OutputStream JavaDoc;
35 import java.util.zip.ZipEntry JavaDoc;
36 import java.util.zip.ZipOutputStream JavaDoc;
37
38 import com.sun.enterprise.tools.common.util.diagnostics.Reporter;
39 import com.sun.enterprise.tools.common.util.diagnostics.StackTrace;
40 import com.sun.enterprise.tools.common.util.Assertion;
41 import com.sun.enterprise.tools.common.util.ContainerHelper;
42
43 public class ZipWriter
44 {
45     public ZipWriter(String JavaDoc zipFilename, String JavaDoc dirName, String JavaDoc[] fileList) throws ZipFileException
46     {
47         try
48         {
49             // note -- these asserts will be caught & repackaged as a ZipFileException
50
Reporter.assertIt(zipFilename); //NOI18N
51
Reporter.assertIt(dirName); //NOI18N
52
Reporter.assertIt(fileList); //NOI18N
53
Reporter.assertIt(fileList.length > 0); //NOI18N
54

55             //make sure it's really a directory
56
File JavaDoc f = new File JavaDoc(dirName);
57             Reporter.assertIt(f.exists(), "directory (" + dirName + ") doesn't exist");//NOI18N
58
Reporter.assertIt(f.isDirectory()); //NOI18N
59

60             // change the filename to be full-path & UNIX style
61
try
62             {
63                 dirName = f.getCanonicalPath();
64             }
65             catch(IOException JavaDoc e)
66             {
67                 Reporter.warn("Couldn't getCanonicalPath() for " + dirName);//NOI18N
68
}
69             
70             dirName = dirName.replace('\\', '/'); // all UNIX-style filenames...
71

72             
73             // we need the dirname to end in a '/'
74
if(!dirName.endsWith("/"))//NOI18N
75
dirName += "/";//NOI18N
76

77             // make sure the zipFile requested isn't the name of an existing directory
78
f = new File JavaDoc(zipFilename);
79             Reporter.assertIt(!f.isDirectory(), "zipFile (" + zipFilename + ") is actually a directory!" );//NOI18N
80

81             for(int i = 0; i < fileList.length; i++)
82             {
83                 fileList[i] = fileList[i].replace('\\', '/'); // just in case...
84
}
85             
86             this.zipFilename = zipFilename;
87             this.dirName = dirName;
88             this.fileList = fileList;
89                         zipStream = new ZipOutputStream JavaDoc(new FileOutputStream JavaDoc(zipFilename));
90         }
91         catch(Exception JavaDoc f)
92         {
93                         Reporter.critical(new StackTrace(f)); //NOI18N
94
throw new ZipFileException(f);
95         }
96     }
97
98     public ZipWriter(OutputStream JavaDoc outStream, String JavaDoc dirName, String JavaDoc[] fileList) throws ZipFileException
99     {
100         try
101         {
102             // note -- these asserts will be caught & repackaged as a ZipFileException
103
//Reporter.assertIt(zipFilename); //NOI18N
104
Reporter.assertIt(dirName); //NOI18N
105
Reporter.assertIt(fileList); //NOI18N
106
Reporter.assertIt(fileList.length > 0); //NOI18N
107

108             //make sure it's really a directory
109
File JavaDoc f = new File JavaDoc(dirName);
110             Reporter.assertIt(f.exists(), "directory (" + dirName + ") doesn't exist");//NOI18N
111
Reporter.assertIt(f.isDirectory()); //NOI18N
112

113             // change the filename to be full-path & UNIX style
114
try
115             {
116                 dirName = f.getCanonicalPath();
117             }
118             catch(IOException JavaDoc e)
119             {
120                 Reporter.warn("Couldn't getCanonicalPath() for " + dirName);//NOI18N
121
}
122             
123             dirName = dirName.replace('\\', '/'); // all UNIX-style filenames...
124

125             
126             // we need the dirname to end in a '/'
127
if(!dirName.endsWith("/"))//NOI18N
128
dirName += "/";//NOI18N
129

130             // make sure the zipFile requested isn't the name of an existing directory
131
//f = new File(zipFilename);
132
//Reporter.assertIt(!f.isDirectory(), "zipFile (" + zipFilename + ") is actually a directory!" );//NOI18N
133

134             for(int i = 0; i < fileList.length; i++)
135             {
136                 fileList[i] = fileList[i].replace('\\', '/'); // just in case...
137
}
138             
139             //this.zipFilename = zipFilename;
140
this.dirName = dirName;
141             this.fileList = fileList;
142                         zipStream = new ZipOutputStream JavaDoc(outStream);
143         }
144         catch(Assertion.Failure f)
145         {
146             throw new ZipFileException(f);
147         }
148     }
149
150         //////////////////////////////////////////////////////////////////////////////////////////////////////////////
151

152     public void write() throws ZipFileException
153     {
154         try
155         {
156             //zipStream = new ZipOutputStream(new FileOutputStream(zipFilename));
157

158             for(int i = 0; i < fileList.length; i++)
159             {
160                 addEntry(fileList[i]);
161             }
162             
163             zipStream.close();
164         }
165         catch(ZipFileException z)
166         {
167                     Reporter.critical(new StackTrace(z)); //NOI18N
168
throw z;
169         }
170         catch(Exception JavaDoc e)
171         {
172                     Reporter.critical(new StackTrace(e)); //NOI18N
173
throw new ZipFileException(e);
174         }
175     }
176
177     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
178

179     public void addEntry(String JavaDoc entryName) throws ZipFileException, IOException JavaDoc
180     {
181         int totalBytes = 0;
182         FileInputStream JavaDoc in = new FileInputStream JavaDoc(dirName + entryName);
183         ZipEntry JavaDoc ze = new ZipEntry JavaDoc(entryName);
184         
185         zipStream.putNextEntry(ze);
186
187         for(int numBytes = in.read(buffer); numBytes > 0; numBytes = in.read(buffer))
188         {
189             zipStream.write(buffer, 0, numBytes);
190             totalBytes += numBytes;
191         }
192
193         zipStream.closeEntry();
194         Reporter.verbose("Wrote " + entryName + " to Zip File. Wrote " + totalBytes + " bytes.");//NOI18N
195
}
196
197         
198     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
199

200     public String JavaDoc toString()
201     {
202         String JavaDoc s = "Zip File Name: " + zipFilename + "\n";//NOI18N
203
s += "Directory Name: " + dirName + "\n";//NOI18N
204
s += "***** File Contents *********\n";//NOI18N
205
s += ContainerHelper.toOneString(fileList);
206         
207         return s;
208     }
209
210     //////////////////////////////////////////////////////////////////////////////////////////////////////////////
211

212     public static void main(String JavaDoc[] notUsed)
213     {
214         Reporter.setSeverityLevel(0); //NOI18N
215

216         try
217         {
218             String JavaDoc[] array = { "hello.txt", "a\\a.txt", "a\\b/b.txt" };//NOI18N
219
ZipWriter zw = new ZipWriter("E:\\temp\\hello/ZipWriter.jar", "E:/Temp\\hello", array);//NOI18N
220
zw.write();
221             Reporter.verbose("" + zw);//NOI18N
222
}
223         catch(ZipFileException e)
224         {
225             Reporter.verbose("ZipFileException: " + e);//NOI18N
226
}
227     }
228
229     /////////////////////////////////////////////////////////////////////////////////////////
230

231     private String JavaDoc zipFilename = null;
232     private String JavaDoc dirName = null;
233     private ZipOutputStream JavaDoc zipStream = null;
234     private String JavaDoc[] fileList = null;
235     private byte[] buffer = new byte[16384];
236 }
237
Popular Tags