您的位置:首页 > 编程语言 > Java开发

将指定目录下的某些 af8e 相同类型的文件打成zip包存放到指定目录下面并删除原文件

2018-03-13 22:38 399 查看
主要写这个类:package com.zy.common.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
* <p>Title: ZipUtil</p>
* <p>Description: 将指定目录下的多个相同类型的文件打成zip包并删除源文件,其他文件不理</p>
* <p>Company: http://www.berchina.com </p>
* @author ZY
* <p> Just go on !!!</p>
* @date 2018-3-13 下午8:32:18
* @version v1.0
*/
public class ZipUtil {

/**
* <p>Description: 将目录sourcePath下面以prefix开头且以suffix结尾的文件打
* 成名为zipName的zip包放在targetPath目录下面并删除原文件</p>
* <p>Company: </p>
* @author ZY
* @param sourcePath 源路径
* @param prefix 文件名前缀
* @param suffix 文件名后缀
* @param targetPath 目标路径
* @param zipName 打成的zip包名
* @return
* @throws Exception
*/
public static boolean packFileToZip(String sourcePath,String prefix,String suffix,String targetPath,String zipName) throws Exception{
boolean retFlag = false;
File sourceFiles = new File(sourcePath);
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;

if (!sourceFiles.exists()) {
System.out.println("源路径:" + sourcePath + "不存在");
throw new Exception("source path did not exist");
}else {
try {
//如果目标文件夹不存在,则创建
File targetFileDir = new File(targetPath);
if (!targetFileDir.exists()) {
targetFileDir.mkdir();
}

//1.定义压缩的目标zip文件
File targetFile = new File(targetPath + zipName + ".zip");
//2.判断此文件是否已存在
/*if (targetFile.exists()) {
targetFile.delete();
System.out.println(targetPath + "目录下已经存在名为:" + zipName + ".zip的文件,先删除成功");
}*/
//3.列出源路径下的所有文件
File[] files = sourceFiles.listFiles();
//4.源路径下没有文件
if (files == null || files.length < 1) {
System.out.println("源路径:" + sourcePath + "下没有文件用来压缩" );
throw new Exception("source path did not include any file");
}else {
//5.源路径下有文件
fos = new FileOutputStream(targetFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));

byte[] bs = new byte[10240];
//6.遍历文件,选出需要的文件
//6.1普通for循环
/*for(int i=0;i<files.length;i++){
if (files[i].getName().startsWith(prefix) && files[i].getName().endsWith(suffix)) {
//7.创建zip实体,并添加进ZipOutputStream中
ZipEntry zipEntry = new ZipEntry(files[i].getName());
zos.putNextEntry(zipEntry);

//8.读取待压缩的文件,写进压缩包里
fis = new FileInputStream(files[i]);
bis = new BufferedInputStream(fis,10240);
int read = 0;
while((read = bis.read(bs,0,10240)) != -1){
zos.write(bs,0,read);
}
}
}*/
//6.遍历文件,选出需要的文件
//6.1增强for循环
for (File file : files) {
if (file.getName().startsWith(prefix) && file.getName().endsWith(suffix)) {
//7.创建zip实体,并添加进ZipOutputStream中
ZipEntry zipEntry = new ZipEntry(file.getName());
zos.putNextEntry(zipEntry);

//8.读取待压缩的文件,写进压缩包里
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis,10240);
int read = 0;
while((read = bis.read(bs,0,10240)) != -1){
zos.write(bs,0,read);
}
//必须先关闭输入流才能删除源文件
if (fis != null) {
fis.close();
}
file.delete();
}
}
retFlag = true;
}

} catch (Exception e) {
throw e;
}finally{
if (bis != null) {
bis.close();
}
if (zos != null) {
zos.flush();
zos.close();
}
}

}
return retFlag;
}
}
然后写测试类:package com.zy.test;

import org.junit.Test;

import com.zy.common.utils.ZipUtil;

public class TestZipUtil {

@Test
public void testDemo(){

String sourcePath = "D://temp//";
String prefix = "test";
String suffix = ".pdf";
String targetPath = "D://temp1//";
String zipName = "test";
boolean b;
try {
b = ZipUtil.packFileToZip(sourcePath, prefix, suffix, targetPath, zipName);
System.out.println(b);
} catch (Exception e) {
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息