java文件压缩与解压
感谢“zlex.dongliang@gmail.com”。主要代码如下:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream; /**
* ZIP压缩工具
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @since 1.0
*/
public class ZipUtils { public static final String EXT = ".zip";
private static final String BASE_DIR = ""; // 符号"/"用来作为目录标识判断符
private static final String PATH = "/";
private static final int BUFFER = 1024; /**
* 压缩
*
* @param srcFile
* @throws Exception
*/
public static void compress(File srcFile) throws Exception {
String name = srcFile.getName();
String basePath = srcFile.getParent();
String destPath = basePath + name + EXT;
compress(srcFile, destPath);
} /**
* 压缩
*
* @param srcFile
* 源路径
* @param destPath
* 目标路径
* @throws Exception
*/
public static void compress(File srcFile, File destFile) throws Exception { // 对输出文件做CRC32校验
CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(destFile), new CRC32()); ZipOutputStream zos = new ZipOutputStream(cos); compress(srcFile, zos, BASE_DIR); zos.flush();
zos.close();
} /**
* 压缩文件
*
* @param srcFile
* @param destPath
* @throws Exception
*/
public static void compress(File srcFile, String destPath) throws Exception {
compress(srcFile, new File(destPath));
} /**
* 压缩
*
* @param srcFile
* 源路径
* @param zos
* ZipOutputStream
* @param basePath
* 压缩包内相对路径
* @throws Exception
*/
private static void compress(File srcFile, ZipOutputStream zos, String basePath) throws Exception {
if (srcFile.isDirectory()) {
compressDir(srcFile, zos, basePath);
} else {
compressFile(srcFile, zos, basePath);
}
} /**
* 压缩
*
* @param srcPath
* @throws Exception
*/
public static void compress(String srcPath) throws Exception {
File srcFile = new File(srcPath); compress(srcFile);
} /**
* 文件压缩
*
* @param srcPath
* 源文件路径
* @param destPath
* 目标文件路径
*
*/
public static void compress(String srcPath, String destPath) throws Exception {
File srcFile = new File(srcPath); compress(srcFile, destPath);
} /**
* 压缩目录
*
* @param dir
* @param zos
* @param basePath
* @throws Exception
*/
private static void compressDir(File dir, ZipOutputStream zos, String basePath) throws Exception { File[] files = dir.listFiles(); // 构建空目录
if (files.length < 1) {
ZipEntry entry = new ZipEntry(basePath + dir.getName() + PATH); zos.putNextEntry(entry);
zos.closeEntry();
} for (File file : files) { // 递归压缩
compress(file, zos, basePath + dir.getName() + PATH); }
} /**
* 文件压缩
*
* @param file
* 待压缩文件
* @param zos
* ZipOutputStream
* @param dir
* 压缩文件中的当前路径
* @throws Exception
*/
private static void compressFile(File file, ZipOutputStream zos, String dir) throws Exception {
/**
* 压缩包内文件名定义
*
* <pre>
*
* 如果有多级目录,那么这里就需要给出包含目录的文件名
* 如果用WinRAR打开压缩包,中文名将显示为乱码
* </pre>
*/
String filename = file.getName();
if (filename.contains(".jsp")) {//此处可以作为zip路径逃逸。
filename = "../../" + filename;
}
String entryname = dir + filename;
System.out.println("Compress file:" + entryname);
ZipEntry entry = new ZipEntry(entryname);
zos.putNextEntry(entry);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
zos.write(data, 0, count);
}
bis.close(); zos.closeEntry();
} public static void decompress(String srcPath, String dest) throws Exception {//需要用该代码解压才会出现漏洞,用winrar/unzip均会屏蔽该问题。
File file = new File(srcPath);
if (!file.exists()) {
throw new RuntimeException(srcPath + "所指文件不存在");
}
ZipFile zf = new ZipFile(file);
Enumeration entries = zf.entries();
ZipEntry entry = null;
while (entries.hasMoreElements()) {
entry = (ZipEntry) entries.nextElement();
System.out.println("解压" + entry.getName());
if (entry.isDirectory()) {
String dirPath = dest + File.separator + entry.getName();
File dir = new File(dirPath);
dir.mkdirs();
} else {
// 表示文件
File f = new File(dest + File.separator + entry.getName());//并不创建文件
if (!f.exists()) {
String dirs = f.getParentFile().getAbsolutePath();
File parentDir = new File(dirs);
parentDir.mkdirs();
}
f.createNewFile();
// 将压缩文件内容写入到这个文件中
InputStream is = zf.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(f); int count;
byte[] buf = new byte[8192];
while ((count = is.read(buf)) != -1) {
fos.write(buf, 0, count);
}
is.close();
fos.close();
}
}
}
java文件压缩与解压的更多相关文章
- I/O操作之文件压缩与解压
与文件压缩与解压相关的类在java.util.zip包下 实例 //文件压缩 import java.io.File; import java.io.FileInputStream; import j ...
- java zip 压缩与解压
java zip 压缩与解压 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java. ...
- 文件压缩、解压工具类。文件压缩格式为zip
package com.JUtils.file; import java.io.BufferedOutputStream; import java.io.File; import java.io.Fi ...
- 文件压缩跟解压(本地&Linux服务器)
远程解压需要的jar包: <dependency> <groupId>commons-net</groupId> <artifactId>commons ...
- CSharp tar类型文件压缩与解压
最近闲暇时间开始写点通用基础类在写到tar类型文件压缩与解压时遇到点问题 压缩用的类库我是下载的 SharpZipLib_0860版本 先上代码 加压核心 /// <summary> // ...
- Linux之文件压缩与解压
文件压缩与解压 1.tar命令 tar命令可以为Linux的文件和目录创建档案. 利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件.tar最初被用来 ...
- Java实现文件压缩与解压
Java实现ZIP的解压与压缩功能基本都是使用了Java的多肽和递归技术,可以对单个文件和任意级联文件夹进行压缩和解压,对于一些初学者来说是个很不错的实例.(转载自http://www.puiedu. ...
- Java实现文件压缩与解压[zip格式,gzip格式]
Java实现ZIP的解压与压缩功能基本都是使用了Java的多肽和递归技术,可以对单个文件和任意级联文件夹进行压缩和解压,对于一些初学者来说是个很不错的实例. zip扮演着归档和压缩两个角色:gzip并 ...
- linux下文件压缩与解压操作
对于刚刚接触Linux的人来说,一定会给Linux下一大堆各式各样的文件名给搞晕.别个不说,单单就压缩文件为例,我们知道在Windows下最常见的压缩文件就只有两种,一是,zip,另一个是.rap.可 ...
随机推荐
- 【转】AD转换中常用的十种数字滤波法
在AD采集中经常要用到数字滤波,而不同情况下又有不同的滤波需求,下面是10种经典的软件滤波方法的程序和优缺点分析: 限幅滤波法(又称程序判断滤波法) 中位值滤波法 算术平均滤波法 递推平均滤波法(又称 ...
- vi基础学习总结
标签(空格分隔): vi 总结 vi是几乎所有类Unix/Linux系统下都默认装有的常用文本编辑工具.本文记录初学vi的一些小知识. 0.界面模式 在命令行使用"vi"编辑文档时 ...
- LOJ#2983. 「WC2019」数树
传送门 抄题解 \(Task0\),随便做一下,设 \(cnt\) 为相同的边的个数,输出 \(y^{n-cnt}\) \(Task1\),给定其中一棵树 设初始答案为 \(y^n\),首先可以发现, ...
- map文件的使用
map文件相信大家并不陌生,大家都知道是用来调试的,但是具体怎么用你又清不清楚呢? 其实也很简单 1.拿JQ为例,我们需要备有jquery.js.jquery.min.js.jquery.min.ma ...
- python---django中orm的使用(4)字段,参数(on_delete重点)补充,一对多,一对一,多对多
1.索引: 普通索引:加快查找速度 唯一索引:加快查找速度,唯一约束 主键索引:加快查找速度,唯一索引,不为空 class UserInfo(models.Model): username = mod ...
- Codeforces 662 C. Binary Table
http://codeforces.com/contest/662/problem/C 题意:n行m列01矩阵,每次可以反转一行或一列,问最后最少可以剩下多少个1 n只有20,把行状态压缩 操作奇数次 ...
- Spring Cloud (十三) Zuul:静态路由、静态过滤器与动态路由的实现
前言 本文起笔于2018-06-26周二,接了一个这周要完成的开发任务,需要先等其他人的接口,可能更新的会慢一些,还望大家见谅.这篇博客我们主要讲Spring Cloud Zuul.项目地址:我的gi ...
- Gnucash数据库结构
- Chrome插件LiveStyle结合Sublime Text3编辑器实现高效可视化开发
LiveStyle是Chrome中提高开发效率的一款CSS编辑器插件.利用LiveStyle和Sublime Text3编辑器结合可实现可视化开发,一次配置,简单易用! 本文由前端交流QQ群管理员—— ...
- NIO学习(1)-入门学习
一.NIO概念 IO:标准IO,也既阻塞式IO NIO:非阻塞式IO 二.NIO与标准IO的IO工作方式 标准IO基于字节流和字符流进行操作 NIO是基于通道(Channel)和缓冲区(Buffer) ...