Example usage for org.apache.commons.compress.archivers.tar TarArchiveInputStream close

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream close

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.tar TarArchiveInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this stream.

Usage

From source file:hudson.gridmaven.gridlayer.HadoopInstance.java

/**
 * This method decompress filesystem structure from HDFS archive
 *///from   w ww  .  j  av a  2  s . c  om
public void getAndUntar(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;
    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:io.covert.binary.analysis.BuildSequenceFileFromTarball.java

public void load(FileSystem fs, Configuration conf, File inputTarball, Path outputDir) throws Exception {
    Text key = new Text();
    BytesWritable val = new BytesWritable();

    Path sequenceName = new Path(outputDir, inputTarball.getName() + ".seq");
    System.out.println("Writing to " + sequenceName);
    SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, sequenceName, Text.class,
            BytesWritable.class, CompressionType.RECORD);

    InputStream is = new FileInputStream(inputTarball);
    if (inputTarball.toString().toLowerCase().endsWith(".gz")) {
        is = new GZIPInputStream(is);
    } else if (inputTarball.toString().toLowerCase().endsWith(".bz")
            || inputTarball.toString().endsWith(".bz2")) {
        is.read(); // read 'B'
        is.read(); // read 'Z'
        is = new CBZip2InputStream(is);
    }/*from  w  w  w .  ja  va 2s .  co  m*/

    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
        if (!entry.isDirectory()) {

            try {
                final ByteArrayOutputStream outputFileStream = new ByteArrayOutputStream();
                IOUtils.copy(debInputStream, outputFileStream);
                outputFileStream.close();
                byte[] outputFile = outputFileStream.toByteArray();
                val.set(outputFile, 0, outputFile.length);

                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(outputFile);
                byte[] digest = md.digest();
                String hexdigest = "";
                for (int i = 0; i < digest.length; i++) {
                    hexdigest += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1);
                }
                key.set(hexdigest);
                writer.append(key, val);
            } catch (IOException e) {
                System.err.println("Warning: tarball may be truncated: " + inputTarball);
                // Truncated Tarball
                break;
            }
        }
    }
    debInputStream.close();
    writer.close();
}

From source file:com.platform.APIClient.java

public boolean tryExtractTar(File inputFile) {
    String extractFolderName = MainActivity.app.getFilesDir().getAbsolutePath() + bundlesFileName + "/"
            + extractedFolder;//w  w w  . jav a 2  s .c om
    boolean result = false;
    TarArchiveInputStream debInputStream = null;
    try {
        final InputStream is = new FileInputStream(inputFile);
        debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {

            final String outPutFileName = entry.getName().replace("./", "");
            final File outputFile = new File(extractFolderName, outPutFileName);
            if (!entry.isDirectory()) {
                FileUtils.writeByteArrayToFile(outputFile,
                        org.apache.commons.compress.utils.IOUtils.toByteArray(debInputStream));
            }
        }

        result = true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (debInputStream != null)
                debInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;

}

From source file:com.jivesoftware.os.jive.utils.shell.utils.Untar.java

public static List<File> unTar(boolean verbose, final File outputDir, final File inputFile,
        boolean deleteOriginal) throws FileNotFoundException, IOException, ArchiveException {

    if (verbose) {
        System.out.println(String.format("untaring %s to dir %s.", inputFile.getAbsolutePath(),
                outputDir.getAbsolutePath()));
    }//from   w  w  w  .  j  a v  a 2  s . c  o m

    final List<File> untaredFiles = new LinkedList<>();
    final InputStream is = new FileInputStream(inputFile);
    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
        String entryName = entry.getName();
        entryName = entryName.substring(entryName.indexOf("/") + 1);
        final File outputFile = new File(outputDir, entryName);
        if (entry.isDirectory()) {
            if (verbose) {
                System.out.println(String.format("Attempting to write output directory %s.",
                        getRelativePath(outputDir, outputFile)));
            }
            if (!outputFile.exists()) {
                if (verbose) {
                    System.out.println(String.format("Attempting to create output directory %s.",
                            getRelativePath(outputDir, outputFile)));
                }
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(String.format("Couldn't create directory %s.",
                            getRelativePath(outputDir, outputFile)));
                }
            }
        } else {
            try {
                if (verbose) {
                    System.out.println(
                            String.format("Creating output file %s.", getRelativePath(outputDir, outputFile)));
                }
                outputFile.getParentFile().mkdirs();
                final OutputStream outputFileStream = new FileOutputStream(outputFile);
                IOUtils.copy(debInputStream, outputFileStream);
                outputFileStream.close();

                if (getRelativePath(outputDir, outputFile).contains("bin/")
                        || outputFile.getName().endsWith(".sh")) { // Hack!
                    if (verbose) {
                        System.out.println(
                                String.format("chmod +x file %s.", getRelativePath(outputDir, outputFile)));
                    }
                    outputFile.setExecutable(true);
                }

            } catch (Exception x) {
                System.err.println("failed to untar " + getRelativePath(outputDir, outputFile) + " " + x);
            }
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();

    if (deleteOriginal) {
        FileUtils.forceDelete(inputFile);
        if (verbose) {
            System.out.println(String.format("deleted original file %s.", inputFile.getAbsolutePath()));
        }
    }
    return untaredFiles;
}

From source file:com.boxupp.utilities.PuppetUtilities.java

private void extrectFile(String saveFilePath, String moduleDirPath, String moduleName) {
    try {/* www .  j a va 2  s .co  m*/
        File file = new File(saveFilePath);
        FileInputStream fin = new FileInputStream(file);
        BufferedInputStream in = new BufferedInputStream(fin);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);
        TarArchiveEntry entry = null;
        int length = 0;
        File unzipFile = null;
        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                if (length == 0) {
                    File f = new File(moduleDirPath + entry.getName());
                    f.mkdirs();
                    unzipFile = f;
                    length++;
                } else {
                    File f = new File(moduleDirPath + entry.getName());
                    f.mkdirs();
                }
            } else {
                int count;
                byte data[] = new byte[4096];
                FileOutputStream fos;
                fos = new FileOutputStream(moduleDirPath + entry.getName());
                BufferedOutputStream dest = new BufferedOutputStream(fos, 4096);
                while ((count = tarIn.read(data, 0, 4096)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.close();
            }

        }
        File renamedFile = new File(moduleDirPath + "/" + moduleName);
        if (unzipFile.isDirectory()) {
            unzipFile.renameTo(renamedFile);
        }
        tarIn.close();

    } catch (IOException e) {
        logger.error("Error in unzip the module file :" + e.getMessage());
    }
}

From source file:in.neoandroid.neoupdate.neoUpdate.java

private String processFromNPK() {
    try {//  w ww  .  j a  v  a2  s  .  com
        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:com.sshtools.common.vomanagementtool.common.VOHelper.java

private static void untar(InputStream in, String untarDir) throws IOException {

    TarArchiveInputStream tin = new TarArchiveInputStream(in);
    TarArchiveEntry entry = tin.getNextTarEntry();

    //TarInputStream tin = new TarInputStream(in);
    //TarEntry tarEntry = tin.getNextEntry();
    if (new File(untarDir).exists()) {
        //To handle the normal files (not symbolic files)
        while (entry != null) {
            if (!entry.isSymbolicLink()) {
                String filename = untarDir + File.separatorChar + entry.getName();
                File destPath = new File(filename);

                if (!entry.isDirectory()) {
                    FileOutputStream fout = new FileOutputStream(destPath);
                    IOUtils.copy(tin, fout);
                    //tin.copyEntryContents(fout);
                    fout.close();/*from  w  w w . j a  v  a  2 s.co m*/
                } else {
                    if (!destPath.exists()) {
                        boolean success = destPath.mkdir();
                        if (!success) {
                            throw new IOException("Couldn't create directory for VOMS support: "
                                    + destPath.getAbsolutePath());
                        }
                    }
                }
            }
            entry = tin.getNextTarEntry();
            //tarEntry = tin.getNextEntry();
        }
        //tin.close();

        //To handle the symbolic link files
        tin = new TarArchiveInputStream(in);
        entry = tin.getNextTarEntry();
        while (entry != null) {

            if (entry.isSymbolicLink()) {
                File srcPath = new File(untarDir + File.separatorChar + entry.getLinkName());
                File destPath = new File(untarDir + File.separatorChar + entry.getName());
                copyFile(srcPath, destPath);

                //tarEntry = tin.getNextEntry();
            }
            entry = tin.getNextTarEntry();
        }
        tin.close();
    } else {
        throw new IOException("Couldn't find directory: " + untarDir);
    }

}

From source file:net.sf.sveditor.core.tests.utils.BundleUtils.java

public void unpackBundleTarToFS(String bundle_path, File fs_path) {
    URL url = fBundle.getEntry(bundle_path);
    TestCase.assertNotNull(url);//from   ww w . j a  v  a2  s .c o  m

    if (!fs_path.isDirectory()) {
        TestCase.assertTrue(fs_path.mkdirs());
    }
    InputStream in = null;
    TarArchiveInputStream tar_stream = null;

    try {
        in = url.openStream();
    } catch (IOException e) {
        TestCase.fail("Failed to open data file " + bundle_path + " : " + e.getMessage());
    }

    tar_stream = new TarArchiveInputStream(in);

    try {
        byte tmp[] = new byte[4 * 1024];
        int cnt;

        ArchiveEntry te;

        while ((te = tar_stream.getNextEntry()) != null) {
            // System.out.println("Entry: \"" + ze.getName() + "\"");
            File entry_f = new File(fs_path, te.getName());
            if (te.getName().endsWith("/")) {
                // Directory
                continue;
            }
            if (!entry_f.getParentFile().exists()) {
                TestCase.assertTrue(entry_f.getParentFile().mkdirs());
            }
            FileOutputStream fos = new FileOutputStream(entry_f);
            BufferedOutputStream bos = new BufferedOutputStream(fos, tmp.length);

            while ((cnt = tar_stream.read(tmp, 0, tmp.length)) > 0) {
                bos.write(tmp, 0, cnt);
            }
            bos.flush();
            bos.close();
            fos.close();

            //            tar_stream.closeEntry();
        }
        tar_stream.close();
    } catch (IOException e) {
        e.printStackTrace();
        TestCase.fail("Failed to unpack tar file: " + e.getMessage());
    }
}

From source file:com.alcatel_lucent.nz.wnmsextract.reader.FileSelector.java

/**
 * extract tarfile to constituent parts processing gzips along the way
 * yyyyMMdd.tar->/yyyyMMdd/INode-CH_RNC01/A2010...gz
 *///w  ww  . j av  a  2 s .  co m
protected void untar(File tf) throws FileNotFoundException {

    try {
        TarArchiveInputStream tais = new TarArchiveInputStream(new FileInputStream(tf));
        TarArchiveEntry t1 = null;
        while ((t1 = tais.getNextTarEntry()) != null) {
            if (t1.isDirectory()) {
                if (t1.getName().contains("account"))
                    identifier = ".vcc";
                else
                    identifier = "";
            } else {
                String fn = t1.getName().substring(t1.getName().lastIndexOf("/"));
                File f = new File(getCalTempPath() + fn);
                FileOutputStream fos = new FileOutputStream(f);
                BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

                int n = 0;
                byte[] content = new byte[BUFFER];
                while (-1 != (n = tais.read(content))) {
                    fos.write(content, 0, n);
                }

                bos.flush();
                bos.close();
                fos.close();

                File unz = null;
                if (f.getName().endsWith("zip"))
                    unz = unzip3(f);
                else
                    unz = ungzip(f);

                if (unz != null)
                    allfiles.add(unz);
                f.delete();
            }
        }
        tais.close();
    } catch (IOException ioe) {
        jlog.fatal("IO read error :: " + ioe);
    }

}

From source file:com.baidu.rigel.biplatform.tesseract.util.FileUtils.java

/**
 * Uncompress the incoming file.//from  ww  w. jav a  2s .c  o m
 * 
 * @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;
}