将指定目录下的某些 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();
}
}
}
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();
}
}
}
相关文章推荐
- linux下递归删除目录下所有指定类型文件
- Java实现将目录下指定类型文件全部删除_FileFilter文件过滤
- 统计指定目录下面的文件类型及数量
- 批量删除目录下指定类型文件
- java遍历删除指定文件夹下面的相同格式或指定格式的file文件
- shell 删除目录及子目录指定类型的文件
- Python批量删除指定目录下的指定类型的文件
- Linux 删除目录及其子目录指定类型文件
- Linux下面删除指定目录下非指定文件的所有文件
- Python 批量删除文件夹下面指定类型的所有文件
- windows 批处理删除指定目录下 指定类型 指定天数之前文件
- 删除365天前指定目录下指定类型文件 perl
- C语言 递归地删除一个指定目录下所有指定类型的文件
- java遍历删除windows下的指定文件夹下面的相同格式或指定格式的file文件
- Python 批量删除文件夹下面指定类型的所有文件
- 递归删除指定目录下的指定类型文件和目录
- 用批处理删除指定目录或文件
- 删除指定天数文件,再删除所有的空目录批处理文件
- find删除目录下面的所有文件 按照创建时间保留部分文件
- Python中实现从目录中过滤出指定文件类型的文件