List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry getName
public String getName()
From source file:org.apache.sqoop.test.utils.CompressionUtils.java
/** * Untar given stream (tar.gz archive) to given directory. * * Directory structure will be preserved. * * @param inputStream InputStream of tar.gz archive * @param targetDirectory Target directory for tarball content * @throws IOException/*ww w. ja v a 2 s . c om*/ */ public static void untarStreamToDirectory(InputStream inputStream, String targetDirectory) throws IOException { assert inputStream != null; assert targetDirectory != null; LOG.info("Untaring archive to directory: " + targetDirectory); TarArchiveInputStream in = new TarArchiveInputStream(new GzipCompressorInputStream(inputStream)); TarArchiveEntry entry = null; int BUFFER_SIZE = 2048; while ((entry = (TarArchiveEntry) in.getNextEntry()) != null) { LOG.info("Untaring file: " + entry.getName()); if (entry.isDirectory()) { (new File(HdfsUtils.joinPathFragments(targetDirectory, entry.getName()))).mkdirs(); } else { int count; byte data[] = new byte[BUFFER_SIZE]; FileOutputStream fos = new FileOutputStream( HdfsUtils.joinPathFragments(targetDirectory, entry.getName())); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE); while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) { dest.write(data, 0, count); } dest.close(); } } in.close(); }
From source file:org.apache.storm.utils.ServerUtils.java
private static void unpackEntries(TarArchiveInputStream tis, TarArchiveEntry entry, File outputDir) throws IOException { if (entry.isDirectory()) { File subDir = new File(outputDir, entry.getName()); if (!subDir.mkdirs() && !subDir.isDirectory()) { throw new IOException("Mkdirs failed to create tar internal dir " + outputDir); }/* ww w. j a v a 2 s .c o m*/ for (TarArchiveEntry e : entry.getDirectoryEntries()) { unpackEntries(tis, e, subDir); } return; } File outputFile = new File(outputDir, entry.getName()); if (!outputFile.getParentFile().exists()) { if (!outputFile.getParentFile().mkdirs()) { throw new IOException("Mkdirs failed to create tar internal dir " + outputDir); } } int count; byte data[] = new byte[2048]; BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); while ((count = tis.read(data)) != -1) { outputStream.write(data, 0, count); } outputStream.flush(); outputStream.close(); }
From source file:org.apache.sysml.validation.ValidateLicAndNotice.java
/** * This will return the file from tgz file and store it in specified location. * * @param tgzFileName is the name of tgz file from which file to be extracted. * @param fileName is the name of the file to be extracted. * @param strDestLoc is the location where file will be extracted. * @param bFirstDirLevel to indicate to get file from first directory level. * @return Sucess or Failure//from w ww . ja v a2 s . c om */ public static boolean extractFileFromTGZ(String tgzFileName, String fileName, String strDestLoc, boolean bFirstDirLevel) { boolean bRetCode = Constants.bFAILURE; TarArchiveInputStream tarIn = null; try { tarIn = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tgzFileName)))); } catch (Exception e) { Utility.debugPrint(Constants.DEBUG_ERROR, "Exception in unzipping tar file: " + e); return bRetCode; } try { BufferedOutputStream bufOut = null; BufferedInputStream bufIn = null; TarArchiveEntry tarEntry = null; while ((tarEntry = tarIn.getNextTarEntry()) != null) { if (!tarEntry.getName().endsWith(fileName)) continue; //Get file at root (in single directory) level. This is for License in root location. if (bFirstDirLevel && (tarEntry.getName().indexOf('/') != tarEntry.getName().lastIndexOf('/'))) continue; bufIn = new BufferedInputStream(tarIn); int count; byte data[] = new byte[Constants.BUFFER]; String strOutFileName = strDestLoc == null ? tarEntry.getName() : strDestLoc + "/" + fileName; FileOutputStream fos = new FileOutputStream(strOutFileName); bufOut = new BufferedOutputStream(fos, Constants.BUFFER); while ((count = bufIn.read(data, 0, Constants.BUFFER)) != -1) { bufOut.write(data, 0, count); } bufOut.flush(); bufOut.close(); bufIn.close(); bRetCode = Constants.bSUCCESS; break; } } catch (Exception e) { e.printStackTrace(); } return bRetCode; }
From source file:org.apache.sysml.validation.ValidateLicAndNotice.java
/** * This will return the list of files from tgz file. * * @param tgzFileName is the name of tgz file from which list of files with specified file extension will be returned. * @param fileExt is the file extension to be used to get list of files to be returned. * @return Returns list of files having specified extension from tgz file. *//* ww w . j a v a2 s. c o m*/ public static List<String> getFilesFromTGZ(String tgzFileName, String fileExt) { TarArchiveInputStream tarIn = null; try { tarIn = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(tgzFileName)))); } catch (Exception e) { Utility.debugPrint(Constants.DEBUG_ERROR, "Exception in unzipping tar file: " + e); return null; } List<String> files = new ArrayList<String>(); try { TarArchiveEntry tarEntry = null; while ((tarEntry = tarIn.getNextTarEntry()) != null) { if (tarEntry.getName().endsWith("." + fileExt)) { int iPos = tarEntry.getName().lastIndexOf("/"); if (iPos == 0) --iPos; String strFileName = tarEntry.getName().substring(iPos + 1); files.add(strFileName); } } } catch (Exception e) { e.printStackTrace(); } return (files); }
From source file:org.canova.api.util.ArchiveUtils.java
/** * Extracts files to the specified destination * @param file the file to extract to//from w ww . j a v a2 s . c o m * @param dest the destination directory * @throws java.io.IOException */ public static void unzipFileTo(String file, String dest) throws IOException { File target = new File(file); if (!target.exists()) throw new IllegalArgumentException("Archive doesnt exist"); FileInputStream fin = new FileInputStream(target); int BUFFER = 2048; byte data[] = new byte[BUFFER]; if (file.endsWith(".zip")) { //getFromOrigin the zip file content ZipInputStream zis = new ZipInputStream(fin); //getFromOrigin the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(dest + File.separator + fileName); if (ze.isDirectory()) { newFile.mkdirs(); zis.closeEntry(); ze = zis.getNextEntry(); continue; } log.info("file unzip : " + newFile.getAbsoluteFile()); //create all non exists folders //else you will hit FileNotFoundException for compressed folder FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(data)) > 0) { fos.write(data, 0, len); } fos.flush(); fos.close(); zis.closeEntry(); ze = zis.getNextEntry(); } zis.close(); } else if (file.endsWith(".tar")) { BufferedInputStream in = new BufferedInputStream(fin); TarArchiveInputStream tarIn = new TarArchiveInputStream(in); TarArchiveEntry entry = null; /** Read the tar entries using the getNextEntry method **/ while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { log.info("Extracting: " + entry.getName()); /** If the entry is a directory, createComplex the directory. **/ if (entry.isDirectory()) { File f = new File(dest + File.separator + entry.getName()); f.mkdirs(); } /** * If the entry is a file,write the decompressed file to the disk * and close destination stream. **/ else { int count; FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName()); BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER); while ((count = tarIn.read(data, 0, BUFFER)) != -1) { destStream.write(data, 0, count); } destStream.flush(); ; IOUtils.closeQuietly(destStream); } } /** Close the input stream **/ tarIn.close(); } else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) { BufferedInputStream in = new BufferedInputStream(fin); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn); TarArchiveEntry entry = null; /** Read the tar entries using the getNextEntry method **/ while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { log.info("Extracting: " + entry.getName()); /** If the entry is a directory, createComplex the directory. **/ if (entry.isDirectory()) { File f = new File(dest + File.separator + entry.getName()); f.mkdirs(); } /** * If the entry is a file,write the decompressed file to the disk * and close destination stream. **/ else { int count; FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName()); BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER); while ((count = tarIn.read(data, 0, BUFFER)) != -1) { destStream.write(data, 0, count); } destStream.flush(); IOUtils.closeQuietly(destStream); } } /** Close the input stream **/ tarIn.close(); } else if (file.endsWith(".gz")) { GZIPInputStream is2 = new GZIPInputStream(fin); File extracted = new File(target.getParent(), target.getName().replace(".gz", "")); if (extracted.exists()) extracted.delete(); extracted.createNewFile(); OutputStream fos = FileUtils.openOutputStream(extracted); IOUtils.copyLarge(is2, fos); is2.close(); fos.flush(); fos.close(); } }
From source file:org.cloudfoundry.client.v2.ApplicationsTest.java
private static Consumer<byte[]> isTestApplicationDroplet() { return bytes -> { Set<String> names = new HashSet<>(); try (TarArchiveInputStream in = new TarArchiveInputStream( new GZIPInputStream(new ByteArrayInputStream(bytes)))) { TarArchiveEntry entry; while ((entry = in.getNextTarEntry()) != null) { names.add(entry.getName()); }/*from w w w .j a va 2s . c om*/ } catch (IOException e) { throw Exceptions.propagate(e); } assertThat(names).contains("./app/Staticfile", "./app/public/index.html"); }; }
From source file:org.cloudifysource.esc.util.TarGzUtils.java
/** * Extract a tar.gz file./*from www . ja v a 2s.c o m*/ * * @param source * The file to extract from. * @param destination * The destination folder. * @throws IOException * An error occured during the extraction. */ public static void extract(final File source, final String destination) throws IOException { LOGGER.fine(String.format("Extracting %s to %s", source.getName(), destination)); if (!FilenameUtils.getExtension(source.getName().toLowerCase()).equals("gz")) { throw new IllegalArgumentException("Expecting tar.gz file: " + source.getAbsolutePath()); } if (!new File(destination).isDirectory()) { throw new IllegalArgumentException("Destination should be a folder: " + destination); } /** create a TarArchiveInputStream object. **/ FileInputStream fin = new FileInputStream(source); BufferedInputStream in = new BufferedInputStream(fin); GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn); TarArchiveEntry entry = null; /** Read the tar entries using the getNextEntry method **/ while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { LOGGER.finer("Extracting: " + entry.getName()); /** If the entry is a directory, create the directory. **/ if (entry.isDirectory()) { File f = new File(destination, entry.getName()); f.mkdirs(); } else { int count; byte[] data = new byte[BUFFER]; FileOutputStream fos = new FileOutputStream(new File(destination, entry.getName())); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); while ((count = tarIn.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } dest.close(); } } /** Close the input stream **/ tarIn.close(); }
From source file:org.cmuchimps.gort.modules.helper.CompressionHelper.java
public static void untar(File f) { if (f == null || !f.exists() || !f.canRead()) { return;//from ww w . jav a2 s . c o m } else { System.out.println("Untarring file: " + f.getAbsolutePath()); } File parent = f.getParentFile(); if (parent == null || !parent.exists() || !parent.canWrite()) { return; } FileInputStream fin = null; BufferedInputStream in = null; TarArchiveInputStream tarIn = null; TarArchiveEntry entry = null; try { fin = new FileInputStream(f); in = new BufferedInputStream(fin); tarIn = new TarArchiveInputStream(in); while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { final File outputFile = new File(parent, entry.getName()); if (entry.isDirectory()) { if (!outputFile.exists()) { outputFile.mkdirs(); } } else { final FileOutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(tarIn, outputFileStream); outputFileStream.close(); } //System.out.println("Processed: " + outputFile.getAbsolutePath()); } } catch (FileNotFoundException ex) { ex.printStackTrace(); ; } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException ex) { ex.printStackTrace(); } } if (fin != null) { try { fin.close(); } catch (IOException ex) { ex.printStackTrace(); } } if (tarIn != null) { try { tarIn.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
From source file:org.codehaus.mojo.unix.deb.DpkgDebTool.java
private static List<UnixFsObject> process(InputStream is) throws IOException { TarArchiveInputStream tarInputStream = new TarArchiveInputStream(is); List<UnixFsObject> objects = new ArrayList<UnixFsObject>(); TarArchiveEntry entry = (TarArchiveEntry) tarInputStream.getNextEntry(); while (entry != null) { Option<UnixFileMode> mode = some(UnixFileMode.fromInt(entry.getMode())); FileAttributes attributes = new FileAttributes(some(entry.getUserName()), some(entry.getGroupName()), mode);//from ww w .j a v a 2 s. c o m RelativePath path = relativePath(entry.getName()); LocalDateTime lastModified = LocalDateTime.fromDateFields(entry.getModTime()); UnixFsObject object; if (entry.isDirectory()) { object = directory(path, lastModified, attributes); } else if (entry.isSymbolicLink()) { object = symlink(path, lastModified, some(entry.getUserName()), some(entry.getGroupName()), entry.getLinkName()); } else if (entry.isFile()) { object = regularFile(path, lastModified, entry.getSize(), attributes); } else { throw new IOException("Unsupported link type: name=" + entry.getName()); } objects.add(object); entry = (TarArchiveEntry) tarInputStream.getNextEntry(); } return objects; }
From source file:org.codehaus.plexus.archiver.tar.TarArchiveContentLister.java
protected List<ArchiveContentEntry> execute() throws ArchiverException { ArrayList<ArchiveContentEntry> archiveContentList = new ArrayList<ArchiveContentEntry>(); getLogger().debug("listing: " + getSourceFile()); TarArchiveInputStream tis = null;//from ww w .j av a2 s . co m try { TarFile tarFile = new TarFile(getSourceFile()); tis = new TarArchiveInputStream(decompress(compression, getSourceFile(), new BufferedInputStream(new FileInputStream(getSourceFile())))); TarArchiveEntry te; while ((te = tis.getNextTarEntry()) != null) { MyTarResource fileInfo = new MyTarResource(tarFile, te); if (isSelected(te.getName(), fileInfo)) { ArchiveContentEntry ae = fileInfo.asArchiveContentEntry(); archiveContentList.add(ae); } } getLogger().debug("listing complete"); } catch (final IOException ioe) { throw new ArchiverException("Error while listing " + getSourceFile().getAbsolutePath(), ioe); } finally { IOUtil.close(tis); } return archiveContentList; }