List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream TarArchiveInputStream
public TarArchiveInputStream(InputStream is)
From source file:com.baidu.rigel.biplatform.tesseract.util.FileUtils.java
/** * Uncompress the incoming file./*ww w . j a v a2 s .com*/ * * @param inFileName * Name of the file to be uncompressed * @param outFileName * Name of target */ public static String doUncompressFile(String inFileName, String outFileName) throws IOException { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_BEGIN, "doUncompressFile", "[inFileName:" + inFileName + "]")); if (StringUtils.isEmpty(inFileName)) { throw new IllegalArgumentException(); } String decompressedFileName = outFileName; File inFile = new File(inFileName); if (StringUtils.isEmpty(decompressedFileName)) { // not specified outFileName StringBuilder sb = new StringBuilder(); sb.append(inFile.getParentFile().getAbsolutePath()); sb.append(File.separator); // sb.append(inFile.getName().substring(0, // inFile.getName().indexOf("."))); // sb.append(File.separator); decompressedFileName = sb.toString(); } File outFile = new File(decompressedFileName); if (!outFile.exists()) { outFile.mkdirs(); } /** * create a TarArchiveInputStream object. */ FileInputStream fin = null; BufferedInputStream in = null; GzipCompressorInputStream gzIn = null; TarArchiveInputStream tarIn = null; FileOutputStream fos = null; BufferedOutputStream dest = null; TarArchiveEntry entry = null; try { fin = new FileInputStream(inFile); in = new BufferedInputStream(fin); gzIn = new GzipCompressorInputStream(in); tarIn = new TarArchiveInputStream(gzIn); /** * Read the tar entries using the getNextEntry method */ while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_DECOMPRESS_PROCESS, "Extracting File:" + entry.getName())); if (entry.isDirectory()) { /** * If the entry is a directory, create the directory. */ File f = new File(decompressedFileName + entry.getName()); f.mkdirs(); } else { /** * If the entry is a file,write the decompressed file to the * disk and close destination stream. */ int count; byte[] data = new byte[TesseractConstant.FILE_BLOCK_SIZE]; String fileName = decompressedFileName + entry.getName().substring( entry.getName().indexOf(TesseractConstant.DECOMPRESSION_FILENAME_SPLITTER) + 1); fos = new FileOutputStream(new File(fileName)); dest = new BufferedOutputStream(fos, TesseractConstant.FILE_BLOCK_SIZE); while ((count = tarIn.read(data, 0, TesseractConstant.FILE_BLOCK_SIZE)) != -1) { dest.write(data, 0, count); } dest.close(); } } /** * Close the input stream */ tarIn.close(); } catch (IOException e) { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_DECOMPRESS_ERROR, "IOException")); LOGGER.error(e.getMessage(), e); throw e; } finally { try { fin.close(); in.close(); gzIn.close(); tarIn.close(); fos.close(); dest.close(); } catch (IOException e) { LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_DECOMPRESS_ERROR, "IOException occur when closing fd")); LOGGER.error(e.getMessage(), e); throw e; } } LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_END, "doUncompressFile", "[inFileName:" + inFileName + "]")); return decompressedFileName; }
From source file:in.neoandroid.neoupdate.neoUpdate.java
private String processFromNPK() { try {/* w ww . j a v a2 s .co m*/ GZIPInputStream npkFile = new GZIPInputStream(new FileInputStream(baseUrl)); //FileInputStream npkFile = new FileInputStream(baseUrl); TarArchiveInputStream input = new TarArchiveInputStream(npkFile); TarArchiveEntry ae; while ((ae = input.getNextTarEntry()) != null && filesToDownload.size() > 0 && !stopped) { if (ae.isDirectory()) { Log.e("[neoUpdate]", "Dir: " + ae.getName()); } else { Log.e("[neoUpdate]", "File: " + ae.getName()); String filename = ae.getName(); NewAsset asset = findAndGetAsset(filename); if (asset != null) { downloadFile(asset, null, input, ae); publishProgress((float) (totalFilesToDownload - filesToDownload.size()) / (float) totalFilesToDownload); } } } input.close(); } catch (Exception e) { e.printStackTrace(); return "Unknown Error: Update Failed!"; } return "Success"; }
From source file:gov.nih.nci.caarray.application.translation.geosoft.GeoSoftExporterBeanTest.java
@Test public void testExportArchiveTar() throws Exception { final Project p = makeGoodProject(); final List<PackagingInfo> infos = this.bean.getAvailablePackagingInfos(p); final PackagingInfo zipPi = Iterables.find(infos, new Predicate<PackagingInfo>() { @Override/*from w w w .ja va 2 s . com*/ public boolean apply(PackagingInfo t) { return t.getMethod() == PackagingInfo.PackagingMethod.ZIP; } }); final File f = File.createTempFile("test", zipPi.getName()); final FileOutputStream fos = new FileOutputStream(f); this.bean.export(p, "http://example.com/my_experiemnt", PackagingInfo.PackagingMethod.TGZ, fos); fos.close(); final GZIPInputStream in = new GZIPInputStream(new FileInputStream(f)); final TarArchiveInputStream tar = new TarArchiveInputStream(in); final Set<String> entries = new HashSet<String>(); entries.addAll(java.util.Arrays.asList("test-exp-id.soft.txt", "raw_file.data", "derived_file.data", "supplimental.data", "README.txt")); TarArchiveEntry e = tar.getNextTarEntry(); while (e != null) { assertTrue(e.getName() + " unexpected", entries.remove(e.getName())); e = tar.getNextTarEntry(); } assertTrue(entries.toString() + " not found", entries.isEmpty()); }
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 *//*from w ww . jav a 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:in.neoandroid.neoupdate.neoUpdate.java
private boolean downloadAPKFromNPK() { try {//w w w . j a va 2s .c om String apkName = apkUpdatePath.path.replace("/", ""); GZIPInputStream npkFile = new GZIPInputStream(new FileInputStream(baseUrl)); //FileInputStream npkFile = new FileInputStream(baseUrl); TarArchiveInputStream input = new TarArchiveInputStream(npkFile); TarArchiveEntry ae; while ((ae = input.getNextTarEntry()) != null) { if (!ae.isDirectory() && ae.getName().equalsIgnoreCase(apkName)) { String apkPath = tmpDir + apkUpdatePath.path; boolean status = downloadFile(apkUpdatePath, apkPath, input, ae); input.close(); return status; } } input.close(); } catch (Exception e) { e.printStackTrace(); } return false; }
From source file:gdt.data.entity.ArchiveHandler.java
/** * Discover if the tar file contains the default directory for entities entities * @param tarfile$ the path of the archive file. * @return true if contains, false otherwise. *//* w ww. java 2 s .c om*/ public static boolean hasEntitiesDirInTar(String tarfile$) { try { //System.out.println("ArchiveHandler:hasEntitiesDirInTar:tar file="+tarfile$); TarArchiveInputStream tis = new TarArchiveInputStream(new FileInputStream(new File(tarfile$))); return hasEntitiesDirInTarStream(tis); } catch (Exception e) { Logger.getLogger(ArchiveHandler.class.getName()).severe(e.toString()); } return false; }
From source file:com.buaa.cfs.utils.FileUtil.java
private static void unTarUsingJava(File inFile, File untarDir, boolean gzipped) throws IOException { InputStream inputStream = null; TarArchiveInputStream tis = null;/*from w ww. java 2 s. c o m*/ try { if (gzipped) { inputStream = new BufferedInputStream(new GZIPInputStream(new FileInputStream(inFile))); } else { inputStream = new BufferedInputStream(new FileInputStream(inFile)); } tis = new TarArchiveInputStream(inputStream); for (TarArchiveEntry entry = tis.getNextTarEntry(); entry != null;) { unpackEntries(tis, entry, untarDir); entry = tis.getNextTarEntry(); } } finally { IOUtils.cleanup(LOG, tis, inputStream); } }
From source file:gdt.data.entity.ArchiveHandler.java
/** * Discover if the tar.gz file contains the default directory for entities entities * @param tgzfile$ the path of the archive file. * @return true if contains, false otherwise. *//*w w w.j av a 2 s . c om*/ public static boolean hasEntitiesDirInTgz(String tgzfile$) { try { //FileInputStream fis = new FileInputStream(gzipFile); GZIPInputStream gis = new GZIPInputStream(new FileInputStream(new File(tgzfile$))); TarArchiveInputStream tis = new TarArchiveInputStream(gis); return hasEntitiesDirInTarStream(tis); } catch (Exception e) { Logger.getLogger(ArchiveHandler.class.getName()).severe(e.toString()); } return false; }
From source file:hudson.gridmaven.MavenBuilder.java
public void getAndUntar(FileSystem fs, String src, String targetPath) throws FileNotFoundException, IOException { BufferedOutputStream dest = null; InputStream tarArchiveStream = new FSDataInputStream(fs.open(new Path(src))); TarArchiveInputStream tis = new TarArchiveInputStream(new BufferedInputStream(tarArchiveStream)); TarArchiveEntry entry = null;/*w w w. j a va 2 s . c om*/ try { while ((entry = tis.getNextTarEntry()) != null) { int count; File outputFile = new File(targetPath, entry.getName()); if (entry.isDirectory()) { // entry is a directory if (!outputFile.exists()) { outputFile.mkdirs(); } } else { // entry is a file byte[] data = new byte[BUFFER_MAX]; FileOutputStream fos = new FileOutputStream(outputFile); dest = new BufferedOutputStream(fos, BUFFER_MAX); while ((count = tis.read(data, 0, BUFFER_MAX)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); } } } catch (Exception e) { e.printStackTrace(); } finally { if (dest != null) { dest.flush(); dest.close(); } tis.close(); } }
From source file:bb.io.TarUtil.java
/** * Returns all the {@link TarArchiveEntry}s inside tarFile. * <p>//from w ww . j a va 2 s .co m * @param tarFile the TAR format file to be read * @param sortResult if true, then the result is first sorted by each entry's name before return; * otherwise the order is the sequence read from tarFile * @throws IllegalArgumentException if tarFile fails {@link Check#validFile Check.validFile} * @throws IOException if an I/O problem occurs */ public static TarArchiveEntry[] getEntries(File tarFile, boolean sortResult) throws IllegalArgumentException, IOException { Check.arg().validFile(tarFile); TarArchiveInputStream tais = new TarArchiveInputStream(getInputStream(tarFile)); // closed by getEntries below return getEntries(tais, sortResult); }