一、tar包解压代码:
说明:额外需要的jar: javatar-2.5.jar和jtar-1.1.jar,下载链接
http://pan.baidu.com/s/1dE2XPPR
http://pan.baidu.com/s/1geJiLZP
package tartofile;
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 org.xeustechnologies.jtar.TarEntry;
import org.xeustechnologies.jtar.TarInputStream;
public class TarToFile {
public void uncompress(String sourceTarPath,String FilePath) {
InputStream inputstream = null;
OutputStream outputstream = null;
TarInputStream zis = null;
File tempFile = null;
try {
//读取压缩包的内容
File file = new File(sourceTarPath);
inputstream = new FileInputStream(file);
zis = new TarInputStream(inputstream);
//tarEntry:每一个文件对应一个tarEntry
TarEntry tarEntry = null;
while ((tarEntry = zis.getNextEntry()) != null) {
tempFile = new File(FilePath+"//"+ tarEntry.getName());
//如果解压的当前路径不存在,就创建
tempFile.createNewFile();
outputstream = new FileOutputStream(tempFile);
//定义一个缓冲池,缓冲池的大小,决定着每次读取的数据量大小,可以根据实际要处理的数据量大小和服务器的自身的配置,对其进行修改
byte[] buffer = new byte[1024 * 50];
while (true) {
int readsize = zis.read(buffer);
outputstream.write(buffer);
if (readsize < 1024 * 50) {
break;
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputstream.flush();
inputstream.close();
zis.close();
outputstream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
二、测试:
package tartofile;
public class TestTarMain {
public static void main(String[] args) {
TarToFile tarToFile = new TarToFile();
//待解压tar包名称所在目录位置
String sourceTarPath="D://test//zipRes//res//test.tar";
//解压出来的文件存放位置
String FilePath="D://test//zipRes//002";
tarToFile.uncompress(sourceTarPath, FilePath);
System.out.println("解压完成...");
}
}
三、测试结果: