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

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

Introduction

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

Prototype

public int read(byte[] buf, int offset, int numToRead) throws IOException 

Source Link

Document

Reads bytes from the current tar archive entry.

Usage

From source file:edu.udel.ece.infolab.btc.Utils.java

/**
 * Read size bytes from the reader at the current position
 * //from  w ww  . ja  v  a2 s  .  c om
 * @param reader
 *          the TarArchiveInputStream reader
 * @param size
 *          the number of bytes to read
 * @param data
 *          the buffer to store the content
 * @throws IOException
 */
public static void getFile(final TarArchiveInputStream reader, long size, final StringBuilder data)
        throws IOException {
    bbuffer.clear();
    while (size > bbuffer.capacity()) {
        reader.read(bbuffer.array(), 0, bbuffer.capacity());
        size -= bbuffer.capacity();
        toAsciiString(data, bbuffer.capacity());
        bbuffer.clear();
    }
    reader.read(bbuffer.array(), 0, (int) size);
    toAsciiString(data, (int) size);
}

From source file:msec.org.TarUtil.java

private static void dearchiveFile(File destFile, TarArchiveInputStream tais) throws Exception {

    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));

    int count;/*www .  j av a  2s.  c  o  m*/
    byte data[] = new byte[BUFFERSZ];
    while ((count = tais.read(data, 0, BUFFERSZ)) != -1) {
        bos.write(data, 0, count);
    }

    bos.close();
}

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 w w .j ava  2s.  c  om
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  w w  w.j  a va  2 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: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;//www  .  ja  v  a2s .c  o  m
    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();
                }
                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: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 {//from  w  w w  .  j  a  v a2s .  c om
        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:examples.utils.CifarReader.java

public static void downloadAndExtract() {

    if (new File("data", TEST_DATA_FILE).exists() == false) {
        try {/*from ww w. ja v  a2 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.impetus.ankush.common.controller.listener.StartupListener.java

/**
 * Sets the agent version./*from ww w.  j av a2s  .  c  o m*/
 */
private static void setAgentVersion() {
    // current agent version
    String agentBuildVersion = new String();
    try {
        // Resource base path.
        String basePath = AppStoreWrapper.getResourcePath();
        // Creating agent bundle path.
        String agentBundlePath = basePath + "scripts/agent/" + AgentDeployer.AGENT_BUNDLE_NAME;

        FileInputStream fileInputStream = new FileInputStream(agentBundlePath);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        GzipCompressorInputStream gzInputStream = new GzipCompressorInputStream(bufferedInputStream);
        TarArchiveInputStream tarInputStream = new TarArchiveInputStream(gzInputStream);
        TarArchiveEntry entry = null;

        while ((entry = (TarArchiveEntry) tarInputStream.getNextEntry()) != null) {
            if (entry.getName().equals(AgentDeployer.AGENT_VERSION_FILENAME)) {
                final int BUFFER = 10;
                byte data[] = new byte[BUFFER];
                tarInputStream.read(data, 0, BUFFER);
                String version = new String(data);
                agentBuildVersion = version.trim();
                // Set the agent version in the AppStore with key as
                // agentVersion
                AppStore.setObject(AppStoreWrapper.KEY_AGENT_VERISON, agentBuildVersion);
            }
        }
    } catch (Exception e) {
        // log error message.
        log.error(e.getMessage(), e);
    }
}

From source file:com.goldmansachs.kata2go.tools.utils.TarGz.java

public static void decompressFromStream2(InputStream inputStream) throws IOException {
    GzipCompressorInputStream gzipStream = new GzipCompressorInputStream(inputStream);
    TarArchiveInputStream tarInput = new TarArchiveInputStream(gzipStream);
    TarArchiveEntry entry;//from  w  w  w. j a  v  a2  s  .  co m
    int bufferSize = 1024;
    while ((entry = (TarArchiveEntry) tarInput.getNextEntry()) != null) {
        String entryName = entry.getName();
        // strip out the leading directory like the --strip tar argument
        String entryNameWithoutLeadingDir = entryName.substring(entryName.indexOf("/") + 1);
        if (entryNameWithoutLeadingDir.isEmpty()) {
            continue;
        }
        if (entry.isDirectory()) {
            System.out.println("found dir " + entry.getName());
        } else {
            int count;
            byte data[] = new byte[bufferSize];
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((count = tarInput.read(data, 0, bufferSize)) != -1) {
                baos.write(data, 0, count);
            }
            JarOutputStream jarOutputStream = new JarOutputStream(baos);
            System.out.println(new String(baos.toByteArray()));
        }
    }
    tarInput.close();
    gzipStream.close();
}

From source file:at.illecker.sentistorm.commons.util.io.IOUtils.java

public static void extractTarGz(InputStream inputTarGzStream, String outDir, boolean logging) {
    try {//from   ww  w  .  j  a  v a  2  s  .c  o  m
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(inputTarGzStream);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

        // read Tar entries
        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
            if (logging) {
                LOG.info("Extracting: " + outDir + File.separator + entry.getName());
            }
            if (entry.isDirectory()) { // create directory
                File f = new File(outDir + File.separator + entry.getName());
                f.mkdirs();
            } else { // decompress file
                int count;
                byte data[] = new byte[EXTRACT_BUFFER_SIZE];

                FileOutputStream fos = new FileOutputStream(outDir + File.separator + entry.getName());
                BufferedOutputStream dest = new BufferedOutputStream(fos, EXTRACT_BUFFER_SIZE);
                while ((count = tarIn.read(data, 0, EXTRACT_BUFFER_SIZE)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.close();
            }
        }

        // close input stream
        tarIn.close();

    } catch (IOException e) {
        LOG.error("IOException: " + e.getMessage());
    }
}