1 public class Test { 2 3 public static void main(String[] args) throws IOException { 4 //GZIP压缩与解压缩 5 6 /* FileInputStream fis = new FileInputStream("E:\\1.txt"); 7 GZIPOutputStream gos = new GZIPOutputStream(new FileOutputStream("E:\\1.gz")); 8 9 System.out.println("1.txt压缩成1.gz"); 10 int len; 11 while ((len = fis.read()) != -1) { 12 gos.write(len); 13 } 14 fis.close(); 15 gos.close();*/ 16 17 18 19 20 /* GZIPInputStream gis = new GZIPInputStream(new FileInputStream("E:\\1.gz")); 21 FileOutputStream fos = new FileOutputStream("E:\\1.txt"); 22 23 System.out.println("1.gz解压缩成1.txt"); 24 int len; 25 while ((len = gis.read()) !=-1) { 26 fos.write(len); 27 } 28 gis.close(); 29 fos.close();*/ 30 31 32 33 34 35 //ZIP压缩与解压缩 36 37 /* System.out.println("ZIP压缩"); 38 ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("E:\\1.zip")); 39 String a[] = {"E:/1.txt","E:/2.txt"}; 40 int len; 41 for (int i = 0; i < a.length; i++) { 42 System.out.println("压缩文件"+a[i]); 43 FileInputStream fis = new FileInputStream(a[i]); 44 zos.putNextEntry(new ZipEntry(a[i]));//写入一个ZipEntry入口 45 46 while ((len = fis.read()) != -1) { 47 zos.write(len); 48 } 49 fis.close(); 50 } 51 zos.close(); 52 System.out.println("压缩完毕!");*/ 53 54 55 System.out.println("ZIP解压缩"); 56 ZipInputStream zis = new ZipInputStream(new FileInputStream("E:/1.zip")); 57 ZipEntry entry; 58 int c; 59 while ((entry = zis.getNextEntry()) != null) { 60 System.out.println("文件:"+entry.getName()); 61 FileOutputStream fos = new FileOutputStream(entry.getName()); 62 63 while ((c = zis.read()) != -1) { 64 fos.write(c); 65 } 66 fos.close(); 67 } 68 zis.close(); 69 System.out.println("解压缩完毕!"); 70 } 71 72 }
标个链接:https://www.cnblogs.com/stevenhqq/archive/2011/05/19/2050771.html