java 解压缩Zip文件 ziputil
package com.lanyuan.assembly.util;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
/**
* 解压Zip文件工具类
* @author zhangyongbo
*
*/
public class ZipUtil
{
private static final int buffer = 2048;
/**
* 解压Zip文件
* @param path 文件目录
*/
public static void unZip(String path)
{
int count = -1;
String savepath = "";
File file = null;
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
savepath = path.substring(0, path.lastIndexOf(".")) + File.separator; //保存解压文件目录
new File(savepath).mkdir(); //创建保存目录
ZipFile zipFile = null;
try
{
zipFile = new ZipFile(path,"gbk"); //解决中文乱码问题
Enumeration<?> entries = zipFile.getEntries();
while(entries.hasMoreElements())
{
byte buf[] = new byte[buffer];
ZipEntry entry = (ZipEntry)entries.nextElement();
String filename = entry.getName();
boolean ismkdir = false;
if(filename.lastIndexOf("/") != -1){ //检查此文件是否带有文件夹
ismkdir = true;
}
filename = savepath + filename;
if(entry.isDirectory()){ //如果是文件夹先创建
file = new File(filename);
file.mkdirs();
continue;
}
file = new File(filename);
if(!file.exists()){ //如果是目录先创建
if(ismkdir){
new File(filename.substring(0, filename.lastIndexOf("/"))).mkdirs(); //目录先创建
}
}
file.createNewFile(); //创建文件
is = zipFile.getInputStream(entry);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, buffer);
while((count = is.read(buf)) > -1)
{
bos.write(buf, 0, count);
}
bos.flush();
bos.close();
fos.close();
is.close();
}
zipFile.close();
}catch(IOException ioe){
ioe.printStackTrace();
}finally{
try{
if(bos != null){
bos.close();
}
if(fos != null) {
fos.close();
}
if(is != null){
is.close();
}
if(zipFile != null){
zipFile.close();
}
}catch(Exception e) {
e.printStackTrace();
}
}
}
/*public static void main(String[] args)
{
unZip("F:\\110000002.zip");
String f = "F:\\110000002";
File file = new File(f);
String[] test=file.list();
for(int i=0;i<test.length;i++){
System.out.println(test[i]);
}
System.out.println("------------------");
String fileName = "";
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
System.out.println("文 件:"+tempList[i]);
fileName = tempList[i].getName();
System.out.println("文件名:"+fileName);
}
if (tempList[i].isDirectory()) {
System.out.println("文件夹:"+tempList[i]);
}
}
} */
}
java 解压缩Zip文件 ziputil的更多相关文章
- 利用Java进行zip文件压缩与解压缩
摘自: https://www.cnblogs.com/alphajuns/p/12442315.html 工具类: package com.alphajuns.util; import java.i ...
- Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php
Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php 1. Jdk zip 跟apache ant zip 1 2. Apache Ant包进行ZIP文件压缩,upzip ...
- java 生成zip文件并导出
总结一下,关于Java下载zip文件并导出的方法,浏览器导出. String downloadName = "下载文件名称.zip"; downloadName = Browser ...
- java对 zip文件的压缩和解压(ant解决中文乱码)
说明: 1.对于压缩的文件,当文件名称是中文时,若使用JDK API中自带的类(java.util.zip.ZipEntry; java.util.zip.ZipOutputStream;)进行压缩, ...
- java压缩zip文件中文乱码问题(转——作者:riching)
本人遇到了同样的问题,用了以下方案,奇迹般的解决了.我很纳闷为什么,经理说:好读书,不求甚解,不要问为什么... 用java来打包文件生成压缩文件,有两个地方会出现乱码 1.内容的中文乱码问题,这个问 ...
- java解压缩zip
依赖的包: <!-- https://mvnrepository.com/artifact/org.apache.ant/ant --> <dependency> <gr ...
- java压缩zip文件中文乱码问题
用java来打包文件生成压缩文件,有两个地方会出现乱码 1.内容的中文乱码问题,这个问题网上很多人给出了解决方法,两种:修改sun的源码:使用开源的类库org.apache.tools.zip.Zip ...
- java 读取Zip文件进行写入
直接读取ZIp文件读取写入到别的文件中. package jp.co.misumi.mdm.batch; import java.io.BufferedReader; import java.io.F ...
- Java处理ZIP文件的解决方案——Zip4J(不解压直接通过InputStream形式读取其中的文件,解决中文乱码)
一.JDK内置操作Zip文件其实,在JDK中已经存在操作ZIP的工具类:ZipInputStream. 基本使用: public static Map<String, String> re ...
随机推荐
- Oracle utl_file_dir参数详解
1 UTL_FILE_DIR参数定义 UTL_FILE_DIR是Oracle中的一个"静态参数",可以设置一个或多个路径.用于在PL/SQL中进行文件I/O操作(可以用utl_fi ...
- Scrapy安装指南(windows)
windows开发,难免遇到很多坑,比一般开发是艰苦得多.先不吐槽windows,我们直接看这个scrapy怎么安装. 首先,要有一份文档,比如我用这个: http://scrapy-chs.read ...
- 基于WebImage的图片上传工具类
支持缩略图和水印. using System; using System.IO; using System.Linq; using System.Web; using System.Web.Helpe ...
- 「BZOJ1924」「SDOI2010」 所驼门王的宝藏 tarjan + dp(DAG 最长路)
「BZOJ1924」[SDOI2010] 所驼门王的宝藏 tarjan + dp(DAG 最长路) -------------------------------------------------- ...
- 客户端与服务器cookie
认识cookie 第一部分: 概要 cookie是一种早期使用的客户端存储机制(现在仍在广泛使用),cookie数据会在Web浏览器和Web服务器之间传输, 因为早期cookie是针对服务器脚本设计的 ...
- [转]JQuery控制div外点击隐藏,div内点击不会隐藏
一直弄清楚这个效果如何实现,看了这篇博客的几行代码原来如此简单,就是利用了事件冒泡而已. 比如有个div其id为body,实现在div外点击隐藏,div内点击不隐藏,采用jQuery实现如下: $(& ...
- 016-hibernateutils模板
package ${enclosing_package}; import org.hibernate.HibernateException; import org.hibernate.Session; ...
- unity5.5 5.6 使用c#6.0或7.0
http://blog.csdn.net/davied9/article/details/77281393
- 【Unity】工具类系列教程—— 代码自动化生成!
转载自:https://zhuanlan.zhihu.com/p/30716595?utm_medium=social&utm_source=qq [为什么要做自动化工具] 工具类的创建是为了 ...
- 简单的一个php验证登陆代码
<?php/** */ if ( !isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || $_SERVER ...