码迷,mamicode.com
首页 > 编程语言 > 详细

Java原始的压缩和解压

时间:2015-04-07 20:02:40      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:package   import   public   java   source   

package com.ahzc.test;

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

public class Compress {

	/** 
	  * 功能:把 sourceDir 目录下的所有文件进行 zip 格式的压缩,保存为指定 zip 文件
	  * @param sourceDir 如果是目录,eg:D:\\MyEclipse\\first\\testFile,则压缩目录下所有文件;
	  *      如果是文件,eg:D:\\MyEclipse\\first\\testFile\\aa.zip,则只压缩本文件
	  * @param zipFile 最后压缩的文件路径和名称,eg:D:\\MyEclipse\\first\\testFile\\aa.zip
	  */
	 public static File doZip(String sourceDir, String zipFilePath) 
	 throws IOException {
	  
	  File file = new File(sourceDir);
	  File zipFile = new File(zipFilePath);
	  ZipOutputStream zos = null;
	  try {
	   // 创建写出流操作
	   OutputStream os = new FileOutputStream(zipFile);
	   BufferedOutputStream bos = new BufferedOutputStream(os);
	   zos = new ZipOutputStream(bos);
	   
	   String basePath = null;
	   
	   // 获取目录
	   if(file.isDirectory()) {
	    basePath = file.getPath();
	   }else {
	    basePath = file.getParent();
	   }
	   
	   zipFile(file, basePath, zos);
	  }finally {
	   if(zos != null) {
	    zos.closeEntry();
	    zos.close();
	   }
	  }
	  
	  return zipFile;
	 }

	 /** 
	  * @param source 源文件
	  * @param basePath 
	  * @param zos 
	  */
	 private static void zipFile(File source, String basePath, ZipOutputStream zos) 
	 throws IOException {
	  File[] files = null;
	  if (source.isDirectory()) {
	   files = source.listFiles();
	  } else {
	   files = new File[1];
	   files[0] = source;
	  }
	  
	  InputStream is = null;
	  String pathName;
	  byte[] buf = new byte[1024];
	  int length = 0;
	  try{
	   for(File file : files) {//循环压缩文件夹下面的所有文件,知道zos.close的时候才算压缩完成
	    if(file.isDirectory()) {
	     pathName = file.getPath().substring(basePath.length() + 1) + "/";
	     zos.putNextEntry(new ZipEntry(pathName));
	     zipFile(file, basePath, zos);
	    }else {
	     pathName = file.getPath().substring(basePath.length() + 1);
	     is = new FileInputStream(file);
	     BufferedInputStream bis = new BufferedInputStream(is);
	     zos.putNextEntry(new ZipEntry(pathName));
	     while ((length = bis.read(buf)) > 0) {
	      zos.write(buf, 0, length);
	     }
	    }
	   }
	  }finally {
	   if(is != null) {
	    is.close();
	   }
	  }

	 }
	 
	 /**
		 * 解压到指定目录
		 * @param zipPath  压缩包目录
		 * @param descDir  生成文件保存目录
		 * 
		 */
		public static void unZipFiles(String zipPath,String descDir)throws IOException{
			unZipFiles(new File(zipPath), descDir);
		}
		/**
		 * 解压文件到指定目录
		 * @param zipFile
		 * @param descDir
		 * 
		 */
		@SuppressWarnings("rawtypes")
		public static void unZipFiles(File zipFile2,String descDir)throws IOException{
			 try {
		         //根据ZIP文件创建ZipFile对象
		         ZipFile zipFile = new ZipFile(zipFile2);
		            ZipEntry entry = null;
		            String entryName = null;
		            String targetFileName = null;
		            byte[] buffer = new byte[1024];
		            int bytes_read; 
		            //获取ZIP文件里所有的entry
		            Enumeration entrys = zipFile.entries();
		            //遍历所有entry
		            while (entrys.hasMoreElements()) {
		             entry = (ZipEntry)entrys.nextElement();
		             //获得entry的名字
		             entryName =  entry.getName();
		             targetFileName = descDir+File.separator+entryName;
		             if (entry.isDirectory()){
		              //  如果entry是一个目录,则创建目录
		              new File(targetFileName).mkdirs();
		              continue;
		             } else {
		              // 如果entry是一个文件,则创建父目录
		              new File(targetFileName).getParentFile().mkdirs();
		             }

		             //否则创建文件
		             File targetFile = new File(targetFileName);
		             System.out.println("创建文件:" + targetFile.getAbsolutePath());
		             //打开文件输出流
		             FileOutputStream os = new FileOutputStream(targetFile);
		             //从ZipFile对象中打开entry的输入流
		             InputStream  is = zipFile.getInputStream(entry);
		             while ((bytes_read = is.read(buffer)) != -1){
		              os.write(buffer, 0, bytes_read);
		             }
		             //关闭流
		             os.close( );
		             is.close( );
		            }
		            System.out.println("解压缩文件成功!");
		        } catch (IOException err) {
		            System.err.println("解压缩文件失败: " + err);
		        }
		}
}


本文出自 “breezewindlw” 博客,请务必保留此出处http://breezewindlw.blog.51cto.com/6504579/1629659

Java原始的压缩和解压

标签:package   import   public   java   source   

原文地址:http://breezewindlw.blog.51cto.com/6504579/1629659

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!