wieder mal was nettes in Java, daß man immer mal wieder braucht
benütigt wird org.apache.commons.compress !
Bestimmtes File aus einem tar.gz extrahieren
private boolean extractFile(String targzFile, String fileName) throws FileNotFoundException, IOException {
boolean extracted = false;
final File inputFile = new File(targzFile);
final GZIPInputStream in = new GZIPInputStream(new FileInputStream(inputFile));
final TarArchiveInputStream tarInput = new TarArchiveInputStream(in);
OutputStream outputFileStream = null;
TarArchiveEntry entry;
while (null!=(entry=tarInput.getNextTarEntry())) {
if(entry.getName().contains(fileName)){
String foundFile = entry.getName();
System.out.println("Found " + foundFile);
try{
foundFile = foundFile.substring(foundFile.indexOf("/")+1);
final File outputFile = new File(OUTPUT_DIR + "/",foundFile);
outputFileStream = new FileOutputStream(outputFile);
IOUtils.copy(tarInput, outputFileStream);
outputFileStream.close();
}catch (Exception ex){
ex.printStackTrace();
System.out.println("Failed extracting " + foundFile);
}
}
}
in.close();
tarInput.close();
outputFileStream.close();
return extracted;
}
tar File aus einem tar.gz extrahieren
private File unGzip(String targzFile, String tarFile) throws FileNotFoundException, IOException {
File inputFile = new File(targzFile);
File outputDir = new File(tarFile);
o.logDebug(String.format("Unzipping %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));
final File outputFile = new File(outputDir, inputFile.getName().substring(0, inputFile.getName().length() - 3));
final GZIPInputStream in = new GZIPInputStream(new FileInputStream(inputFile));
final FileOutputStream out = new FileOutputStream(outputFile);
IOUtils.copy(in, out);
in.close();
out.close();
return outputFile;
}