摘要:本文将带你了解java入门之文件压缩(3),希望本文对大家学JAVA有所帮助。
java压缩文件三
前面介绍的使用第三方的jar进行压缩,下面介绍使用jdk自带的方法进行压缩:
Test: [java] view plain copy 1. package com.home; 2. 3. import java.io.IOException; 4. 5. public class Test { 6. 7. public Test() { 8. try { 9. ZipUtil.doZip("D://test", "D://test1.zip");// 压缩整个文件来 10. ZipUtil.doZip("D://test.doc", "D://test2.zip");// 压缩单个文件 11. } catch (IOException e) { 12. e.printStackTrace(); 13. } 14. } 15. 16. public static void main(String[] args) { 17. new Test(); 18. } 19. 20. } ZipUtil: [java] view plain copy 1. package com.home; 2. 3. import java.io.BufferedInputStream; 4. import java.io.BufferedOutputStream; 5. import java.io.File; 6. import java.io.FileInputStream; 7. import java.io.FileOutputStream; 8. import java.io.IOException; 9. import java.util.zip.ZipEntry; 10. import java.util.zip.ZipOutputStream; 11. 12. public class ZipUtil { 13. /** 14. * 压缩的接口 15. * 16. * @param src 17. * 源文件全路径 18. * @param des 19. * 目标zip文件全路径 20. * @throws IOException 21. */ 22. public static void doZip(String src, String des) throws IOException { 23. File srcFile = new File(src);// 源文件 24. File zipFile = new File(des);// 目标zip文件 25. ZipOutputStream zos = null; 26. try { 27. zos = new ZipOutputStream(new BufferedOutputStream( 28. new FileOutputStream(zipFile))); 29. String basePath = null; 30. if (srcFile.isDirectory()) { 31. basePath = srcFile.getPath(); 32. } else { 33. basePath = srcFile.getParent(); 34. } 35. zipFile(srcFile, basePath, zos); 36. 37. } finally { 38. if (zos != null) { 39. zos.closeEntry(); 40. zos.close(); 41. } 42. } 43. 44. } 45. 46. /** 47. * 压缩文件 48. * 49. * @param srcFile 50. * @param basePath 51. * @param zos 52. * @throws IOException 53. */ 54. private static void zipFile(File srcFile, String basePath, 55. ZipOutputStream zos) throws IOException { 56. File[] files = null; 57. if (srcFile.isDirectory()) { 58. files = srcFile.listFiles(); 59. } else { 60. files = new File[1]; 61. files[0] = srcFile; 62. } 63. 64. BufferedInputStream bis = null; 65. String pathName; 66. byte[] buf = new byte[1024]; 67. int len = 0; 68. try { 69. for (File file : files) { 70. if (file.isDirectory()) { 71. if (!basePath.endsWith("\\")) { 72. basePath += "\\"; 73. } 74. pathName = file.getPath().substring(basePath.length()) 75. + "\\"; 76. zos.putNextEntry(new ZipEntry(pathName)); 77. zipFile(file, basePath, zos); 78. } else { 79. if (!basePath.endsWith("\\")) { 80. basePath += "\\"; 81. } 82. pathName = file.getPath().substring(basePath.length()); 83. bis = new BufferedInputStream(new FileInputStream(file)); 84. zos.putNextEntry(new ZipEntry(pathName)); 85. while ((len = bis.read(buf)) > 0) { 86. zos.write(buf, 0, len); 87. } 88. } 89. } 90. } finally { 91. if (bis != null) { 92. bis.close(); 93. } 94. } 95. 96. } 97. 98. }
以上就介绍了JAVA的相关知识,希望对JAVA有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言JAVA频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号