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:org.mcisb.subliminal.SubliminalUtils.java

/**
 * //from  ww w  .j a  va  2 s. c  o m
 * @param url
 * @param destinationDirectory
 * @throws IOException
 */
public static void untar(final URL url, final File destinationDirectory) throws IOException {
    if (!destinationDirectory.exists()) {
        if (!destinationDirectory.mkdir()) {
            throw new IOException();
        }
    }

    TarArchiveInputStream is = null;

    try {
        is = new TarArchiveInputStream(new GZIPInputStream(url.openStream()));
        ArchiveEntry tarEntry = null;

        while ((tarEntry = is.getNextEntry()) != null) {
            final File destination = new File(destinationDirectory, tarEntry.getName());

            if (tarEntry.isDirectory()) {
                if (!destination.mkdir()) {
                    // Take no action.
                    // throw new IOException();
                }
            } else {
                try (final OutputStream os = new FileOutputStream(destination)) {
                    new StreamReader(is, os).read();
                }
            }
        }
    } catch (IOException e) {
        if (!destinationDirectory.delete()) {
            throw new IOException();
        }
        throw e;
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:org.mcisb.util.io.TarUtils.java

/**
 * //w  ww. j  a  va2 s.  c o m
 * @param tarInputStream
 * @param destinationDirectory
 * @throws IOException
 */
public static void untar(final InputStream tarInputStream, final File destinationDirectory) throws IOException {
    if (!destinationDirectory.exists()) {
        if (!destinationDirectory.mkdir()) {
            throw new IOException();
        }
    }

    TarArchiveInputStream is = null;

    try {
        is = new TarArchiveInputStream(new GZIPInputStream(tarInputStream));
        ArchiveEntry tarEntry = null;

        while ((tarEntry = is.getNextEntry()) != null) {
            final File destination = new File(destinationDirectory, tarEntry.getName());

            if (tarEntry.isDirectory()) {
                if (!destination.mkdir()) {
                    // Take no action.
                    // throw new IOException();
                }
            } else {
                try (final OutputStream os = new FileOutputStream(destination)) {
                    new StreamReader(is, os).read();
                }
            }
        }
    } catch (IOException e) {
        if (!destinationDirectory.delete()) {
            throw new IOException();
        }
        throw e;
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:org.mitre.xtext.converters.ArchiveNavigator.java

public File untar(File tarFile) throws IOException {

    String _working = tempDir.getAbsolutePath() + File.separator + FilenameUtils.getBaseName(tarFile.getPath());
    File workingDir = new File(_working);
    workingDir.mkdir();//from  ww  w.  j a  v a  2s .  c  o  m

    InputStream input = new BufferedInputStream(new FileInputStream(tarFile));
    try {
        TarArchiveInputStream in = (TarArchiveInputStream) (new ArchiveStreamFactory()
                .createArchiveInputStream("tar", input));

        TarArchiveEntry tarEntry;
        while ((tarEntry = (TarArchiveEntry) in.getNextEntry()) != null) {
            if (filterEntry(tarEntry)) {
                continue;
            }

            try {
                File tmpFile = saveArchiveEntry(tarEntry, in, _working);
                converter.convert(tmpFile);
            } catch (IOException err) {
                log.error("Unable to save item, FILE=" + tarFile.getName() + "!" + tarEntry.getName(), err);
            }
        }
        in.close();
    } catch (ArchiveException ae) {
        throw new IOException(ae);
    }
    return workingDir;
}

From source file:org.mulima.internal.freedb.FreeDbTarDaoImpl.java

/**
 * {@inheritDoc}//from  w  w  w  .  j  a  v a  2s  .  c o  m
 */
@Override
public List<Disc> getAllDiscsFromOffset(int startNum, int numToRead) {
    FileInputStream fin = null;
    BufferedInputStream bfin = null;
    TarArchiveInputStream tin = null;
    List<Disc> discs = new ArrayList<Disc>();
    try {
        fin = new FileInputStream(tarArchive);
        bfin = new BufferedInputStream(fin);
        tin = new TarArchiveInputStream(bfin);

        int currentNum = 0;
        TarArchiveEntry entry = tin.getNextTarEntry();
        ProgressBar progress = new SLF4JProgressBar("TAR getDiscs", numToRead);
        while (entry != null && (numToRead < 0 || currentNum < startNum + numToRead)) {
            if (!entry.isDirectory() && currentNum >= startNum) {
                logger.debug("Loading: " + entry.getName());
                int offset = 0;
                byte[] content = new byte[(int) entry.getSize()];
                while (offset < content.length) {
                    offset += tin.read(content, offset, content.length - offset);
                }
                Disc disc = bytesToDisc(content);
                if (disc == null) {
                    logger.warn("Invalid file: " + entry.getName());
                } else {
                    logger.debug(disc.toString());
                    discs.add(disc);
                }
            }

            entry = tin.getNextTarEntry();
            currentNum++;
            progress.next();
        }

        if (entry == null) {
            progress.done();
        }
    } catch (IOException e) {
        logger.error("Problem reading tar archive.", e);
        throw new UncheckedIOException("Problem reading tar archive.", e);
    } finally {
        try {
            if (tin != null) {
                tin.close();
            } else if (bfin != null) {
                bfin.close();
            } else if (fin != null) {
                fin.close();
            }
        } catch (IOException e) {
            logger.error("Problem closing streams.", e);
        }
    }
    return discs;
}

From source file:org.nd4j.util.ArchiveUtils.java

/**
 * Extracts files to the specified destination
 *
 * @param file the file to extract to/*from  ww w.jav  a  2  s .c om*/
 * @param dest the destination directory
 * @throws IOException
 */
public static void unzipFileTo(String file, String dest) throws IOException {
    File target = new File(file);
    if (!target.exists())
        throw new IllegalArgumentException("Archive doesnt exist");
    FileInputStream fin = new FileInputStream(target);
    int BUFFER = 2048;
    byte data[] = new byte[BUFFER];

    if (file.endsWith(".zip") || file.endsWith(".jar")) {
        try (ZipInputStream zis = new ZipInputStream(fin)) {
            //get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();

            while (ze != null) {
                String fileName = ze.getName();
                File newFile = new File(dest + File.separator + fileName);

                if (ze.isDirectory()) {
                    newFile.mkdirs();
                    zis.closeEntry();
                    ze = zis.getNextEntry();
                    continue;
                }

                FileOutputStream fos = new FileOutputStream(newFile);

                int len;
                while ((len = zis.read(data)) > 0) {
                    fos.write(data, 0, len);
                }

                fos.close();
                ze = zis.getNextEntry();
                log.debug("File extracted: " + newFile.getAbsoluteFile());
            }

            zis.closeEntry();
        }
    } else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) {

        BufferedInputStream in = new BufferedInputStream(fin);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

        TarArchiveEntry entry;
        /* Read the tar entries using the getNextEntry method **/
        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
            log.info("Extracting: " + entry.getName());
            /* If the entry is a directory, create the directory. */

            if (entry.isDirectory()) {
                File f = new File(dest + File.separator + entry.getName());
                f.mkdirs();
            }
            /*
             * If the entry is a file,write the decompressed file to the disk
             * and close destination stream.
             */
            else {
                int count;
                try (FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
                        BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);) {
                    while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                        destStream.write(data, 0, count);
                    }

                    destStream.flush();
                    IOUtils.closeQuietly(destStream);
                }
            }
        }

        // Close the input stream
        tarIn.close();
    } else if (file.endsWith(".gz")) {
        File extracted = new File(target.getParent(), target.getName().replace(".gz", ""));
        if (extracted.exists())
            extracted.delete();
        extracted.createNewFile();
        try (GZIPInputStream is2 = new GZIPInputStream(fin);
                OutputStream fos = FileUtils.openOutputStream(extracted)) {
            IOUtils.copyLarge(is2, fos);
            fos.flush();
        }
    } else {
        throw new IllegalStateException(
                "Unable to infer file type (compression format) from source file name: " + file);
    }
    target.delete();
}

From source file:org.ngrinder.common.util.CompressionUtil.java

/**
 * Untar an input file into an output file.
 * //from w w w .  j  av a 2 s.  com
 * The output file is created in the output folder, having the same name as the input file,
 * minus the '.tar' extension.
 * 
 * @param inFile
 *            the input .tar file
 * @param outputDir
 *            the output directory file.
 * @throws IOException
 * @throws FileNotFoundException
 * 
 * @return The {@link List} of {@link File}s with the untared content.
 * @throws ArchiveException
 */
public static List<File> untar(final File inFile, final File outputDir) {
    final List<File> untaredFiles = new LinkedList<File>();
    try {
        final InputStream is = new FileInputStream(inFile);
        final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
                .createArchiveInputStream("tar", is);
        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
            final File outputFile = new File(outputDir, entry.getName());
            if (entry.isDirectory()) {
                if (!outputFile.exists()) {
                    if (!outputFile.mkdirs()) {
                        throw new IllegalStateException(
                                String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                    }
                }
            } else {
                File parentFile = outputFile.getParentFile();
                if (!parentFile.exists()) {
                    parentFile.mkdirs();
                }
                final OutputStream outputFileStream = new FileOutputStream(outputFile);

                IOUtils.copy(debInputStream, outputFileStream);
                outputFileStream.close();
                if (FilenameUtils.isExtension(outputFile.getName(), EXECUTABLE_EXTENSION)) {
                    outputFile.setExecutable(true, true);
                }
                outputFile.setReadable(true);
                outputFile.setWritable(true, true);
            }
            untaredFiles.add(outputFile);
        }
        debInputStream.close();
    } catch (Exception e) {
        LOGGER.error("Error while untar {} file by {}", inFile, e.getMessage());
        LOGGER.debug("Trace is : ", e);
        throw new NGrinderRuntimeException("Error while untar file", e);
    }
    return untaredFiles;
}

From source file:org.onebusaway.util.FileUtility.java

/**
 * Untar an input file into an output file.
 * // w  w  w .  j a  v  a 2  s .c  o m
 * The output file is created in the output folder, having the same name as
 * the input file, minus the '.tar' extension.
 * 
 * @param inputFile the input .tar file
 * @param outputDir the output directory file.
 * @throws IOException
 * @throws FileNotFoundException
 * 
 * @return The {@link List} of {@link File}s with the untared content.
 * @throws ArchiveException
 */
public List<File> unTar(final File inputFile, final File outputDir)
        throws FileNotFoundException, IOException, ArchiveException {

    _log.info(
            String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));

    final List<File> untaredFiles = new LinkedList<File>();
    final InputStream is = new FileInputStream(inputFile);
    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
        final File outputFile = new File(outputDir, entry.getName());
        if (entry.isDirectory()) {
            _log.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
            if (!outputFile.exists()) {
                _log.info(String.format("Attempting to create output directory %s.",
                        outputFile.getAbsolutePath()));
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(
                            String.format("CHUNouldn't create directory %s.", outputFile.getAbsolutePath()));
                }
            }
        } else {
            _log.info(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            IOUtils.copy(debInputStream, outputFileStream);
            outputFileStream.close();
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();

    return untaredFiles;
}

From source file:org.openjump.core.ui.plugin.file.open.OpenFileWizardState.java

public void setupFileLoaders(File[] files, FileLayerLoader fileLayerLoader) {
    Set<File> fileSet = new TreeSet<File>(Arrays.asList(files));
    multiLoaderFiles.clear();//from  ww  w. j av  a  2s  . com
    // explicit loader chosen
    if (fileLayerLoader != null) {
        fileLoaderMap.clear();
        for (File file : fileSet) {
            setFileLoader(file.toURI(), fileLayerLoader);
        }
    } else {
        // Remove old entries in fileloadermap
        fileLoaderMap.clear();
        //      for (Iterator<Entry<URI, FileLayerLoader>> iterator = fileLoaderMap.entrySet()
        //        .iterator(); iterator.hasNext();) {
        //        Entry<URI, FileLayerLoader> entry = iterator.next();
        //        URI fileUri = entry.getKey();
        //        File file;

        //        if (fileUri.getScheme().equals("zip")) {
        //          file = UriUtil.getZipFile(fileUri);
        //        } else {
        //          file = new File(fileUri);
        //        }
        //        
        //        if (!fileSet.contains(file)) {
        //          FileLayerLoader loader = entry.getValue();
        //          fileLoaderFiles.get(loader);
        //          Set<URI> loaderFiles = fileLoaderFiles.get(loader);
        //          if (loaderFiles != null) {
        //            loaderFiles.remove(fileUri);
        //          }
        //          iterator.remove();
        //        }
        //      }

        // manually add compressed files here
        for (File file : files) {
            // zip files
            if (CompressedFile.isZip(file.getName())) {
                try {
                    ZipFile zipFile = new ZipFile(file);
                    URI fileUri = file.toURI();
                    Enumeration entries = zipFile.getEntries();
                    while (entries.hasMoreElements()) {
                        ZipArchiveEntry entry = (ZipArchiveEntry) entries.nextElement();
                        if (!entry.isDirectory()) {
                            URI entryUri = UriUtil.createZipUri(file, entry.getName());
                            String entryExt = UriUtil.getFileExtension(entryUri);
                            //System.out.println(entryUri+"<->"+entryExt);
                            addFile(entryExt, entryUri);
                        }
                    }
                } catch (Exception e) {
                    errorHandler.handleThrowable(e);
                }
            }
            // tar[.gz,.bz...] (un)compressed archive files
            else if (CompressedFile.isTar(file.getName())) {
                try {
                    InputStream is = CompressedFile.openFile(file.getAbsolutePath(), null);
                    TarArchiveEntry entry;
                    TarArchiveInputStream tis = new TarArchiveInputStream(is);
                    while ((entry = tis.getNextTarEntry()) != null) {
                        if (!entry.isDirectory()) {
                            URI entryUri = UriUtil.createZipUri(file, entry.getName());

                            String entryExt = UriUtil.getFileExtension(entryUri);
                            addFile(entryExt, entryUri);
                        }
                    }
                    tis.close();
                } catch (Exception e) {
                    errorHandler.handleThrowable(e);
                }
            }
            // 7zip compressed files
            else if (CompressedFile.isSevenZ(file.getName())) {
                try {
                    //System.out.println(file.getName());
                    SevenZFile sevenZFile = new SevenZFile(file);
                    SevenZArchiveEntry entry;
                    while ((entry = sevenZFile.getNextEntry()) != null) {
                        if (!entry.isDirectory()) {
                            URI entryUri = UriUtil.createZipUri(file, entry.getName());

                            String entryExt = UriUtil.getFileExtension(entryUri);
                            addFile(entryExt, entryUri);
                        }
                    }
                    sevenZFile.close();
                } catch (IOException e) {
                    errorHandler.handleThrowable(e);
                }
            }
            // compressed files
            else if (CompressedFile.hasCompressedFileExtension(file.getName())) {
                String[] parts = file.getName().split("\\.");
                if (parts.length > 2)
                    addFile(parts[parts.length - 2], file.toURI());
            }
            // anything else is a plain data file
            else {
                URI fileUri = file.toURI();
                addFile(FileUtil.getExtension(file), fileUri);
            }
        }
    }
}

From source file:org.openmrs.module.openconceptlab.client.OclClient.java

@SuppressWarnings("resource")
public OclResponse ungzipAndUntarResponse(InputStream response, Date date) throws IOException {
    GZIPInputStream gzipIn = new GZIPInputStream(response);
    TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn);
    boolean foundEntry = false;
    try {/*from  w  ww .j a  va2  s. c  o  m*/
        TarArchiveEntry entry = tarIn.getNextTarEntry();
        while (entry != null) {
            if (entry.getName().equals("export.json")) {
                foundEntry = true;
                return new OclResponse(tarIn, entry.getSize(), date);
            }
            entry = tarIn.getNextTarEntry();
        }

        tarIn.close();
    } finally {
        if (!foundEntry) {
            IOUtils.closeQuietly(tarIn);
        }
    }
    throw new IOException("Unsupported format of response. Expected tar.gz archive with export.json.");
}

From source file:org.opensextant.xtext.collectors.ArchiveNavigator.java

public File untar(File tarFile) throws IOException, ConfigException {

    String _working = FilenameUtils.concat(getWorkingDir(), FilenameUtils.getBaseName(tarFile.getPath()));
    if (_working == null) {
        throw new IOException("Invalid archive path for " + tarFile.getPath());
    }//from   w  w  w.ja  v  a  2 s  . c  o m
    File workingDir = new File(_working);
    workingDir.mkdir();

    InputStream input = new BufferedInputStream(new FileInputStream(tarFile));
    TarArchiveInputStream in = null;
    try {
        in = (TarArchiveInputStream) (new ArchiveStreamFactory().createArchiveInputStream("tar", input));

        TarArchiveEntry tarEntry;
        while ((tarEntry = (TarArchiveEntry) in.getNextEntry()) != null) {
            if (filterEntry(tarEntry)) {
                continue;
            }

            try {
                File tmpFile = saveArchiveEntry(tarEntry, in, _working);
                converter.convert(tmpFile);
            } catch (IOException err) {
                log.error("Unable to save item, FILE=" + tarFile.getName() + "!" + tarEntry.getName(), err);
            }
        }
    } catch (ArchiveException ae) {
        throw new IOException(ae);
    } finally {
        in.close();
    }
    return workingDir;
}