KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > packager > impl > WinUtility


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 package org.jdesktop.jdic.packager.impl;
22
23 import java.io.BufferedInputStream JavaDoc;
24 import java.io.BufferedOutputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.FileNotFoundException JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.jar.JarEntry JavaDoc;
32 import java.util.jar.JarInputStream JavaDoc;
33 import java.util.zip.CRC32 JavaDoc;
34 import java.util.zip.CheckedOutputStream JavaDoc;
35 import java.util.zip.CheckedInputStream JavaDoc;
36 import java.util.zip.ZipOutputStream JavaDoc;
37 import java.util.zip.ZipEntry JavaDoc;
38
39 /**
40  * WinUtility will provide some general utility functions for Windows platform.
41  */

42 public class WinUtility {
43     /**
44      * Block size for file operation.
45      */

46     private static final int BUFFER = 2048;
47     /**
48      * Resource ID for the binary resource.
49      */

50     public static final int BIN_RES_ID = 104;
51     /**
52      * Resource ID for the UUID string resource.
53      */

54     public static final int STRING_RES_UUID_ID = 1600;
55     /**
56      * Resource ID for the localization flag string resource.
57      */

58     public static final int STRING_RES_LOCALIZATION_FLAG_ID = 1760;
59
60     /**
61      * Checks whether the give file is a valid file.
62      *
63      * @param filePath The given file path.
64      * @throws IOException If the file is not valid.
65      */

66     public static void checkFileValid(String JavaDoc filePath) throws IOException JavaDoc {
67         File JavaDoc file = new File JavaDoc(filePath);
68         if (!FileOperUtility.isFileReadable(file)) {
69             throw new IOException JavaDoc("The file "
70                                   + filePath
71                                   + " does not exist or can not be accessed!");
72         }
73     }
74
75     /**
76      * Checks whether the given directory is a valid directory.
77      * @param dirPath The given directory path.
78      * @throws IOException If the given directory is not a valid directory.
79      */

80     private static void checkDirValid(String JavaDoc dirPath) throws IOException JavaDoc {
81         File JavaDoc dir = new File JavaDoc(dirPath);
82         if (!FileOperUtility.isDirectoryReadable(dir)) {
83             throw new IOException JavaDoc("The dir "
84                                   + dirPath
85                                   + " does not exist or can not be accessed!");
86         }
87     }
88
89     /**
90      * Puts one file into the zip file.
91      *
92      * @param oneFileName The given file name
93      * @param oneFilePath The file absolute path
94      * @param out The zip file handle
95      * @throws IOException If failed to package the file into the zip file.
96      */

97     private static void putIntoZip(String JavaDoc oneFileName,
98                                    String JavaDoc oneFilePath,
99                                    ZipOutputStream JavaDoc out) throws IOException JavaDoc {
100         try {
101             byte[] data = new byte[BUFFER];
102             BufferedInputStream JavaDoc origin = null;
103             FileInputStream JavaDoc fi = new FileInputStream JavaDoc(oneFilePath);
104             origin = new BufferedInputStream JavaDoc(fi, BUFFER);
105
106             //Loop 1: iterate the file to get the size of the file
107
int bytes = 0;
108             int count = 0;
109             while ((count = origin.read(data, 0, BUFFER)) != -1) {
110                 bytes += count; //file size are recoreded into bytes variable
111
}
112             origin.close();
113             fi.close();
114
115             //Loop 2: iterate the file to get the CRC checksum
116
fi = new FileInputStream JavaDoc(oneFilePath);
117             origin = new BufferedInputStream JavaDoc(fi, BUFFER);
118             CheckedInputStream JavaDoc originCheck = new CheckedInputStream JavaDoc(
119                                                         origin,
120                                                         new CRC32 JavaDoc());
121             long crcChecksum = 0; //crc check sum value is stored here
122
while ((count = originCheck.read(data, 0, BUFFER)) != -1) {
123                 ;
124             }
125             crcChecksum = originCheck.getChecksum().getValue();
126             originCheck.close();
127             origin.close();
128             fi.close();
129
130             fi = new FileInputStream JavaDoc(oneFilePath);
131             origin = new BufferedInputStream JavaDoc(fi, BUFFER);
132             ZipEntry JavaDoc entry = new ZipEntry JavaDoc(oneFileName);
133             entry.setMethod(ZipEntry.STORED);
134             entry.setSize(bytes);
135             entry.setCrc(crcChecksum);
136             out.putNextEntry(entry);
137             while ((count = origin.read(data, 0, BUFFER)) != -1) {
138                 out.write(data, 0, count);
139             }
140             origin.close();
141             out.closeEntry();
142
143         } catch (Exception JavaDoc e) {
144             throw new IOException JavaDoc("Failed to package the zip file!");
145         }
146     }
147
148     /**
149      * Create a jar file includs all the file in the given file list.
150      *
151      * @param jarFilePath The jar file to be created.
152      * @param pkgInfo The given jnlp package info.
153      *
154      * @throws IOException If failed to jar the relevant files.
155      */

156     public static void jarJnlpFiles(String JavaDoc jarFilePath,
157                                     JnlpPackageInfo pkgInfo)
158                                     throws IOException JavaDoc {
159         try {
160             String JavaDoc sourcePath = pkgInfo.getResourceDirPath();
161             //Check if the fileList is empty
162
Iterator JavaDoc it = pkgInfo.getJnlpRefFilePaths();
163             if (it == null) {
164                 return;
165             }
166             //Initialize all the input/output stream
167
FileOutputStream JavaDoc dest = new FileOutputStream JavaDoc(jarFilePath);
168             CheckedOutputStream JavaDoc checksum = new CheckedOutputStream JavaDoc(
169                                                     dest,
170                                                     new CRC32 JavaDoc());
171             ZipOutputStream JavaDoc out = new ZipOutputStream JavaDoc(
172                                             new BufferedOutputStream JavaDoc(checksum));
173             //Set compression rate to be zero: Just package, no deflation.
174
out.setLevel(0);
175             out.setMethod(ZipOutputStream.STORED);
176             //Non-empty file list, loop each file to add into the jar file
177
checkDirValid(sourcePath);
178             //The first entry is the jnlp file specified in the absolute path,
179
//so there would be no need to locate it relevant to the sourcepath.
180
boolean isJnlpFile = true;
181             while (it.hasNext()) {
182                 String JavaDoc oneFileName = (String JavaDoc) it.next();
183                 String JavaDoc oneFilePath = null;
184                 if (!isJnlpFile) {
185                     oneFilePath = sourcePath
186                                   + File.separator
187                                   + oneFileName;
188                 } else {
189                     oneFilePath = oneFileName;
190                     isJnlpFile = false;
191                 }
192                 checkFileValid(oneFilePath);
193                 putIntoZip(oneFileName, oneFilePath, out);
194             }
195             //Flush and close the jar package
196
out.flush();
197             out.close();
198             checksum.close();
199             dest.close();
200         } catch (Exception JavaDoc e) {
201             throw new IOException JavaDoc("Failed to create the jar file "
202                                   + jarFilePath);
203         }
204     }
205
206     /**
207      * Extract specified source file from the jar package and copy it as the
208      * destination file.
209      *
210      * @param jarFilePath The given jar file.
211      * @param sourceFileName The given source file name.
212      * @param destFilePath The given destination file path.
213      * @throws IOException If failed to extract the specified file from
214      * the jar file.
215      */

216      public static void extractFileFromJarFile(String JavaDoc jarFilePath,
217                                                String JavaDoc sourceFileName,
218                                                String JavaDoc destFilePath)
219                                                throws IOException JavaDoc {
220         try {
221             //Check if the specified jar file is a valid file
222
checkFileValid(jarFilePath);
223
224             boolean findSourceFile = false;
225             BufferedOutputStream JavaDoc dest = null;
226             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(jarFilePath);
227             JarInputStream JavaDoc jis = new JarInputStream JavaDoc(
228                                                 new BufferedInputStream JavaDoc(fis));
229             JarEntry JavaDoc entry;
230             while ((entry = jis.getNextJarEntry()) != null) {
231                 String JavaDoc entryName = entry.getName();
232                 if (entry.getName().equalsIgnoreCase(sourceFileName)) {
233                     //Find the source file
234
findSourceFile = true;
235                     int count;
236                     byte[] data = new byte[BUFFER];
237                     //wrtie the files to the disk
238
FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(destFilePath);
239                     dest = new BufferedOutputStream JavaDoc(fos, BUFFER);
240                     while ((count = jis.read(data, 0, BUFFER)) != -1) {
241                         dest.write(data, 0, count);
242                     }
243                     dest.flush();
244                     dest.close();
245                 }
246             }
247             jis.close();
248             fis.close();
249             if (!findSourceFile) {
250                 throw new IOException JavaDoc("The jar file "
251                                       + jarFilePath
252                                       + " does not contain file "
253                                       + sourceFileName);
254             }
255         } catch (FileNotFoundException JavaDoc e) {
256             throw new IOException JavaDoc("Error: Illegal File Name: " + destFilePath);
257         } catch (Exception JavaDoc e) {
258             throw new IOException JavaDoc("Error extracting data from the jar file!");
259         }
260     }
261
262     /**
263      * Update/Add a string into the executable file resource's string table.
264      *
265      * @param appFilePath The given executable file path.
266      * @param contentStr The string to be added.
267      * @param resID The resource ID of the added string.
268      *
269      * @throws IOException If failed to update the resource string.
270      */

271     public static void updateResourceString(String JavaDoc appFilePath,
272                                              String JavaDoc contentStr,
273                                              int resID)
274                                              throws IOException JavaDoc {
275         checkFileValid(appFilePath);
276         WinMsiWrapper.winUpdateResourceString(appFilePath, contentStr, resID);
277     }
278
279     /**
280      * Update/Add a binary data into the the executable file resource.
281      *
282      * @param appFilePath The given executable file path.
283      * @param dataFilePath The file containing the data to be added.
284      * @param resID The resource ID of the added resource.
285      *
286      * @throws IOException If failed to update the binary resource field.
287      */

288     public static void updateResourceData(String JavaDoc appFilePath,
289                                            String JavaDoc dataFilePath,
290                                            int resID)
291                                            throws IOException JavaDoc {
292         checkFileValid(appFilePath);
293         checkFileValid(dataFilePath);
294         WinMsiWrapper.winUpdateResourceData(appFilePath, dataFilePath, resID);
295     }
296 }
297
Popular Tags