1 20 21 package org.jdesktop.jdic.packager.impl; 22 23 import java.io.BufferedInputStream ; 24 import java.io.BufferedOutputStream ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.FileNotFoundException ; 30 import java.util.Iterator ; 31 import java.util.jar.JarEntry ; 32 import java.util.jar.JarInputStream ; 33 import java.util.zip.CRC32 ; 34 import java.util.zip.CheckedOutputStream ; 35 import java.util.zip.CheckedInputStream ; 36 import java.util.zip.ZipOutputStream ; 37 import java.util.zip.ZipEntry ; 38 39 42 public class WinUtility { 43 46 private static final int BUFFER = 2048; 47 50 public static final int BIN_RES_ID = 104; 51 54 public static final int STRING_RES_UUID_ID = 1600; 55 58 public static final int STRING_RES_LOCALIZATION_FLAG_ID = 1760; 59 60 66 public static void checkFileValid(String filePath) throws IOException { 67 File file = new File (filePath); 68 if (!FileOperUtility.isFileReadable(file)) { 69 throw new IOException ("The file " 70 + filePath 71 + " does not exist or can not be accessed!"); 72 } 73 } 74 75 80 private static void checkDirValid(String dirPath) throws IOException { 81 File dir = new File (dirPath); 82 if (!FileOperUtility.isDirectoryReadable(dir)) { 83 throw new IOException ("The dir " 84 + dirPath 85 + " does not exist or can not be accessed!"); 86 } 87 } 88 89 97 private static void putIntoZip(String oneFileName, 98 String oneFilePath, 99 ZipOutputStream out) throws IOException { 100 try { 101 byte[] data = new byte[BUFFER]; 102 BufferedInputStream origin = null; 103 FileInputStream fi = new FileInputStream (oneFilePath); 104 origin = new BufferedInputStream (fi, BUFFER); 105 106 int bytes = 0; 108 int count = 0; 109 while ((count = origin.read(data, 0, BUFFER)) != -1) { 110 bytes += count; } 112 origin.close(); 113 fi.close(); 114 115 fi = new FileInputStream (oneFilePath); 117 origin = new BufferedInputStream (fi, BUFFER); 118 CheckedInputStream originCheck = new CheckedInputStream ( 119 origin, 120 new CRC32 ()); 121 long crcChecksum = 0; 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 (oneFilePath); 131 origin = new BufferedInputStream (fi, BUFFER); 132 ZipEntry entry = new ZipEntry (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 e) { 144 throw new IOException ("Failed to package the zip file!"); 145 } 146 } 147 148 156 public static void jarJnlpFiles(String jarFilePath, 157 JnlpPackageInfo pkgInfo) 158 throws IOException { 159 try { 160 String sourcePath = pkgInfo.getResourceDirPath(); 161 Iterator it = pkgInfo.getJnlpRefFilePaths(); 163 if (it == null) { 164 return; 165 } 166 FileOutputStream dest = new FileOutputStream (jarFilePath); 168 CheckedOutputStream checksum = new CheckedOutputStream ( 169 dest, 170 new CRC32 ()); 171 ZipOutputStream out = new ZipOutputStream ( 172 new BufferedOutputStream (checksum)); 173 out.setLevel(0); 175 out.setMethod(ZipOutputStream.STORED); 176 checkDirValid(sourcePath); 178 boolean isJnlpFile = true; 181 while (it.hasNext()) { 182 String oneFileName = (String ) it.next(); 183 String 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 out.flush(); 197 out.close(); 198 checksum.close(); 199 dest.close(); 200 } catch (Exception e) { 201 throw new IOException ("Failed to create the jar file " 202 + jarFilePath); 203 } 204 } 205 206 216 public static void extractFileFromJarFile(String jarFilePath, 217 String sourceFileName, 218 String destFilePath) 219 throws IOException { 220 try { 221 checkFileValid(jarFilePath); 223 224 boolean findSourceFile = false; 225 BufferedOutputStream dest = null; 226 FileInputStream fis = new FileInputStream (jarFilePath); 227 JarInputStream jis = new JarInputStream ( 228 new BufferedInputStream (fis)); 229 JarEntry entry; 230 while ((entry = jis.getNextJarEntry()) != null) { 231 String entryName = entry.getName(); 232 if (entry.getName().equalsIgnoreCase(sourceFileName)) { 233 findSourceFile = true; 235 int count; 236 byte[] data = new byte[BUFFER]; 237 FileOutputStream fos = new FileOutputStream (destFilePath); 239 dest = new BufferedOutputStream (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 ("The jar file " 251 + jarFilePath 252 + " does not contain file " 253 + sourceFileName); 254 } 255 } catch (FileNotFoundException e) { 256 throw new IOException ("Error: Illegal File Name: " + destFilePath); 257 } catch (Exception e) { 258 throw new IOException ("Error extracting data from the jar file!"); 259 } 260 } 261 262 271 public static void updateResourceString(String appFilePath, 272 String contentStr, 273 int resID) 274 throws IOException { 275 checkFileValid(appFilePath); 276 WinMsiWrapper.winUpdateResourceString(appFilePath, contentStr, resID); 277 } 278 279 288 public static void updateResourceData(String appFilePath, 289 String dataFilePath, 290 int resID) 291 throws IOException { 292 checkFileValid(appFilePath); 293 checkFileValid(dataFilePath); 294 WinMsiWrapper.winUpdateResourceData(appFilePath, dataFilePath, resID); 295 } 296 } 297 | Popular Tags |