java .tar.gz文件读写_Java解压tar.gz文件,文件中的文件路径乱码

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

问题补充:如XXX.tar.gz文件中的目录是【aa/中文/中文_2014_12_18_18:54:30.txt】,请问在Java里面该如何解压该文件,求大神帮忙,先谢了!!!

/**

* 解压tar.gz 文件

* @param file 要解压的tar.gz文件对象

* @param outputDir 要解压到某个指定的目录下

* @throws UnsupportedEncodingException

* @throws IOException

*/

public void unTarGz(String fileNamePath, String outputDir) throws UnsupportedEncodingException {

File file = new File(fileNamePath);

TarInputStream tarIn = null;

try {

tarIn = new TarInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))), 8192);

createDirectory(outputDir, null);// 创建输出目录

TarEntry entry = null;

while ((entry = tarIn.getNextEntry()) != null) {

if (entry.isDirectory()) {// 是目录

createDirectory(outputDir, entry.getName());// 创建空目录

} else {// 是文件

File tmpFile = new File(outputDir + "/" + entry.getName());

createDirectory(tmpFile.getParent() + "/", null);// 创建输出目录

OutputStream out = null;

try {

out = new FileOutputStream(tmpFile);

int length = 0;

byte[] b = new byte[2048];

while ((length = tarIn.read(b)) != -1) {

out.write(b, 0, length);

}

} catch (IOException ex) {

throw ex;

} finally {

if (out != null) {

out.close();

}

}

}

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (tarIn != null) {

tarIn.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 构建目录

* @param outputDir

* @param subDir

*/

public void createDirectory(String outputDir, String subDir) {

File file = new File(outputDir);

if (!(subDir == null || subDir.trim().equals(""))) {// 子目录不为空

file = new File(outputDir + "/" + subDir);

}

if (!file.exists()) {

file.mkdirs();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值