Java生成压缩文件工具类

文件压缩功能在日常项目中经常会使用到,例如文件太多,需要发送给用户,这时就需要将多个文件压缩成一个压缩包,然后再通过邮件或其它方式发送给用户;在这里给大家提供一种生成zip文件压缩工具类,并附带测试代码。

测试代码目录结构:
在这里插入图片描述

文件压缩核心工具类:
主要有三个入参:
List fileList:存放所有压缩源文件的集合
File zipFile:压缩后的文件
Map<String, String> myMap:key-文件压缩前名称,value-文件压缩后在压缩包中的名称

package com.bbu.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 文件压缩工具类
 * 
 * @author code_now
 */
public class ZipUtils {
	
	/**
	 * 生成压缩文件
	 * @param fileList	存放所有压缩源文件
	 * @param zipFile	压缩后文件
	 * @param myMap	key-文件压缩前名称,value-文件压缩后在压缩包中的名称
	 * @throws Exception
	 */
	public static void createFileZip(List<File> fileList, File zipFile, Map<String, String> myMap) throws Exception{
		if(fileList.size()>0){
			byte[] buf = new byte[1024];
			try {
				ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
				for(File file:fileList){
					FileInputStream in = new FileInputStream(file);  
					out.putNextEntry(new ZipEntry((String) myMap.get(file.getName())));
					int len;  
					while ((len = in.read(buf)) > 0) {  
						out.write(buf, 0, len);  
					}  
					out.closeEntry();  
					in.close();
					file.delete();// 写进压缩文件后,删除临时目录中的源文件
				}
				out.close();
			} catch (IOException e) {
				throw new Exception("文件压缩失败!" + e.getMessage());
			}  
		}
	}
}

测试代码:

package com.bbu.test;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;

import com.bbu.utils.ZipUtils;

public class TestCreateZip {
	
	Log logger = LogFactory.getLog(getClass());
	
	@Test
	public void createZipTest() {
		
		// 源文件存储路径
		String srcPath = this.getClass().getClassLoader().getResource("").getPath();
		// 压缩包存储路径
		String desPath = "D:/zipfile/";
		// 用于临时存放所有的压缩文件
		List<File> fileList = new ArrayList<File>();
		// key-文件压缩前名称,value-文件压缩后在压缩包中的名称
		Map<String,String> myMpa = new HashMap<String,String>();
		
		// 放入文件test.pdf
		String testPdf = srcPath + "test.pdf";
		File testPdfFile =new File(testPdf); 
		myMpa.put(testPdfFile.getName(), "newName.pdf"); 
		fileList.add(testPdfFile);
		// 放入文件test.docx
		String testDocx = srcPath + "test.docx"; 
		File testDocxFile =new File(testDocx);  
		myMpa.put(testDocxFile.getName(), "newName.docx"); 
 		fileList.add(testDocxFile);
 		
		// 生成压缩包zip文件
		File zipFile = new File(desPath + File.separator+System.currentTimeMillis()+".zip");
		logger.info(zipFile);// 打印压缩包文件全路径
		
		try {
			// 调用压缩工具进行压缩
			ZipUtils.createFileZip(fileList, zipFile, myMpa);
			logger.info("压缩成功!");
		} catch (Exception e) {
			logger.info("压缩失败!", e);
		}
	}
}

测试结果:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值