Javaオリジナルの圧縮と解凍


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);
		        }
		}
}
/**
	 *  zip          
	 * @param srcPath     
	 * @param zipPath zip     
	 * @return boolean ret       
	 * */
	public static boolean swapZipToDirectory(String srcPath, String zipPath)
	{
		boolean ret = true;
		//     zipPath      SwapPath  
		if (!new File(zipPath).exists()){
			ret = false;
			return ret;
		}
		
		if (!new File(srcPath).exists()){
			(new File(srcPath)).mkdirs();
		}
		
		Project proj = new Project();
		Expand expand = new Expand();
		expand.setProject(proj);
		expand.setTaskType("UNZIP");
		expand.setTaskName("UNZIP");
		expand.setEncoding("GBK");

		expand.setSrc(new File(zipPath));
		expand.setDest(new File(srcPath));
		expand.execute();
		
		return ret;
	}
	
	
	
/**
	 *         zip   
	 * @param srcPath     
	 * @param zipPath zip     
	 * @return boolean ret       
	 * */
	public static boolean swapDirectoryToZip(String srcPath, String zipPath)
	{
		//    xmldirPath      
		boolean ret = true;
		File objFile = new File(srcPath);
		if (!objFile.exists()){
			ret = false;
			return ret;
		}
		Project objProject = new Project();
		FileSet objFileSet = new FileSet();
		objFileSet.setProject(objProject);
		//          
		if (objFile.isDirectory()) {
			objFileSet.setDir(objFile);
		} else {
			objFileSet.setFile(objFile);
		}

		Zip objZip = new Zip();
		objZip.setProject(objProject);
		objZip.setDestFile(new File(zipPath));
		objZip.addFileset(objFileSet);
		objZip.setEncoding("GBK");
		objZip.execute();
		
		return ret;
	}