Example usage for org.apache.commons.compress.compressors.bzip2 BZip2Utils isCompressedFilename

List of usage examples for org.apache.commons.compress.compressors.bzip2 BZip2Utils isCompressedFilename

Introduction

In this page you can find the example usage for org.apache.commons.compress.compressors.bzip2 BZip2Utils isCompressedFilename.

Prototype

public static boolean isCompressedFilename(String filename) 

Source Link

Document

Detects common bzip2 suffixes in the given filename.

Usage

From source file:net.sf.util.zip.FileNameUtil.java

/**
 *
 * @param name the file name/*from   w  w  w.j av a 2s.co  m*/
 * @return whether the file is an valid compressed file or not (based on suffix)
 */
public static boolean isCompressedFile(String name) {
    if (BZip2Utils.isCompressedFilename(name) || GzipUtils.isCompressedFilename(name)
            || XZUtils.isCompressedFilename(name))
        return true;
    return false;
}

From source file:net.sf.util.zip.FileNameUtil.java

/**
 *
 * @param fileName  the file name/*  www.  jav a  2s  . co m*/
 * @return   Compressed file type, as defined in CompressorStreamFactory
 */
public static String[] getCompressFileType(String fileName) {
    String s = fileName.toLowerCase();
    String[] ret = { null, null };
    if (GzipUtils.isCompressedFilename(s)) {
        ret[0] = CompressorStreamFactory.GZIP;
        ret[1] = GzipUtils.getUncompressedFilename(fileName);
    } else if (BZip2Utils.isCompressedFilename(s)) {
        ret[0] = CompressorStreamFactory.BZIP2;
        ret[1] = BZip2Utils.getUncompressedFilename(fileName);
    } else if (XZUtils.isCompressedFilename(s)) {
        ret[0] = CompressorStreamFactory.XZ;
        ret[1] = XZUtils.getUncompressedFilename(fileName);
    }

    return ret;
}

From source file:io.ecarf.core.compress.NTripleGzipProcessor.java

/**
 * Get a deflated stream from the provided input
 * @param input/*from  w w  w.j  a  v  a 2 s.c o  m*/
 * @return
 * @throws IOException
 */
private InputStream getDeflatedStream(InputStream input) throws IOException {

    InputStream deflated = input;

    // gzip
    if (GzipUtils.isCompressedFilename(this.inputFile)) {
        deflated = new GZIPInputStream(input, Constants.GZIP_BUF_SIZE);

    }
    // bz2
    else if (BZip2Utils.isCompressedFilename(this.inputFile)) {
        deflated = new BZip2CompressorInputStream(new BufferedInputStream(input));
    }

    return deflated;
}

From source file:io.ecarf.core.compress.NxGzipProcessor.java

/**
 * Get a deflated stream from the provided input
 * @param input//  w  w  w .  ja  v  a 2 s.  co m
 * @return
 * @throws IOException
 */
private InputStream getDeflatedInputStream(InputStream input) throws IOException {

    InputStream deflated = input;

    // gzip
    if (GzipUtils.isCompressedFilename(this.inputFile)) {
        deflated = new GZIPInputStream(input, Constants.GZIP_BUF_SIZE);

    }
    // bz2
    else if (BZip2Utils.isCompressedFilename(this.inputFile)) {
        deflated = new BZip2CompressorInputStream(new BufferedInputStream(input));
    }

    return deflated;
}

From source file:io.ecarf.core.compress.NxGzipProcessor.java

/**
 * Get inflated output stream form the provided output stream
 * @param output//  w  w w. ja va 2 s. c o m
 * @return
 * @throws IOException
 */
private OutputStream getInflatedOutputStream(OutputStream output) throws IOException {
    OutputStream inflated = output;

    // gzip
    if (GzipUtils.isCompressedFilename(this.inputFile)) {
        inflated = new GZIPOutputStream(output, Constants.GZIP_BUF_SIZE);

    }
    // bz2
    else if (BZip2Utils.isCompressedFilename(this.inputFile)) {
        inflated = new BZip2CompressorOutputStream(new BufferedOutputStream(output));
    }

    return inflated;
}

From source file:algorithm.TarPackaging.java

@Override
protected List<RestoredFile> restore(File tarFile) throws IOException {
    List<RestoredFile> extractedFiles = new ArrayList<RestoredFile>();
    FileInputStream inputStream = new FileInputStream(tarFile);
    if (GzipUtils.isCompressedFilename(tarFile.getName())) {
        TarArchiveInputStream tarInputStream = new TarArchiveInputStream(
                new GzipCompressorInputStream(new BufferedInputStream(inputStream)));
        TarArchiveEntry entry = tarInputStream.getNextTarEntry();
        while (entry != null) {
            RestoredFile outputFile = new RestoredFile(RESTORED_DIRECTORY + entry.getName());
            if (entry.isDirectory()) {
                if (!outputFile.exists()) {
                    outputFile.mkdirs();
                }/*from   w w  w  .java 2 s . c o  m*/
            } else if (entry.isFile()) {
                FileOutputStream outputFileStream = new FileOutputStream(outputFile);
                IOUtils.copy(tarInputStream, outputFileStream);
                outputFileStream.close();
            }
            extractedFiles.add(outputFile);
            entry = tarInputStream.getNextTarEntry();
        }
        tarInputStream.close();
    } else if (BZip2Utils.isCompressedFilename(tarFile.getName())) {
        TarArchiveInputStream tarInputStream = new TarArchiveInputStream(
                new BZip2CompressorInputStream(new BufferedInputStream(inputStream)));
        TarArchiveEntry entry = tarInputStream.getNextTarEntry();
        while (entry != null) {
            RestoredFile outputFile = new RestoredFile(RESTORED_DIRECTORY + entry.getName());
            if (entry.isDirectory()) {
                if (!outputFile.exists()) {
                    outputFile.mkdirs();
                }
            } else if (entry.isFile()) {
                FileOutputStream outputFileStream = new FileOutputStream(outputFile);
                IOUtils.copy(tarInputStream, outputFileStream);
                outputFileStream.close();
            }
            extractedFiles.add(outputFile);
            entry = tarInputStream.getNextTarEntry();
        }
        tarInputStream.close();
    } else {
        TarArchiveInputStream tarInputStream = new TarArchiveInputStream(new BufferedInputStream(inputStream));
        TarArchiveEntry entry = tarInputStream.getNextTarEntry();
        while (entry != null) {
            RestoredFile outputFile = new RestoredFile(RESTORED_DIRECTORY + entry.getName());
            if (entry.isDirectory()) {
                if (!outputFile.exists()) {
                    outputFile.mkdirs();
                }
            } else if (entry.isFile()) {
                FileOutputStream outputFileStream = new FileOutputStream(outputFile);
                IOUtils.copy(tarInputStream, outputFileStream);
                outputFileStream.close();
            }
            extractedFiles.add(outputFile);
            entry = tarInputStream.getNextTarEntry();
        }
        tarInputStream.close();
    }
    for (RestoredFile file : extractedFiles) {
        file.algorithm = this;
        file.checksumValid = true;
        file.restorationNote = "The algorithm doesn't alter these files, so they are brought to its original state. No checksum validation executed.";
        // All files in a .tar file are payload files:
        file.wasPayload = true;
        for (RestoredFile relatedFile : extractedFiles) {
            if (file != relatedFile) {
                file.relatedFiles.add(relatedFile);
            }
        }
    }
    return extractedFiles;
}

From source file:edu.harvard.iq.dvn.core.web.study.AddFilesPage.java

private List<StudyFileEditBean> createStudyFilesFromTar(File uploadedInputFile) {
    List<StudyFileEditBean> fbList = new ArrayList<StudyFileEditBean>();

    File dir = new File(uploadedInputFile.getParentFile(), study.getId().toString());
    if (!dir.exists()) {
        dir.mkdir();// ww  w .  j ava  2s  . c o m
    }

    List<File> directoriesToDelete = new ArrayList<File>();
    File unzippedFile = null;
    TarArchiveInputStream tiStream = null;
    FileOutputStream tempOutStream = null;
    String unzipError = "";
    if (GzipUtils.isCompressedFilename(uploadedInputFile.getName())) {
        try {
            GZIPInputStream zippedInput = new GZIPInputStream(new FileInputStream(uploadedInputFile));
            unzippedFile = new File(dir, "unzipped-file-" + UUID.randomUUID());
            FileOutputStream unzippedOutput = new FileOutputStream(unzippedFile);
            byte[] dataBuffer = new byte[8192];
            int i = 0;
            while ((i = zippedInput.read(dataBuffer)) > 0) {
                unzippedOutput.write(dataBuffer, 0, i);
                unzippedOutput.flush();
            }
            tiStream = new TarArchiveInputStream(new FileInputStream(unzippedFile));
        } catch (Exception ex) {
            unzipError = " A common gzip extension was found but is the file corrupt?";
        }
    } else if (BZip2Utils.isCompressedFilename(uploadedInputFile.getName())) {
        try {
            BZip2CompressorInputStream zippedInput = new BZip2CompressorInputStream(
                    new FileInputStream(uploadedInputFile));
            unzippedFile = new File(dir, "unzipped-file-" + UUID.randomUUID());
            FileOutputStream unzippedOutput = new FileOutputStream(unzippedFile);
            byte[] dataBuffer = new byte[8192];
            int i = 0;
            while ((i = zippedInput.read(dataBuffer)) > 0) {
                unzippedOutput.write(dataBuffer, 0, i);
                unzippedOutput.flush();
            }
            tiStream = new TarArchiveInputStream(new FileInputStream(unzippedFile));
        } catch (Exception ex) {
            unzipError = " A common bzip2 extension was found but is the file corrupt?";
        }
    } else {
        try {
            // no need to decompress, carry on
            tiStream = new TarArchiveInputStream(new FileInputStream(uploadedInputFile));
        } catch (FileNotFoundException ex) {
            unzipError = " Is the tar file corrupt?";
        }
    }

    TarArchiveEntry tEntry = null;

    if (tiStream == null) {
        String msg = "Problem reading uploaded file." + unzipError;
        setTarValidationErrorMessage(msg);
        uploadedInputFile.delete();
        fbList = new ArrayList<StudyFileEditBean>();
        return fbList;
    }
    try {
        while ((tEntry = tiStream.getNextTarEntry()) != null) {

            String fileEntryName = tEntry.getName();

            if (!tEntry.isDirectory()) {

                if (fileEntryName != null && !fileEntryName.equals("")) {

                    String dirName = null;
                    String finalFileName = fileEntryName;

                    int ind = fileEntryName.lastIndexOf('/');

                    if (ind > -1) {
                        finalFileName = fileEntryName.substring(ind + 1);
                        if (ind > 0) {
                            dirName = fileEntryName.substring(0, ind);
                            dirName = dirName.replace('/', '-');
                        }
                    } else {
                        finalFileName = fileEntryName;
                    }

                    // only process normal tar entries, not the ones that start with "._" because they were created on a mac:
                    // http://superuser.com/questions/61185/why-do-i-get-files-like-foo-in-my-tarball-on-os-x
                    // http://superuser.com/questions/212896/is-there-any-way-to-prevent-a-mac-from-creating-dot-underscore-files
                    if (!finalFileName.startsWith("._")) {

                        File tempUploadedFile = null;
                        try {
                            tempUploadedFile = FileUtil.createTempFile(dir, finalFileName);
                        } catch (Exception ex) {
                            Logger.getLogger(AddFilesPage.class.getName()).log(Level.SEVERE, null, ex);
                            String msg = "Problem creating temporary file.";
                            setTarValidationErrorMessage(msg);
                        }

                        tempOutStream = new FileOutputStream(tempUploadedFile);

                        byte[] dataBuffer = new byte[8192];
                        int i = 0;

                        while ((i = tiStream.read(dataBuffer)) > 0) {
                            tempOutStream.write(dataBuffer, 0, i);
                            tempOutStream.flush();
                        }

                        tempOutStream.close();

                        try {
                            StudyFileEditBean tempFileBean = new StudyFileEditBean(tempUploadedFile,
                                    studyService.generateFileSystemNameSequence(), study);
                            tempFileBean.setSizeFormatted(tempUploadedFile.length());
                            if (dirName != null) {
                                tempFileBean.getFileMetadata().setCategory(dirName);
                            }
                            fbList.add(tempFileBean);
                            setTarValidationErrorMessage(null);
                        } catch (Exception ex) {
                            String msg = "Problem preparing files for ingest. Is the tar file corrupt?";
                            setTarValidationErrorMessage(msg);
                            uploadedInputFile.delete();
                            tempUploadedFile.delete();
                            fbList = new ArrayList<StudyFileEditBean>();
                        }
                    }
                }
            } else {
                File directory = new File(dir, fileEntryName);
                directory.mkdir();
                directoriesToDelete.add(directory);
            }
        }
    } catch (IOException ex) {
        String msg = "Problem reading tar file. Is it corrupt?";
        setTarValidationErrorMessage(msg);
    }

    // teardown and cleanup
    try {
        tiStream.close();
    } catch (IOException ex) {
        Logger.getLogger(AddFilesPage.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (tiStream != null) {
        try {
            tiStream.close();
        } catch (IOException ex) {
            Logger.getLogger(AddFilesPage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    if (tempOutStream != null) {
        try {
            tempOutStream.close();
        } catch (IOException ex) {
            Logger.getLogger(AddFilesPage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    uploadedInputFile.delete();
    if (unzippedFile != null) {
        unzippedFile.delete();
    }
    for (File dirToDelete : directoriesToDelete) {
        if (dirToDelete.exists()) {
            try {
                FileUtils.forceDelete(dirToDelete);
            } catch (IOException ex) {
                Logger.getLogger(AddFilesPage.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    return fbList;
}

From source file:org.apache.marmotta.loader.core.MarmottaLoader.java

/**
 * Detect the compression format from the filename, or null in case auto-detection failed.
 * @param file//w  ww  .j  a  va 2s .c  o  m
 * @return
 */
private String detectCompression(File file) {
    if (BZip2Utils.isCompressedFilename(file.getName())) {
        return CompressorStreamFactory.BZIP2;
    } else if (GzipUtils.isCompressedFilename(file.getName())) {
        return CompressorStreamFactory.GZIP;
    } else if (XZUtils.isCompressedFilename(file.getName())) {
        return CompressorStreamFactory.XZ;
    } else {
        return null;
    }
}

From source file:org.apache.marmotta.loader.core.MarmottaLoader.java

private String uncompressedName(File file) {
    if (BZip2Utils.isCompressedFilename(file.getAbsolutePath())) {
        return BZip2Utils.getUncompressedFilename(file.getName());
    } else if (GzipUtils.isCompressedFilename(file.getAbsolutePath())) {
        return GzipUtils.getUncompressedFilename(file.getName());
    } else if (XZUtils.isCompressedFilename(file.getAbsolutePath())) {
        return XZUtils.getUncompressedFilename(file.getName());
    } else {//from  ww w. j  a  v a2  s.c o m
        return file.getName();
    }
}

From source file:org.apache.marmotta.platform.core.services.importer.ImportWatchServiceImpl.java

private InputStream openStream(Path file) throws IOException {
    final String fName = file.getFileName().toString();
    final FileInputStream fis = new FileInputStream(file.toFile());

    if (GzipUtils.isCompressedFilename(fName)) {
        log.trace("{} looks GZIP compressed,", file);
        return new GZIPInputStream(fis);
    } else if (BZip2Utils.isCompressedFilename(fName)) {
        log.trace("{} looks BZ2 compressed", file);
        return new BZip2CompressorInputStream(fis);
    } else {//from   www .  j a  v a2  s .  c o  m
        return fis;
    }
}