1 37 38 package net.sourceforge.cruisecontrol.distributed.util; 39 40 import java.io.BufferedInputStream ; 41 import java.io.BufferedOutputStream ; 42 import java.io.File ; 43 import java.io.FileInputStream ; 44 import java.io.FileNotFoundException ; 45 import java.io.FileOutputStream ; 46 import java.io.IOException ; 47 import java.io.InputStream ; 48 import java.util.Enumeration ; 49 import java.util.zip.ZipEntry ; 50 import java.util.zip.ZipException ; 51 import java.util.zip.ZipFile ; 52 import java.util.zip.ZipOutputStream ; 53 54 import org.apache.log4j.Logger; 55 56 public final class ZipUtil { 57 58 private static final Logger LOG = Logger.getLogger(ZipUtil.class); 59 60 private ZipUtil() { } 61 62 public static void zipFolderContents(final String outFilename, final String folderToZip) { 63 validateParams(outFilename, folderToZip); 64 BufferedOutputStream bos = null; 65 ZipOutputStream zipOut = null; 66 try { 67 bos = new BufferedOutputStream (new FileOutputStream (outFilename)); 68 zipOut = new ZipOutputStream (bos); 69 final File folder = new File (folderToZip); 70 String message = "Zipping files from: " + folderToZip + " to: " + outFilename; 71 LOG.info(message); 72 zipFiles(folder, folder, zipOut); 73 message = "Finished zipping files"; 74 LOG.info(message); 75 } catch (FileNotFoundException fnfe) { 76 final String message = "File not found while zipping files to: " + outFilename; 77 LOG.error(message, fnfe); 78 throw new RuntimeException (message, fnfe); 79 } finally { 80 try { 81 if (zipOut != null) { 82 zipOut.close(); 83 } 84 } catch (ZipException ze) { 85 final File file = new File (outFilename); 86 if ((file.length() == 0) && (file.exists())) { 87 final String message = "Empty zip file created: " + outFilename; 88 LOG.debug(message); 89 try { 90 bos.close(); 92 } catch (IOException e) { 93 LOG.error(e); 94 } 95 if (!file.delete()) { 99 throw new RuntimeException ("Error deleting empty zip file: " 100 + file.getAbsolutePath()); 101 } 102 final String message2 = "Deleted empty zip file: " + outFilename; 103 LOG.debug(message2); 104 } 106 } catch (IOException ioe) { 107 final String message = "Error occured while closing zip file: " + outFilename; 108 LOG.error(message, ioe); 109 throw new RuntimeException (message, ioe); 110 } 111 } 112 } 113 114 private static void zipFiles(final File rootDir, final File folderToZip, final ZipOutputStream zipOutputStream) { 115 final byte[] buf = new byte[1024]; 116 117 final String relativePath = folderToZip.toString().substring(rootDir.toString().length()); 118 119 FileInputStream in; 120 final File [] files = folderToZip.listFiles(); 121 122 for (int i = 0; i < files.length; i++) { 123 final String filename = files[i].getName(); 124 if (files[i].isDirectory()) { 125 final String dirName = relativePath + File.separator + filename; 126 LOG.debug("adding dir [" + dirName + "]"); 127 zipFiles(rootDir, files[i], zipOutputStream); 128 } else { 129 String filePath = relativePath + File.separator + filename; 130 if (filePath.charAt(0) == File.separatorChar) { 131 filePath = filePath.substring(1); 132 } 133 LOG.debug("adding file [" + filePath + "]"); 134 try { 135 in = new FileInputStream (new File (folderToZip, filename)); 136 zipOutputStream.putNextEntry(new ZipEntry (filePath.replace(File.separatorChar, '/'))); 137 int len; 138 while ((len = in.read(buf)) > 0) { 139 zipOutputStream.write(buf, 0, len); 140 } 141 zipOutputStream.closeEntry(); 142 in.close(); 143 } catch (IOException ioe) { 144 final String message = "Error occured while zipping file " + filePath; 145 LOG.error(message, ioe); 146 throw new RuntimeException (message, ioe); 147 } 148 } 149 } 150 } 151 152 private static void validateParams(final String outFilename, final String folderToZip) { 153 if (outFilename == null) { 154 final String message = "Missing output zip file name"; 155 LOG.error(message); 156 throw new IllegalArgumentException (message); 157 } 158 if (new File (outFilename).isDirectory()) { 159 final String message = "Output file already exists as directory"; 160 LOG.error(message); 161 throw new IllegalArgumentException (message); 162 } 163 if (folderToZip == null) { 164 final String message = "Missing folder to zip"; 165 LOG.error(message); 166 throw new IllegalArgumentException (message); 167 } 168 if (!(new File (folderToZip).isDirectory())) { 169 final String message = "Target folder to zip does not exist or is not a directory"; 170 LOG.error(message); 171 throw new IllegalArgumentException (message); 172 } 173 } 174 175 180 public static void unzipFileToLocation(final String zipFilePath, final String toDirName) throws IOException { 181 final ZipFile zipFile; 182 final Enumeration enumr; 183 boolean isEmptyFile = false; 184 185 try { 186 zipFile = new ZipFile (zipFilePath); 187 if (zipFile.size() == 0) { 188 isEmptyFile = true; 189 } else { 190 final String infoMessage = "Unzipping file: " + zipFilePath; 191 LOG.info(infoMessage); 192 193 enumr = zipFile.entries(); 194 while (enumr.hasMoreElements()) { 195 final ZipEntry target = (ZipEntry ) enumr.nextElement(); 196 final String message = "Exploding: " + target.getName(); 197 LOG.debug(message); 198 saveItem(zipFile, toDirName, target); 199 } 200 zipFile.close(); 201 } 202 } catch (FileNotFoundException fnfe) { 203 final String message = "Could not find zip file" + zipFilePath; 204 LOG.error(message, fnfe); 205 throw new RuntimeException (message, fnfe); 206 } catch (ZipException ze) { 207 final String message = "Zip error occured while unzipping file " + zipFilePath; 208 LOG.error(message, ze); 209 throw new RuntimeException (message, ze); 210 } catch (IOException ioe) { 211 final String message = "Error occured while unzipping file " + zipFilePath; 212 LOG.error(message, ioe); 213 throw new RuntimeException (message, ioe); 214 } 215 216 if (isEmptyFile) { 217 final String message = "Zip file has no entries: " + zipFilePath; 218 LOG.warn(message); 219 throw new IOException (message); 220 } 221 222 final String infoMessage = "Unzip complete"; 223 LOG.info(infoMessage); 224 } 225 226 private static void saveItem(final ZipFile zipFile, final String rootDirName, final ZipEntry entry) 227 throws ZipException , IOException { 228 final InputStream is; 229 BufferedInputStream inStream = null; 230 final FileOutputStream outStream; 231 BufferedOutputStream bufferedOutStream = null; 232 try { 233 final File file = new File (rootDirName, entry.getName()); 234 if (entry.isDirectory()) { 235 file.mkdirs(); 236 } else { 237 is = zipFile.getInputStream(entry); 238 inStream = new BufferedInputStream (is); 239 final File dir = new File (file.getParent()); 240 dir.mkdirs(); 241 outStream = new FileOutputStream (file); 242 bufferedOutStream = new BufferedOutputStream (outStream); 243 244 int c; 245 while ((c = inStream.read()) != -1) { 246 bufferedOutStream.write((byte) c); 247 } 248 } 249 } catch (ZipException ze) { 250 final String message = "Zip error unzipping entry: " + entry.getName(); 251 LOG.error(message, ze); 252 throw new RuntimeException (message, ze); 253 } catch (IOException ioe) { 254 final String message = "I/O error unzipping entry: " + entry.getName(); 255 LOG.error(message, ioe); 256 throw new RuntimeException (message, ioe); 257 } finally { 258 if (bufferedOutStream != null) { 259 bufferedOutStream.close(); 260 } 261 if (inStream != null) { 262 inStream.close(); 263 } 264 } 265 } 266 } 267 | Popular Tags |