Source:
private static void extractTxz(InputStream stream, String targetDir) throws IOException { try ( XZInputStream xzIn = new XZInputStream(stream); TarArchiveInputStream tarIn = new TarArchiveInputStream(xzIn) ) { Phaser phaser = new Phaser(1); TarArchiveEntry entry; while ((entry = tarIn.getNextTarEntry()) != null) { final String individualFile = entry.getName(); final File fsObject = new File(targetDir + "/" + individualFile); if (entry.isSymbolicLink() || entry.isLink()) { Path target = FileSystems.getDefault().getPath(entry.getLinkName()); Files.createSymbolicLink(fsObject.toPath(), target); } else if (entry.isFile()) { byte[] content = new byte[(int) entry.getSize()]; int read = tarIn.read(content, 0, content.length); if (read == -1) { throw new IllegalStateException("could not read " + individualFile); } mkdirs(fsObject.getParentFile()); AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(fsObject.toPath(), CREATE_NEW, WRITE); ByteBuffer buffer = ByteBuffer.wrap(content); phaser.register(); fileChannel.write(buffer, 0, fileChannel, new CompletionHandler<Integer, Channel>() { @Override public void completed(Integer written, Channel channel) { closeChannel(channel); } @Override public void failed(Throwable error, Channel channel) { closeChannel(channel); } private void closeChannel(Channel channel) { try { channel.close(); } catch (IOException e) { LOG.error("Unexpected error while closing the channel", e); } finally { phaser.arriveAndDeregister(); } } }); } else if (entry.isDirectory()) { mkdirs(fsObject); } else { throw new UnsupportedOperationException( String.format("Unsupported entry found: %s", individualFile) ); } if (individualFile.startsWith("bin/") || individualFile.startsWith("./bin/")) { fsObject.setExecutable(true); } } phaser.arriveAndAwaitAdvance(); } }

Target:
private static void extractTxz(InputStream stream, String targetDir) throws IOException { try ( XZInputStream xzIn = new XZInputStream(stream); TarArchiveInputStream tarIn = new TarArchiveInputStream(xzIn) ) { final Phaser phaser = new Phaser(1); TarArchiveEntry entry; while ((entry = tarIn.getNextTarEntry()) != null) { final String individualFile = entry.getName(); final File fsObject = new File(targetDir + "/" + individualFile); if (entry.isSymbolicLink() || entry.isLink()) { Path target = FileSystems.getDefault().getPath(entry.getLinkName()); Files.createSymbolicLink(fsObject.toPath(), target); } else if (entry.isFile()) { byte[] content = new byte[(int) entry.getSize()]; int read = tarIn.read(content, 0, content.length); if (read == -1) { throw new IllegalStateException("could not read " + individualFile); } mkdirs(fsObject.getParentFile()); final AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(fsObject.toPath(), CREATE_NEW, WRITE); final ByteBuffer buffer = ByteBuffer.wrap(content); phaser.register(); fileChannel.write(buffer, 0, fileChannel, new CompletionHandler<Integer, Channel>() { @Override public void completed(Integer written, Channel channel) { closeChannel(channel); } @Override public void failed(Throwable error, Channel channel) { LOG.error("Could not write file {}", fsObject.getAbsolutePath(), error); closeChannel(channel); } private void closeChannel(Channel channel) { try { channel.close(); } catch (IOException e) { LOG.error("Unexpected error while closing the channel", e); } finally { phaser.arriveAndDeregister(); } } }); } else if (entry.isDirectory()) { mkdirs(fsObject); } else { throw new UnsupportedOperationException( String.format("Unsupported entry found: %s", individualFile) ); } if (individualFile.startsWith("bin/") || individualFile.startsWith("./bin/")) { fsObject.setExecutable(true); } } phaser.arriveAndAwaitAdvance(); } }