java Zip文件解压缩

java Zip文件解压缩

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

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

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

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

这个项目用maven构建的依赖jar坐标如下文章来源地址https://www.yii666.com/article/758256.html文章地址https://www.yii666.com/article/758256.html网址:yii666.com<文章来源地址:https://www.yii666.com/article/758256.html

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

代码如下:网址:yii666.com

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. java对压缩文件进行加密,winrar和好压 直接输入解密密码来使用

  2. Java ZIP File Example---refernce

  3. java Zip文件解压缩

  4. java zip 压缩文件

  5. Linux为grub菜单加密码

  6. java zip 压缩与解压

  7. 【设计模式】C++单例模式的几种写法——Java自动加载内部类对象,C++怎么破?

    单例模式是最简单的设计模式,就让我像玩简单的游戏一样写下去吧。v1: 简单模式和这个版本有过一面之缘,但不敢苟同。问题:何时析构不明确;最重要的是调用多次getIns函数会产生多个static Singleton指针,指向每次都调用都new出来的实例。v2: 一般模式典型写法问题:仍然

  8. Java ZIP压缩和解压缩文件(解决中文文件名乱码问题)

    Java ZIP压缩和解压缩文件(解决中文文件名乱码问题)学习了:http://www.tuicool.com/articles/V7BBvy引用原文:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信图片_20190322181744_03.jpg

微信扫一扫打赏

请作者喝杯咖啡吧~

支付宝扫一扫领取红包,优惠每天领

二维码1

zhifubaohongbao.png

二维码2

zhifubaohongbao2.png