2009/09/01

javaでtar.gzを解凍せずに中身をみる

以下のプログラムでOK
ant.jarをクラスパスに指定する必要がある.

dis.readLine();
で怒られるけど,まあいいでしょう.


import java.util.zip.*;
import java.io.*;

import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;


public class UncompressTar {


public static void main(String[] args) throws Exception{

FileInputStream fis = new FileInputStream(args[0]);
TarInputStream tin = new TarInputStream(new GZIPInputStream(fis));
TarEntry tarEnt = tin.getNextEntry();
while (tarEnt != null) {
String name = tarEnt.getName();
int size = (int)tarEnt.getSize();
System.out.println(String.format("%s:: size= %d byte",name,size));
ByteArrayOutputStream bos = new ByteArrayOutputStream(size);
tin.copyEntryContents(bos);
byte[] data = bos.toByteArray();
DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));
System.out.println(dis.readLine());
dis.close();
tarEnt = tin.getNextEntry();
}
tin.close();
}
}

0 件のコメント: