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.可 ...
随机推荐
- Python3中的编码问题
编码方式介绍 我们首先来熟悉一下常见的一些编码方式,按照时间轴来划分,共有以下几种编码方式(常见): ASCII编码方式:对127个常见字符进行编码,其中包含了10个阿拉伯数字,共52个大小写英文字母 ...
- bzoj5164: 餐厅计划问题(三分+贪心)
网络流经典题里餐巾计划的加强版...天数变成了$10^5$,那就不能用费用流做了... 考虑费用流的时候,单位费用随流量的增加而减少,也就是说费用其实是个单峰(下凸)函数. 那么可以三分要买的餐巾个数 ...
- js实现数字键盘
效果图: 1.引入jquery.js文件 2.css样式 <style type="text/css"> #numberkeyboard { border: 1px s ...
- Windows完成端口与Linux epoll技术简介
收藏自:http://www.cnblogs.com/cr0-3/archive/2011/09/09/2172280.html WINDOWS完成端口编程1.基本概念2.WINDOWS完成端口的特点 ...
- 《剑指offer》— JavaScript(32)把数组排成最小的数
把数组排成最小的数 题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为3213 ...
- pat1001. Battle Over Cities - Hard Version 解题报告
/**题目:删去一个点,然后求出需要增加最小代价的边集合生成连通图思路:prim+最小堆1.之前图中未破坏的边必用,从而把两两之间可互达的点集合 合并成一个点2.求出不同点集合的最短距离,用prim+ ...
- python的类的 静态属性 类方法 静态方法
一.静态属性 静态属性说的就是数据属性 1.定义一个房间的类,求下这个房间的面积 class Room: def __init__(self,name,ower,width,length,heigh) ...
- 函数和常用模块【day04】:内置函数分类总结(十一)
重点掌握 字符串格式化format() 字符串格式化百分号 判断 转换 数据类型 帮助信息 map和filter()函数 局部变量全局变量 计算内置函数 常用内置函数(其他) 后续会讲 不常用
- A*算法–A* algorithm tutorial
Author:Justin Heyes-Jones From: http://heyes-jones.com/astar.php Date:2014.8.16 本文地址:http://www.cnbl ...
- SpringBoot入门Demo(Hello Word Boot)
Spring Boot 是由Pivotal团队提供的全新框架,其设计目的是用来简化新的Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. ...