Example usage for org.apache.commons.compress.archivers.tar TarArchiveEntry getSize

List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveEntry getSize

Introduction

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

Prototype

public long getSize() 

Source Link

Document

Get this entry's file size.

Usage

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  ww  w .  j a v  a2  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.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;
    untarDir.mkdirs();/*from   www  .  jav  a2 s. c  o m*/
    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();
}

From source file:com.ibm.util.merge.CompareArchives.java

/**
 * @param archive/*from w  w  w. java  2 s . c o  m*/
 * @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:ie.pars.bnc.preprocess.MainBNCProcess.java

private static void getZippedFile() throws IOException, ArchiveException, Exception {
    String taggerPath = "edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger";
    String parseModel = LexicalizedParser.DEFAULT_PARSER_LOC;

    InputStream is = new FileInputStream(pathInput);
    TarArchiveInputStream tarStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    int countfiles = 0;
    while ((entry = (TarArchiveEntry) tarStream.getNextEntry()) != null) {
        //     for(File lf: listFiles){ 
        if (!entry.isDirectory()) {

            byte[] content = new byte[(int) entry.getSize()];
            int offset = 0;
            tarStream.read(content, offset, content.length - offset);
            String id = entry.getName().split("/")[entry.getName().split("/").length - 1].split(".xml")[0];

            if (!filesProcesed.contains(id) && id.startsWith(letter.toUpperCase())) {
                if (countfiles++ % 10 == 0) {
                    tagger = new MaxentTagger(taggerPath);
                    m = new Morphology();
                    parser = ParserGrammar.loadModel(parseModel);
                    parser.loadTagger();
                }/*from  w  ww  .  j a v a2 s. c  om*/
                System.out.print("Entry " + entry.getName());

                InputStream bis = new ByteArrayInputStream(content);
                StringBuilder parseBNCXML = ProcessNLP.parseBNCXML(bis, m, tagger, parser);
                bis.close();
                OutputStream out = new FileOutputStream(pathOutput + File.separatorChar + id + ".vert");
                Writer writer = new OutputStreamWriter(out, "UTF-8");

                writer.write("<text id=\"" + id + "\">\n");
                writer.write(parseBNCXML.toString());
                writer.write("</text>\n");
                writer.close();
                out.close();
            } else {
                System.out.println(">> Bypass Entry " + entry.getName());
            }
            //break;
        }

    }
    is.close();
    System.out.println("There are " + countfiles);
    //    tarStream.close();

}

From source file:com.github.pemapmodder.pocketminegui.utils.Utils.java

private static File installWindowsPHP(File home, InstallProgressReporter progress) {
    progress.report(0.0);/* www  . ja  v a2 s . co  m*/
    try {
        String link = System.getProperty("os.arch").contains("64")
                ? "https://bintray.com/artifact/download/pocketmine/PocketMine/PHP_7.0.3_x64_Windows.tar.gz"
                : "https://bintray.com/artifact/download/pocketmine/PocketMine/PHP_7.0.3_x86_Windows.tar.gz";
        URL url = new URL(link);
        InputStream gz = url.openStream();
        GzipCompressorInputStream tar = new GzipCompressorInputStream(gz);
        TarArchiveInputStream is = new TarArchiveInputStream(tar);
        TarArchiveEntry entry;
        while ((entry = is.getNextTarEntry()) != null) {
            if (!entry.isDirectory()) {
                String name = entry.getName();
                byte[] buffer = new byte[(int) entry.getSize()];
                IOUtils.read(is, buffer);
                File real = new File(home, name);
                real.getParentFile().mkdirs();
                IOUtils.write(buffer, new FileOutputStream(real));
            }
        }
        File output = new File(home, "bin/php/php.exe");
        progress.completed(output);
        return output;
    } catch (IOException e) {
        e.printStackTrace();
        progress.errored();
        return null;
    }
}

From source file:net.rwx.maven.asciidoc.utils.FileUtils.java

public static String uncompress(InputStream is, String destination) throws IOException {

    BufferedInputStream in = new BufferedInputStream(is);
    GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
    TarArchiveInputStream tarInput = new TarArchiveInputStream(gzIn);

    TarArchiveEntry entry = tarInput.getNextTarEntry();
    do {/* ww  w . ja  va 2 s . com*/
        File f = new File(destination + "/" + entry.getName());
        FileUtils.forceMkdir(f.getParentFile());

        if (!f.isDirectory()) {
            OutputStream os = new FileOutputStream(f);
            byte[] content = new byte[(int) entry.getSize()];
            int byteRead = 0;
            while (byteRead < entry.getSize()) {
                byteRead += tarInput.read(content, byteRead, content.length - byteRead);
                os.write(content, 0, byteRead);
            }

            os.close();
            forceDeleteOnExit(f);
        }
        entry = tarInput.getNextTarEntry();
    } while (entry != null);

    gzIn.close();

    return destination;
}

From source file:com.opentable.db.postgres.embedded.BundledPostgresBinaryResolver.java

/**
 * Unpack archive compressed by tar with bzip2 compression. By default system tar is used
 * (faster). If not found, then the java implementation takes place.
 *
 * @param tbzPath/*www. j ava  2 s. c  om*/
 *        The archive path.
 * @param targetDir
 *        The directory to extract the content to.
 */
private static void extractTxz(String tbzPath, String targetDir) throws IOException {
    try (InputStream in = Files.newInputStream(Paths.get(tbzPath));
            XZInputStream xzIn = new XZInputStream(in);
            TarArchiveInputStream tarIn = new TarArchiveInputStream(xzIn)) {
        TarArchiveEntry entry;

        while ((entry = tarIn.getNextTarEntry()) != null) {
            final String individualFile = entry.getName();
            final File fsObject = new File(targetDir + "/" + individualFile);

            if (entry.isSymbolicLink() || entry.isLink()) {
                Path target = FileSystems.getDefault().getPath(entry.getLinkName());
                Files.createSymbolicLink(fsObject.toPath(), target);
            } else if (entry.isFile()) {
                byte[] content = new byte[(int) entry.getSize()];
                int read = tarIn.read(content, 0, content.length);
                Verify.verify(read != -1, "could not read %s", individualFile);
                mkdirs(fsObject.getParentFile());
                try (OutputStream outputFile = new FileOutputStream(fsObject)) {
                    IOUtils.write(content, outputFile);
                }
            } else if (entry.isDirectory()) {
                mkdirs(fsObject);
            } else {
                throw new UnsupportedOperationException(
                        String.format("Unsupported entry found: %s", individualFile));
            }

            if (individualFile.startsWith("bin/") || individualFile.startsWith("./bin/")) {
                fsObject.setExecutable(true);
            }
        }
    }
}

From source file:eu.eubrazilcc.lvl.core.io.FileCompressor.java

/**
 * Uncompress the content of a tarball file to the specified directory.
 * @param srcFile - the tarball to be uncompressed
 * @param outDir - directory where the files (and directories) extracted from the tarball will be written
 * @return the list of files (and directories) extracted from the tarball
 * @throws IOException when an exception occurs on uncompressing the tarball
 *//*from  w  w  w  .j  a  v a2  s  .  co m*/
public static List<String> unGzipUnTar(final String srcFile, final String outDir) throws IOException {
    final List<String> uncompressedFiles = newArrayList();
    FileInputStream fileInputStream = null;
    BufferedInputStream bufferedInputStream = null;
    GzipCompressorInputStream gzipCompressorInputStream = null;
    TarArchiveInputStream tarArchiveInputStream = null;
    BufferedInputStream bufferedInputStream2 = null;

    try {
        forceMkdir(new File(outDir));
        fileInputStream = new FileInputStream(srcFile);
        bufferedInputStream = new BufferedInputStream(fileInputStream);
        gzipCompressorInputStream = new GzipCompressorInputStream(fileInputStream);
        tarArchiveInputStream = new TarArchiveInputStream(gzipCompressorInputStream);

        TarArchiveEntry entry = null;
        while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
            final File outputFile = new File(outDir, entry.getName());
            if (entry.isDirectory()) {
                // attempting to write output directory
                if (!outputFile.exists()) {
                    // attempting to create output directory
                    if (!outputFile.mkdirs()) {
                        throw new IOException("Cannot create directory: " + outputFile.getCanonicalPath());
                    }
                }
            } else {
                // attempting to create output file
                final byte[] content = new byte[(int) entry.getSize()];
                tarArchiveInputStream.read(content, 0, content.length);
                final FileOutputStream outputFileStream = new FileOutputStream(outputFile);
                write(content, outputFileStream);
                outputFileStream.close();
                uncompressedFiles.add(outputFile.getCanonicalPath());
            }
        }
    } finally {
        if (bufferedInputStream2 != null) {
            bufferedInputStream2.close();
        }
        if (tarArchiveInputStream != null) {
            tarArchiveInputStream.close();
        }
        if (gzipCompressorInputStream != null) {
            gzipCompressorInputStream.close();
        }
        if (bufferedInputStream != null) {
            bufferedInputStream.close();
        }
        if (fileInputStream != null) {
            fileInputStream.close();
        }
    }
    return uncompressedFiles;
}

From source file:com.espringtran.compressor4j.processor.TarProcessor.java

/**
 * Read from compressed file//from   w  w w.  ja  va  2  s .  co m
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    TarArchiveInputStream ais = new TarArchiveInputStream(bais);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        TarArchiveEntry entry = ais.getNextTarEntry();
        while (entry != null && entry.getSize() > 0) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = ais.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = ais.read(buffer);
            }
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = ais.getNextTarEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        baos.close();
        ais.close();
        bais.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}

From source file:com.espringtran.compressor4j.processor.TarBz2Processor.java

/**
 * Read from compressed file/*www  .ja v  a 2s . co  m*/
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    BZip2CompressorInputStream cis = new BZip2CompressorInputStream(bais);
    TarArchiveInputStream ais = new TarArchiveInputStream(cis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        TarArchiveEntry entry = ais.getNextTarEntry();
        while (entry != null && entry.getSize() > 0) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = ais.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = ais.read(buffer);
            }
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = ais.getNextTarEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        baos.close();
        ais.close();
        cis.close();
        bais.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}