摘要:本文将带你了解java入门之文件解压,希望本文对大家学JAVA有所帮助。
前面介绍了几种压缩方式,这里介绍一下文件解压,使用的是java自带的方式,代码如下:
测试类:
[java] view plain copy 1. package com.home; 2. 3. import java.io.IOException; 4. 5. public class Test { 6. 7. public static void main(String[] args) { 8. try { 9. UnzipUtil.unZipFiles("D:\\test\\test.zip", "D:\\test"); 10. } catch (IOException e) { 11. e.printStackTrace(); 12. } 13. } 14. 15. } 解压工具类: [java] view plain copy 1. package com.home; 2. 3. import java.io.File; 4. import java.io.FileOutputStream; 5. import java.io.IOException; 6. import java.io.InputStream; 7. import java.io.OutputStream; 8. import java.util.Enumeration; 9. import java.util.zip.ZipEntry; 10. import java.util.zip.ZipFile; 11. 12. public class UnzipUtil { 13. /** 14. * 解压文件到指定目录 15. * 16. * @param zipPath 17. * 源zip文件全路径 18. * @param descDir 19. * 输出目录 20. * @throws IOException 21. */ 22. @SuppressWarnings("rawtypes") 23. public static void unZipFiles(String zipPath, String descDir) 24. throws IOException { 25. File pathFile = new File(descDir); 26. if (!pathFile.exists()) { 27. pathFile.mkdirs(); 28. } 29. ZipFile zip = new ZipFile(new File(zipPath)); 30. for (Enumeration entries = zip.entries(); entries.hasMoreElements();) { 31. ZipEntry entry = (ZipEntry) entries.nextElement(); 32. String zipEntryName = entry.getName(); 33. InputStream is = zip.getInputStream(entry); 34. String outPath = (descDir + zipEntryName).replaceAll("\\*", "/"); 35. File file = new File(outPath.substring(0, outPath.lastIndexOf('/'))); 36. if (!file.exists()) { 37. file.mkdirs(); 38. } 39. if (new File(outPath).isDirectory()) {// 如果是全路径是目录,不需要解压 40. continue; 41. } 42. OutputStream os = new FileOutputStream(outPath); 43. byte[] buffer = new byte[1024]; 44. int len; 45. while ((len = is.read(buffer)) > 0) { 46. os.write(buffer, 0, len); 47. } 48. is.close(); 49. os.close(); 50. } 51. } 52. }
以上就介绍了JAVA的相关知识,希望对JAVA有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言JAVA频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号