这个程序是运行在windows环境下的,不能调用gunzip 而且在unix下 tar 是将几个文件打成一个包不进行压缩,结合gzip 压缩成.tar.gz文件。 结合compress 压缩成.tar.Z 文件。现在对.tar.gz文件能够进行解压缩了。用下面代码实现:public static InputStream getInputStream(String tarFileName) throws Exception{ if(tarFileName.substring(tarFileName.lastIndexOf(".") + 1, tarFileName.lastIndexOf(".") + 3).equalsIgnoreCase("gz")){ System.out.println("Creating an GZIPInputStream for the file"); return new GZIPInputStream(new FileInputStream(new File(tarFileName))); }else{ System.out.println("Creating an InputStream for the file"); return new FileInputStream(new File(tarFileName)); } }
public static void readTar(InputStream in, String untarDir) throws IOException{ System.out.println("Reading TarInputStream... (using classes from http://www.trustice.com/java/tar/)"); TarInputStream tin = new TarInputStream(in); TarEntry tarEntry = tin.getNextEntry(); if(new File(untarDir).exists()){ while (tarEntry != null){ File destPath = new File(untarDir + File.separatorChar + tarEntry.getName()); System.out.println("Processing " + destPath.getAbsoluteFile()); if(!tarEntry.isDirectory()){ FileOutputStream fout = new FileOutputStream(destPath); tin.copyEntryContents(fout); fout.close(); }else{ destPath.mkdir(); } tarEntry = tin.getNextEntry(); } tin.close(); }else{ System.out.println("That destination directory doesn't exist! " + untarDir); } }java支持zip gzip 压缩方式进行的压缩,通过ZipInputStream,ZipOutPutStream 来实现。但是不支持 compress方式进行的压缩。 希望高手能够帮助小弟!!! 这是小弟第一次发贴,感谢大家的支持!!!
而且在unix下 tar 是将几个文件打成一个包不进行压缩,结合gzip 压缩成.tar.gz文件。
结合compress 压缩成.tar.Z 文件。现在对.tar.gz文件能够进行解压缩了。用下面代码实现:public static InputStream getInputStream(String tarFileName) throws Exception{
if(tarFileName.substring(tarFileName.lastIndexOf(".") + 1, tarFileName.lastIndexOf(".") + 3).equalsIgnoreCase("gz")){
System.out.println("Creating an GZIPInputStream for the file");
return new GZIPInputStream(new FileInputStream(new File(tarFileName)));
}else{
System.out.println("Creating an InputStream for the file");
return new FileInputStream(new File(tarFileName));
}
}
public static void readTar(InputStream in, String untarDir) throws IOException{
System.out.println("Reading TarInputStream... (using classes from http://www.trustice.com/java/tar/)");
TarInputStream tin = new TarInputStream(in);
TarEntry tarEntry = tin.getNextEntry();
if(new File(untarDir).exists()){
while (tarEntry != null){
File destPath = new File(untarDir + File.separatorChar + tarEntry.getName());
System.out.println("Processing " + destPath.getAbsoluteFile());
if(!tarEntry.isDirectory()){
FileOutputStream fout = new FileOutputStream(destPath);
tin.copyEntryContents(fout);
fout.close();
}else{
destPath.mkdir();
}
tarEntry = tin.getNextEntry();
}
tin.close();
}else{
System.out.println("That destination directory doesn't exist! " + untarDir);
}
}java支持zip gzip 压缩方式进行的压缩,通过ZipInputStream,ZipOutPutStream 来实现。但是不支持 compress方式进行的压缩。
希望高手能够帮助小弟!!!
这是小弟第一次发贴,感谢大家的支持!!!