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.可 ...
随机推荐
- 【bzoj2434】 Noi2011—阿狸的打字机
http://www.lydsy.com/JudgeOnline/problem.php?id=2434 (题目链接) 题意 给出一个字符串,$P$表示输出,$B$表示退格.$m$组询问$(x,y)$ ...
- 【codevs1065】01字符串
题目大意:输出仅由 0 和 1 组成的长度为 N 的字符串个数,并且其中不能含有 3 个连续的相同子串. 题解:数据太水,正规解法应该是枚举后缀进行判断. 代码如下 #include <bits ...
- 【转】Android 编程下的代码混淆
什么是代码混淆 代码混淆(Obfuscated code)亦称花指令,是将计算机程序的代码,转换成一种功能上等价,但是难于阅读和理解的形式的行为.代码混淆可以用于程序源代码,也可以用于程序编译而成的中 ...
- cookie工具包
package com.taotao.common.utils; import java.io.UnsupportedEncodingException; import java.net.URLDec ...
- Chapter 9 (排序)
1.排序算法: //****************************Sort.h******************************************** #ifndef SOR ...
- linux ------ 使用 TFTP 在两个主机之前传输文件
TFTP是用来下载远程文件的最简单网络协议,它是基于UDP协议而实现.嵌入式linux的tftp开发环境包括两个方面:一是linux服务器端的tftp-server支持,二是嵌入式目标系统的tftp- ...
- 函数和常用模块【day05】:生成器并行计算(五)
本节内容 1.概述 2.生成器执行原理 3.send()和__next__()方法的区别 4.yield实现并行效果 一.概述 之前只是介绍生成器,那有些同学就说了,这个生成器除了能节省资源,提高工作 ...
- bzoj千题计划264:bzoj3884: 上帝与集合的正确用法
http://www.lydsy.com/JudgeOnline/problem.php?id=3884 欧拉降幂公式 #include<cmath> #include<cstdio ...
- Spring Cloud (十五)Stream 入门、主要概念与自定义消息发送与接收
前言 不写随笔的日子仿佛就是什么都没有产出一般--上节说到要学Spring Cloud Bus,这里发现按照官方文档的顺序反而会更好些,因为不必去后边的章节去为当前章节去打基础,所以我们先学习Spri ...
- 8个提高效率的CSS实用工具
CSS,也就是Cascading Style Sheets,推出于1997年,差不多是17年前,至此为我们开发网页大开方便之门,协助我们制作出一个又一个惊艳绝伦的网站设计和模板,提升了我们的创造能力, ...