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 ...
随机推荐
- GCC-4.6.3编译linux2.6.32.12内核出现“重复的成员‘page’”错误的解决方法
使用gcc4.6.3编译linux2.6.32.12内核出现错误如下: In file included from drivers/net/igbvf/ethtool.c:36:0: drivers/ ...
- PHP扩展开发及内核应用
目录中汉字部分代表已经翻译完成的章节,带链接的表示已经发布的,未待链接的表示正在校正即将发布的. PHP的生命周期 让我们从SAPI开始 PHP的启动与终止 PHP的生命周期 线程安全 小结 PHP变 ...
- 【转】CentOS系统中常用查看日志命令
来源:http://www.centoscn.com/CentOS/help/2014/0310/2540.html Linux IDE RedHat 防火墙活动 .cat tail -f 日 志 文 ...
- vim编辑指令(转)
跳跃指令 类似于游览器中的<前进><后退>按钮 CTRL-] -> 跟着link/tag转入 (follow link/tag) CTRL-o -> 回到上一次 ...
- Spark之键值RDD转换(转载)
1.mapValus(fun):对[K,V]型数据中的V值map操作(例1):对每个的的年龄加2 object MapValues { def main(args: Array[String]) { ...
- js,jquery获取浏览器信息
1.js获取: 查看:window.navigator 2.jQuery获取: chrome firefox 推荐使用navigator获取userAgent然后去正则匹配 参考匹配规则:http:/ ...
- 简明外贸报价单(Price List)范本
简明外贸报价单(Price List)范本 简明外贸报价单(Price List)范本 报价单 Price List 报价日期:年 月 日 Supplier Address 供应商 公司地址 Co ...
- 阐述ArrayList、Vector、LinkedList的存储性能和特性。
答:ArrayList 和Vector都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内存操作,所以索引数据快 ...
- jquery常用插件及用法总结
http://www.jb51.net/Special/200.htm
- 20161013001 DataGridView 数据转 DataTable
DataTable dt2 = GetDgvToTable(Form_CY_ProjectRequirements_D); MessageBox.Show( dt2.Rows.Count.To ...