List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry isDirectory
public boolean isDirectory()
From source file:com.goldmansachs.kata2go.tools.utils.TarGz.java
public static void decompressFromStream2(InputStream inputStream) throws IOException { GzipCompressorInputStream gzipStream = new GzipCompressorInputStream(inputStream); TarArchiveInputStream tarInput = new TarArchiveInputStream(gzipStream); TarArchiveEntry entry; int bufferSize = 1024; while ((entry = (TarArchiveEntry) tarInput.getNextEntry()) != null) { String entryName = entry.getName(); // strip out the leading directory like the --strip tar argument String entryNameWithoutLeadingDir = entryName.substring(entryName.indexOf("/") + 1); if (entryNameWithoutLeadingDir.isEmpty()) { continue; }//from www . j a v a2 s .c o m if (entry.isDirectory()) { System.out.println("found dir " + entry.getName()); } else { int count; byte data[] = new byte[bufferSize]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((count = tarInput.read(data, 0, bufferSize)) != -1) { baos.write(data, 0, count); } JarOutputStream jarOutputStream = new JarOutputStream(baos); System.out.println(new String(baos.toByteArray())); } } tarInput.close(); gzipStream.close(); }
From source file:io.crate.testing.Utils.java
static void uncompressTarGZ(File tarFile, File dest) throws IOException { TarArchiveInputStream tarIn = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tarFile)))); TarArchiveEntry tarEntry = tarIn.getNextTarEntry(); // tarIn is a TarArchiveInputStream while (tarEntry != null) { Path entryPath = Paths.get(tarEntry.getName()); if (entryPath.getNameCount() == 1) { tarEntry = tarIn.getNextTarEntry(); continue; }//w ww . ja v a2s . c o m Path strippedPath = entryPath.subpath(1, entryPath.getNameCount()); File destPath = new File(dest, strippedPath.toString()); if (tarEntry.isDirectory()) { destPath.mkdirs(); } else { destPath.createNewFile(); byte[] btoRead = new byte[1024]; BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath)); int len; while ((len = tarIn.read(btoRead)) != -1) { bout.write(btoRead, 0, len); } bout.close(); if (destPath.getParent().equals(dest.getPath() + "/bin")) { destPath.setExecutable(true); } } tarEntry = tarIn.getNextTarEntry(); } tarIn.close(); }
From source file:de.uka.ilkd.key.dl.gui.initialdialog.gui.ToolInstaller.java
/** * @param tmp/*from w ww .j a v a 2 s . c o m*/ * @param dir * @throws IOException * @throws ArchiveException */ private static void untar(File file, File dir, FileType ft, PropertySetter ps, JProgressBar bar) throws IOException, ArchiveException { FileInputStream fis = new FileInputStream(file); InputStream is; switch (ft) { case TARGZ: is = new GZIPInputStream(fis); break; case TARBZ2: is = new BZip2CompressorInputStream(fis); break; default: throw new IllegalArgumentException("Don't know how to handle filetype: " + ft); } final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); TarArchiveEntry entry = null; int value = 0; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { bar.setValue(value++); final File outputFile = new File(dir, entry.getName()); if (entry.isDirectory()) { if (!outputFile.exists()) { if (!outputFile.mkdirs()) { throw new IllegalStateException( String.format("Couldn't create directory %s.", outputFile.getAbsolutePath())); } } } else { final OutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(debInputStream, outputFileStream); if (OSInfosDefault.INSTANCE.getOs() == OperatingSystem.OSX) { // FIXME: we need to make everything executable as somehow // the executable bit is not preserved in // OSX outputFile.setExecutable(true); } if (ps.filterFilename(outputFile)) { ps.setProperty(outputFile.getAbsolutePath()); } outputFileStream.flush(); outputFileStream.close(); } } debInputStream.close(); is.close(); file.delete(); }
From source file:com.goldmansachs.kata2go.tools.utils.TarGz.java
public static void decompress(InputStream tarGzInputStream, Path outDir) throws IOException { GzipCompressorInputStream gzipStream = new GzipCompressorInputStream(tarGzInputStream); TarArchiveInputStream tarInput = new TarArchiveInputStream(gzipStream); TarArchiveEntry entry; int bufferSize = 1024; while ((entry = (TarArchiveEntry) tarInput.getNextEntry()) != null) { String entryName = entry.getName(); // strip out the leading directory like the --strip tar argument String entryNameWithoutLeadingDir = entryName.substring(entryName.indexOf("/") + 1); if (entryNameWithoutLeadingDir.isEmpty()) { continue; }/*from w w w . ja va 2 s. com*/ Path outFile = outDir.resolve(entryNameWithoutLeadingDir); if (entry.isDirectory()) { outFile.toFile().mkdirs(); continue; } int count; byte data[] = new byte[bufferSize]; BufferedOutputStream fios = new BufferedOutputStream(new FileOutputStream(outFile.toFile()), bufferSize); while ((count = tarInput.read(data, 0, bufferSize)) != -1) { fios.write(data, 0, count); } fios.close(); } tarInput.close(); gzipStream.close(); }
From source file:com.ning.billing.beatrix.osgi.SetupBundleWithAssertion.java
private static void unTar(final File inputFile, final File outputDir) throws IOException, ArchiveException { InputStream is = null;// w ww. java 2s .c o m TarArchiveInputStream archiveInputStream = null; TarArchiveEntry entry = null; try { is = new FileInputStream(inputFile); archiveInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is); while ((entry = (TarArchiveEntry) archiveInputStream.getNextEntry()) != null) { final File outputFile = new File(outputDir, entry.getName()); if (entry.isDirectory()) { if (!outputFile.exists()) { if (!outputFile.mkdirs()) { throw new IllegalStateException( String.format("Couldn't create directory %s.", outputFile.getAbsolutePath())); } } } else { final OutputStream outputFileStream = new FileOutputStream(outputFile); ByteStreams.copy(archiveInputStream, outputFileStream); outputFileStream.close(); } } } finally { if (archiveInputStream != null) { archiveInputStream.close(); } if (is != null) { is.close(); } } }
From source file:com.anthemengineering.mojo.infer.InferMojo.java
/** * Extracts a given infer.tar.xz file to the given directory. * * @param tarXzToExtract the file to extract * @param inferDownloadDir the directory to extract the file to *//* w ww. j ava 2s . c o m*/ private static void extract(File tarXzToExtract, File inferDownloadDir) throws IOException { FileInputStream fin = null; BufferedInputStream in = null; XZCompressorInputStream xzIn = null; TarArchiveInputStream tarIn = null; try { fin = new FileInputStream(tarXzToExtract); in = new BufferedInputStream(fin); xzIn = new XZCompressorInputStream(in); tarIn = new TarArchiveInputStream(xzIn); TarArchiveEntry entry; while ((entry = tarIn.getNextTarEntry()) != null) { final File fileToWrite = new File(inferDownloadDir, entry.getName()); if (entry.isDirectory()) { FileUtils.forceMkdir(fileToWrite); } else { BufferedOutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(fileToWrite)); final byte[] buffer = new byte[4096]; int n = 0; while (-1 != (n = tarIn.read(buffer))) { out.write(buffer, 0, n); } } finally { if (out != null) { out.close(); } } } // assign file permissions final int mode = entry.getMode(); fileToWrite.setReadable((mode & 0004) != 0, false); fileToWrite.setReadable((mode & 0400) != 0, true); fileToWrite.setWritable((mode & 0002) != 0, false); fileToWrite.setWritable((mode & 0200) != 0, true); fileToWrite.setExecutable((mode & 0001) != 0, false); fileToWrite.setExecutable((mode & 0100) != 0, true); } } finally { if (tarIn != null) { tarIn.close(); } } }
From source file:lucee.commons.io.compress.CompressUtil.java
private static void extractTar(Resource tarFile, Resource targetDir) throws IOException { if (!targetDir.exists() || !targetDir.isDirectory()) throw new IOException(targetDir + " is not a existing directory"); if (!tarFile.exists()) throw new IOException(tarFile + " is not a existing file"); if (tarFile.isDirectory()) { Resource[] files = tarFile.listResources(new ExtensionResourceFilter("tar")); if (files == null) throw new IOException("directory " + tarFile + " is empty"); extract(FORMAT_TAR, files, targetDir); return;//from w w w .j a v a 2 s . co m } // read the zip file and build a query from its contents TarArchiveInputStream tis = null; try { tis = new TarArchiveInputStream(IOUtil.toBufferedInputStream(tarFile.getInputStream())); TarArchiveEntry entry; int mode; while ((entry = tis.getNextTarEntry()) != null) { //print.ln(entry); Resource target = targetDir.getRealResource(entry.getName()); if (entry.isDirectory()) { target.mkdirs(); } else { Resource parent = target.getParentResource(); if (!parent.exists()) parent.mkdirs(); IOUtil.copy(tis, target, false); } target.setLastModified(entry.getModTime().getTime()); mode = entry.getMode(); if (mode > 0) target.setMode(mode); //tis.closeEntry() ; } } finally { IOUtil.closeEL(tis); } }
From source file:com.cloudbees.clickstack.util.Files2.java
/** * TODO recopy file permissions/* www.ja va2 s .c o m*/ * <p/> * Uncompress the specified tgz file to the specified destination directory. * Replaces any files in the destination, if they already exist. * * @param tgzFile the name of the zip file to extract * @param destDir the directory to unzip to * @throws RuntimeIOException */ public static void untgz(@Nonnull Path tgzFile, @Nonnull Path destDir) throws RuntimeIOException { try { //if the destination doesn't exist, create it if (Files.notExists(destDir)) { logger.trace("Create dir: {}", destDir); Files.createDirectories(destDir); } TarArchiveInputStream in = new TarArchiveInputStream( new GzipCompressorInputStream(Files.newInputStream(tgzFile))); TarArchiveEntry entry; while ((entry = in.getNextTarEntry()) != null) { if (entry.isDirectory()) { Path dir = destDir.resolve(entry.getName()); logger.trace("Create dir {}", dir); Files.createDirectories(dir); } else { Path file = destDir.resolve(entry.getName()); logger.trace("Create file {}: {} bytes", file, entry.getSize()); OutputStream out = Files.newOutputStream(file); IOUtils.copy(in, out); out.close(); } } in.close(); } catch (IOException e) { throw new RuntimeIOException("Exception expanding " + tgzFile + " to " + destDir, e); } }
From source file:adams.core.io.TarUtils.java
/** * Lists the files stored in the tar file. * * @param input the tar file to obtain the file list from * @param listDirs whether to include directories in the list * @return the stored files/*from w w w .j a va 2 s . co m*/ */ public static List<File> listFiles(File input, boolean listDirs) { List<File> result; FileInputStream fis; TarArchiveInputStream archive; TarArchiveEntry entry; result = new ArrayList<>(); archive = null; fis = null; try { fis = new FileInputStream(input.getAbsolutePath()); archive = openArchiveForReading(input, fis); while ((entry = archive.getNextTarEntry()) != null) { if (entry.isDirectory()) { if (listDirs) result.add(new File(entry.getName())); } else { result.add(new File(entry.getName())); } } } catch (Exception e) { e.printStackTrace(); } finally { FileUtils.closeQuietly(fis); if (archive != null) { try { archive.close(); } catch (Exception e) { // ignored } } } return result; }
From source file:frameworks.Masken.java
public static void uncompressTarGZ(File tarFile, File dest) throws IOException { dest.mkdir();/*w w w . ja v a 2s.co m*/ TarArchiveInputStream tarIn = null; tarIn = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tarFile)))); TarArchiveEntry tarEntry = tarIn.getNextTarEntry(); // tarIn is a TarArchiveInputStream while (tarEntry != null) {// create a file with the same name as the tarEntry File destPath = new File(dest, tarEntry.getName()); System.out.println("working: " + destPath.getCanonicalPath()); if (tarEntry.isDirectory()) { destPath.mkdirs(); } else { if (!destPath.getParentFile().exists()) { destPath.getParentFile().mkdirs(); } destPath.createNewFile(); //byte [] btoRead = new byte[(int)tarEntry.getSize()]; byte[] btoRead = new byte[1024]; //FileInputStream fin // = new FileInputStream(destPath.getCanonicalPath()); BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath)); int len = 0; while ((len = tarIn.read(btoRead)) != -1) { bout.write(btoRead, 0, len); } bout.close(); btoRead = null; } tarEntry = tarIn.getNextTarEntry(); } tarIn.close(); }