java解压tar.gz

//引入jtar-(版本号).jar
public class C {
	/*
	 * 引入jtar-(版本号).jar 使用javatar-2.5.jar 参考
	 * http://zhidao.baidu.com/question/808277839243169492.html
	 * http://billclod.iteye.com/blog/350001
	 */
	public void readtar() throws IOException {
		String tarFile = "D:/20120725.tar.gz";
		String destFolder = "D:/20120725";

		File ss = new File(tarFile);
		TarInputStream tis = null;
		tis = new TarInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(ss))));
		TarEntry entry;

		try {
			while ((entry = tis.getNextEntry()) != null) {
				int count = 0;
				byte data[] = new byte[204800];

				FileOutputStream fos = new FileOutputStream(new File(destFolder + "/" + entry.getName()));
				BufferedOutputStream dest = new BufferedOutputStream(fos);

				while ((count = tis.read(data)) != -1) {
					dest.write(data, 0, count);
				}
				dest.flush();
				dest.close();
			}
			tis.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

你可能感兴趣的:(java)