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

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

Introduction

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

Prototype

public TarArchiveInputStream(InputStream is) 

Source Link

Document

Constructor for TarInputStream.

Usage

From source file:com.renard.ocr.help.OCRLanguageInstallService.java

@Override
protected void onHandleIntent(Intent intent) {
    if (!intent.hasExtra(DownloadManager.EXTRA_DOWNLOAD_ID)) {
        return;//  ww  w  .  j av  a 2s. c o  m
    }
    final long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
    if (downloadId != 0) {

        DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        ParcelFileDescriptor file;
        try {
            file = dm.openDownloadedFile(downloadId);
            FileInputStream fin = new FileInputStream(file.getFileDescriptor());
            BufferedInputStream in = new BufferedInputStream(fin);
            FileOutputStream out = openFileOutput("tess-lang.tmp", Context.MODE_PRIVATE);
            GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
            final byte[] buffer = new byte[2048 * 2];
            int n = 0;
            while (-1 != (n = gzIn.read(buffer))) {
                out.write(buffer, 0, n);
            }
            out.close();
            gzIn.close();
            FileInputStream fileIn = openFileInput("tess-lang.tmp");
            TarArchiveInputStream tarIn = new TarArchiveInputStream(fileIn);

            TarArchiveEntry entry = tarIn.getNextTarEntry();

            while (entry != null && !(entry.getName().endsWith(".traineddata")
                    && !entry.getName().endsWith("_old.traineddata"))) {
                entry = tarIn.getNextTarEntry();
            }
            if (entry != null) {
                File tessDir = Util.getTrainingDataDir(this);
                final String langName = entry.getName().substring("tesseract-ocr/tessdata/".length());
                File trainedData = new File(tessDir, langName);
                FileOutputStream fout = new FileOutputStream(trainedData);
                int len;
                while ((len = tarIn.read(buffer)) != -1) {
                    fout.write(buffer, 0, len);
                }
                fout.close();
                String lang = langName.substring(0, langName.length() - ".traineddata".length());
                notifyReceivers(lang);
            }
            tarIn.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            String tessDir = Util.getTessDir(this);
            File targetFile = new File(tessDir, OCRLanguageActivity.DOWNLOADED_TRAINING_DATA);
            if (targetFile.exists()) {
                targetFile.delete();
            }
        }

    }
}

From source file:adams.core.io.TarUtils.java

/**
 * Returns an input stream for the specified tar archive. Automatically
 * determines the compression used for the archive.
 *
 * @param file   the tar archive to create the input stream for
 * @param stream   the stream to wrap// w ww  .ja v a2s.c  om
 * @return      the input stream
 * @throws Exception   if file not found or similar problems
 */
protected static TarArchiveInputStream openArchiveForReading(File file, FileInputStream stream)
        throws Exception {
    Compression comp;

    comp = determineCompression(file);
    if (comp == Compression.GZIP)
        return new TarArchiveInputStream(new GzipCompressorInputStream(new BufferedInputStream(stream)));
    else if (comp == Compression.BZIP2)
        return new TarArchiveInputStream(new BZip2CompressorInputStream(new BufferedInputStream(stream)));
    else
        return new TarArchiveInputStream(new BufferedInputStream(stream));
}

From source file:gobblin.data.management.copy.converter.UnGzipConverterTest.java

private static String readGzipStreamAsString(InputStream is) throws Exception {
    TarArchiveInputStream tarIn = new TarArchiveInputStream(is);
    try {//w w  w  . j  a  v a  2s .  c o m
        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.espringtran.compressor4j.processor.XzProcessor.java

/**
 * Read from compressed file// w w w.  ja va 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);
    XZCompressorInputStream cis = new XZCompressorInputStream(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());
}

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

/**
 * Read from compressed file/*w  ww  .j a va2 s.  c o 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);
    GzipCompressorInputStream cis = new GzipCompressorInputStream(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());
}

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

/**
 * Read from compressed file//from   w  ww. j a  v  a2 s.c om
 * 
 * @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());
}

From source file:com.openshift.client.utils.TarFileTestUtils.java

/**
 * Returns all paths within the given archive.
 * // w w  w .j  a  v a  2  s. c o  m
 * @param in
 *            the archive
 * @return all paths
 * @throws IOException
 * @throws CompressorException
 */
public static List<String> getAllPaths(InputStream in) throws IOException {
    Assert.notNull(in);

    List<String> paths = new ArrayList<String>();
    TarArchiveInputStream archiveIn = new TarArchiveInputStream(new GZIPInputStream(in));
    try {
        for (ArchiveEntry nextEntry = null; (nextEntry = archiveIn.getNextEntry()) != null;) {
            paths.add(nextEntry.getName());
        }
        return paths;
    } finally {
        StreamUtils.close(archiveIn);
    }
}

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

private static File installWindowsPHP(File home, InstallProgressReporter progress) {
    progress.report(0.0);/*from w  w w . j av a 2  s . c om*/
    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:io.hightide.TemplateFetcher.java

public static Path extractTemplate(Path tempFile, Path targetDir) throws IOException {
    try (FileInputStream fin = new FileInputStream(tempFile.toFile());
            BufferedInputStream in = new BufferedInputStream(fin);
            GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
            TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn)) {
        TarArchiveEntry entry;//from  w  ww .ja va  2 s.  com
        Path rootDir = null;
        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
            System.out.println("Extracting: " + entry.getName());
            if (entry.isDirectory()) {
                if (isNull(rootDir)) {
                    rootDir = targetDir.resolve(entry.getName());
                }
                Files.createDirectory(targetDir.resolve(entry.getName()));
            } else {
                int count;
                byte data[] = new byte[BUFFER];

                FileOutputStream fos = new FileOutputStream(targetDir.resolve(entry.getName()).toFile());
                BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
                while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.close();
            }
        }

        return rootDir;
    }
}

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./*from  w  w  w  . ja va 2 s .  co  m*/
 *
 * @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();
    }
}