자바 파일 압축 및 압축 풀기 (4)

14306 단어
package com.cn;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

//    :
//1   file.isFile() file.isDirectory()      .
//        Directory   file,file    Directory
//    :file Directory    .   file Directory     .
//     1            .

//2     :
//  zip()        zos.             

//    :
//1   zip unzip             !!!
//        directory,              ,        .
//         ,    directory,               .

//2  JAVA            ZipEntry  
//                       (            )
//               new()  ZipEntry

//3     zipEntry          .        
//             entrys.hasMoreElements()   
//      zipEntry
	
public class TestZipAndUnZip {
   public static void main(String[] args) throws Exception {
	   TestZipAndUnZip test=new TestZipAndUnZip();
	   //         zip
//	   test.zip("E:\\", "aa\\1.txt", "E:\\cc1.zip");
	   
	   //         zip
	   //test.zip("E:\\aa", "bb\\", "E:\\zz.zip");//right
//	  
	  // test.zip("E:\\", "aa", "E:\\zz678910.zip");//right
//
//	   //    zip  
//	   test.unZipFile("E:\\zz.zip", "E:\\zzzz");
	   
	   /////////////////////    ///////////////////////////////
	   //         
//	   test.zip2("E:\\aa\\1.txt","E:\\aaaaaaa\\aa1234.zip");
//	   test.unZipFile2("E:\\aaaaaaa\\aa1234.zip", "E:\\aaaaaaa\\aaxixihaha");
	   
	   //         
	   test.zip2("D:\\developmentTools","E:\\20.zip");
	   test.unZipFile2("E:\\20.zip", "E:\\555aaxx99xxx");
	   
	   /////////////////////    //////////////////////////////
   }
   
/**
 * @param willZipDirPath         (  )    
 * @param willZipFileName        (  )   
 * @param toFilePath             (  ) 
 */
	public void zip(String willZipDirPath, String willZipFileName, String zipedFileName) {
		System.out.println("…………………   zip()  …………………………");
		if (willZipDirPath == null) {
			return;
		}
		File willZipDir = new File(willZipDirPath);
		if (!willZipDir.exists() || !willZipDir.isDirectory()) {
			return;
		}
		//         
		String willZipDirAbsolutePath = willZipDir.getAbsolutePath();
		System.out.println("willZipDir.getAbsolutePath()="+willZipDirAbsolutePath);
		//      
		File zipedFile = new File(zipedFileName);
		try {
			//              ZipOutputStream
			//   zos             .     
			//           ?
			//     ZipEntry!!!
			// fileToZip()        ZipEntry   !!
			ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipedFile));
			if (willZipFileName.equals("*")) {
				//     *               
				//    dirToZip()
				dirToZip(willZipDirAbsolutePath, willZipDir, zos);
			} else {
				//       
				File willZipFile = new File(willZipDirPath, willZipFileName);
				System.out.println("ccccccccccc name="+willZipFile.getName());
				System.out.println("ccccccccccc getAbsolutePath="+willZipFile.getAbsolutePath());
				
				if (willZipFile.isFile()) {
					System.out.println("…………………         ………………………");
					fileToZip(willZipDirPath, willZipFile, zos);
					System.out.println("…………………         ………………………");
				}
				if (willZipFile.isDirectory()) {
					System.out.println("…………………         ………………………");
					dirToZip(willZipDirPath, willZipFile, zos);
					System.out.println("…………………         ………………………");
				}
				//    !!!
				zos.close();
				System.out.println("…………………   zip()  …………………………");
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

	}
	
   /**
 * @param dirPath               
 * @param willZipFile          
 * @param zos             
 */
public void fileToZip(String dirPath, File willZipFile,ZipOutputStream zos){
	   FileInputStream fis=null;
	   ZipEntry zipEntry=null;
	   byte [] buffer=new byte[1024*8];
	   int len=0;
	   if (willZipFile.isFile()) {
		try {
			fis=new FileInputStream(willZipFile);
			zipEntry=new ZipEntry(getEntryName(dirPath, willZipFile));
			zos.putNextEntry(zipEntry);
			System.out.println("…………………   fileToZip()  …………………………");
			System.out.println("zipEntry.getName="+zipEntry.getName());
			System.out.println("zipEntry.isDirectory="+zipEntry.isDirectory());
			System.out.println("zipEntry.getSize="+zipEntry.getSize());
			System.out.println("zipEntry.getTime="+zipEntry.getTime());
			System.out.println("zipEntry.getComment="+zipEntry.getComment());
			System.out.println("…………………   fileToZip()  …………………………");
			while((len=fis.read(buffer))!=-1){
				zos.write(buffer, 0, len);
			}
			zos.closeEntry();
			fis.close();
		} catch (Exception e) {
		}
	}
  }

	/**
	 * @param dirPath                 
	 * @param willZipDir       
	 * @param zos            
	 */
	public void dirToZip(String dirPath, File willZipDir, ZipOutputStream zos) {
		if (willZipDir.isDirectory()) {
			File[] files = willZipDir.listFiles();
			//  -->        
            if (files.length==0) {
				ZipEntry zipEntry=new ZipEntry(getEntryName(dirPath, willZipDir));
				try {
					zos.putNextEntry(zipEntry);
					zos.closeEntry();
				} catch (Exception e) {
					e.printStackTrace();
				}
				return;
			}
            //  -->          
            for (int i = 0; i < files.length; i++) {
				File file = files[i];
				//    ,    fileToZip()
				if (file.isFile()) {
					System.out.println("xxxxxxxxxx    fileToZip()  xxxxxxxxxx");
					fileToZip(dirPath, file, zos);
					System.out.println("xxxxxxxxxx  fileToZip()    xxxxxxxxxx");
				}
				//    ,    dirToZip()
				if (file.isDirectory()) {
					System.out.println("xxxxxxxxxx    dirToZip()  xxxxxxxxxx");
					dirToZip(dirPath, file, zos);
					System.out.println("xxxxxxxxxx  dirToZip()    xxxxxxxxxx");
				}
			}
		}
	}
   
	/**
	 * @param dirPath                
	 * @param willZipFile         
	 * @return            
	 */
	//             (            )
	//     zipEntry           .        
	public String getEntryName(String dirPath, File willZipFile) {
		if (!dirPath.endsWith(File.separator)) {
			dirPath += File.separator;
		}
		String willZipFilePath=willZipFile.getAbsolutePath();
		if (willZipFile.isDirectory()) {
			willZipFilePath+="/";
		}
		int index=willZipFilePath.indexOf(dirPath);
		
		System.out.println("xx    entryName="+ willZipFilePath.substring(index+dirPath.length()));
		return willZipFilePath.substring(index+dirPath.length());
	}
	
	
	
	
	/**
	 * @param zipedFileName    zip  
	 * @param unzipDirPath              
	 * @throws IOException 
	 */
	public void unZipFile(String zipedFileName,String unzipDirPath) throws Exception{
	
		if (!unzipDirPath.endsWith(File.separator)) {
			unzipDirPath+=File.separator;
		}
		
		try {
			ZipFile zipedFile=new ZipFile(zipedFileName);
			ZipEntry zipEntry=null;
			String entryName=null;
			String unzipedFileName=null;
			Enumeration entrys=zipedFile.entries();
			byte [] buffer=new byte[1024*8];
			int len=0;
			while (entrys.hasMoreElements()) {
				zipEntry=(ZipEntry) entrys.nextElement();
				entryName=zipEntry.getName();
				unzipedFileName=unzipDirPath+entryName;
				System.out.println("…………………   unZipFile()  …………………………");
				System.out.println("zipedFileName="+zipedFileName);
				System.out.println("unzipDirPath="+unzipDirPath);
				System.out.println("entryName="+entryName);
				System.out.println("unzipedFileName="+unzipedFileName);
				System.out.println("…………………   unZipFile()  …………………………");
				if (zipEntry.isDirectory()) {
					//       
					new File(unzipedFileName).mkdirs();
				} else {
					//       .                  .
                    new File(unzipedFileName).getParentFile().mkdirs();
				}
				File unzipedFile=new File(unzipedFileName);
				FileOutputStream fos=new FileOutputStream(unzipedFile);
				InputStream is=zipedFile.getInputStream(zipEntry);
				while ((len=is.read(buffer))!=-1) {
					fos.write(buffer, 0, len);
				}
				fos.close();
				is.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/////////////////////////////////////////////////////////////////////////////////////
	/**
	 *                
	 * @param willZipPath           
	 * @param zipedPath            
	 */
	public void zip2(String willZipPath,String zipedPath){
		try {
			File willZipedFile=new File(willZipPath);
			File zipedFile=new File(zipedPath);
			
			ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(zipedFile));
			if (willZipedFile.isFile()) {
				fileToZip2(willZipPath,willZipedFile, zos);
			}
			if (willZipedFile.isDirectory()) {
				dirToZip2(willZipPath,willZipedFile, zos);
			}
			//          
			zos.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
		
	}
	
	/**
	 * @param rawPath     
	 * @param willZipedFile
	 * @param zos
	 */
	 //test.zip2("E:\\aa\\1.txt","E:\\aaaaaaa\\aa1234.zip");
	public void fileToZip2(String rawPath,File willZipedFile,ZipOutputStream zos){
		try {
			//        ab.txt ZipEntry  
			File file=new File(rawPath);
			ZipEntry entry = new ZipEntry(getEntryName2(rawPath, file));
			//    ZipEntry          
			//                    ZipEntry    
			zos.putNextEntry(entry);
			InputStream is = new FileInputStream(rawPath);
			int len = 0;
			while ((len = is.read()) != -1){
				zos.write(len);
			}
			is.close();
			//      !!
			//zos.close();

		} catch (Exception e) {
		}

	}
	
	/**
	 * @param rawPath            
	 * @param zipedFile         
	 * @param zos
	 */
	public void dirToZip2(String rawPath,File zipedFile, ZipOutputStream zos) {
			if (zipedFile.isDirectory()) {
				File[] files = zipedFile.listFiles();
				//  -->        
//	            if (files.length==0) {
//					//ZipEntry zipEntry=new ZipEntry(getEntryName(dirPath, willZipDir));
//					ZipEntry zipEntry=new ZipEntry("");
//					try {
//						zos.putNextEntry(zipEntry);
//						zos.closeEntry();
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//					return;
//				}
	            for (int i = 0; i < files.length; i++) {
					File file = files[i];
					//    ,    fileToZip()
					if (file.isFile()) {
						fileToZip2(file.getAbsolutePath(),file, zos);
					}
					//    ,    dirToZip()
					if (file.isDirectory()) {
						dirToZip2(file.getAbsolutePath(),file, zos);
					}
				}
			}
		}
	
	
	/**
	 * @param rawPath                  
	 * @param file            
	 * @return         entryName
	 */
	public String getEntryName2(String rawPath,File file){//file rawPath   file
		try {
			String rawDir=rawPath.substring(3);
			int rawDirIndex=file.getAbsolutePath().indexOf(rawDir);
			String entryName=file.getAbsolutePath().substring(rawDirIndex);
			return entryName;
		} catch (Exception e) {
		}
		return null;
	}
	
	/**
	 * @param zipedFilePath          
	 * @param unzipPath              
	 */
	public void unZipFile2(String zipedFilePath, String unzipPath){
		try {
			//          
			File unzipFile=new File(unzipPath);
			if (!unzipFile.exists()) {
				unzipFile.mkdirs();
			}
			
			File zipedFile = new File(zipedFilePath);
			File perUnzipedFile = null;
			//    ZipFile         !!!!
			// ZipFile zipFile = new ZipFile(file) ;
			ZipInputStream zis = new ZipInputStream(new FileInputStream(zipedFile));
			FileOutputStream fos = null; 
			ZipEntry entry = null; 
			
			while ((entry = zis.getNextEntry()) != null) { //         
				perUnzipedFile = new File(unzipPath+File.separator+entry.getName());
				if (!perUnzipedFile.getParentFile().exists()) { 
					perUnzipedFile.getParentFile().mkdirs(); //      
				}
				
				if (!perUnzipedFile.exists()) { //           
					perUnzipedFile.createNewFile(); //     
				}
				//            
				fos = new FileOutputStream(perUnzipedFile); 
				int len = 0;
				while ((len = zis.read()) != -1) {
					fos.write(len);
				}
			}
			zis.close(); 
			fos.close(); 
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
	

}

좋은 웹페이지 즐겨찾기