我们经常会使用WinZIP等压缩软件将文件进行压缩以方便传输。在java里面也提供了将文件进行压缩以减少传输时的数据量的类,可以很方便的将文件压缩成ZIP、JAR、GZIP等形式,GZIP主要是在Linux系统下的压缩文件。
下面主要讲的就是ZIP形式的压缩文件,而JAR、GZIP形式的压缩文件也是类似的用法。
ZIP是一种很常见的压缩形式,在java中要实现ZIP的压缩主要用到的是java.util.zip这个包里面的类。主要有ZipFile、ZipOutputStream、ZipInputStream和ZipEntry。ZipOutputStream是用来压缩文件的,ZipInputStream和ZipFile是用来解压缩文件的,在压缩和解压缩的过程中,ZipEntry都会用到。在java的Zip压缩文件中,每一个子文件都是一个ZipEntry对象。
压缩文件:

1. import
2. import
3. import
4. import
5. import
6. import
7. import
8. import
9.
10. public class
11.
12. public static void main(String args[]) throws
13. test1();
14. test2();
15. }
16.
17. public static void test1() throws
18. new ZipOutputStream(new FileOutputStream("D:\\testZip.zip"), Charset.forName("GBK"));
19. //实例化一个名称为ab.txt的ZipEntry对象
20. new ZipEntry("ab.txt");
21. //设置注释
22. "zip测试for单个文件");
23. //把生成的ZipEntry对象加入到压缩文件中,而之后往压缩文件中写入的内容都会放在这个ZipEntry对象里面
24. zos.putNextEntry(entry);
25. new FileInputStream("D:\\ab.txt");
26. int len = 0;
27. while ((len = is.read()) != -1)
28. zos.write(len);
29. is.close();
30. zos.close();
31. }
32.
33. public static void test2() throws
34. new File("D:\\test");
35. new ZipOutputStream(new FileOutputStream("D:\\test.zip"), Charset.forName("GBK"));
36. "多文件处理");
37. "");
38. zos.close();
39. }
40. //压缩文件
41. public static void zipFile(File inFile, ZipOutputStream zos, String dir) throws
42. if
43. File[] files = inFile.listFiles();
44. for
45. "\\"
46. else
47. null;
48. if (!"".equals(dir))
49. "\\"
50. else
51. entryName = inFile.getName();
52. new
53. zos.putNextEntry(entry);
54. new
55. int len = 0;
56. while ((len = is.read()) != -1)
57. zos.write(len);
58. is.close();
59. }
60.
61. }
62.
63. }
解压缩文件:

1. import
2. import
3. import
4. import
5. import
6. import
7. import
8. import
9. import
10. import
11. import
12.
13. public class
14.
15. public static void main(String args[]) throws
16. new File("D:\\test.zip");//压缩文件
17. new ZipFile(file, Charset.forName("GBK"));//实例化ZipFile,并指定其编码格式,防止中文乱码和解压错误,每一个zip压缩文件都可以表示为一个ZipFile
18. //实例化一个Zip压缩文件的ZipInputStream对象,可以利用该类的getNextEntry()方法依次拿到每一个ZipEntry对象
19. new ZipInputStream(new FileInputStream(file), Charset.forName("GBK"));
20.
21. //直接利用ZipFile进行解压
22. unpack(zipFile);
23.
24. //利用ZipFile和ZipInputStream进行解压
25. unpack(zipFile, zipInputStream);
26.
27. }
28.
29. /**
30. * 该方法是通过ZipFile和ZipInputStream共同来解压文件的,通过ZipInputStream可以拿到ZipFile中的每一个
31. * ZipEntry对象,然后通过ZipFile的getInputStream(ZipEntry zipEntry)方法拿到对应的每一个ZipEntry的
32. * 输入流,从而实现文件的解压
33. * @param zipFile
34. * @param zipInputStream
35. * @throws IOException
36. */
37. public static void unpack(ZipFile zipFile, ZipInputStream zipInputStream) throws
38. null;
39. while ((zipEntry = zipInputStream.getNextEntry()) != null) {
40. String fileName = zipEntry.getName();
41. new File("D:\\unpackTest\\"
42. if
43. temp.getParentFile().mkdirs();
44. new
45. //通过ZipFile的getInputStream方法拿到具体的ZipEntry的输入流
46. InputStream is = zipFile.getInputStream(zipEntry);
47. int len = 0;
48. System.out.println(zipEntry.getName());
49. while ((len = is.read()) != -1)
50. os.write(len);
51. os.close();
52. is.close();
53. }
54. zipInputStream.close();
55. }
56.
57. /**
58. * 该方法是直接通过ZipFile的entries()方法拿到包含所有的ZipEntry对象的Enumeration对象,
59. * 接下来的操作和上面的是一样的
60. * @param zipFile
61. * @throws IOException
62. */
63. public static void unpack(ZipFile zipFile) throws
64. Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zipFile.entries();
65. while
66. ZipEntry zipEntry = entries.nextElement();
67. String fileName = zipEntry.getName();
68. new File("D:\\unpackTest2\\"
69. if
70. temp.getParentFile().mkdirs();
71. new
72. //通过ZipFile的getInputStream方法拿到具体的ZipEntry的输入流
73. InputStream is = zipFile.getInputStream(zipEntry);
74. int len = 0;
75. while ((len = is.read()) != -1)
76. os.write(len);
77. os.close();
78. is.close();
79. }
80. }
81.
82. }
















