java Zip文件解压缩

为了解压缩zip都折腾两天了,查看了许多谷歌、百度来的code,

真实无语了,绝大多数是不能用的。这可能跟我的开发环境有关吧。

我用的是Ubuntu14.04,eclipse 用的是STS3.5,jdk81.8.0_20

经过两天的努力检验了无数的code终于让我找到一个还能用的可以解决中文乱码问题。

这个项目用maven构建的依赖jar坐标如下

<!-- 用于zip文件解压缩 -->
<dependency>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
<version>1.7.0</version>
</dependency>

代码如下:

package com.uujava.mbfy.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import org.apache.tools.zip.ZipOutputStream;
/**
* @author k
* @date 2014年10月7日 上午8:04:51
* @Description: TODO(用于压缩和解压缩zip文件)
* 尚须解决bug:
* 这里采用硬编码这定文件编码,是否有办法读取压缩文件时判断起内部文件名编码。
*/
public final class ZipUtils { public static void main(String[] args) throws Exception {
// ZipFile("/home/k/Documents/testzip/宽屏透明html5产品展示.zip", "/home/k/Documents/testzip/index.html");
unZipFile("/home/k/Documents/testzip/宽屏透明html5产品展示.zip",
"/home/k/Documents/testzip/zip");
}
public static void zip(ZipOutputStream out, File f, String base,
boolean first) throws Exception {
if (first) {
if (f.isDirectory()) {
out.putNextEntry(new org.apache.tools.zip.ZipEntry("/"));
base = base + f.getName();
first = false;
} else
base = f.getName();
}
if (f.isDirectory()) {
File[] fl = f.listFiles();
base = base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName(), first);
}
} else {
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
System.out.println(base);
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
}
} @SuppressWarnings("unchecked")
public static void unZipFileByOpache(org.apache.tools.zip.ZipFile zipFile,
String unZipRoot) throws Exception, IOException {
java.util.Enumeration e = zipFile.getEntries();
System.out.println(zipFile.getEncoding());
org.apache.tools.zip.ZipEntry zipEntry;
while (e.hasMoreElements()) {
zipEntry = (org.apache.tools.zip.ZipEntry) e.nextElement();
InputStream fis = zipFile.getInputStream(zipEntry);
if (zipEntry.isDirectory()) {
} else {
File file = new File(unZipRoot + File.separator
+ zipEntry.getName());
File parentFile = file.getParentFile();
parentFile.mkdirs();
FileOutputStream fos = new FileOutputStream(file);
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b, 0, b.length)) != -1) {
fos.write(b, 0, len);
}
fos.close();
fis.close();
}
}
} public static void ZipFile(String zipFileName, String inputFileName)
throws Exception {
org.apache.tools.zip.ZipOutputStream out = new org.apache.tools.zip.ZipOutputStream(
new FileOutputStream(zipFileName));
out.setEncoding("gbk");// 设置的和文件名字格式一样或开发环境编码设置一样的话就能正常显示了
File inputFile = new File(inputFileName);
zip(out, inputFile, "", true);
System.out.println("zip done");
out.close();
} public static void unZipFile(String unZipFileName, String unZipPath)
throws Exception {
org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile(
unZipFileName, "gbk");
unZipFileByOpache(zipFile, unZipPath);
System.out.println("unZip Ok");
} }

java Zip文件解压缩的更多相关文章

  1. Android Zip文件解压缩代码

    2011-04-01 17:58:52|  分类: Android |举报 |字号 订阅   在Android平台中如何实现Zip文件的解压 缩功能呢? 因为Android内部已经集成了zlib库,对 ...

  2. Android中用Java代码实现zip文件解压缩

    如果需要下载的文件有很多是中文名的,解压时有中文名的文件出现乱码,试了很多方法不能解决问题.据说有一个Java插件包,用这个插件包可以解决中文名乱码的问题,但不知解压的文件是否要用它提供的类压缩后的文 ...

  3. java zip文件的解压缩(支持中文文件名)

    用的apache的ant包,下载导入即可.由于过程比较简单,直接上代码. 代码可直接复制使用. 如果想在android上使用,记得要在AndroidManifest.xml里添加权限: <use ...

  4. Zip文件压缩(加密||非加密||压缩指定目录||压缩目录下的单个文件||根据路径压缩||根据流压缩)

    1.写入Excel,并加密压缩.不保存文件 String dcxh = String.format("%03d", keyValue); String folderFileName ...

  5. android压缩解压zip文件

    网上各种方法的收集: 1.上次写了个解压缩功能,但有局限性,比如压缩文件xx.zip 里包括子目录的情况下,执行上次解压缩的功能就不能实现我们想要的效果,于是在网上参考了一下java的解压缩功能.对上 ...

  6. Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php

    Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php 1. Jdk zip 跟apache ant zip 1 2. Apache Ant包进行ZIP文件压缩,upzip ...

  7. 利用Java进行zip文件压缩与解压缩

    摘自: https://www.cnblogs.com/alphajuns/p/12442315.html 工具类: package com.alphajuns.util; import java.i ...

  8. java生成zip压缩文件,解压缩文件

    1.生成zip public static void main(String[] args) { try { // testZip("c:\\temp.txt", "c: ...

  9. java 解压缩Zip文件 ziputil

    package com.lanyuan.assembly.util; import java.io.BufferedOutputStream;import java.io.File;import ja ...

随机推荐

  1. Android4大组件

    http://www.cnblogs.com/bravestarrhu/archive/2012/05/02/2479461.html 快乐阅读: http://www.360doc.com/cont ...

  2. Android中使用开源框架android-image-indicator实现图片轮播部署

    之前的博文中有介绍关于图片轮播的实现方式,分别为(含超链接): 1.<Android中使用ViewFlipper实现屏幕切换> 2.<Android中使用ViewPager实现屏幕页 ...

  3. python操作 redis-list

    #!/usr/bin/python #!coding: utf-8 import redis if __name__=="__main__": try: conn=redis.St ...

  4. 把自定义控件集成到Qt Designer中

    要想在Qt Designer中使用自定义控件,必须要使Qt Designer能够知道我们的自定义控件的存在.有两种方法可以把新自定义控件的信息通知给Qt Designer:“升级(promotion) ...

  5. Centos7网络配置+图形界面设置

    一. 查看网络地址: centos7取消了ifconfig命令,使用ip addr命令查看IP地址 二.配置网络 用VirtualBox安装的CentOS7,安装完成后,发现无法上网,于是到网上查了一 ...

  6. 为什么不能在scrollview中直接添加一个image,然后使animation.begin()??

    http://stackoverflow.com/questions/17267451/animation-cant-begin-in-scrollview-in-windows-phone 以上是我 ...

  7. NODE.JS安装配置

  8. 无限的路_hdu_2073(AC).java

    无限的路 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  9. H264源码分析(二)

    原文出自http://blog.csdn.net/xfding/article/details/5476763(转载收集) (四)图像参数集语义 pic_parameter_set_rbsp( ) { ...

  10. C#基础:事件(二) 【转】

    上篇文章介绍了C#中事件的基本实现方式,在本文中,将对最常见的事件委托EventHandler和EventHandler<T>做介绍. 事实上,在前面文章的介绍中,已经涉及到了EventH ...