【JAVA】压缩文件以及解压缩
下面是JAVA中将文件/文件夹压缩成zip以及解压缩的代码:
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.util.Enumeration; 7 import java.util.zip.ZipEntry; 8 import java.util.zip.ZipFile; 9 import java.util.zip.ZipOutputStream; 10 11 public class FileZip { 12 public static void zip(String src, String dest) throws IOException { 13 //提供了一个数据项压缩成一个ZIP归档输出流 14 ZipOutputStream out = null; 15 try { 16 File outFile = new File(dest);//源文件或者目录 17 File fileOrDirectory = new File(src);//压缩文件路径 18 out = new ZipOutputStream(new FileOutputStream(outFile)); 19 20 //如果此文件是一个文件,否则为false。 21 if (fileOrDirectory.isFile()) { 22 zipFileOrDirectory(out, fileOrDirectory, ""); 23 } else { 24 //返回一个文件或空阵列。 25 File[] entries = fileOrDirectory.listFiles(); 26 for (int i = 0; i < entries.length; i++) { 27 // 递归压缩,更新curPaths 28 zipFileOrDirectory(out, entries[i], ""); 29 } 30 } 31 } catch (IOException ex) { 32 ex.printStackTrace(); 33 } finally { 34 //关闭输出流 35 if (out != null) { 36 try { 37 out.close(); 38 } catch (IOException ex) { 39 ex.printStackTrace(); 40 } 41 } 42 } 43 } 44 45 private static void zipFileOrDirectory(ZipOutputStream out, 46 File fileOrDirectory, String curPath) throws IOException { 47 //从文件中读取字节的输入流 48 FileInputStream in = null; 49 try { 50 //如果此文件是一个目录,否则返回false。 51 if (!fileOrDirectory.isDirectory()) { 52 // 压缩文件 53 byte[] buffer = new byte[4096]; 54 int bytes_read; 55 in = new FileInputStream(fileOrDirectory); 56 //实例代表一个条目内的ZIP归档 57 ZipEntry entry = new ZipEntry(curPath + fileOrDirectory.getName()); 58 //条目的信息写入底层流 59 out.putNextEntry(entry); 60 while ((bytes_read = in.read(buffer)) != -1) { 61 out.write(buffer, 0, bytes_read); 62 } 63 out.closeEntry(); 64 } else { 65 // 压缩目录 66 File[] entries = fileOrDirectory.listFiles(); 67 for (int i = 0; i < entries.length; i++) { 68 // 递归压缩,更新curPaths 69 zipFileOrDirectory(out, entries[i], curPath + fileOrDirectory.getName() + "/"); 70 } 71 } 72 } catch (IOException ex) { 73 ex.printStackTrace(); 74 // throw ex; 75 } finally { 76 if (in != null) { 77 try { 78 in.close(); 79 } catch (IOException ex) { 80 ex.printStackTrace(); 81 } 82 } 83 } 84 } 85 86 @SuppressWarnings("unchecked") 87 public static void unzip(String zipFileName, String outputDirectory) throws IOException { 88 ZipFile zipFile = null; 89 try { 90 zipFile = new ZipFile(zipFileName); 91 Enumeration e = zipFile.entries(); 92 ZipEntry zipEntry = null; 93 File dest = new File(outputDirectory); 94 dest.mkdirs(); 95 while (e.hasMoreElements()) { 96 zipEntry = (ZipEntry) e.nextElement(); 97 String entryName = zipEntry.getName(); 98 InputStream in = null; 99 FileOutputStream out = null; 100 try { 101 if (zipEntry.isDirectory()) { 102 String name = zipEntry.getName(); 103 name = name.substring(0, name.length() - 1); 104 File f = new File(outputDirectory + File.separator + name); 105 f.mkdirs(); 106 } else { 107 int index = entryName.lastIndexOf("\\"); 108 if (index != -1) { 109 File df = new File(outputDirectory + File.separator 110 + entryName.substring(0, index)); 111 df.mkdirs(); 112 } 113 index = entryName.lastIndexOf("/"); 114 if (index != -1) { 115 File df = new File(outputDirectory + File.separator 116 + entryName.substring(0, index)); 117 df.mkdirs(); 118 } 119 File f = new File(outputDirectory + File.separator 120 + zipEntry.getName()); 121 // f.createNewFile(); 122 in = zipFile.getInputStream(zipEntry); 123 out = new FileOutputStream(f); 124 int c; 125 byte[] by = new byte[1024]; 126 while ((c = in.read(by)) != -1) { 127 out.write(by, 0, c); 128 } 129 out.flush(); 130 } 131 } catch (IOException ex) { 132 ex.printStackTrace(); 133 throw new IOException("解压失败:" + ex.toString()); 134 } finally { 135 if (in != null) { 136 try { 137 in.close(); 138 } catch (IOException ex) { 139 } 140 } 141 if (out != null) { 142 try { 143 out.close(); 144 } catch (IOException ex) { 145 } 146 } 147 } 148 } 149 } catch (IOException ex) { 150 ex.printStackTrace(); 151 throw new IOException("解压失败:" + ex.toString()); 152 } finally { 153 if (zipFile != null) { 154 try { 155 zipFile.close(); 156 } catch (IOException ex) { 157 } 158 } 159 } 160 } 161 }
测试下来比MAC上面的压缩软件效果还要好一点,压得更小。代码非常清楚,两个函数,直接调用即可,下面是实例:
1 import java.io.IOException; 2 3 public class Test { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 FileZip zip = new FileZip(); 8 try { 9 zip.zip("/Users/mac/Desktop/test", "/Users/mac/Desktop/test1.zip"); 10 zip.unzip("/Users/mac/Desktop/test1.zip", "/Users/mac/Desktop/testun"); 11 } catch (IOException e) { 12 // TODO Auto-generated catch block 13 e.printStackTrace(); 14 } 15 } 16 }
会生成test1.zip压缩包和testun解压缩软件。十分方便!
(zz:http://blog.sina.com.cn/s/blog_600ff0750100x61j.html)(例子是自己的)