java ZipOutputStream压缩文件,ZipInputStream解压缩
java中实现zip的压缩与解压缩。java自带的 能实现的功能比较有限。
本程序功能:实现简单的压缩和解压缩,压缩文件夹下的所有文件(文件过滤的话需要对File进一步细节处理)。
对中文的支持需要使用java7或java8,可以在ZipOutputStream和ZipInputStream中指定Charset参数,详见API中的构造参数。
1.压缩文件或文件夹
public void zip() throws IOException {
File fileSrc = new File("E:\\abc");
File destFile = new File("E:\\abc.zip");
zip(fileSrc,destFile);
}
public void zip(File fileSrc,File dectFile) throws IOException {
ZipOutputStream zipOutputStream = new ZipOutputStream(new CheckedOutputStream(new FileOutputStream(dectFile),new CRC32()));
String name = fileSrc.getName();
zip(zipOutputStream, name,fileSrc);
zipOutputStream.flush();
zipOutputStream.close();
}
zip
private void zip(ZipOutputStream zipOutputStream,String name, File fileSrc) throws IOException {
if (fileSrc.isDirectory()) {
File[] files = fileSrc.listFiles(new FilenameFilter() { //过滤文件
Pattern pattern = Pattern.compile(".+");//所有文件,正则表达式
@Override
public boolean accept(File dir, String name) {
return pattern.matcher(name).matches();
}
});
zipOutputStream.putNextEntry(new ZipEntry(name+"/")); // 建一个文件夹
name = name+"/";
for (File f : files) {
zip(zipOutputStream,name+f.getName(),f);
}
}else {
zipOutputStream.putNextEntry(new ZipEntry(name));
FileInputStream input = new FileInputStream(fileSrc);
byte[] buf = new byte[1024];
int len = -1;
while ((len = input.read(buf)) != -1) {
zipOutputStream.write(buf, 0, len);
}
zipOutputStream.flush();
input.close();
}
}
zip
2.解压缩zip文件
public void unzip() throws IOException {
File zipFile = new File("E:\\java.zip");
String destDir = "E:\\java\\";
unzip(zipFile,destDir);
}
private void unzip(File zipFile, String destDir) throws IOException {
ZipInputStream zipInputStream = new ZipInputStream(new CheckedInputStream(new FileInputStream(zipFile),new CRC32()));
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
System.out.println(zipEntry.getName());
File f = new File(destDir + zipEntry.getName());
if(zipEntry.getName().endsWith("/")){
f.mkdirs();
}else {
// f.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(f);
byte[] buf = new byte[1024];
int len = -1;
while ((len = zipInputStream.read(buf)) != -1) { // 直到读到该条目的结尾
fileOutputStream.write(buf, 0, len);
}
fileOutputStream.flush();
fileOutputStream.close();
}
zipInputStream.closeEntry(); //关闭该条目
}
zipInputStream.close();
}
unzip
java ZipOutputStream压缩文件,ZipInputStream解压缩的更多相关文章
- Java实现压缩文件与解压缩文件
由于工作需要,需要将zip的压缩文件进行解压,经过调查发现,存在两个开源的工具包,一个是Apache的ant工具包,另一个就是Java api自带的工具包:但是Java自带的工具包存在问题:如果压缩或 ...
- java ZIP压缩文件
问题描述: 使用java ZIP压缩文件和目录 问题解决: (1)单个文件压缩 注: 以上是实现单个文件写入压缩包的代码,注意其中主要是在ZipOutStream流对象中创建Z ...
- Java生成压缩文件(zip、rar 格式)
jar坐标: <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</ar ...
- java生成压缩文件
在工作过程中,需要将一个文件夹生成压缩文件,然后提供给用户下载.所以自己写了一个压缩文件的工具类.该工具类支持单个文件和文件夹压缩.放代码: import java.io.BufferedOutput ...
- java zip 压缩文件
zip压缩:ZipOutputStream.ZipFile.ZipInputStream 三个类的作用 一段 java zip 压缩的代码: File dir = new File("C ...
- java打包压缩文件
package com.it.simple.util; import java.io.BufferedOutputStream;import java.io.ByteArrayOutputStream ...
- java zip压缩文件和文件夹
public class FileUtil { /** * 压缩文件-File * @param out zip流 * @param srcFiles 要压缩的文件 * @param path 相对路 ...
- java对压缩文件进行加密,winrar和好压 直接输入解密密码来使用
<!-- https://mvnrepository.com/artifact/net.lingala.zip4j/zip4j --> <dependency> <gro ...
- Java读取压缩文件信息
不解压压缩文件,获取其中包含的文件,通过文件名检查是否包含非法文件.(后续再根据文件头或内容吧) zip: import java.util.zip.ZipEntry;import java.util ...
随机推荐
- HTML5--拖动02-dragstart、drag、dragenter、dragover、dragleave、drop、dragend属性
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 自己赚钱送女友iPhone做惊喜
都说谈恋爱是件费时费力又费钱的事情,你要给女朋友准备各种节日的惊喜,你要给女朋友买她喜欢的裙子,你要请女朋友吃各种美味的食物......但是也别抱怨,一个男人若是连自己女朋友的这点物质要求都满足不了的 ...
- 解决jquery mobile的header和footer在点击屏幕的时候消失的办法
给header和footer添加 data-position="fixed" 和 data-tap-toggle="false"即可,代码如下: <div ...
- CentOS7安装ftp服务器
一.问题的提出 想在windows环境下远程连接CentOS的文件并编辑 二.问题的解决 # 安装vsftp服务[root@localhost ~]# yum -y install ftp vsftp ...
- EF 数据初始化
数据库不存在时重新创建数据库: Database.SetInitializer<testContext>(new DropCreateDatabaseAlways<testConte ...
- PhpStorm设置编码
PhpStorm是一个轻量级且便捷的PHP IDE,其旨在提供用户效率,可深刻理解用户的编码,提供智能代码补全,快速导航以及即时错误检查. 本文为大家讲解的是如何设置phpstorm 编辑器的编码,感 ...
- WCF、Net remoting、Web service概念及区别
Windows通信基础(Windows Communication Foundation,WCF)是基于Windows平台下开发和部署服务的软件开发包(Software Development Kit ...
- Swift语言之类型方法
Swift语言有很多特性,其中之一就是类型方法,相对于其他比较流行的编程语言(C#.Java),在Swift中类型方法最大的特征在于它的可继承性,我们举个例子说明: 俗话说,龙生龙凤生凤老鼠生儿会打洞 ...
- 利用CNN进行人脸年龄预测
很久之前做的东西了,最近做了一个人脸相似度检测,里面用到了这里的一个模型,所以抽个空把人脸年龄检测的思路总结一下. 与其他CNN分类问题类似,人脸年龄预测无非就是将人脸分为多个类别,然后训练卷积神经网 ...
- TOJ 1191. The Worm Turns
191. The Worm Turns Time Limit: 1.0 Seconds Memory Limit: 65536K Total Runs: 5465 Accepted Run ...