1 package com.bull.eclipse.jonas.utils; 2 3 import java.io.BufferedInputStream ; 4 import java.io.BufferedOutputStream ; 5 import java.io.File ; 6 import java.io.FileInputStream ; 7 import java.io.FileOutputStream ; 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 import java.io.OutputStream ; 11 import java.util.ArrayList ; 12 import java.util.Hashtable ; 13 import java.util.List ; 14 import java.util.Vector ; 15 import java.util.zip.CRC32 ; 16 import java.util.zip.ZipEntry ; 17 import java.util.zip.ZipFile ; 18 import java.util.zip.ZipOutputStream ; 19 20 import com.bull.eclipse.jonas.ExtraResourcesEntries; 21 22 23 public class Zipper { 24 25 private static final int BUFFER = 2048; 26 27 private static final long EMPTY_CRC = new CRC32 ().getValue (); 28 protected String emptyBehavior = "skip"; 29 private Vector filesets = new Vector (); 30 31 protected Hashtable addedDirs = new Hashtable (); 32 private File outputFile = null; 33 private File directory = null; 34 private ExtraResourcesEntries extraList = null; 35 private FileOutputStream fos = null; 36 private ZipOutputStream zos = null; 37 private String currentDirName; 38 39 public Zipper(File outputFile, File directory) throws IOException { 40 this.outputFile = outputFile; 41 this.directory = directory; 42 currentDirName = directory.getAbsolutePath(); 43 } 44 45 public Zipper(File outputFile, File directory, ExtraResourcesEntries extraList) throws IOException { 46 this.outputFile = outputFile; 47 this.directory = directory; 48 this.extraList = extraList; 49 currentDirName = directory.getAbsolutePath(); 50 } 51 52 public void zip() throws IOException { 53 fos = new FileOutputStream (outputFile); 54 zos = new ZipOutputStream (fos); 55 zipDir(directory); 56 zipExtraResources(extraList); 57 ArrayList a ; 58 zos.flush(); 59 zos.close(); 60 fos.close(); 61 } 62 63 64 static public void unzip(ZipEntry zipEntry,ZipFile zipFile,File output) throws IOException { 65 copyInputStream(zipFile.getInputStream(zipEntry), new BufferedOutputStream (new FileOutputStream (output))); 66 } 67 68 69 private void zipDir(File dir) throws IOException { 70 if(!dir.getPath().equals(currentDirName)) { 71 String entryName = dir.getPath().substring(currentDirName.length()+1); 72 entryName = entryName.replace('\\', '/'); 73 ZipEntry ze = new ZipEntry (entryName + "/"); 74 if (dir != null && dir.exists()) { 75 ze.setTime(dir.lastModified()); 76 } else { 77 ze.setTime(System.currentTimeMillis()); 78 } 79 ze.setSize (0); 80 ze.setMethod (ZipEntry.STORED); 81 ze.setCrc (EMPTY_CRC); 83 zos.putNextEntry (ze); 84 } 85 86 if (dir.exists() && dir.isDirectory()) { 87 File [] fileList = dir.listFiles(); 88 89 for (int i = 0; i < fileList.length; i++) { 90 if (fileList[i].isDirectory() && this.acceptDir(fileList[i])) { 91 zipDir(fileList[i]); 92 } 93 if (fileList[i].isFile() && this.acceptFile(fileList[i])) { 94 zipFile(fileList[i]); 95 } 96 } 97 } 98 99 } 100 101 102 private void zipExtraResources(ExtraResourcesEntries extraList) throws IOException { 103 if (extraList != null) { 104 List resourcesList = extraList.getList(); 105 int i = 0; 106 String tempFile = null; 107 while (i < resourcesList.size()) { 108 tempFile = new File ((String )resourcesList.get(i)).getName(); 109 zipFile(new File ((String )resourcesList.get(i)) , "WEB-INF" + File.separator + "lib" + File.separator + tempFile); 110 i++; 111 } 112 } 113 } 114 115 private void zipFile(File file) throws IOException { 116 if(!file.equals(this.outputFile)) { 117 BufferedInputStream bis = new BufferedInputStream (new FileInputStream (file),BUFFER); 118 119 String entryName = file.getPath().substring(currentDirName.length()+1); 120 entryName = entryName.replace('\\', '/'); 121 ZipEntry fileEntry = new ZipEntry (entryName); 122 zos.putNextEntry(fileEntry); 123 124 byte[] data = new byte[BUFFER]; 125 int byteCount; 126 while ((byteCount = bis.read(data, 0, BUFFER)) != -1) { 127 zos.write(data, 0, byteCount); 128 } 129 130 bis.close(); 131 } 132 } 133 134 private void zipFile(File file, String zipEntry) throws IOException { 135 if(!file.equals(this.outputFile)) { 136 BufferedInputStream bis = new BufferedInputStream (new FileInputStream (file),BUFFER); 137 138 String entryName = zipEntry; 140 entryName = entryName.replace('\\', '/'); 141 ZipEntry fileEntry = new ZipEntry (entryName); 142 zos.putNextEntry(fileEntry); 143 144 byte[] data = new byte[BUFFER]; 145 int byteCount; 146 while ((byteCount = bis.read(data, 0, BUFFER)) != -1) { 147 zos.write(data, 0, byteCount); 148 } 149 150 bis.close(); 151 } 152 } 153 154 protected boolean acceptDir(File dir) { 155 return true; 156 } 157 158 protected boolean acceptFile(File file) { 159 return true; 160 } 161 162 163 private static final void copyInputStream(InputStream in, OutputStream out) 164 throws IOException 165 { 166 byte[] buffer = new byte[1024]; 167 int len; 168 169 while((len = in.read(buffer)) >= 0) 170 out.write(buffer, 0, len); 171 172 in.close(); 173 out.close(); 174 } 175 176 } 177 | Popular Tags |