使用javatar解压tar包等

1、下载javatar-2.5.jar

资源:http://pan.baidu.com/s/1pJOQ6wz

 

2、使用方法:

 

/**
	 * 解压tar包
	 * 
	 * @author: 
	 * @createTime: 2015年9月23日 下午5:41:56
	 * @history:
	 * @param filename
	 *            tar文件
	 * @param directory
	 *            解压目录
	 * @return
	 */
	private static boolean extTarFileList(String filename, String directory) {
		boolean flag = false;
		OutputStream out = null;
		try {
			TarInputStream in = new TarInputStream(new FileInputStream(new File(filename)));
			TarEntry entry = null;
			while ((entry = in.getNextEntry()) != null) {
				if (entry.isDirectory()) {
					continue;
				}
				System.out.println(entry.getName());
				File outfile = new File(directory + entry.getName());
				new File(outfile.getParent()).mkdirs();
				out = new BufferedOutputStream(new FileOutputStream(outfile));
				int x = 0;
				while ((x = in.read()) != -1) {
					out.write(x);
				}
				out.close();
			}
			in.close();
			flag = true;
		} catch (IOException ioe) {
			ioe.printStackTrace();
			flag = false;
		}
		return flag;
	}


3、测试

 

 

extTarFileList("D:/Test/ibs/20150925.tar", "D:/Test/ibs/temp/");

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Java中,tar文件通常用于压缩和归档目录或文件。如果你想解压一个.tar文件,可以使用一些Java库如Apache Tarball Utility (Taru) 或者是通过Runtime.getRuntime().exec()方法调用操作系统自带的命令行工具。 以下是一个简单的示例,说明如何使用Java的标准工具解压tar文件: ```java import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class Main { public static void main(String[] args) { String archivePath = "path_to_your_tar_file.tar"; String destinationDir = "path_to_extract_to"; try { // 创建一个进程运行tar命令 Process proc = Runtime.getRuntime().exec("tar -xvf " + archivePath + " -C " + destinationDir); // 获取进程的输入输出流,用于获取解压过程的日志信息 BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream())); BufferedReader errorReader = new BufferedReader(new InputStreamReader(proc.getErrorStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } // 检查错误流是否为空,如果非空则表明有错误发生 if ((line = errorReader.readLine()) != null) { System.err.println("Error during extraction: " + line); } // 等待进程结束 int exitCode = proc.waitFor(); if (exitCode != 0) { System.err.println("Command execution failed with exit code: " + exitCode); } } catch (IOException | InterruptedException e) { e.printStackTrace(); } } } ``` 记得替换`archivePath`和`destinationDir`为实际路径。这个例子假设tar文件是标准的tar格式,并且目标目录已经存在。如果需要更复杂的操作,比如处理gzip压缩的tar.gz文件,你可以引入额外的库,如`org.apache.commons.compress`。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时间辜负了谁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值