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 ...
随机推荐
- SQLServer数据库监控代码
SQLServer数据库监控代码: creation_time, total_worker_time, last_worker_time, max_worker_time, min_worker_ti ...
- 利用wireshark抓包获取cookie信息
以下是一些过滤规则: 1. 百度的cookie: http.cookie matches "BDUSS" 2. 博客园的cookie: http.cookie matches &q ...
- SSIS变量属性中EvaluateAsExpression设置的作用
我们在做SqlServer SSIS包开发的时候,经常会用到SSIS的变量,我们可以使用和修改SSIS变量的值使得SSIS包的逻辑更灵活,如下图所示: 在定义SSIS变量的时候可以使用固定值(如上图中 ...
- Oracle 正则表达式使用示例
正则表达式的基本例子 在使用这个新功能之前,您需要了解一些元字符的含义.句号 (.) 匹配一个正规表达式中的任意字符(除了换行符).例如,正规表达式 a.b 匹配的字符串中首先包含字母 a,接着是其它 ...
- Html的智能表单
-新的输入类型 -email -url -number -range -Date pickers ( date ,month ,week, time , datetime ,datetime-loca ...
- 01.线性表 ArrayList
public class MyArrayList { //容量 ; //存放数组元素 private object[] _items; //数组大小 private int _size; //元素个数 ...
- OA工作流规格--转
工作流是整个OA系统的核心,也是BPM的核心,工作流到 底需要实现哪些功能,本文就此以用户的需求为蓝本进行阐述.工作流表面看起来是很简单的,无非是一个表单模板,一个流程定义,然后起草后根据设定的流程一 ...
- 通达信5分钟.lc5和.lc1文件格式
一.通达信日线*.day文件 文件名即股票代码 每32个字节为一天数据 每4个字节为一个字段,每个字段内低字节在前 00 ~ 03 字节:年月日, 整型 04 ~ 07 ...
- INNO SETUP 读取可变注册表路径的问题
;INNO 读取可变注册表路径的问题 ;问题:;我想自动为 FireFox 安装上 Real 的 Mozilla 插件~但是它的路径存放在"HKEY_CURRENT_USER\Softwar ...
- [TCPIP] 分层 Note
TCP/IP 分层 TCP/IP是一组不同层次上的多个协议的组合. 通常被分为:链路层.网络层.运输层.应用层 1. 链路层(数据链路层 或 网络接口层) 通常包括操作系统中的设备驱动程序和计算机中 ...