Java中Zip进行多文件保存

本文介绍了使用Java实现文件的解压和压缩功能,包括查看压缩包内的文件、解压文件到指定目录以及压缩多个文件到一个压缩包的过程。
<pre name="code" class="java">package com.io.zip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class DeZipFile {

	public static void main(String[] args) throws Exception {
		String filepath = "F:\\z321.zip";
		String destpath = "F:\\dezip";
		// test1(filepath);
		test2(filepath, destpath);

	}

	/**
	 * 查看压缩包下面有哪些文件
	 * 
	 * @param filepath
	 * @throws IOException
	 */
	private static void test1(String filepath) throws IOException {
		ZipFile zf = new ZipFile(filepath);
		Enumeration enums = zf.entries();
		while (enums.hasMoreElements()) {
			ZipEntry entry = (ZipEntry) enums.nextElement();
			System.out.println("文件:" + entry.getName());
		}
	}

	/**
	 * 解压filepath下面的文件到destpath路径下面
	 * 
	 * @param filepath
	 * @param destpath
	 * @throws IOException
	 */
	private static void test2(String filepath, String destpath)
			throws IOException {
		File d = new File(destpath);
		if(!d.exists()){
			d.mkdirs();
		}
		
		FileInputStream fis = new FileInputStream(filepath);
		CheckedInputStream cis = new CheckedInputStream(fis, new Adler32());
		ZipInputStream zis = new ZipInputStream(cis);

		FileOutputStream out;
		
		ZipEntry ze;
		while ((ze = zis.getNextEntry()) != null) {
			out = new FileOutputStream(destpath + File.separator + ze.getName());
			int x;
			while ((x = zis.read()) != -1) {
				out.write(x);
			}
			out.close();
		}
		
		zis.close();
	}

}

package com.io.zip;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FilenameFilter;import java.io.IOException;import java.nio.charset.Charset;import java.util.regex.Pattern;import java.util.zip.Adler32;import java.util.zip.CRC32;import java.util.zip.CheckedOutputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class ZipManyFile {public static void main(String[] args) throws Exception {// String file1 = "E:\\cookie.java",file2 = "F:\\core.log",file3 = "E:\\wold.png";// zipfiles(file1,file2,file3);//压缩整个文件下的文件String filedir = "F:\\testzip";zipfiles2(filedir);}<span style="white-space:pre"> </span>

	private static void zipfiles(String... files) throws IOException {
		FileOutputStream fos = new FileOutputStream("F:\\z321.zip");
		CheckedOutputStream cos = new CheckedOutputStream(fos, new CRC32());
		ZipOutputStream zos = new ZipOutputStream(cos);
		zos.setComment("z321test测试");

		for (String fileName : files) {
			File file = new File(fileName);
			FileInputStream fis = new FileInputStream(file);
			zos.putNextEntry(new ZipEntry(file.getName()));
			int x;
			while ((x = fis.read()) != -1) {
				zos.write(x);
			}
			fis.close();
		}

		zos.close();
		System.out.println("checksum: " + cos.getChecksum().getValue());

	}

	private static void zipfiles2(String filedir) throws IOException {
		FileOutputStream fos = new FileOutputStream("F:\\some3333.zip");
		CheckedOutputStream cos = new CheckedOutputStream(fos, new Adler32());
		ZipOutputStream zos = new ZipOutputStream(cos, Charset.forName("gb2312"));
		zos.setComment("测试压缩一个文件下的所有文件haha");
		BufferedOutputStream bos = new BufferedOutputStream(zos);
		
		final String reg = "[hH].*";
		File file = new File(filedir);
		File[] fs = file.listFiles(new FilenameFilter() {
			private Pattern pattern = Pattern.compile(reg);
			
			@Override
			public boolean accept(File dir, String name) {
				return pattern.matcher(name).matches();
			}
		});
		for(File f : fs){
//			FileInputStream fis = new FileInputStream(f);
			BufferedReader br = new BufferedReader(new FileReader(f));
			zos.putNextEntry(new ZipEntry(file.getName()+"\\"+f.getName()));
			
			
			int x;
			while((x=br.read())!=-1){
				bos.write(x);
			}
			br.close();
		}
		
		bos.close();
		System.out.println("checksum: "+cos.getChecksum().getValue());

	}

}
</pre><pre name="code" class="java">

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值