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 ...
随机推荐
- CSS符合选择器
CSS复合选择器 后代选择器 后代选择器又称为包含选择器,可以选择父元素里面的子元素.其写法就是把外层标签写在前面,内层标签写在后面,中间用空格分隔,当标签发生嵌套时,内层标签就成为外层标签的后代 元 ...
- iOS10跳转至设置页面
在iOS10之前,跳转到系统设置界面的某个指定界面的方式如下: //打开定位服务界面 NSURL*url=[NSURL URLWithString:@"prefs:root=Privacy& ...
- [转]Redis之(一)初识Redis
原文地址:http://blog.csdn.net/u012152619/article/details/52550315 Redis之(一)初识Redis 标签: Redisredis-server ...
- C# 第三方库
基本上选用的都是 https://www.nuget.org 分类中最流行的那个库 1. 日志工具库 NLOG Stackify.com 简单入门文章 https://stackify.com/nl ...
- mysql综合性练习
题目描述 设定有一个数据库,里面有4张表: 学生表(student) 课程表(course) 成绩表(score) 教师信息表(teacher) 表结构如下: 表一_学生表(student) 属性名 ...
- 6.JavaCC官方入门指南-例1
例1:整数加法运算 在这个例子中,我们将判断如下输入的式子是否是一个合法的加法运算: 99 + 42 + 0 + 15 并且在输入上面式子的时候,数字与加号之间的任何位置,都是可以有空格或者换 ...
- HTML&CSS基础-子元素和后代元素选择器
HTML&CSS基础-子元素和后代元素选择器 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.html源代码 <!DOCTYPE html> <html ...
- luoguP3017Brownie Slicing
https://www.luogu.org/problem/P3017 题意 给你一个蛋糕,R行C列 ,每个点有巧克力碎屑(如下) 1 2 2 1 3 1 1 1 2 0 1 3 1 1 1 1 1 ...
- day74_10_21 三大认证
一.权限六表. 一般在django中,基于用户权限访问控制的认证是RBAC(Role-Based Access Control) 还有一些基于auth的认证规则. Django框架采用的是RBAC认证 ...
- SpringBoot关于静态js资源的报错问题
2019-12-02 09:45:01.636 WARN 9572 --- [nio-8080-exec-2] o.s.web.servlet.PageNotFound : No mapping fo ...