1. <span style="font-size:18px;">/**
  2. * lsz
  3. */
  4. public final class ZipUtil {
  5. /**
  6. * 解压zip文件
  7. * @param unZipfile
  8. * @param destFile
  9. */
  10. public static void unZip(String unZipfile, String destFile) {
  11. FileOutputStream fileOut;
  12. File file;
  13. InputStream inputStream;
  14. byte[]   buf = new byte[1024*4];
  15. try {
  16. //生成一个zip的文件
  17. ZipFile   zipFile = new ZipFile(unZipfile, "GBK");
  18. //遍历zipFile中所有的实体,并把他们解压出来
  19. for (@SuppressWarnings("unchecked")
  20. Enumeration<ZipEntry> entries = zipFile.getEntries(); entries
  21. .hasMoreElements();) {
  22. ZipEntry entry =  entries.nextElement();
  23. //生成他们解压后的一个文件
  24. file = new File(destFile+File.separator+entry.getName());
  25. if (entry.isDirectory()) {
  26. file.mkdirs();
  27. } else {
  28. // 如果指定文件的目录不存在,则创建之.
  29. File parent = file.getParentFile();
  30. if (!parent.exists()) {
  31. parent.mkdirs();
  32. }
  33. //获取出该压缩实体的输入流
  34. inputStream = zipFile.getInputStream(entry);
  35. fileOut = new FileOutputStream(file);
  36. int length = 0;
  37. //将实体写到本地文件中去
  38. while ((length = inputStream.read(buf)) > 0) {
  39. fileOut.write(buf, 0, length);
  40. }
  41. fileOut.close();
  42. inputStream.close();
  43. }
  44. }
  45. zipFile.close();
  46. //解压完后将原压缩文件删除
  47. File zipfile = new File(unZipfile);
  48. if(zipfile.exists()){
  49. zipfile.delete();
  50. }
  51. } catch (IOException ioe) {
  52. ioe.printStackTrace();
  53. }
  54. }
  55. /**
  56. * 一个文件夹压缩
  57. * 压缩文件夹
  58. * @param filepath
  59. * @param savepath
  60. * @throws Exception
  61. */
  62. public static void toZip(String filepath,String savepath) throws Exception{
  63. File file = new File(filepath);
  64. if(file.exists()){
  65. //判断导出路径是否为空,如果为空,则将压缩包生成到当前路径下
  66. if(StringUtils.isBlank(savepath)){
  67. savepath = filepath+".zip";
  68. }else{
  69. savepath = savepath+".zip";
  70. }
  71. ZipOutputStream outPut = new ZipOutputStream(new FileOutputStream(new File(savepath)));
  72. outPut.setEncoding("GBK");//设置编码
  73. createZip(outPut,file.listFiles(),null);
  74. outPut.flush();
  75. outPut.close();
  76. }else{
  77. //not found
  78. throw new RuntimeException("Err :not found file exception:"+filepath);
  79. }
  80. }
  81. private static void createZip(org.apache.tools.zip.ZipOutputStream outPut,File[] listFiles,String fuPath) throws Exception {
  82. for(File f : listFiles){
  83. String name = fuPath==null?f.getName():fuPath+"/"+f.getName();;
  84. if(f.isDirectory()){
  85. outPut.putNextEntry(new ZipEntry(name+"/"));
  86. createZip(outPut,f.listFiles(),name);
  87. }else{
  88. outPut.putNextEntry(new ZipEntry(name));
  89. InputStream is = new FileInputStream(f);
  90. byte[] bys = new byte[1024];
  91. int len = 0;
  92. while((len = is.read(bys))!=-1)
  93. outPut.write(bys, 0, len);
  94. is.close();
  95. outPut.flush();
  96. }
  97. }
  98. }
  99. /*
  100. * 复制文件 只能使复制文件,不能复制文件夹
  101. */
  102. public static void fileChannelCopy(File fromfile, File tofile) {
  103. FileInputStream fi = null;
  104. FileOutputStream fo = null;
  105. FileChannel in = null;
  106. FileChannel out = null;
  107. try {
  108. fi = new FileInputStream(fromfile);
  109. fo = new FileOutputStream(tofile);
  110. in = fi.getChannel();//得到对应的文件通道
  111. out = fo.getChannel();//得到对应的文件通道
  112. in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道
  113. } catch (IOException e) {
  114. e.printStackTrace();
  115. } finally {
  116. try {
  117. fi.close();
  118. in.close();
  119. fo.close();
  120. out.close();
  121. } catch (IOException e) {
  122. e.printStackTrace();
  123. }
  124. }
  125. }
  126. }</span>

zip文件解压或压缩的更多相关文章

  1. c++builder ZIP文件解压与压缩(ZLIB DLL调用)(转载 )

    转载:http://blog.csdn.net/goodai007/article/details/7414512 头文件:ZipAndFile.h //----------------------- ...

  2. ZIP文件解压

    public class DZip { /// <summary> /// 压缩为ZIP文件 /// </summary> public void Zip(string dir ...

  3. linux下压缩成zip文件解压zip文件

    linux  zip命令的基本用法是: zip [参数] [打包后的文件名] [打包的目录路径] linux  zip命令参数列表: -a     将文件转成ASCII模式 -F     尝试修复损坏 ...

  4. Linux:文件解压与压缩

    文件打包与压缩 常见压缩文件格式: |文件后缀名 |说明| |.zip |zip程序打包压缩的文件| |.rar |rar程序压缩的文件| |.7z |7zip程序压缩的文件| |.tar |tar程 ...

  5. asp.net实现文件解压和压缩

    C#解压RAR压缩文件(--转载--测试通过) using System; using System.Collections.Generic; using System.Text; using Sys ...

  6. Linux 下 zip 文件解压乱码解决方案,ubuntu16.10亲测可用

    文章来源: https://www.zhihu.com/question/20523036 今天邮件中收到了一个压缩文件,解压后却是乱码,从网上也找了几个方法,目前这个方法还是比较可靠的,如下所示: ...

  7. Python实现加密的ZIP文件解压(密码已知)

    博主在上篇博文介绍了<Python实现加密的RAR文件解压(密码已知)>后,又尝试了ZIP文件的解压方法,下面开始分享. 当ZIP文件的压缩密码已知时,可以通过调用zipfile库进行解压 ...

  8. linux .tar.xz 文件解压和压缩

    场景:centos7.0下文件格式为xxx.tar.xz,解压和压缩命令: 压缩 tar -Jcf linux-3.10.0-123.13.1.el7.tar.xz(文件名) linux-3.10.0 ...

  9. Liunx文件解压与压缩

    文件压缩和解压缩 常见压缩格式如下 .zip .gz .bz2 .tar.gz .tar.gz2 .zip压缩 zip 压缩文件名 源文件 压缩文件 zip -r(递归) 压缩文件名 源目录 压缩目录 ...

随机推荐

  1. 如何在PowerDesigner将PDM导出生成WORD文档或者html文件

    a)         使用PowerDesigner打开pdm文件 b)         点击Report Temlates 制作模板 点击PowerDesigner菜单栏“Report” -> ...

  2. ActiveMq+zookeeper+levelDB集群整合配置

    ActiveMq+zookeeper+levelDB集群整合配置 环境:linux系统,jdk1.7  三台linux系统电脑.我这里使用一台window,分别远程3台linux电脑.三台电脑的ip分 ...

  3. 2015年1月最新中国行政区划县及以上代码mysql数据库

    中华人民共和国国家统计局>> 行政区划代码>>mysql数据格式 截图如下 行政区划mysql数据库文件下载:nation.zip 转载:http://www.sdhack.c ...

  4. Spark Streaming揭秘 Day3-运行基石(JobScheduler)大揭秘

    Spark Streaming揭秘 Day3 运行基石(JobScheduler)大揭秘 引子 作为一个非常强大框架,Spark Streaming兼具了流处理和批处理的特点.还记得第一天的谜团么,众 ...

  5. Oracle用户进程跟踪

    用户进程跟踪 分为 基于会话级别跟踪和 实例级别跟踪: 会话级别跟踪又包括 当前会话跟踪和 非当前会话跟踪 跟踪文件位置由user_dump_dest设定,大小由max_dump_file_size ...

  6. WPF简单的口算案例

    前几天在博客园,看到有博友利用Winform做了一个口算案例,于是我想把它移植在WPF程序中.Winform程序:http://www.cnblogs.com/ImYZF/p/3345452.html ...

  7. 2014年辛星完全解读Javascript第六节 对象

    随着面向对象的普及,现在很多语言都在支持面向对象,Javascript也不例外,所谓对象,就是拥有属性和方法的数据.这里的属性其实就是变量,这里的方法,其实就是函数.但是Javascript的面向对象 ...

  8. 【学习总结】【多线程】 多线程概要 & GDC & NSOperation

    基本需要知道的 :  进程 :  简单点来说就是,操作系统中正在运行的一个应用程序,每个进程之间是独立的,每个进程均运行在受保护的内存空间内 线程 :  一个进程(进程)想执行任务,必须有线程(所以, ...

  9. 南京邮电大学CTF隐写术部分Writeup

    女神 听说这是女神的私房照,里面藏着flag哦 http://115.28.150.176/misc1.jpg 这个链接居然打不开,摔!万念俱灰!主办方可否给力点! P.S.为了方便日后学习,暂时列下 ...

  10. 自定义Angular指令与jQuery实现的Bootstrap风格数据双向绑定的单选&多选下拉框

    先说点闲话,熟悉Angular的猿们会喜欢这个插件的. 00.本末倒置 不得不承认我是一个喜欢本末倒置的人,学生时代就喜欢先把晚交的作业先做,留着马上就要交的作业不做,然后慢悠悠做完不重要的作业,卧槽 ...