1 56 package org.objectstyle.cayenne.util; 57 58 import java.io.BufferedInputStream ; 59 import java.io.BufferedOutputStream ; 60 import java.io.File ; 61 import java.io.FileInputStream ; 62 import java.io.FileOutputStream ; 63 import java.io.IOException ; 64 import java.io.InputStream ; 65 import java.io.OutputStream ; 66 import java.util.Enumeration ; 67 import java.util.zip.ZipEntry ; 68 import java.util.zip.ZipFile ; 69 import java.util.zip.ZipOutputStream ; 70 71 76 public class ZipUtil { 77 78 81 public ZipUtil() { 82 super(); 83 } 84 85 92 public static void unzip(File zipFile, File destDir) throws IOException { 93 ZipFile zip = new ZipFile (zipFile); 94 95 try { 96 Enumeration en = zip.entries(); 97 int bufSize = 8 * 1024; 98 99 while (en.hasMoreElements()) { 100 ZipEntry entry = (ZipEntry ) en.nextElement(); 101 File file = 102 (destDir != null) 103 ? new File (destDir, entry.getName()) 104 : new File (entry.getName()); 105 106 if (entry.isDirectory()) { 107 if (!file.mkdirs()) { 108 throw new IOException ( 109 "Error creating directory: " + file); 110 } 111 } else { 112 File parent = file.getParentFile(); 113 if (parent != null && !parent.exists()) { 114 if (!parent.mkdirs()) { 115 throw new IOException ( 116 "Error creating directory: " + parent); 117 } 118 } 119 120 InputStream in = zip.getInputStream(entry); 121 try { 122 OutputStream out = 123 new BufferedOutputStream ( 124 new FileOutputStream (file), 125 bufSize); 126 127 try { 128 Util.copyPipe(in, out, bufSize); 129 } finally { 130 out.close(); 131 } 132 133 } finally { 134 in.close(); 135 } 136 } 137 } 138 } finally { 139 zip.close(); 140 } 141 } 142 143 154 public static void zip( 155 File zipFile, 156 File parentDir, 157 File [] sources, 158 char pathSeparator) 159 throws IOException { 160 161 String stripPath = (parentDir != null) ? parentDir.getPath() : ""; 162 if (stripPath.length() > 0 && !stripPath.endsWith(File.separator)) { 163 stripPath += File.separator; 164 } 165 166 ZipOutputStream out = 167 new ZipOutputStream (new FileOutputStream (zipFile)); 168 out.setMethod(ZipOutputStream.DEFLATED); 169 170 try { 171 for (int i = 0; i < sources.length; i++) { 173 if (!sources[i].exists()) { 174 throw new IllegalArgumentException ( 175 "File or directory does not exist: " + sources[i]); 176 } 177 178 if (sources[i].isDirectory()) { 179 zipDirectory(out, stripPath, sources[i], pathSeparator); 180 } else { 181 zipFile(out, stripPath, sources[i], pathSeparator); 182 } 183 } 184 } finally { 185 out.close(); 186 } 187 } 188 189 193 private static void zipDirectory( 194 ZipOutputStream out, 195 String stripPath, 196 File dir, 197 char pathSeparator) 198 throws IOException { 199 200 String [] entries = dir.list(); 201 202 if (entries == null || entries.length == 0) { 203 return; 204 } 205 206 for (int i = 0; i < entries.length; i++) { 208 File file = new File (dir, entries[i]); 209 if (file.isDirectory()) { 210 zipDirectory(out, stripPath, file, pathSeparator); 211 } else { 212 zipFile(out, stripPath, file, pathSeparator); 213 } 214 } 215 } 216 217 221 private static void zipFile( 222 ZipOutputStream out, 223 String stripPath, 224 File file, 225 char pathSeparator) 226 throws IOException { 227 ZipEntry ze = 228 new ZipEntry (processPath(file.getPath(), stripPath, pathSeparator)); 229 ze.setTime(file.lastModified()); 230 out.putNextEntry(ze); 231 232 byte[] buffer = new byte[8 * 1024]; 233 BufferedInputStream in = 234 new BufferedInputStream (new FileInputStream (file), buffer.length); 235 236 try { 237 int count = 0; 238 while ((count = in.read(buffer, 0, buffer.length)) >= 0) { 239 if (count != 0) { 240 out.write(buffer, 0, count); 241 } 242 } 243 } finally { 244 in.close(); 245 } 246 } 247 248 private static String processPath( 249 String path, 250 String stripPath, 251 char pathSeparator) { 252 if (!path.startsWith(stripPath)) { 253 throw new IllegalArgumentException ( 254 "Invalid entry: " 255 + path 256 + "; expected to start with " 257 + stripPath); 258 } 259 260 return path.substring(stripPath.length()).replace( 261 File.separatorChar, 262 pathSeparator); 263 } 264 } 265 | Popular Tags |