1 19 20 package org.apache.cayenne.util; 21 22 import java.io.BufferedInputStream ; 23 import java.io.BufferedOutputStream ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.OutputStream ; 30 import java.util.Enumeration ; 31 import java.util.zip.ZipEntry ; 32 import java.util.zip.ZipFile ; 33 import java.util.zip.ZipOutputStream ; 34 35 40 public class ZipUtil { 41 42 45 public ZipUtil() { 46 super(); 47 } 48 49 56 public static void unzip(File zipFile, File destDir) throws IOException { 57 ZipFile zip = new ZipFile (zipFile); 58 59 try { 60 Enumeration en = zip.entries(); 61 int bufSize = 8 * 1024; 62 63 while (en.hasMoreElements()) { 64 ZipEntry entry = (ZipEntry ) en.nextElement(); 65 File file = 66 (destDir != null) 67 ? new File (destDir, entry.getName()) 68 : new File (entry.getName()); 69 70 if (entry.isDirectory()) { 71 if (!file.mkdirs()) { 72 throw new IOException ( 73 "Error creating directory: " + file); 74 } 75 } else { 76 File parent = file.getParentFile(); 77 if (parent != null && !parent.exists()) { 78 if (!parent.mkdirs()) { 79 throw new IOException ( 80 "Error creating directory: " + parent); 81 } 82 } 83 84 InputStream in = zip.getInputStream(entry); 85 try { 86 OutputStream out = 87 new BufferedOutputStream ( 88 new FileOutputStream (file), 89 bufSize); 90 91 try { 92 Util.copyPipe(in, out, bufSize); 93 } finally { 94 out.close(); 95 } 96 97 } finally { 98 in.close(); 99 } 100 } 101 } 102 } finally { 103 zip.close(); 104 } 105 } 106 107 118 public static void zip( 119 File zipFile, 120 File parentDir, 121 File [] sources, 122 char pathSeparator) 123 throws IOException { 124 125 String stripPath = (parentDir != null) ? parentDir.getPath() : ""; 126 if (stripPath.length() > 0 && !stripPath.endsWith(File.separator)) { 127 stripPath += File.separator; 128 } 129 130 ZipOutputStream out = 131 new ZipOutputStream (new FileOutputStream (zipFile)); 132 out.setMethod(ZipOutputStream.DEFLATED); 133 134 try { 135 for (int i = 0; i < sources.length; i++) { 137 if (!sources[i].exists()) { 138 throw new IllegalArgumentException ( 139 "File or directory does not exist: " + sources[i]); 140 } 141 142 if (sources[i].isDirectory()) { 143 zipDirectory(out, stripPath, sources[i], pathSeparator); 144 } else { 145 zipFile(out, stripPath, sources[i], pathSeparator); 146 } 147 } 148 } finally { 149 out.close(); 150 } 151 } 152 153 157 private static void zipDirectory( 158 ZipOutputStream out, 159 String stripPath, 160 File dir, 161 char pathSeparator) 162 throws IOException { 163 164 String [] entries = dir.list(); 165 166 if (entries == null || entries.length == 0) { 167 return; 168 } 169 170 for (int i = 0; i < entries.length; i++) { 172 File file = new File (dir, entries[i]); 173 if (file.isDirectory()) { 174 zipDirectory(out, stripPath, file, pathSeparator); 175 } else { 176 zipFile(out, stripPath, file, pathSeparator); 177 } 178 } 179 } 180 181 185 private static void zipFile( 186 ZipOutputStream out, 187 String stripPath, 188 File file, 189 char pathSeparator) 190 throws IOException { 191 ZipEntry ze = 192 new ZipEntry (processPath(file.getPath(), stripPath, pathSeparator)); 193 ze.setTime(file.lastModified()); 194 out.putNextEntry(ze); 195 196 byte[] buffer = new byte[8 * 1024]; 197 BufferedInputStream in = 198 new BufferedInputStream (new FileInputStream (file), buffer.length); 199 200 try { 201 int count = 0; 202 while ((count = in.read(buffer, 0, buffer.length)) >= 0) { 203 if (count != 0) { 204 out.write(buffer, 0, count); 205 } 206 } 207 } finally { 208 in.close(); 209 } 210 } 211 212 private static String processPath( 213 String path, 214 String stripPath, 215 char pathSeparator) { 216 if (!path.startsWith(stripPath)) { 217 throw new IllegalArgumentException ( 218 "Invalid entry: " 219 + path 220 + "; expected to start with " 221 + stripPath); 222 } 223 224 return path.substring(stripPath.length()).replace( 225 File.separatorChar, 226 pathSeparator); 227 } 228 } 229 | Popular Tags |