java代码实现tar包文件解压

2022-06-21 00:00:00 代码 文件 解压

使用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();
			}
		 

	}

}

 

    原文作者:上掊
    原文地址: https://blog.csdn.net/qq_28825857/article/details/86616566
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章