List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream close
public void close() throws IOException
From source file:msec.org.TarUtil.java
public static void dearchive(File srcFile, File destFile) throws Exception { TarArchiveInputStream tais = new TarArchiveInputStream(new FileInputStream(srcFile)); dearchive(destFile, tais);/*w w w .j a v a2 s . c o m*/ tais.close(); }
From source file:gobblin.data.management.copy.converter.UnGzipConverterTest.java
private static String readGzipStreamAsString(InputStream is) throws Exception { TarArchiveInputStream tarIn = new TarArchiveInputStream(is); try {/*from w ww . j a v a 2 s .c om*/ TarArchiveEntry tarEntry; while ((tarEntry = tarIn.getNextTarEntry()) != null) { if (tarEntry.isFile() && tarEntry.getName().endsWith(".txt")) { return IOUtils.toString(tarIn, "UTF-8"); } } } finally { tarIn.close(); } return Strings.EMPTY; }
From source file:com.ibm.util.merge.CompareArchives.java
/** * @param archive//from w ww.java 2s .co m * @return * @throws IOException */ private static final HashMap<String, TarArchiveEntry> getMembers(String archive) throws IOException { TarArchiveInputStream input = new TarArchiveInputStream( new BufferedInputStream(new FileInputStream(archive))); TarArchiveEntry entry; HashMap<String, TarArchiveEntry> map = new HashMap<>(); while ((entry = input.getNextTarEntry()) != null) { map.put(entry.getName(), entry); } input.close(); return map; }
From source file:com.pinterest.deployservice.common.TarUtils.java
/** * Unbundle the given tar bar as a map, with key as file name and value as content. *//*from w ww . ja v a 2 s. c o m*/ public static Map<String, String> untar(InputStream is) throws Exception { TarArchiveInputStream tais = new TarArchiveInputStream(new GZIPInputStream(is)); Map<String, String> data = new HashMap<String, String>(); TarArchiveEntry entry; while ((entry = tais.getNextTarEntry()) != null) { String name = entry.getName(); byte[] content = new byte[(int) entry.getSize()]; tais.read(content, 0, content.length); data.put(name, new String(content, "UTF8")); } tais.close(); return data; }
From source file:com.ibm.util.merge.CompareArchives.java
/** * @param archive//from www. ja v a2 s . c om * @param name * @return * @throws IOException */ private static final String getTarFile(String archive, String name) throws IOException { TarArchiveInputStream input = new TarArchiveInputStream( new BufferedInputStream(new FileInputStream(archive))); TarArchiveEntry entry; while ((entry = input.getNextTarEntry()) != null) { if (entry.getName().equals(name)) { byte[] content = new byte[(int) entry.getSize()]; input.read(content, 0, content.length); input.close(); return new String(content); } } input.close(); return ""; }
From source file:com.dubture.symfony.core.util.UncompressUtils.java
/** * Uncompress all entries found in a tar archive file into the given output * directory. Entry names are translated using the translator before being * put on the file system./* ww w . j av a 2 s.com*/ * * @param archiveFile The tar archive file to uncompress * @param outputDirectory The output directory where to put uncompressed entries * @param entryNameTranslator The entry name translator to use * * @throws IOException When an error occurs while uncompressing the tar archive */ public static void uncompressTarArchive(File archiveFile, File outputDirectory, EntryNameTranslator entryNameTranslator) throws IOException { FileInputStream fileInputStream = new FileInputStream(archiveFile); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); TarArchiveInputStream tarInputStream = new TarArchiveInputStream(bufferedInputStream); try { TarArchiveEntry tarEntry = null; while ((tarEntry = tarInputStream.getNextTarEntry()) != null) { uncompressTarArchiveEntry(tarInputStream, tarEntry, outputDirectory, entryNameTranslator); } } finally { tarInputStream.close(); } }
From source file:com.netflix.spinnaker.clouddriver.appengine.artifacts.StorageUtils.java
public static void untarStreamToPath(InputStream inputStream, String basePath) throws IOException { class DirectoryTimestamp { public DirectoryTimestamp(File d, long m) { directory = d;//from w w w .ja v a2s . co m millis = m; } public File directory; public long millis; } ; // Directories come in hierarchical order within the stream, but // we need to set their timestamps after their children have been written. Stack<DirectoryTimestamp> directoryStack = new Stack<DirectoryTimestamp>(); File baseDirectory = new File(basePath); baseDirectory.mkdir(); TarArchiveInputStream tarStream = new TarArchiveInputStream(inputStream); for (TarArchiveEntry entry = tarStream.getNextTarEntry(); entry != null; entry = tarStream .getNextTarEntry()) { File target = new File(baseDirectory, entry.getName()); if (entry.isDirectory()) { directoryStack.push(new DirectoryTimestamp(target, entry.getModTime().getTime())); continue; } writeStreamToFile(tarStream, target); target.setLastModified(entry.getModTime().getTime()); } while (!directoryStack.empty()) { DirectoryTimestamp info = directoryStack.pop(); info.directory.setLastModified(info.millis); } tarStream.close(); }
From source file:it.geosolutions.tools.compress.file.reader.TarReader.java
/** * A method to read tar file://from w w w .java2s.c om * extract its content to the 'destDir' file (which could be * a directory or a file depending on the srcF file content) * @throws CompressorException */ public static void readTar(File srcF, File destDir) throws Exception { if (destDir == null) throw new IllegalArgumentException("Unable to extract to a null destination dir"); if (!destDir.canWrite() && !destDir.mkdirs()) throw new IllegalArgumentException("Unable to extract to a not writeable destination dir: " + destDir); FileInputStream fis = null; TarArchiveInputStream tis = null; BufferedInputStream bis = null; try { fis = new FileInputStream(srcF); bis = new BufferedInputStream(fis); tis = new TarArchiveInputStream(bis); TarArchiveEntry te = null; while ((te = tis.getNextTarEntry()) != null) { File curr_dest = new File(destDir, te.getName()); if (te.isDirectory()) { // create destination folder if (!curr_dest.exists()) curr_dest.mkdirs(); } else { writeFile(curr_dest, tis); } } } finally { if (tis != null) { try { tis.close(); } catch (IOException ioe) { } } if (bis != null) { try { bis.close(); } catch (IOException ioe) { } } if (fis != null) { try { fis.close(); } catch (IOException ioe) { } } } }
From source file:examples.utils.CifarReader.java
public static void downloadAndExtract() { if (new File("data", TEST_DATA_FILE).exists() == false) { try {/* w w w .j av a 2 s . co m*/ if (new File("data", ARCHIVE_BINARY_FILE).exists() == false) { URL website = new URL("http://www.cs.toronto.edu/~kriz/" + ARCHIVE_BINARY_FILE); FileOutputStream fos = new FileOutputStream("data/" + ARCHIVE_BINARY_FILE); fos.getChannel().transferFrom(Channels.newChannel(website.openStream()), 0, Long.MAX_VALUE); fos.close(); } TarArchiveInputStream tar = new TarArchiveInputStream( new GZIPInputStream(new FileInputStream("data/" + ARCHIVE_BINARY_FILE))); TarArchiveEntry entry = null; while ((entry = tar.getNextTarEntry()) != null) { if (entry.isDirectory()) { new File("data", entry.getName()).mkdirs(); } else { byte data[] = new byte[2048]; int count; BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(new File("data/", entry.getName())), 2048); while ((count = tar.read(data, 0, 2048)) != -1) { bos.write(data, 0, count); } bos.close(); } } tar.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:com.aliyun.odps.local.common.utils.ArchiveUtils.java
public static void unTar(File inFile, File untarDir) throws IOException, ArchiveException { final InputStream is = new FileInputStream(inFile); final TarArchiveInputStream in = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream(ArchiveStreamFactory.TAR, is); TarArchiveEntry entry = null;/*from w ww .ja va 2s .co m*/ untarDir.mkdirs(); while ((entry = (TarArchiveEntry) in.getNextEntry()) != null) { byte[] content = new byte[(int) entry.getSize()]; in.read(content); final File entryFile = new File(untarDir, entry.getName()); if (entry.isDirectory() && !entryFile.exists()) { if (!entryFile.mkdirs()) { throw new IOException("Create directory failed: " + entryFile.getAbsolutePath()); } } else { final OutputStream out = new FileOutputStream(entryFile); IOUtils.write(content, out); out.close(); } } in.close(); }