Java压缩zip文件(怎样用Java将文件追加到zip文件)

学习Java压缩zip文件:

  1. 压缩单个文件和多个文件,不包含文件夹;
  2. 怎样用Java将文件追加到zip文件中。

测试类:

/**
 * @FileName:ZipTest.java
 * @Description: TODO
 * @Copyright: All rights Reserved, Designed By [meter] Copyright(C) 2010-2018
 * @Company: 
 * @author:meter
 * @version: V1.0 
 * @Create:2018年5月4日 上午9:50:22
 *
 * Modification  History:
 * Date         Author        Version        Discription
 * -----------------------------------------------------------------------------------
 * 2018年5月4日      meter          1.0             1.0
 * Why & What is modified: <修改原因描述>
 */
package org.meter.demo;

import java.io.File;
import java.io.FileFilter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.meter.zip.ZipTools;


/**      
 * @项目名称:meter   
 * @类名称:ZipTest   
 * @类描述:   
 * @创建人:meter   
 * @创建时间:2018年5月4日 上午9:50:22   
 * @版权:Copyright@2018.All Rights Reserved    
 * @version v1.0      
 */
public class ZipTest {
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSSSS");
	private static ThreadPoolExecutor workerPool=new ThreadPoolExecutor(20,
				25,
				1,
				TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(15),
				new ThreadPoolExecutor.CallerRunsPolicy());
		
	private static void test(String path,Long size) throws Exception{
		workerPool.allowCoreThreadTimeOut(true);// 设置超时时关闭线程
		File dir=new File(path);
		if(!dir.exists()){
			System.out.println("当前路径:"+path+"不存在");
			return;
		}
		System.out.println("当前路径:"+dir.getAbsolutePath());
		File[] files =dir.listFiles(new FileFilter(){
			@Override
			public boolean accept(File pathname) {
				if(pathname.getName().toLowerCase().endsWith(".zip")&&pathname.getName().toLowerCase().contains("sca_2018")){
					return true;
				}
				return false;
			}
		});
		long fileSize=0;
		List<File> fileList=new ArrayList<File>();
		String zipName="sca_"+sdf.format(new Date())+".zip";
		int flag=files.length;
		for(File file:files){
			System.out.println("读取到文件:"+file.getName());
			fileSize+=file.length();
			fileList.add(file);
			flag--;
			if(fileSize>=size){
				workerPool.execute(new InnerWorker(fileList,zipName));
				fileSize=0;
				fileList=new ArrayList<File>();
				zipName="sca_"+sdf.format(new Date())+".zip";
			} else if(flag<=0){
				ZipTools.zipFiles(fileList,new File(zipName));
			}
		}
	}

    
    private static class InnerWorker implements Runnable{
    	private List<File> fileList;
    	private String zipName;
    	public InnerWorker(List<File> fileList,String zipName){
    		this.fileList=fileList;
    		this.zipName=zipName;
    	}
    	
		@Override
		public void run() {
			try {
				ZipTools.zipFiles(fileList,new File(zipName));
				System.out.println("压缩文件["+zipName+"]成功。");
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("压缩失败文件:"+zipName);
			}
			
		}
    	
    }
	/** @创建日期:2018年5月4日 上午9:50:22
	 * @作者: meter
	 * @描述:TODO
	 * @Title: main
	 * @param args   
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		System.out.println("Please input the path:");
		Scanner input = new Scanner(System.in);
		String path=input.nextLine();
		test(path,2147483648L);
	}

}

工具类:

/**
 * @FileName:ZipTools.java
 * @Description: TODO
 * @Copyright: All rights Reserved, Designed By [meter] Copyright(C) 2010-2018
 * @Company: 米特娱乐乐园
 * @author:meter
 * @version: V1.0 
 * @Create:2018年5月4日 上午10:25:09
 *
 * Modification  History:
 * Date         Author        Version        Discription
 * -----------------------------------------------------------------------------------
 * 2018年5月4日      meter          1.0             1.0
 * Why & What is modified: <修改原因描述>
 */
package org.meter.zip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;



/**
 * @项目名称:meter
 * @类名称:ZipTools 
 * @类描述:采用zip压缩文件
 * @创建人:meter
 * @创建时间:2018年5月4日 上午10:25:09
 * @版权:Copyright@2018.All Rights Reserved
 * @version v1.0
 */
public class ZipTools {
	/**
	 * @创建日期:2018年5月4日 下午1:37:22
	 * @作者: meter
	 * @描述:压缩多个文件的情况
	 * @Title: zip
	 * @param fileList
	 * @param zipFile
	 * @throws Exception
	 */
	public static void zipFiles(List<File> fileList,File zipFile) throws Exception{
		System.out.println("zipFiles待压缩文件:"+zipFile.getAbsolutePath());
		if(fileList != null){
			if(zipFile.exists()){//压缩文件已经存在,则只能单个添加
				for(File file:fileList){
					zip(zipFile,file);
				}
			}else{//不存在则新建
				// 创建zip输出流
				ZipOutputStream zipOutStream = new ZipOutputStream(new FileOutputStream(zipFile), Charset.forName("UTF-8"));
				// 创建缓冲输出流
				BufferedOutputStream bufferOutStream = new BufferedOutputStream(zipOutStream);
				for(File file:fileList){
					zipFile(file,zipOutStream,bufferOutStream);
				}
				//最后关闭输出流
				bufferOutStream.close();
				zipOutStream.close();
			}
			
		}
	}
	/**
	 * @创建日期:2018年5月4日 下午1:44:55
	 * @作者: meter
	 * @描述:执行文件压缩
	 * @Title: zipFile
	 * @param file
	 * @param zipOutStream
	 * @param bufferOutStream
	 * @throws IOException
	 */
	private static void zipFile(File file,ZipOutputStream zipOutStream,BufferedOutputStream bufferOutStream) throws IOException{
		// 创建压缩文件实体
		ZipEntry entry = new ZipEntry(file.getName());
		// 添加实体
		zipOutStream.putNextEntry(entry);
		// 创建输入流
		BufferedInputStream bufferInputStream = new BufferedInputStream(new FileInputStream(file));
		write(bufferInputStream, bufferOutStream);
		zipOutStream.closeEntry();
	}
	/**
	 * @创建日期:2018年5月4日 下午1:37:01
	 * @作者: meter
	 * @描述:压缩单个文件
	 * @Title: zip
	 * @param zipFile
	 * @param sourceFile
	 * @throws Exception
	 */
	public static void zip(File zipFile, File sourceFile) throws Exception {
		System.out.println("待压缩文件:"+zipFile.getAbsolutePath());
		if (zipFile.exists()) {// 添加到已经存在的压缩文件中
			
			File tempFile=new File(zipFile.getAbsolutePath()+".tmp");
			// 创建zip输出流
			ZipOutputStream zipOutStream = new ZipOutputStream(new FileOutputStream(tempFile), Charset.forName("UTF-8"));
			// 创建缓冲输出流
			BufferedOutputStream bufferOutStream = new BufferedOutputStream(zipOutStream);
			ZipFile zipOutFile = new ZipFile(zipFile);
			
			Enumeration<? extends ZipEntry> entries = zipOutFile.entries();
			while (entries.hasMoreElements()) {
				ZipEntry entry = entries.nextElement();
				System.out.println("copy: " + entry.getName());
				zipOutStream.putNextEntry(entry);
				if (!entry.isDirectory()) {
					write(zipOutFile.getInputStream(entry), bufferOutStream);
				}
				zipOutStream.closeEntry();
			}
			zipOutFile.close();//记得关闭zip文件,否则后面无法删除原始文件
			ZipEntry entry = new ZipEntry(sourceFile.getName());
			// 添加实体
			zipOutStream.putNextEntry(entry);
			BufferedInputStream bufferInputStream = new BufferedInputStream(new FileInputStream(sourceFile));
			write(bufferInputStream, bufferOutStream);
			//最后关闭输出流
			bufferOutStream.close();
			zipOutStream.close();
			boolean flag=zipFile.delete();
			if(flag){
			tempFile.renameTo(zipFile);
			}else{
				System.out.println("删除文件失败。");
			}
		} else {// 新创建压缩文件
			// 创建zip输出流
			ZipOutputStream zipOutStream = new ZipOutputStream(new FileOutputStream(zipFile), Charset.forName("UTF-8"));
			// 创建缓冲输出流
			BufferedOutputStream bufferOutStream = new BufferedOutputStream(zipOutStream);
			// 创建压缩文件实体
			ZipEntry entry = new ZipEntry(sourceFile.getName());
			// 添加实体
			zipOutStream.putNextEntry(entry);
			// 创建输入流
			BufferedInputStream bufferInputStream = new BufferedInputStream(new FileInputStream(sourceFile));
			write(bufferInputStream, bufferOutStream);
			//最后关闭输出流
			bufferOutStream.close();
			zipOutStream.close();
		}
		
	}
	/**
	 * @创建日期:2018年5月4日 下午2:09:37
	 * @作者: meter
	 * @描述:读写zip文件
	 * @Title: write
	 * @param inputStream
	 * @param outStream
	 * @throws IOException
	 */
    private static void write(InputStream inputStream,OutputStream outStream) throws IOException{
    	byte[] data=new byte[4096];
    	int length=0;
    	while((length=inputStream.read(data)) != -1){
    		outStream.write(data,0,length);
    	}
    	outStream.flush();//刷新输出流
    	inputStream.close();//关闭输入流
    }
    
    /**
     * @创建日期:2018年5月4日 下午3:07:55
     * @作者: meter
     * @描述:压缩指定目录下所有文件
     * @Title: zipDirectory
     * @param dirFile
     * @param zipFile
     * @throws IOException 
     */
    public static void zipDirectory(File dirFile,File zipFile) throws IOException{
    	if(dirFile != null && dirFile.isDirectory()){
    		if(zipFile == null){
    			zipFile=new File(dirFile.getAbsolutePath()+".zip");
    		}
    		String dirName=dirFile.getName()+File.separator;
    		// 创建zip输出流
			ZipOutputStream zipOutStream = new ZipOutputStream(new FileOutputStream(zipFile), Charset.forName("UTF-8"));
			// 创建缓冲输出流
			BufferedOutputStream bufferOutStream = new BufferedOutputStream(zipOutStream);
			dealDirFile( dirFile,dirName,bufferOutStream,zipOutStream);
			
			//最后关闭输出流
			bufferOutStream.close();
			zipOutStream.close();
    	}else{
    		System.out.println("["+dirFile.getName()+"]不是一个文件夹,或者不存在。");
    	}
    }
    /**
     * @创建日期:2018年5月4日 下午4:38:46
     * @作者: meter
     * @描述:处理目录文件
     * @Title: dealDirFile
     * @param dirFile
     * @param parentDir
     * @param bufferOutStream
     * @param zipOutStream
     * @throws IOException
     */
    private static void dealDirFile(File dirFile,String parentDir, BufferedOutputStream bufferOutStream, ZipOutputStream zipOutStream) throws IOException{
    	File[] fileList=dirFile.listFiles();
    	for(File file:fileList){
    		if(file.isFile()){
    			// 创建压缩文件实体
    			ZipEntry entry = new ZipEntry(parentDir+file.getName());
    			// 添加实体
    			zipOutStream.putNextEntry(entry);
    			// 创建输入流
    			BufferedInputStream bufferInputStream = new BufferedInputStream(new FileInputStream(file));
    			write(bufferInputStream, bufferOutStream);
    		}else{
    			dealDirFile(file,  parentDir+file.getName()+File.separator,  bufferOutStream,  zipOutStream);
    		}
    	}
    }
    /**
     * @创建日期:2018年5月4日 下午3:22:11
     * @作者: meter
     * @描述:重载zipDirectory
     * @Title: zipDirectory
     * @param dirPath
     * @param zipPath
     * @throws IOException
     */
    public static void zipDirectory(String dirPath,String zipPath) throws IOException{
    	if(zipPath==null || "".equals(zipPath)){
    		zipDirectory(new File(dirPath),null);
    	}else{
    		zipDirectory(new File(dirPath),new File(zipPath));
    	}
    }
   //------------------------------------------------米特华丽的分割线----------------------------------------------------------------------------------------
   
    /**
     * @创建日期:2018年5月4日 下午4:00:41
     * @作者: meter
     * @描述:解压文件
     * @Title: unzip
     * @param zipFile
     * @param destDir
     * @throws IOException 
     */
	public static void unzip(File zipFile, File destDir) throws IOException {
		ZipFile zipOutFile = new ZipFile(zipFile,Charset.forName("gbk"));
		Enumeration<? extends ZipEntry> entries = zipOutFile.entries();
		while (entries.hasMoreElements()) {
			ZipEntry entry = entries.nextElement();
			if(entry.isDirectory()){
				File tempFile = new File(destDir.getAbsolutePath()+File.separator+entry.getName());
				if (!tempFile.exists()) {
					tempFile.mkdirs();
				}
			}else{
				File tempFile=new File(destDir.getAbsolutePath()+File.separator+entry.getName());
				checkParentDir(tempFile);
				FileOutputStream fileOutStream=new FileOutputStream(tempFile);
				BufferedOutputStream bufferOutStream = new BufferedOutputStream(fileOutStream);
				write(zipOutFile.getInputStream(entry),bufferOutStream);
				bufferOutStream.close();
				fileOutStream.close();
			}
		}
		zipOutFile.close();//记得关闭zip文件
	}
	
	/**
	 * @创建日期:2018年5月4日 下午4:37:41
	 * @作者: meter
	 * @描述:验证父目录是否存在,否则创建
	 * @Title: checkParentDir
	 * @param file
	 */
	private static void checkParentDir(File file){
		if(!file.getParentFile().exists()){
			file.getParentFile().mkdirs();
		}
	}
	
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值