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.io.OutputStream; 7. import java.util.zip.ZipEntry; 8. import java.util.zip.ZipFile; 9. import java.util.zip.ZipInputStream; 10. 11. 12. 13. 14. public class UnZipDemo 15. { 16. 17. public static void main(String [] args) throws IOException 18. { 19. File file = new File("F:"+File.separator+"SWT_Designer.zip");//要解压的文件 20. 21. File outFile = null; 22. ZipFile zipFile = new ZipFile(file);//创建压缩文件对象 23. ZipInputStream zi = null; 24. 25. InputStream is = null; 26. OutputStream os = null; 27. ZipEntry entry = null ; // 每一个压缩实体 28. 29. zi = new ZipInputStream(new FileInputStream(file));// 实例化ZIpInputStream 30. 31. String dir = file.getName().substring(0, file.getName().length()-3);//解压后的文件夹名和压缩文件名(不加拓展名)相同 32. 33. while((entry=zi.getNextEntry())!=null)//得到一个压缩实体 34. { 35. System.out.println("解压缩" + entry.getName() + "文件。") ; 36. outFile = new File("F:"+ File.separator + dir+ File.separator + entry.getName()) ; // 定义输出的文件路径 37. 38. if(!outFile.getParentFile().exists()) 39. { 40. outFile.getParentFile().mkdir(); 41. } 42. 43. if(!outFile.exists()) 44. { 45. if(outFile.isDirectory()||entry.isDirectory()) 46. { 47. outFile.mkdir(); 48. }else{ 49. outFile.createNewFile(); 50. } 51. } 52. 53. 54. 55. 56. 57. is = zipFile.getInputStream(entry) ; // 得到每一个实体的输入流 58. 59. 60. if(outFile.isDirectory()) 61. { 62. continue; 63. }else 64. { 65. os = new FileOutputStream(outFile); 66. int len = 0; 67. byte [] buf = new byte[1024*10]; 68. while((len = is.read(buf))!=-1) 69. { 70. os.write(buf, 0, len); 71. } 72. is.close(); 73. os.close(); 74. entry.clone(); 75. 76. 77. 78. } 79. 80. 81. 82. } 83. zi.close(); 84. 85. } 86. }