qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

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");
}
}

posted on 2014-10-09 10:28 顺其自然EVO 阅读(175) 评论(0)  编辑  收藏 所属分类: 测试学习专栏


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年10月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜