JAVA压缩和解压缩Zip文件

ZIP是一种很常见的压缩形式,在java中要实现ZIP的压缩主要用到的是java.util.zip这个包里面的类。主要有ZipFile、 ZipOutputStream、ZipInputStream和ZipEntry。ZipOutputStream是用来压缩文件 的,ZipInputStream和ZipFile是用来解压缩文件的,在压缩和解压缩的过程中,ZipEntry都会用到。在java的Zip压缩文件 中,每一个子文件都是一个ZipEntry对象。以下是在http://www.open-open.com/bbs/view/1320568080343 基础上修改而来的

压缩:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipOutputStreamTest {

	public static void main(String args[]) throws IOException {
		//test1();
		test2();
	}
	
	public static void test1() throws IOException {
		ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("D:\\testZip.zip"));
		//实例化一个名称为ab.txt的ZipEntry对象
		ZipEntry entry = new ZipEntry("abcc.txt");
		//设置注释
		zos.setComment("zip测试for单个文件");
		//把生成的ZipEntry对象加入到压缩文件中,而之后往压缩文件中写入的内容都会放在这个ZipEntry对象里面
		zos.putNextEntry(entry);
		InputStream is = new FileInputStream("D:\\ab.txt");
		int len = 0;
		while ((len = is.read()) != -1)
			zos.write(len);
		is.close();
		zos.close();
	}
	
	public static void test2() throws IOException {
		File inFile = new File("D:\\test");
		ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("D:\\test.zip"));
		zos.setComment("多文件处理");
		zipFile(inFile, zos, "");
		zos.close();
	}
	
	public static void zipFile(File inFile, ZipOutputStream zos, String dir) throws IOException {
		if (inFile.isDirectory()) {
			File[] files = inFile.listFiles();
			for (File file:files)
				zipFile(file, zos, dir+inFile.getName() + "\\"  );
		} else {
			String entryName = null;
			if (!"".equals(dir))
				entryName = dir + inFile.getName();
			else
				entryName = inFile.getName();
			ZipEntry entry = new ZipEntry(entryName);
			zos.putNextEntry(entry);
			BufferedInputStream is = new BufferedInputStream(new FileInputStream(inFile));//文件缓冲区
			 byte[] datas = new byte[2048];
			int len = 0;
			while ((len = is.read(datas)) != -1)
				zos.write(datas,0,len);
			is.close();
		}

	}
	
}

  解压缩:

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.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class ZipInputStreamTest {

	public static void main(String args[]) throws IOException {
		File file = new File("D:\\test.zip");//压缩文件
		ZipFile zipFile = new ZipFile(file);//实例化ZipFile,每一个zip压缩文件都可以表示为一个ZipFile
		//实例化一个Zip压缩文件的ZipInputStream对象,可以利用该类的getNextEntry()方法依次拿到每一个ZipEntry对象
		ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file));
		ZipEntry zipEntry = null;
		while ((zipEntry = zipInputStream.getNextEntry()) != null) {
			String fileName = zipEntry.getName();
			File temp = new File("D:\\unpackTest\\" + fileName);
			if (! temp.getParentFile().exists())
				temp.getParentFile().mkdirs();
            BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(temp));//文件缓冲区
			//OutputStream os = new FileOutputStream(temp);
			//通过ZipFile的getInputStream方法拿到具体的ZipEntry的输入流
			InputStream is = zipFile.getInputStream(zipEntry);
            byte[] datas = new byte[2048];
			int len = 0;
			while ((len = is.read(datas)) != -1)
				os.write(datas,0,len);
			os.close();
			is.close();
		}
		zipInputStream.close();
	}
	
}

  

posted @ 2013-03-04 15:18  十年半山  阅读(3037)  评论(0编辑  收藏  举报