java代碼實現tar包文件解壓

使用commons-compress-1.9.jar進行解壓 ,通過重寫UnTar方法把源路徑與目標路徑轉爲文件,通過TarArchiveInputStream讀入目標文件流,然後解壓到目的地址。

TarArchiveInputStream使用實例地址如下:

https://www.programcreek.com/java-api-examples/?class=org.apache.commons.compress.archivers.tar.TarArchiveInputStream&method=getNextTarEntry

package cn.com.agree.aesb.utils.pack.tar;

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 org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;

public class UnTar {
	
	private File sourceFile;

	private File targetFile;
	
	private TarArchiveInputStream  TarArchiveInputStream;
	
	
	private static final int BUFFER = 4096; 
	
	 /** 
     * 文件 解歸檔 
     *  
     * @param srcPath 
     *            源文件路徑 
     * @param destPath 
     *            目標文件路徑 
     * @throws Exception 
     */  
		  public UnTar(String sourceFilePath, String targetFilePath)
					throws FileNotFoundException {
			 sourceFile = new File(sourceFilePath);
				  
				if (!sourceFile.exists()) {
					throw new FileNotFoundException("file can not be found:"
							+ sourceFilePath);
				}
				if (sourceFile.isDirectory()) {
					throw new IllegalArgumentException("file can not be directory:"
							+ sourceFilePath);
				}
				targetFile = new File(targetFilePath);
				if (targetFile.isFile()) {
					throw new IllegalArgumentException("file should be directory:"
							+ targetFilePath);
				}
			}
		
		  
		  public void  unTar() throws IOException{
			  
				try {
					TarArchiveInputStream = new TarArchiveInputStream(  
				
		                new FileInputStream(sourceFile)); 
					
			  //buildDerectory(targetFile);
			  TarArchiveEntry entry = null;  
		        while ((entry = TarArchiveInputStream.getNextTarEntry()) != null) {  
					
		        	 String dir = targetFile.getPath() + File.separator + entry.getName();  
						
		        	  File dirFile = new File(dir);  
		        	  
		        	  // 文件檢查  
			            fileProber(dirFile);  
			  
			            if (entry.isDirectory()) {  
			                dirFile.mkdirs();  
			            } else {  
			                dearchiveFile(dirFile, TarArchiveInputStream);  
			            }
			            
		        }
		        	 
				} finally {
					if (TarArchiveInputStream != null) {
						TarArchiveInputStream.close();
					}
				}

		  }
		  
		  /** 
		     * 文件解歸檔 
		     *  
		     * @param destFile 
		     *            目標文件 
		     * @param tais 
		     *            TarArchiveInputStream 
		     * @throws Exception 
		     */  
		    private static void dearchiveFile(File destFile, TarArchiveInputStream tais)  
		            throws IOException {  
		    	
		    	
		        BufferedOutputStream bos = new BufferedOutputStream(  
		                new FileOutputStream(destFile));  
		  
		        int count;  
		        byte data[] = new byte[BUFFER];  
		        while ((count = tais.read(data, 0, BUFFER)) != -1) {  
		            bos.write(data, 0, count);  
		        }  
		  
		        bos.close();  
		    }  
		  
		
		
		 private static void fileProber(File dirFile) {  
	  
	        File parentFile = dirFile.getParentFile();  
	        if (!parentFile.exists()) {  
	  
	        	  // 遞歸尋找上級目錄 
	            fileProber(parentFile);  
	  
	            parentFile.mkdir();  
	        }  
	  
	    }  

	public static void main(String[] args)  {
		// TODO Auto-generated method stub
		
		UnTar untar;
		
		
		 try {
				untar = new UnTar("C:/Users/Desktop/test.tar","C:/Users/sgg/Desktop/test");
				untar.unTar();
				
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		 

	}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章