JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包
package zip; import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.util.Enumeration;
import java.util.zip.Deflater; import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream; public class Zip { public static void main(String[] args){
// new Zip().doZip("d:/报表程序","e:/报表程序.rar");
new Zip().unZip("e:/报表程序.rar","d:/报表程序");
} private ZipFile zipFile;
private ZipOutputStream zipOut; //压缩Zip
private int bufSize; //size of bytes
private byte[] buf; public Zip(){
//要构造函数中去初始化我们的缓冲区
this.bufSize = 1024*4;
this.buf = new byte[this.bufSize];
} /**
* 对传入的目录或者是文件进行压缩
* @param srcFile 需要 压缩的目录或者文件
* @param destFile 压缩文件的路径
*/
public void doZip(String srcFile, String destFile) {// zipDirectoryPath:需要压缩的文件夹名
File zipFile = new File(srcFile);
try {
//生成ZipOutputStream,会把压缩的内容全都通过这个输出流输出,最后写到压缩文件中去
this.zipOut = new ZipOutputStream(new BufferedOutputStream(
new FileOutputStream(destFile)));
//设置压缩的注释
zipOut.setComment("comment");
//设置压缩的编码,如果要压缩的路径中有中文,就用下面的编码
zipOut.setEncoding("GBK");
//启用压缩
zipOut.setMethod(ZipOutputStream.DEFLATED); //压缩级别为最强压缩,但时间要花得多一点
zipOut.setLevel(Deflater.BEST_COMPRESSION); handleFile(zipFile, this.zipOut,"");
//处理完成后关闭我们的输出流
this.zipOut.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
} /**
* 由doZip调用,递归完成目录文件读取
* @param zipFile
* @param zipOut
* @param dirName 这个主要是用来记录压缩文件的一个目录层次结构的
* @throws IOException
*/
private void handleFile(File zipFile, ZipOutputStream zipOut,String dirName) throws IOException {
System.out.println("遍历文件:"+zipFile.getName());
//如果是一个目录,则遍历
if(zipFile.isDirectory()){
File[] files = zipFile.listFiles(); if (files.length == 0) {// 如果目录为空,则单独创建之.
//只是放入了空目录的名字
this.zipOut.putNextEntry(new ZipEntry(dirName+zipFile.getName()+File.separator));
this.zipOut.closeEntry();
} else {// 如果目录不为空,则进入递归,处理下一级文件
for (File file : files) {
// 进入递归,处理下一级的文件
handleFile(file, zipOut, dirName+zipFile.getName()+File.separator);
}
}
}else{//如果是文件,则直接压缩
FileInputStream fileIn = new FileInputStream(zipFile);
//放入一个ZipEntry
this.zipOut.putNextEntry(new ZipEntry(dirName+zipFile.getName()));
int length = 0;
//放入压缩文件的流
while ((length = fileIn.read(this.buf)) > 0) {
this.zipOut.write(this.buf, 0, length);
}
//关闭ZipEntry,完成一个文件的压缩
fileIn.close();
this.zipOut.closeEntry();
}
} /**
* 解压指定zip文件
* @param unZipfile 压缩文件的路径
* @param destFile 解压到的目录
*/
public void unZip(String unZipfile, String destFile) {// unZipfileName需要解压的zip文件名
FileOutputStream fileOut;
File file;
InputStream inputStream; try {
//生成一个zip的文件
this.zipFile = new ZipFile(unZipfile);
//遍历zipFile中所有的实体,并把他们解压出来
for (@SuppressWarnings("unchecked")
Enumeration<ZipEntry> entries = this.zipFile.getEntries(); entries
.hasMoreElements();) {
ZipEntry entry = entries.nextElement();
//生成他们解压后的一个文件
file = new File(destFile+File.separator+entry.getName()); if (entry.isDirectory()) {
file.mkdirs();
} else {
// 如果指定文件的目录不存在,则创建之.
File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
//获取出该压缩实体的输入流
inputStream = zipFile.getInputStream(entry); fileOut = new FileOutputStream(file);
int length = 0;
//将实体写到本地文件中去
while ((length = inputStream.read(this.buf)) > 0) {
fileOut.write(this.buf, 0, length);
}
fileOut.close();
inputStream.close();
}
}
this.zipFile.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包的更多相关文章
- 原生java 压缩解压zip文件
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)
本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...
- (转载)C#压缩解压zip 文件
转载之: C#压缩解压zip 文件 - 大气象 - 博客园http://www.cnblogs.com/greatverve/archive/2011/12/27/csharp-zip.html C# ...
- java实现解压zip文件,(亲测可用)!!!!!!
项目结构: Util.java内容: package com.cfets.demo; import java.io.File; import java.io.FileOutputStream; imp ...
- 解压zip文件中文文件名乱码问题
主要原因是,在windows下压缩文件时,是以系统的默认编码(gbk,gb18030)来压缩,zip文件并没有声明编码的格式,因此,linux下解压缩时,也会使用系统默认的格式(utf-8)解压缩,编 ...
- java 提取(解压)zip文件中特定后缀的文件并保存到指定目录
内容简介 本文主要介绍使用ZipFile来提取zip压缩文件中特定后缀(如:png,jpg)的文件并保存到指定目录下. 导入包:import java.util.zip.ZipFile; 如需添加对r ...
- Java 压缩/ 解压 .Z 文件
1.问题描述 公司项目有需要用 JAVA 解压 .z文件. .z 是 unix 系统常见的压缩文件. 2.源码 import com.chilkatsoft.CkUnixCompress; impor ...
- C#压缩解压zip 文件
/// <summary> /// Zip 压缩文件 /// </summary> public class Zip { public Zip() { } #region 加压 ...
- Ubuntu下解决解压zip文件中文文件名乱码问题
在Ubuntu下解压Windows下压缩的zip文件时,会出现解压出的带中文文件名的文件名乱码,这是因为Ubuntu和Windows默认的编码不同,Ubuntu下默认的编码是UTF-8,而Window ...
随机推荐
- centos jdk 下载
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com% ...
- awk调用shell命令的两种方法:system与print
from:http://www.oklinux.cn/html/developer/shell/20070626/31550.htmlawk中使用的shell命令,有2种方法: 一.使用所以syste ...
- Linux的bash快捷键
Ctrl-A 相当于HOME键,用于将光标定位到本行最前面 Ctrl-E 相当于End键,即将光标移动到本行末尾 Ctrl-B 相当于左箭头键,用于将光标向左移动一格 Ctrl-F 相当于右箭头键,用 ...
- 安装caffe框架所需文件
安装caffe框架所需文件: 1.微软提供的快速卷积神经网络框架caffe-master安装包或者windows提供的caffe-windows安装包. 链接:http://pan.baidu.com ...
- TradingView 自定义指标
TradingView 支持自定义指标,不过是把你要定义的指标写成一个 JS 源文件(customIndex.js),放在图表库 static 文件夹下.自定义指标 JS 源代码模板如下: __cus ...
- POJ2104 K-th Number [整体二分]
题目传送门 K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 69053 Accepted: 24 ...
- Java_正则表达式&时间日期
正则表达式 1.概念 正则表达式(英语:Regular Expression,在代码中常简写为regex). 正则表达式是一个字符串,使用单个字符串来描述.用来定义匹配规则,匹配一系列符合某个句法规则 ...
- JAVAEE——宜立方商城02:服务中间件dubbo、工程改造为基于soa架构、商品列表实现
1. 学习计划 第二天:商品列表功能实现 1.服务中间件dubbo 2.工程改造为基于soa架构 3.商品列表查询功能实现. 2. 将工程改造为SOA架构 2.1. 分析 由于宜立方商城是基于soa的 ...
- 在(Raspberry Pi)树莓派上安装NodeJS
本文主讲如何在树莓派3B上安装node.js 环境描述1. 树莓派安装了`2016-11-25-raspbian-jessie-lite`(PS:在此版本的镜像中,默认禁用了ssh,在烧录好镜像之后, ...
- 下拉框搜索插件chosen
{% load staticfiles %} <!DOCTYPE html> <html lang="en"> <head> <meta ...