java压缩解压

阅读更多

最近看到一个有关用java压缩的例子,自己想总结一下,这个方法可以把一个压缩文件解压,列入文件名称为test.zip,不管test.zip里面有多少文件和文件夹都可以加压出来,也可以把解压后的文件压缩成test.zip代码如下:

package bean;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import javax.swing.text.html.parser.Entity;

public class FileCompressAndUnCompress {
	public static boolean fileUnCompress(String inputFilePath, String outputDirectory){
		boolean flag = true;    // 标示是不是在文件加压的时候出现问题
		ZipInputStream zis = null;    // zip输入流
		ZipFile zfile = null;    // 压缩文件
		try {
			zfile = new ZipFile(new File(inputFilePath));
			zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(inputFilePath)));
		} catch (FileNotFoundException e) {
			flag = false;
			e.printStackTrace();
		} catch (ZipException e) {
			flag = false;
			e.printStackTrace();
		} catch (IOException e) {
			flag = false;
			e.printStackTrace();
		}
		ZipEntry zipEntry = null;
		// 遍历zip文件的子文件
		try {
			while((zipEntry = zis.getNextEntry()) != null){
				// 判断从zip流中读出的是否为文件夹
				if(zipEntry.isDirectory()){
					String directorName = zipEntry.getName();
					File director = new File(outputDirectory + directorName);
					director.mkdir();
				}else{
					File file = new File(outputDirectory + File.separator + zipEntry.getName());
					// 新建文件
					file.createNewFile();
					// 得到文件的输入流
					InputStream is = zfile.getInputStream(zipEntry);
					// 文件的输出流
					FileOutputStream fos = new FileOutputStream(file);
					// 设置写文件的缓存
					byte [] byteBuffer=new byte[1024*8];
					int i = -1;
					while((i = is.read(byteBuffer)) != -1){
						fos.write(byteBuffer,0,i);
					}
					is.close();
					fos.close();
				}
			}		
			} catch (IOException e) {
			flag = false;
			e.printStackTrace();
		}
		return flag;
	}
	public static boolean fileCompress(File file,String basePath,ZipOutputStream zos){
		boolean flag = true;    // 判断是否压缩成功
	    // 判断文件是否为文件夹若使就放入到zip流中然后递归,不是就直接放入zip流中
		if(file.isDirectory()){
			// 遍历目录下的所有文件
			File [] files = file.listFiles();
			for(File f:files){
				try {
					zos.putNextEntry(new ZipEntry(basePath + File.separator));
				} catch (IOException e) {
					flag = false;
					e.printStackTrace();
				}
				basePath = basePath.length() == 0 ? "" : basePath + "/";
				// 递归操作
				fileCompress(f,basePath + f.getName(), zos);
			}
		}else{
			try {
				if(basePath.length()>0){
					zos.putNextEntry(new ZipEntry(basePath));
				}else{
					zos.putNextEntry(new ZipEntry(basePath+ file.getName()));
				}
				
				FileInputStream fis = new FileInputStream(file);
				// 设置写文件的缓存
				byte [] byteBuffer=new byte[1024*8];
				int i = -1;
				while((i = fis.read(byteBuffer)) != -1){
					zos.write(byteBuffer,0,i);
				}
				zos.closeEntry();
				fis.close();
			} catch (IOException e) {
				flag = false;
				e.printStackTrace();
			}
		}
		return flag;
	}
	public static void main(String[] args) throws Exception {
		ZipOutputStream zos =new ZipOutputStream(new FileOutputStream("D:\\test\\unzipApk\\temp.zip"));
		File zipFile = new File("D:\\test\\unzipApk\\temp.zip");
		zipFile.getParentFile().mkdirs();
		//fileUnCompress("D:\\test\\unzipApk\\temp.zip", "D:\\test\\unzipApk\\");
		File file = new File("D:\\test\\unzipApk\\temp");
		fileCompress(file,"temp",zos);
		zos.close();
	}

}

 

你可能感兴趣的:(java,压缩,解压,ZipInputStream,zipOutputStream)