package com.etc.clear.img.job;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 压缩服务器图片任务
* <p>
*
* @ClassName : UnzipImgJob
* </p>
* <p>
* @Description : TODO
* </p>
* <p>
* @Author : HuaZai
* </p>
* <p>
* @ContactInformation : 1461522031@qq.com/huazai6789@aliyun.com
* </p>
*
* @Date : 2018年1月3日 下午1:16:30
*
* @Version : V1.0.0
*
*/
public class CompressImgJob implements Job {
// 定义常量
private static final Logger log = LoggerFactory.getLogger(CompressImgJob.class);
private static final String filePath = "D:/zipfile/"; // 需要压缩文件的根路径
private static final String fileName = "zip"; // 文件夹名
private static final String inputPath = "D:/zipfile/"; // 压缩完成后,保存保存的文件
private static final String UnZipPath = "D:/zipfile/"; // 解压完成后,保存保存的文件
/**
* 压缩图片任务
*/
@SuppressWarnings("static-access")
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
log.info(" = = = = = = = = = 进入图片《压缩》阶段 ...." + context.getTrigger().getKey());
// 初始化压缩组件
//this.InitCompress();
// 初始化解压组件
this.unZipByFile();
}
/**
* 初始化压缩文件
* <p>
*
* @Title : InitCompress
* </p>
* <p>
* @Description : TODO
* </p>
* <p>
* @Author : HuaZai
* </p>
*
* @Date : 2018年1月15日 下午2:21:31
*/
public static void InitCompress() {
try {
log.info(" = = = = = = = = = 初始化压缩组件");
// 创建文件输出流
OutputStream oStream = new FileOutputStream(inputPath + getNextDate() + ".zip");
// 传出到输出流,使用CRC32,确保文件的一致性
CheckedOutputStream cStream = new CheckedOutputStream(oStream, new CRC32());
// 创建压缩文件的输出流
ZipOutputStream zStream = new ZipOutputStream(cStream);
// 设置为 GBK 的编码格式
zStream.setEncoding("GBK");
// 需要压缩的文件夹
File file = new File(filePath + getNextDate());
// 调用压缩工具类
compressByZipFile(zStream, file);
// 注意关闭流
zStream.close();
cStream.close();
oStream.close();
log.info(" = = = = = = = = = 文件压缩结束,请前往该目录进行查看:" + filePath + getNextDate());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 使用递归获取指定文件夹下所有的子文件夹,并对其进行压缩
* <p>
*
* @throws Exception
*
* @Title : CompressByZipFile
* </p>
* <p>
* @Description : TODO
* </p>
* <p>
* @Author : HuaZai
* </p>
*
* @Date : 2018年1月15日 上午11:25:42
*/
public static void compressByZipFile(ZipOutputStream stream, File file) throws Exception {
if (file.isDirectory()) {
// 创建压缩文件的目录结构
stream.putNextEntry(
new ZipEntry(file.getPath().substring(file.getPath().indexOf(fileName)) + File.separator));
// 使用递归实现文件的压缩
for (File dfiles : file.listFiles()) {
compressByZipFile(stream, dfiles);
}
} else {
// 正在压缩的文件名称
log.info(" = = = = = = = = = 当前正在压缩的文件名:" + file.getName());
// 创建压缩文件
stream.putNextEntry(new ZipEntry(file.getPath().substring(file.getPath().indexOf(fileName))));
// 以流的方式创建文件流
InputStream iStream = new FileInputStream(file.getPath());
// 创建流缓冲区
BufferedInputStream bStream = new BufferedInputStream(iStream);
// 创建字节数组
byte[] bt = new byte[1024];
// 将流写入到压缩文件
while (bStream.read(bt) != -1) {
stream.write(bt);
}
// 注意关闭流
bStream.close();
iStream.close();
}
}
/**
* 调用 Apache API 解压文件夹
* <p>
*
* @Title : unZipByFile
* </p>
* <p>
* @Description : TODO
* </p>
* <p>
* @Author : HuaZai
* </p>
*
* @Date : 2018年1月16日 上午10:10:17
*/
@SuppressWarnings("unchecked")
public static void unZipByFile() {
try {
// 构建压缩文件实例
ZipFile zFile = new ZipFile(filePath + getNextDate() + ".zip", "GBK");
// 获取压缩文件中的项
for (Enumeration<ZipEntry> enumeration = zFile.getEntries(); enumeration.hasMoreElements();) {
// 获取元素
ZipEntry zipEntry = enumeration.nextElement();
if (!zipEntry.getName().endsWith(File.separator)) { // 排除空文件夹
log.info(" = = = = = = = = = 正在解压文件:" + zipEntry.getName());
// 创建解压文件夹目录
File file = new File(UnZipPath
+ zipEntry.getName().substring(0, zipEntry.getName().lastIndexOf(File.separator)));
// 判断当前当前解压文件夹是否存在
if (!file.exists()) { // 不存在则创建
file.mkdirs();
}
// 创建解压输出流
OutputStream oStream = new FileOutputStream(UnZipPath + zipEntry.getName());
// 构建输出缓存流
BufferedOutputStream bOutputStream = new BufferedOutputStream(oStream);
// 将元素读取到输入流
InputStream iStream = zFile.getInputStream(zipEntry);
// 构建输入缓存流,并写入元素输入流
BufferedInputStream bInputStream = new BufferedInputStream(iStream);
// 为了保证元素的一致性,使用 CRC32 算法检查输入流
CheckedInputStream cInputStream = new CheckedInputStream(bInputStream, new CRC32());
// 读取文件
byte[] bt = new byte[1024];
while (cInputStream.read(bt) != -1) {
bOutputStream.write(bt);
}
// 注意操作结束后,需要关闭所有流
cInputStream.close();
bInputStream.close();
iStream.close();
bOutputStream.close();
oStream.close();
} else { // 如果为空文件,则创建该文件夹
new File(UnZipPath + zipEntry.getName()).mkdirs();
}
}
log.info(" = = = = = = = = = 解压完成,请前往该目录进行查看:" + UnZipPath + getNextDate());
zFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取指定日期
* <p>
*
* @Title : getNextDate
* </p>
* <p>
* @Description : TODO
* </p>
* <p>
* @Author : HuaZai
* </p>
*
* @Date : 2018年1月16日 上午9:37:43
*/
public static String getNextDate() {
Calendar cd = Calendar.getInstance();
cd.add(Calendar.DATE, -30); // 获取后30天的日期
Date dt = cd.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");// 设置日期格式
String str = df.format(dt);
log.info(" = = = = = = = = = 本次选择压缩的文件夹为:" + str);
return str;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Java 中调用 Apache API 实现图片文件的 压缩 与 解压 的 Demo 考虑到在实际的项目开发的过程中,可能会涉及到文件的路径为中文路径,这时如果使用 Java 原生的 JDK 则无法处理乱码问题,使用 Apache API 就可以在压缩与解压时设置编码格式;
资源推荐
资源详情
资源评论
收起资源包目录
ClearImgETC.zip (37个子文件)
ClearImgETC
bin
com
etc
clear
img
job
CompressImgJob.class 6KB
ClearImgJob.class 1KB
SendImgJob.class 1KB
common
InitImgQuartzCommon.class 998B
utils
ImgQuartzUtils.class 3KB
ImgMD5Utils.class 5KB
data
job
DeleteDataJob.class 748B
SendDataJob.class 745B
UnzipDataJob.class 745B
ExpDataJob.class 1KB
common
InitDataQuartzCommon.class 2KB
utils
DataQuartzUtils.class 5KB
ApplicationMain.class 594B
mail
job
SendMailByJob.class 1KB
DeleteMailByJob.class 1KB
common
InitMailQuartzCommon.class 1KB
utils
MailQuartzUtils.class 4KB
log4j.xml 3KB
.settings
org.eclipse.core.resources.prefs 84B
org.eclipse.jdt.core.prefs 598B
src
com
etc
clear
img
job
CompressImgJob.java 8KB
common
InitImgQuartzCommon.java 1KB
utils
ImgQuartzUtils.java 4KB
ApplicationMain.java 766B
log4j.xml 3KB
.project 387B
resource
quartz_priority.properties 371B
.classpath 849B
lib
log4j-1.2.16.jar 470KB
quartz-jobs-2.2.1.jar 33KB
slf4j-api-1.6.6.jar 26KB
slf4j-log4j12-1.6.6.jar 9KB
ant.jar 1.23MB
commons-codec-1.11.jar 327KB
commons-lang3-3.7.jar 488KB
quartz-2.2.1.jar 645KB
c3p0-0.9.1.1.jar 594KB
共 37 条
- 1
资源评论
- wang_ada2018-03-22不知道下载到哪里了Hello_World_QWP2018-05-28打开你的浏览器,右上角工具栏打开,选择下载历史,再点击打开文件位置即可找到,不用谢!!!
Hello_World_QWP
- 粉丝: 4w+
- 资源: 166
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功