java zip 解压缩实战

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

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

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

import org.junit.Test;


public class TestUnzip {

	//方法1:
	public void unZip(String zipfile) throws IOException {
		//检查是否是zip文件,并判断文件是否存在
		checkFileName(zipfile);
		long startTime = System.currentTimeMillis();
		File zfile = new File(zipfile);
		//获取待解压文件的父路径
		String Parent = zfile.getParent() + "/";
		FileInputStream fis = new FileInputStream(zfile);
		Charset charset = Charset.forName("GBK");//默认UTF-8
		// CheckedInputStream cis = new CheckedInputStream(fis,new CRC32());
		ZipInputStream zis = new ZipInputStream(fis, charset);// 输入源zip路径
		ZipEntry entry = null;
		BufferedOutputStream bos = null;
		while ((entry = zis.getNextEntry()) != null) {
			if (entry.isDirectory()) {
				File filePath = new File(Parent + entry.getName());
				//如果目录不存在,则创建
				if (!filePath.exists()) {
					filePath.mkdirs();
				}
			} else {
				FileOutputStream fos = new FileOutputStream(Parent + entry.getName());
				bos = new BufferedOutputStream(fos);
				byte buf[] = new byte[1024];
				int len;
				while ((len = zis.read(buf)) != -1) {
					bos.write(buf, 0, len);
				}
				zis.closeEntry();
				//关闭的时候会刷新
				bos.close();
			}
		}
		zis.close();
		long endTime = System.currentTimeMillis();
		System.out.println("解压完成!所需时间为:" + (endTime - startTime) + "ms");
		// System.out.println("校验和:"+cis.getChecksum().getValue());
	}

	private void checkFileName(String name) {
		//文件是否存在
		if (!new File(name).exists()) {
			System.err.println("要解压的文件不存在!");
			System.exit(1);
		}
		// 判断是否是zip文件
		int index = name.lastIndexOf(".");
		String str = name.substring(index + 1);
		if (!"zip".equalsIgnoreCase(str)) {
			System.err.println("不是zip文件,无法解压!");
			System.exit(1);
		}
	}

	/*****************************************************************************************/

	public void doUnZip(String zipfile) throws IOException {
		checkFileName(zipfile);
		long startTime = System.currentTimeMillis();
		// 获取待解压文件的父路径
		File zfile = new File(zipfile);
		String Parent = zfile.getParent() + "/";
		// 设置,默认是UTF-8
		Charset charset = Charset.forName("GBK");
		ZipFile zip = new ZipFile(zipfile, charset);
		ZipEntry entry = null;
		//封装解压后的路径
		BufferedOutputStream bos = null;
		//封装待解压文件路径
		BufferedInputStream bis = null;
		Enumeration<ZipEntry> enums = (Enumeration<ZipEntry>) zip.entries();
		while (enums.hasMoreElements()) {
			entry = enums.nextElement();
			if (entry.isDirectory()) {
				File filePath = new File(Parent + entry.getName());
				// 如果目录不存在,则创建
				if (!filePath.exists()) {
					filePath.mkdirs();
				}
			} else {
				bos = new BufferedOutputStream(new FileOutputStream(Parent + entry.getName()));
				//获取条目流
				bis = new BufferedInputStream(zip.getInputStream(entry));
				byte buf[] = new byte[1024];
				int len;
				while ((len = bis.read(buf)) != -1) {
					bos.write(buf, 0, len);
				}

				bos.close();
			}
		}
		bis.close();
		zip.close();
		System.out.println("解压后的路径是:" + Parent);
		long endTime = System.currentTimeMillis();
		System.out.println("解压成功,所需时间为:" + (endTime - startTime) + "ms");
	}


	//解压到当前文件夹的测试     直接解压到文件夹的,默认解压到与压缩文件同路径 
	@Test
	public void testunzipcurrentfiledir() throws IOException {
		TestUnzip test = new TestUnzip();
		test.unZip("C:\\Users\\Administrator\\Desktop\\开发资料\\test.zip");
	}

	//利用zipFile解压,方法跟ZipInputStream类似,都是连续取到Entry,然后再用Entry判断,听说zipFile内建了缓冲流,所以对于同一个文件解压多次效率比ZipInputStream高些
	@Test
	public void testunzipByzipfile() throws IOException {
		TestUnzip test = new TestUnzip();
		//test.unZip("C:\\Users\\Administrator\\Desktop\\开发资料\\test.zip");
		
		test.doUnZip("C:\\Users\\Administrator\\Desktop\\开发资料\\test.zip");
		
	}

}

猜你喜欢

转载自blog.csdn.net/qq_30336433/article/details/81026929