java zip 压缩 实战

参考: https://www.jb51.net/article/125810.htm

           https://docs.oracle.com/javase/7/docs/api/java/util/zip/package-summary.html


使用的是JDK1.7 

     package TestZip;

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.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.junit.Test;

public class TestZIP {

	public void dozip(String srcfile, String zipfile) throws IOException {
		String temp = "";
		File src = new File(srcfile);
		File zipFile = new File(zipfile);
		//判断要压缩的文件存不存在
		if (!src.exists()) {
			System.err.println("要压缩的文件不存在!");
			System.exit(1);
		}
		//如果说压缩路径不存在,则创建
		if (!zipFile.getParentFile().exists()) {
			zipFile.getParentFile().mkdirs();
			//   System.out.println("创建ok");
		}
		// 封装压缩的路径
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(zipfile));
		//这里可以加入校验
		//CheckedOutputStream cos = new CheckedOutputStream(bos,new CRC32());  
		//还可以设置压缩格式,默认UTF-8
		Charset charset = Charset.forName("GBK");
		ZipOutputStream zos = new ZipOutputStream(bos, charset);
		zip(src, zos, temp);
		//关闭流
		zos.flush();
		zos.close();
		System.out.println("压缩完成!");
		System.out.println("压缩文件的位置是:" + zipfile);
		// System.out.println("检验和:"+cos.getChecksum().getValue());
	}

	private void zip(File file, ZipOutputStream zos, String temp) throws IOException {
		
		// 如果不加"/"将会作为文件处理,空文件夹不需要读写操作
		if (file.isDirectory()) {
			String str = temp + file.getName() + "/";
			zos.putNextEntry(new ZipEntry(str));
			File[] files = file.listFiles();
			for (File file2 : files) {
				zip(file2, zos, str);
			}
		} else {
			// System.out.println("当前文件的父路径:"+temp);
			ZipFile(file, zos, temp);
		}
	}

	private void ZipFile(File srcfile, ZipOutputStream zos, String temp) throws IOException {
		// 默认的等级压缩-1
		// zos.setLevel(xxx);
		// 封装待压缩文件
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcfile));
		zos.putNextEntry(new ZipEntry(temp + srcfile.getName()));

		byte buf[] = new byte[1024];
		int len;
		while ((len = bis.read(buf)) != -1) {
			zos.write(buf, 0, len);
		}
		//按标准需要关闭当前条目,不写也行
		zos.closeEntry();
		bis.close();
	}
	
	//压缩多个文件到压缩文件中,不包含外部的文件夹
	private void ZipBatchFile(String srcfile, String zipfile ) throws IOException{
		
		File src = new File(srcfile);
		File zipFile = new File(zipfile);
		//判断要压缩的文件存不存在
		if (!src.exists()) {
			System.err.println("要压缩的文件不存在!");
			System.exit(1);
		}
		//如果说压缩路径不存在,则创建
		if (!zipFile.getParentFile().exists()) {
			zipFile.getParentFile().mkdirs();
			//   System.out.println("创建ok");
		}
		
		// 封装压缩的路径
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(zipfile));
		
		
		//还可以设置压缩格式,默认UTF-8
		Charset charset = Charset.forName("GBK");
		ZipOutputStream zos = new ZipOutputStream(bos, charset);
		
		
		// 默认的等级压缩-1     level - the compression level (0-9)  等级9的时候压缩得到的压缩文件最小
		 zos.setLevel(-1);
		 
		if (src.isDirectory()) {
			IfisDirectory(src, zos);
			zos.close();
			
		} else {
			// System.out.println("当前文件的父路径:"+temp);
			ZipFile(src, zos, "");
		}
	}
	
	public void IfisDirectory(File src,ZipOutputStream zos) throws IOException{
		
		File[] files = src.listFiles();
		for (File file2 : files) {
			//如果是文件夹
			if (file2.isDirectory()) {
				String str = "" + file2.getName() + "/";
				zos.putNextEntry(new ZipEntry(str));
				File[] files2 = file2.listFiles();
				for (File file3 : files2) {
					zip(file3, zos, str);
				}
				continue;
			}
			String fileName=file2.getName();

			// 封装待压缩文件
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file2));
			zos.putNextEntry(new ZipEntry(fileName));
			
			byte buf[] = new byte[1024*6];
			int len;
			while ((len = bis.read(buf)) != -1) {
				zos.write(buf, 0, len);
			}
			
			
			bis.close();
		}
	}

	//测试单个文件的压缩,
	@Test
	public void testCompress() {
		TestZIP test = new TestZIP();
		try {
			//当传入的是一个文件夹,会连同文件夹一起压缩到压缩包里面
			test.dozip("C:\\Users\\Administrator\\Desktop\\测试", "C:\\Users\\Administrator\\Desktop\\test.rar");

			//单个文件会正常 的压缩
			test.dozip("C:\\Users\\Administrator\\Desktop\\text.txt", "C:\\Users\\Administrator\\Desktop\\test.zip");
			
		} catch (IOException e) {

			e.printStackTrace();
		}

	}
	
	
	//测试压缩文件夹下所有文件不包含文件夹本身 
	@Test
	public void testBatchFileCompress() {
		TestZIP test = new TestZIP();
		try {
			test.ZipBatchFile("C:\\Users\\Administrator\\Desktop\\开发资料\\知识", "C:\\Users\\Administrator\\Desktop\\testBatchFile_1.zip");
		} catch (IOException e) {
            	       e.printStackTrace();
		}

	}

}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值