Java操作zip-压缩和解压文件
一、说明
rar格式的压缩包收费,java支持zip格式的压缩和解压
二、工具类
import java.io.*;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
private static final int BUFFER_SIZE = 2 * 1024;
/**
* zip解压
* @param srcFile zip源文件
* @param destDirPath 解压后的目标文件夹
* @throws RuntimeException 解压失败会抛出运行时异常
*/
public static void unZip(File srcFile, String destDirPath) throws RuntimeException {
long start = System.currentTimeMillis();
// 判断源文件是否存在
if (!srcFile.exists()) {
throw new RuntimeException(srcFile.getPath() + "所指文件不存在");
}
// 开始解压
ZipFile zipFile = null;
try {
zipFile = new ZipFile(srcFile);
Enumeration<?> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
System.out.println("解压" + entry.getName());
// 如果是文件夹,就创建个文件夹
if (entry.isDirectory()) {
String dirPath = destDirPath + "/" + entry.getName();
File dir = new File(dirPath);
dir.mkdirs();
} else {
// 如果是文件,就先创建一个文件,然后用io流把内容copy过去
File targetFile = new File(destDirPath + "/" + entry.getName());
// 保证这个文件的父文件夹必须要存在
if(!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdirs();
}
targetFile.createNewFile();
// 将压缩文件内容写入到这个文件中
InputStream is = zipFile.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(targetFile);
int len;
byte[] buf = new byte[BUFFER_SIZE];
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
// 关流顺序,先打开的后关闭
fos.close();
is.close();
}
}
long end = System.currentTimeMillis();
System.out.println("解压完成,耗时:" + (end - start) +" ms");
} catch (Exception e) {
throw new RuntimeException("unzip error from ZipUtils", e);
} finally {
if(zipFile != null){
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 压缩成ZIP 方法
* @param srcFiles 需要压缩的文件列表
* @param out 压缩文件输出流
* @throws RuntimeException 压缩失败会抛出运行时异常
*/
public static void toZip(List<File> srcFiles , OutputStream out)throws Exception {
long start = System.currentTimeMillis();
ZipOutputStream zos = null ;
try {
zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
FileInputStream in = new FileInputStream(srcFile);
while ((len = in.read(buf)) != -1){
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) +" ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils",e);
}finally{
if(zos != null){
try {
zos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
三、使用工具类压缩和解压文件
import com.szfore.utils.ZipUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class TestZip {
public static void main(String[] args) throws Exception{
//testToZip();
testUnzip();
}
/**
* 测试压缩文件
*/
public static void testToZip() throws Exception {
File file1 = new File("c:\\1.txt");
File file2 = new File("c:\\2.txt");
List<File> files = new ArrayList<File>();
files.add(file1);
files.add(file2);
OutputStream out = new FileOutputStream("c:\\1.zip");
ZipUtils.toZip(files,out);
}
/**
* 测试解压文件
* @throws Exception
*/
public static void testUnzip() throws Exception{
File srcFile = new File("c:\\2.zip");
String destDirPath = "c:\\";
ZipUtils.unZip(srcFile,destDirPath);
}
}
Java操作zip-压缩和解压文件的更多相关文章
- Java操作zip压缩和解压缩文件工具类
需要用到ant.jar(这里使用的是ant-1.6.5.jar) import java.io.File; import java.io.FileInputStream; import java.io ...
- java实现zip压缩和解压工具
引入ant.jar package com.develop.web.util; import java.io.BufferedInputStream; import java.io.File; imp ...
- 【转】Java压缩和解压文件工具类ZipUtil
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- Java 的zip压缩和解压缩
Java 的zip压缩和解压缩 好久没有来这写东西了,今天中秋节,有个东西想拿出来分享,一来是工作中遇到的问题,一来是和csdn问候一下,下面就分享一个Java中的zip压缩技术,代码实现比较简单,代 ...
- C#压缩和解压文件
这里用两种方法实现C#压缩和解压文件 1.使用System.IO.Compression名称空间下的相关类(需引用 System.IO.Compression.FileSystem和System.IO ...
- Android_JarZip压缩和解压文件
本文资料来自<android开发权威指南> AndroidSDK中提供了java.util.jar和java.util.zip包中的若干类和接口来完成. 压缩文件基本步骤: 1.创 ...
- linux zip压缩和解压的各种操控
1.把/home文件夹以下的mydata文件夹压缩为mydata.zip zip -r mydata.zip mydata #压缩mydata文件夹 2.把/home文件夹以下的mydata.zip解 ...
- C# 压缩和解压文件(SharpZipLib)
先从网上下载ICSharpCode.SharpZipLib.dll类库 将文件或文件夹压缩为zip,函数如下 /// <summary> /// 压缩文件 /// </summary ...
- linux压缩和解压文件命令
tar 解包:tar zxvf filename.tar 打包:tar czvf filename.tar dirnamegz命令 解压1:gunzip filename.gz 解压2:gzi ...
随机推荐
- Taro多端自定义导航栏Navbar+Tabbar实例
运用Taro实现多端导航栏/tabbar实例 (H5 + 小程序 + React Native) 最近一直在捣鼓taro开发,虽说官网介绍支持编译到多端,但是网上大多数实例都是H5.小程序,很少有支持 ...
- vue-router Uncaught (in promise) NavigationDuplicated 错误
使用 vue-router 编程式实现页面跳转 this.$router.replace({ path: '/pub' }); 出现错误如下图 原因:vue-router 在 3.1 版本之后把 th ...
- PyCharm颜色设置
选择主题和背景图片 选择字体.修改字体大小 新建颜色主题 修改背景颜色 修改注释颜色 File --> Setting (Ctrl + Shift + S) 1.选择不同的主题.选择背景图片 A ...
- Shell命令-网络操作之基础之ping、route
文件及内容处理 - ping.route 1. ping:测试主机之间网络的连通性 ping命令的功能说明 ping 命令用于检测主机.执行 ping 指令会使用 ICMP 传输协议,发出要求回应的信 ...
- 【tyvj1858】xlkxc(拉格朗日插值)
传送门 题意: 求\(\sum_{i=0}^n\sum_{j=1}^{a+id}\sum_{k=1}^{j}k^K,n,a,d\leq 10^9,K\leq 100\). 思路: 最右边这个和式为一个 ...
- Linux 中find命令
1.在当前目录下找以txt结尾的文件 find . -name '*.txt' 2.在当前目录下找以所有字母开头的文件 find . -name '[a-z]*' 3.在/etc 目录下找以host开 ...
- oracle 循环插入数据
参考链接:oracle 行转列 pivot函数基本用法 --建表 --drop table SalesList; create table SalesList( keHu varchar2(20), ...
- [C3] 正则化(Regularization)
正则化(Regularization - Solving the Problem of Overfitting) 欠拟合(高偏差) VS 过度拟合(高方差) Underfitting, or high ...
- CentOs篇
Advanced-高级配置.Security-安全.Boot-启动引导: 1.Removable Devices-移动设备 2.Hard Drive-本地硬盘 3.CD-ROM- Drive-光盘 4 ...
- jQuery中的CSS(四)
1. css(name|pro|[,val|fn]), 访问匹配元素的样式属性 jQuery 1.8中,当你使用CSS属性在css()或animate()中,我们将根据浏览器自动加上前缀(在适当的时候 ...