java zip文件的解压缩(支持中文文件名)
用的apache的ant包,下载导入即可。由于过程比较简单,直接上代码。
代码可直接复制使用。
如果想在android上使用,记得要在AndroidManifest.xml里添加权限:
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipException; import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile; /**
* 可以处理中文文件名
*/
public class jieya { public static void main(String[] args) { /**
* 解压文件
*/
File zipFile = new File("e:/压缩sdhjk.rar"); String path = "e:/zipfile/"; try { unZipFiles(zipFile, path); } catch (Exception e) { System.out.println("解压异常"); }
} /**
* 解压到指定目录
*
* @param zipPath
* @param descDir
* @author isea533
*/
public static void unZipFiles(String zipPath, String descDir) { try { unZipFiles(new File(zipPath), descDir); } catch (Exception e) { e.printStackTrace();
}
} /**
* 解压文件到指定目录
*
* @param zipFile
* @param descDir
* @author isea533
*/
@SuppressWarnings("rawtypes")
public static void unZipFiles(File zipFile, String descDir) { File pathFile = new File(descDir); if (!pathFile.exists()) { pathFile.mkdirs(); } try { ZipFile zip = new ZipFile(zipFile); for (Enumeration entries = zip.getEntries(); entries
.hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); String zipEntryName = entry.getName(); InputStream in = zip.getInputStream(entry); String outPath = (descDir + zipEntryName)
.replaceAll("\\*", "/"); // 判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0,
outPath.lastIndexOf('/'))); if (!file.exists()) { file.mkdirs(); } // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if (new File(outPath).isDirectory()) { continue; }
// 输出文件路径信息
System.out.println(outPath); OutputStream out = new FileOutputStream(outPath); byte[] buf1 = new byte[1024]; int len; while ((len = in.read(buf1)) > 0) { out.write(buf1, 0, len); } in.close(); out.close(); }
} catch (ZipException e) { e.printStackTrace(); System.out.println("压缩文件打开异常"); } catch (FileNotFoundException e) { e.printStackTrace();
System.out.println("文件没有找到"); } catch (IOException e) { e.printStackTrace(); System.out.println("io异常");
} System.out.println("******************解压完毕********************");
} }
java zip文件的解压缩(支持中文文件名)的更多相关文章
- java Zip文件解压缩
java Zip文件解压缩 为了解压缩zip都折腾两天了,查看了许多谷歌.百度来的code, 真实无语了,绝大多数是不能用的.这可能跟我的开发环境有关吧. 我用的是Ubuntu14.04,eclips ...
- Servlet 下载文件及支持中文文件名
Sevlet 下载pdf文件 支持中文文件名 package html2pdf.controller; import java.io.File; import java.io.FileInputStr ...
- FullSync不支持中文文件名
FullSync,能实现多种方式.协议的目录同步软件,但不支持中文文件名.
- tomcat支持中文文件名下载
http://blog.csdn.net/wnczwl369/article/details/7483806 Tomcat 是Java开发者使用得较多的一个Web服务器,因为它占用资源小,运行速度快等 ...
- springmvc上传zip文件并解压缩代码示例
<input type="file" id="file" name="file"> spring中的配置: <!-- ...
- 让Tomcat支持中文文件名
--参考链接:http://blog.chinaunix.net/uid-26284395-id-3044132.html 解决问题的核心在于修改Tomcat的配置,在Server.xml文件中添加一 ...
- 使用Java对文件进行解压缩
最近在一个项目中需要对文件进行自动的解压缩,Java提供了这种支持,还是挺好用的. 工具包封装在java.util.zip中. 1.首先是多个文件压缩成一个ZIP文件 思路:用一个ZipOutputS ...
- koala编译scss文件时不支持中文字体的解决方案
第一种方案:在scss文件第一行加上这行代码@charset "utf-8"; 第二种方案: scss文件编译时候使用ruby环境,出现 Syntax error: Invalid ...
- 161012、JAVA读写文件,如何避免中文乱码
1.JAVA读取文件,避免中文乱码. /** * 读取文件内容 * * @param filePathAndName * String 如 c:\\1.txt 绝对路径 * @return boole ...
随机推荐
- iOS App Icon图标 尺寸规范
Commit to AppStore:1024*1024 //for App IconIcon-60@3x.png:180*180 //iPhone 6 Plus (@3x)Icon-60@2x.pn ...
- Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use.
Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use. The ...
- HDU 4089 Activation 概率DP 难度:3
http://acm.hdu.edu.cn/showproblem.php?pid=4089 这道题中一共有两个循环: 1.事件1 如果一直落在Activation failed事件上,那么就会重新继 ...
- POJ 1012 Joseph 推导,暴力,约瑟夫环,打表 难度:2
http://poj.org/problem?id=1012 答案以954ms飘过,不过这道题可以轻松用打表过 思路:如果我们把每个人位于数组中的原始编号记为绝对编号,每次循环过后相对于绝对编号为0的 ...
- 可滑动的ToggleButton(开关)
2013-12-28 17:25:01 网上看到一篇关于可滑动的ToogleButton的文章,有代码,觉得挺好,但是不符合我的要求,因此在他的代码基础上改了一些.(作者看到了勿喷啊,实在找不到原文了 ...
- C++-模板的声明和实现为何要放在头文件中
源: http://blog.csdn.net/lqk1985/archive/2008/10/24/3136364.aspx 如何组织编写模板程序 发表日期: 1/21/2003 12:28:58 ...
- (转)iOS应用架构谈 view层的组织和调用方案
前言 <iOS应用架构谈 开篇>出来之后,很多人来催我赶紧出第二篇.这一篇文章出得相当艰难,因为公司里的破事儿特别多,我自己又有点私事儿,以至于能用来写博客的时间不够充分. 现在好啦,第二 ...
- ie浏览器兼容性快速处理小招
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- [安卓]AndroidManifest.xml文件简介及结构
1.AndroidManifest.xml文件简介: 每个应用程序在它的根目录中都必须要有一个AndroidManifest.xml(名字须精确一致)文件.这个清单把应用程序的基本信息提交给Andro ...
- AJAX笔记
浏览器脚本——AJAX AJAX = 异步的 JavaScript 和 XML(Asynchronous JavaScript and XML). 是一种新的技术,它可以创建更好.更快且交互性更强的 ...