java压缩与解压缩文件
http://www.oschina.net/code/snippet_2482052_54816
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
Java代码 package com.yabushan.test.util.upload; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; import org.apache.tools.zip.ZipOutputStream; import org.slf4j.LoggerFactory; public class ZipUtil { protected static final org.slf4j.Logger log = LoggerFactory.getLogger(ZipUtil.class); private static final int BUFFER = 2048; /** * 解压文件到指定路径 * * @param filePath * @param upZipPath * @return 返回解压的文件集合 */ public static List<File> unZip(String filePath, String upZipPath) { List<File> list = new ArrayList<File>(); int count = -1; File file = null; InputStream is = null; FileOutputStream fos = null; BufferedOutputStream bos = null; // 生成指定的保存目录 String savePath = upZipPath; if (!new File(savePath).exists()) { new File(savePath).mkdirs(); } try { ZipFile zipFile = new ZipFile(filePath, "GBK"); Enumeration enu = zipFile.getEntries(); while (enu.hasMoreElements()) { ZipEntry zipEntry = (ZipEntry) enu.nextElement(); if (zipEntry.isDirectory()) { new File(savePath + "/" + zipEntry.getName()).mkdirs(); continue; } if (zipEntry.getName().indexOf("/") != -1) { new File(savePath + "/" + zipEntry.getName().substring(0, zipEntry.getName().lastIndexOf("/"))) .mkdirs(); } is = zipFile.getInputStream(zipEntry); file = new File(savePath + "/" + zipEntry.getName()); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos, BUFFER); byte buf[] = new byte[BUFFER]; while ((count = is.read(buf)) > -1) { bos.write(buf, 0, count); } bos.flush(); fos.close(); is.close(); list.add(file); } zipFile.close(); return list; } catch (IOException ioe) { log.error(ioe.getMessage()); return list; } } /** * RAR 需要配置rar路径 * * @param filePath * @param unRarPath * 路径要唯一,否则获取文件列表会出错 * @return */ public static int unRar(String filePath, String unRarPath) { int result = -99; if (!(new File(unRarPath).exists())) { new File(unRarPath).mkdirs(); } try { //String cmd = GlobalConfig.getConfigValue("cmd.path"); String cmd="test"; String unrarCmd = cmd + " e -r -o+ " + filePath + " " + unRarPath; Runtime rt = Runtime.getRuntime(); Process pre = rt.exec(unrarCmd); while(result==-99){ try { Thread.sleep(1000L); result = pre.exitValue(); } catch (Exception e) { // TODO: handle exception result = -99; } } InputStreamReader isr = new InputStreamReader(pre.getInputStream()); BufferedReader bf = new BufferedReader(isr); String line = null; while ((line = bf.readLine()) != null) { line = line.trim(); if ("".equals(line)) { continue; } log.info(line); } bf.close(); if (result != 0) { log.error("unRar " + pre.exitValue()); } // 杀死进程 退出 // pre.destroy(); return result; } catch (Exception e) { log.error(e.getMessage() + ": " + e.getStackTrace()); return -2; } } /*** * 将多个文件打成压缩包 * * @param list * 需打包的文件路径集合 * @param zipfilename * 压缩包名称 */ public static void listToZip(List<String> list, String zipfilename) { FileInputStream is = null; String path = ""; File file = null; ZipOutputStream zos = null; try { if (list != null && list.size() > 0) { //String uri = GlobalConfig.getConfigValue("zipFile.path"); String uri="D:/ZIP"; File f = new File(uri); if(!f.exists()){ f.mkdirs(); } zipfilename = uri + zipfilename; //创建zip文件输出流 zos = new ZipOutputStream(new FileOutputStream(new File( zipfilename))); zos.setEncoding("GBK"); for (int i = 0; i < list.size(); i++) { path = list.get(i); file = new File(path); if (file.exists()) { //创建源文件输入流 is = new FileInputStream(file); zos.putNextEntry(new ZipEntry(file.getName())); byte[] buf = new byte[BUFFER]; int length = -1; while ((length = is.read(buf)) != -1) { zos.write(buf, 0, length); zos.flush(); } zos.closeEntry(); is.close(); } else { System.out.println("源文件不存在"); } } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); } if (zos != null) { zos.close(); } } catch (IOException e) { e.printStackTrace(); } } } /*** * 将多个文件打成压缩包(压缩包内文件名称由参数中传入) * * @param list * 需打包的文件信息集合 * @param zipfilename * 压缩包名称 */ public static void listMapToZip(List<Map<String,Object>> list, String zipfilename) { FileInputStream is = null; String path = ""; File file = null; ZipOutputStream zos = null; try { if (list != null && list.size() > 0) { //String uri = GlobalConfig.getConfigValue("zipFile.path"); String uri="D:/ZIP"; File f = new File(uri); if(!f.exists()){ f.mkdirs(); } zipfilename = uri + zipfilename; //创建zip文件输出流 zos = new ZipOutputStream(new FileOutputStream(new File( zipfilename))); zos.setEncoding("GBK"); for (Map map : list) { path = map.get("filePath")+""; file = new File(path); if (file.exists()) { //创建源文件输入流 is = new FileInputStream(file); zos.putNextEntry(new ZipEntry(map.get("fileName")+"")); byte[] buf = new byte[BUFFER]; int length = -1; while ((length = is.read(buf)) != -1) { zos.write(buf, 0, length); zos.flush(); } zos.closeEntry(); is.close(); } else { System.out.println("源文件不存在"); } } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); } if (zos != null) { zos.close(); } } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) throws Exception { List<String> list = new ArrayList<String>(); list.add("D:/File/img/tempImg/7f7ffd45856be6aa.txt"); list.add("D:/File/img/tempImg/63f8959465093ec0.jpeg"); list.add("D:/File/img/tempImg/815b476dc8cf790e.txt"); list.add("D:/File/img/tempImg/4451519220dad91a.txt"); //ZipUtil.listToZip(list, "测试.zip"); System.out.println(ZipUtil.unZip("D:/ZIP测试.zip", "D:/ZIP/bb/").toString()); } } |