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

主要写这个类:

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: </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();
		}
	}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值