1 23 24 27 28 package com.sun.enterprise.tools.common.util.zip; 29 30 import java.io.File ; 31 import java.io.IOException ; 32 import java.io.FileInputStream ; 33 import java.io.FileOutputStream ; 34 import java.io.OutputStream ; 35 import java.util.zip.ZipEntry ; 36 import java.util.zip.ZipOutputStream ; 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 zipFilename, String dirName, String [] fileList) throws ZipFileException 46 { 47 try 48 { 49 Reporter.assertIt(zipFilename); Reporter.assertIt(dirName); Reporter.assertIt(fileList); Reporter.assertIt(fileList.length > 0); 55 File f = new File (dirName); 57 Reporter.assertIt(f.exists(), "directory (" + dirName + ") doesn't exist"); Reporter.assertIt(f.isDirectory()); 60 try 62 { 63 dirName = f.getCanonicalPath(); 64 } 65 catch(IOException e) 66 { 67 Reporter.warn("Couldn't getCanonicalPath() for " + dirName); } 69 70 dirName = dirName.replace('\\', '/'); 72 73 if(!dirName.endsWith("/")) dirName += "/"; 77 f = new File (zipFilename); 79 Reporter.assertIt(!f.isDirectory(), "zipFile (" + zipFilename + ") is actually a directory!" ); 81 for(int i = 0; i < fileList.length; i++) 82 { 83 fileList[i] = fileList[i].replace('\\', '/'); } 85 86 this.zipFilename = zipFilename; 87 this.dirName = dirName; 88 this.fileList = fileList; 89 zipStream = new ZipOutputStream (new FileOutputStream (zipFilename)); 90 } 91 catch(Exception f) 92 { 93 Reporter.critical(new StackTrace(f)); throw new ZipFileException(f); 95 } 96 } 97 98 public ZipWriter(OutputStream outStream, String dirName, String [] fileList) throws ZipFileException 99 { 100 try 101 { 102 Reporter.assertIt(dirName); Reporter.assertIt(fileList); Reporter.assertIt(fileList.length > 0); 108 File f = new File (dirName); 110 Reporter.assertIt(f.exists(), "directory (" + dirName + ") doesn't exist"); Reporter.assertIt(f.isDirectory()); 113 try 115 { 116 dirName = f.getCanonicalPath(); 117 } 118 catch(IOException e) 119 { 120 Reporter.warn("Couldn't getCanonicalPath() for " + dirName); } 122 123 dirName = dirName.replace('\\', '/'); 125 126 if(!dirName.endsWith("/")) dirName += "/"; 130 134 for(int i = 0; i < fileList.length; i++) 135 { 136 fileList[i] = fileList[i].replace('\\', '/'); } 138 139 this.dirName = dirName; 141 this.fileList = fileList; 142 zipStream = new ZipOutputStream (outStream); 143 } 144 catch(Assertion.Failure f) 145 { 146 throw new ZipFileException(f); 147 } 148 } 149 150 152 public void write() throws ZipFileException 153 { 154 try 155 { 156 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)); throw z; 169 } 170 catch(Exception e) 171 { 172 Reporter.critical(new StackTrace(e)); throw new ZipFileException(e); 174 } 175 } 176 177 179 public void addEntry(String entryName) throws ZipFileException, IOException 180 { 181 int totalBytes = 0; 182 FileInputStream in = new FileInputStream (dirName + entryName); 183 ZipEntry ze = new ZipEntry (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."); } 196 197 198 200 public String toString() 201 { 202 String s = "Zip File Name: " + zipFilename + "\n"; s += "Directory Name: " + dirName + "\n"; s += "***** File Contents *********\n"; s += ContainerHelper.toOneString(fileList); 206 207 return s; 208 } 209 210 212 public static void main(String [] notUsed) 213 { 214 Reporter.setSeverityLevel(0); 216 try 217 { 218 String [] array = { "hello.txt", "a\\a.txt", "a\\b/b.txt" }; ZipWriter zw = new ZipWriter("E:\\temp\\hello/ZipWriter.jar", "E:/Temp\\hello", array); zw.write(); 221 Reporter.verbose("" + zw); } 223 catch(ZipFileException e) 224 { 225 Reporter.verbose("ZipFileException: " + e); } 227 } 228 229 231 private String zipFilename = null; 232 private String dirName = null; 233 private ZipOutputStream zipStream = null; 234 private String [] fileList = null; 235 private byte[] buffer = new byte[16384]; 236 } 237 | Popular Tags |