Example usage for org.apache.commons.compress.compressors CompressorStreamFactory BZIP2

List of usage examples for org.apache.commons.compress.compressors CompressorStreamFactory BZIP2

Introduction

In this page you can find the example usage for org.apache.commons.compress.compressors CompressorStreamFactory BZIP2.

Prototype

String BZIP2

To view the source code for org.apache.commons.compress.compressors CompressorStreamFactory BZIP2.

Click Source Link

Document

Constant used to identify the BZIP2 compression algorithm.

Usage

From source file:org.apache.marmotta.loader.core.test.FilesTest.java

@Parameterized.Parameters
public static Collection<Object[]> data() {
    Object[][] data = new Object[][] { { null, "demo-data.rdf" },
            { CompressorStreamFactory.GZIP, "demo-data.rdf.gz" },
            { CompressorStreamFactory.BZIP2, "demo-data.rdf.bz2" } };
    return Arrays.asList(data);
}

From source file:org.dataconservancy.packaging.tool.impl.generator.BagItPackageAssembler.java

private void validateCompressionFormat() {
    // convert gzip to gz
    if (compressionFormat.equals("gzip")) {
        compressionFormat = CompressorStreamFactory.GZIP;
    }//  w  w w  .  j  a v  a  2 s . c  om

    if (!compressionFormat.equals(CompressorStreamFactory.BZIP2)
            && !compressionFormat.equals(CompressorStreamFactory.GZIP)
            && !compressionFormat.equals(CompressorStreamFactory.PACK200)
            && !compressionFormat.equals("none")) {
        throw new PackageToolException(PackagingToolReturnInfo.PKG_ASSEMBLER_INVALID_PARAMS,
                String.format(
                        "Specified compression format %s is not supported. The supported compression "
                                + "formats are: %s (or %s), %s, %s, none.",
                        compressionFormat, CompressorStreamFactory.GZIP, "gzip", CompressorStreamFactory.BZIP2,
                        CompressorStreamFactory.PACK200));
    }
}

From source file:org.eclipse.cdt.arduino.core.internal.board.ArduinoManager.java

public static void downloadAndInstall(String url, String archiveFileName, Path installPath,
        IProgressMonitor monitor) throws IOException {
    Exception error = null;/*  w w  w . j a  v a 2s  .  co  m*/
    for (int retries = 3; retries > 0 && !monitor.isCanceled(); --retries) {
        try {
            URL dl = new URL(url);
            Path dlDir = ArduinoPreferences.getArduinoHome().resolve("downloads"); //$NON-NLS-1$
            Files.createDirectories(dlDir);
            Path archivePath = dlDir.resolve(archiveFileName);
            URLConnection conn = dl.openConnection();
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(10000);
            Files.copy(conn.getInputStream(), archivePath, StandardCopyOption.REPLACE_EXISTING);

            boolean isWin = Platform.getOS().equals(Platform.OS_WIN32);

            // extract
            ArchiveInputStream archiveIn = null;
            try {
                String compressor = null;
                String archiver = null;
                if (archiveFileName.endsWith("tar.bz2")) { //$NON-NLS-1$
                    compressor = CompressorStreamFactory.BZIP2;
                    archiver = ArchiveStreamFactory.TAR;
                } else if (archiveFileName.endsWith(".tar.gz") || archiveFileName.endsWith(".tgz")) { //$NON-NLS-1$ //$NON-NLS-2$
                    compressor = CompressorStreamFactory.GZIP;
                    archiver = ArchiveStreamFactory.TAR;
                } else if (archiveFileName.endsWith(".tar.xz")) { //$NON-NLS-1$
                    compressor = CompressorStreamFactory.XZ;
                    archiver = ArchiveStreamFactory.TAR;
                } else if (archiveFileName.endsWith(".zip")) { //$NON-NLS-1$
                    archiver = ArchiveStreamFactory.ZIP;
                }

                InputStream in = new BufferedInputStream(new FileInputStream(archivePath.toFile()));
                if (compressor != null) {
                    in = new CompressorStreamFactory().createCompressorInputStream(compressor, in);
                }
                archiveIn = new ArchiveStreamFactory().createArchiveInputStream(archiver, in);

                for (ArchiveEntry entry = archiveIn.getNextEntry(); entry != null; entry = archiveIn
                        .getNextEntry()) {
                    if (entry.isDirectory()) {
                        continue;
                    }

                    // Magic file for git tarballs
                    Path path = Paths.get(entry.getName());
                    if (path.endsWith("pax_global_header")) { //$NON-NLS-1$
                        continue;
                    }

                    // Strip the first directory of the path
                    Path entryPath;
                    switch (path.getName(0).toString()) {
                    case "i586":
                    case "i686":
                        // Cheat for Intel
                        entryPath = installPath.resolve(path);
                        break;
                    default:
                        entryPath = installPath.resolve(path.subpath(1, path.getNameCount()));
                    }

                    Files.createDirectories(entryPath.getParent());

                    if (entry instanceof TarArchiveEntry) {
                        TarArchiveEntry tarEntry = (TarArchiveEntry) entry;
                        if (tarEntry.isLink()) {
                            Path linkPath = Paths.get(tarEntry.getLinkName());
                            linkPath = installPath.resolve(linkPath.subpath(1, linkPath.getNameCount()));
                            Files.deleteIfExists(entryPath);
                            Files.createSymbolicLink(entryPath, entryPath.getParent().relativize(linkPath));
                        } else if (tarEntry.isSymbolicLink()) {
                            Path linkPath = Paths.get(tarEntry.getLinkName());
                            Files.deleteIfExists(entryPath);
                            Files.createSymbolicLink(entryPath, linkPath);
                        } else {
                            Files.copy(archiveIn, entryPath, StandardCopyOption.REPLACE_EXISTING);
                        }
                        if (!isWin && !tarEntry.isSymbolicLink()) {
                            int mode = tarEntry.getMode();
                            Files.setPosixFilePermissions(entryPath, toPerms(mode));
                        }
                    } else {
                        Files.copy(archiveIn, entryPath, StandardCopyOption.REPLACE_EXISTING);
                    }
                }
            } finally {
                if (archiveIn != null) {
                    archiveIn.close();
                }
            }
            return;
        } catch (IOException | CompressorException | ArchiveException e) {
            error = e;
            // retry
        }
    }

    // out of retries
    if (error instanceof IOException) {
        throw (IOException) error;
    } else {
        throw new IOException(error);
    }
}

From source file:org.eclipse.cdt.arduino.core.internal.board.Platform.java

public IStatus install(IProgressMonitor monitor) throws CoreException {
    try {/*w  w w . j a  v  a2s. c om*/
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            HttpGet get = new HttpGet(url);
            try (CloseableHttpResponse response = client.execute(get)) {
                if (response.getStatusLine().getStatusCode() >= 400) {
                    return new Status(IStatus.ERROR, Activator.getId(),
                            response.getStatusLine().getReasonPhrase());
                } else {
                    HttpEntity entity = response.getEntity();
                    if (entity == null) {
                        return new Status(IStatus.ERROR, Activator.getId(), Messages.ArduinoBoardManager_1);
                    }
                    // the archive has the version number as the root
                    // directory
                    Path installPath = getInstallPath().getParent();
                    Files.createDirectories(installPath);
                    Path archivePath = installPath.resolve(archiveFileName);
                    Files.copy(entity.getContent(), archivePath, StandardCopyOption.REPLACE_EXISTING);

                    // extract
                    ArchiveInputStream archiveIn = null;
                    try {
                        String compressor = null;
                        String archiver = null;
                        if (archiveFileName.endsWith("tar.bz2")) { //$NON-NLS-1$
                            compressor = CompressorStreamFactory.BZIP2;
                            archiver = ArchiveStreamFactory.TAR;
                        } else if (archiveFileName.endsWith(".tar.gz") || archiveFileName.endsWith(".tgz")) { //$NON-NLS-1$ //$NON-NLS-2$
                            compressor = CompressorStreamFactory.GZIP;
                            archiver = ArchiveStreamFactory.TAR;
                        } else if (archiveFileName.endsWith(".tar.xz")) { //$NON-NLS-1$
                            compressor = CompressorStreamFactory.XZ;
                            archiver = ArchiveStreamFactory.TAR;
                        } else if (archiveFileName.endsWith(".zip")) { //$NON-NLS-1$
                            archiver = ArchiveStreamFactory.ZIP;
                        }

                        InputStream in = new BufferedInputStream(new FileInputStream(archivePath.toFile()));
                        if (compressor != null) {
                            in = new CompressorStreamFactory().createCompressorInputStream(compressor, in);
                        }
                        archiveIn = new ArchiveStreamFactory().createArchiveInputStream(archiver, in);

                        for (ArchiveEntry entry = archiveIn.getNextEntry(); entry != null; entry = archiveIn
                                .getNextEntry()) {
                            if (entry.isDirectory()) {
                                continue;
                            }

                            // TODO check for soft links in tar files.
                            Path entryPath = installPath.resolve(entry.getName());
                            Files.createDirectories(entryPath.getParent());
                            Files.copy(archiveIn, entryPath, StandardCopyOption.REPLACE_EXISTING);
                        }
                    } finally {
                        if (archiveIn != null) {
                            archiveIn.close();
                        }
                    }
                }
            }
        }
        return Status.OK_STATUS;
    } catch (IOException | CompressorException | ArchiveException e) {
        throw new CoreException(new Status(IStatus.ERROR, Activator.getId(), "Installing Platform", e));
    }
}

From source file:org.graphipedia.dataextract.ExtractData.java

@Override
public void run() {
    logger.info("Start extracting data...");
    long startTime = System.currentTimeMillis();

    DisambiguationPageExtractor dpExtractor = new DisambiguationPageExtractor(settings, this.language,
            dpRootCategory, checkpoint, loggerMessageSuffix);
    dpExtractor.start();/*from w  ww  .  j  a v a  2  s . co  m*/
    ExtractNamespaces nsExtractor = new ExtractNamespaces(settings, language, loggerMessageSuffix,
            new File(settings.wikipediaEditionDirectory(language), ExtractNamespaces.NAMESPACE_FILE),
            checkpoint);
    nsExtractor.start();
    InfoboxTemplatesExtractor itExtractor = new InfoboxTemplatesExtractor(settings, language, itRootCategory,
            checkpoint, loggerMessageSuffix);
    itExtractor.start();
    ExtractGeoTags geotagsExtractor = new ExtractGeoTags(settings, language, loggerMessageSuffix);
    geotagsExtractor.start();
    try {
        dpExtractor.join();
        nsExtractor.join();
        this.ns = nsExtractor.namespaces();
        itExtractor.join();
        geotagsExtractor.join();
        this.geotags = geotagsExtractor.getGeoTags();
    } catch (InterruptedException e) {
        logger.severe("Problems with the threads.");
        e.printStackTrace();
        System.exit(-1);
    }
    XMLOutputFactory outputFactory = XMLOutputFactory2.newInstance();
    File outputFile = new File(settings.wikipediaEditionDirectory(language), TEMPORARY_LINK_FILE);
    if (checkpoint.isLinksExtracted(this.language)) {
        logger.info("Using pages and links from a previous computation");
        return;
    }
    try {
        FileOutputStream fout = new FileOutputStream(outputFile.getAbsolutePath());
        BufferedOutputStream bos = new BufferedOutputStream(fout);
        CompressorOutputStream output = new CompressorStreamFactory()
                .createCompressorOutputStream(CompressorStreamFactory.BZIP2, bos);
        XMLStreamWriter writer = outputFactory.createXMLStreamWriter(output, "UTF-8");
        writer.writeStartDocument();
        writer.writeStartElement("d");

        LinkExtractor linkExtractor = new LinkExtractor(writer, logger, settings, language,
                dpExtractor.disambiguationPages(), itExtractor.infoboxTemplates(), this.ns);
        linkExtractor.parse(settings.getWikipediaXmlFile(language).getAbsolutePath());
        writer.writeEndElement();
        writer.writeEndDocument();
        output.close();
        fout.close();
        bos.close();
        writer.close();
        long elapsed = System.currentTimeMillis() - startTime;
        logger.info("Data extracted in " + ReadableTime.readableTime(elapsed));
    } catch (Exception e) {
        logger.severe("Error while parsing the XML file ");
        e.printStackTrace();
        System.exit(-1);
    }
    try {
        checkpoint.addLinksExtracted(this.language, true);
    } catch (IOException e) {
        logger.severe("Error while saving the checkpoint to file");
        e.printStackTrace();
        System.exit(-1);
    }
    settings.getWikipediaXmlFile(language).delete();
}

From source file:org.jboss.as.plugin.common.Files.java

private static boolean requiresExtraction(final File file) {
    final String extension = getExtension(file);
    return CompressorStreamFactory.BZIP2.equals(extension) || CompressorStreamFactory.GZIP.equals(extension)
            || CompressorStreamFactory.PACK200.equals(extension)
            || CompressorStreamFactory.XZ.equals(extension);
}

From source file:org.moe.cli.manager.PrebuildCocoaPodsManager.java

public IExecutor processCocoapods(String source, SpecObject spec, String packageName, String[] javaSource,
        String outputJar) throws IOException, CompressorException, ArchiveException,
        InvalidParameterSpecException, InterruptedException, URISyntaxException, UnsupportedTypeException {
    File tmpFolder = NatJFileUtils.getNewTempDirectory();
    File download = new File(tmpFolder, "download/");
    if (!download.exists()) {
        download.mkdirs();//from  w ww. ja v a 2s . c o m
    }

    int nameIdx = source.lastIndexOf("/");
    File outFile = new File(download, source.substring(nameIdx + 1));

    GrabUtils.download(new URI(source), outFile);

    int extIdx = outFile.getName().lastIndexOf(".");
    String folderUnzip = outFile.getName().substring(0, extIdx);
    File destination = new File(download, folderUnzip);
    if (!destination.exists()) {
        destination.mkdirs();
    }

    String type = spec.getSource().get("type");
    if (outFile.getName().endsWith(".zip") || (type != null && type.equals("zip"))) {
        ZipFile apachZip = new ZipFile(outFile);
        ArchiveUtils.unzipArchive(apachZip, destination);
    } else if (outFile.getName().endsWith("tar.bzip2") || outFile.getName().endsWith("tar.gz")
            || outFile.getName().endsWith("tar.bz2") || type.equals("tgz")) {
        InputStream inputC = null;

        int extArchIdx = outFile.getName().lastIndexOf(".");
        String ext = outFile.getName().substring(extArchIdx + 1);
        if (ext.equals("tar")) {
            inputC = new FileInputStream(outFile);
        } else if (ext.equals("bzip2") || ext.equals("gz") || ext.equals("bz2")) {
            InputStream fin = new FileInputStream(outFile);

            String compressorType = null;
            if (ext.equals("bzip2") || ext.equals("bz2")) {
                compressorType = CompressorStreamFactory.BZIP2;
            } else if (ext.equals("gz") || type.equals("tgz")) {
                compressorType = CompressorStreamFactory.GZIP;
            }
            inputC = new CompressorStreamFactory().createCompressorInputStream(compressorType, fin);
        } else {
            throw new InvalidParameterException("Unsupported archive type");
        }

        ArchiveInputStream input = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR,
                inputC);
        ArchiveUtils.untarArchive(input, destination);
    }

    //update destination for git repo
    if (spec.getSource().containsKey("git")) {
        String git = spec.getSource().get("git");
        String tag = spec.getSource().get("tag");

        int gitNameIdx = git.lastIndexOf("/");
        String gitName = git.substring(gitNameIdx + 1);
        if (gitName.endsWith(".git")) {
            gitName = gitName.substring(0, gitName.length() - 4);
        }

        if (!Character.isDigit(tag.charAt(0)) && Character.isDigit(tag.charAt(1))) {
            tag = tag.substring(1);
        }

        String zipSubfolder = String.format("%s-%s", gitName, tag);
        destination = new File(destination, zipSubfolder);
    }

    List<String> commandList = spec.getPreparedCommands();
    for (String command : commandList) {
        if (command != null && !command.isEmpty()) {
            executePrepareCommands(destination, command);
        }
    }

    IExecutor executor = null;

    //find all bundles
    Set<String> bundleContent = new HashSet<String>();
    List<String> resources = spec.getResources();
    if (resources != null && resources.size() > 0) {
        for (String bundle : resources) {
            Set<String> bundleWildCard = getBundleResources(bundle, destination);
            bundleContent.addAll(bundleWildCard);
        }
    }
    String[] bundleRes = bundleContent.toArray(new String[0]);

    //get additional linker flags
    String ldFlags = spec.getLdFlags();

    //create 
    Set<String> headersContent = new HashSet<String>();
    List<String> headerList = spec.getSourceFiles();
    if (headerList != null && headerList.size() > 0) {
        for (String header : headerList) {
            int recursivIdx = header.indexOf("**");

            if (recursivIdx >= 0) {
                File headerFile = new File(destination, header.substring(0, recursivIdx));
                headersContent.add(headerFile.getPath());
            } else {
                Set<String> bundleWildCard = getBundleResources(header, destination);
                headersContent.addAll(bundleWildCard);
            }
        }
    }

    if (spec.getVendoredFrameworks() != null && spec.getVendoredFrameworks().size() > 0) {
        List<File> frameworkList = new ArrayList<>();
        Set<String> frameworkContent = new HashSet<String>();
        for (String vFramework : spec.getVendoredFrameworks()) {
            Set<String> frName = getBundleResources(vFramework, destination);
            if (frName.size() != 1)
                throw new RuntimeException(
                        "Something wrong with this code. Refactor it! And the same with libraries");
            File frameworkFile = new File(frName.toArray(new String[0])[0]);
            if (frameworkFile.exists()) {
                frameworkList.add(frameworkFile);
                frameworkContent.add(frameworkFile.getPath());

                int frameworkNameIdx = frameworkFile.getName().lastIndexOf(".");
                if (frameworkNameIdx >= 0) {
                    ldFlags = ldFlags + "-framework " + frameworkFile.getName().substring(0, frameworkNameIdx)
                            + ";";
                }
            }
        }

        if (headersContent.isEmpty()) {
            for (File framework : frameworkList) {
                File headerFile = new File(framework, "Headers");
                headersContent.add(headerFile.getPath());
            }
        }

        executor = new ThirdPartyFrameworkLinkExecutor(packageName, frameworkContent.toArray(new String[0]),
                javaSource, headersContent.toArray(new String[0]), bundleRes, outputJar, ldFlags);
    } else if (spec.getVendoredLibraries() != null && spec.getVendoredLibraries().size() > 0) {
        Set<String> libContent = new HashSet<String>();

        for (String lib : spec.getVendoredLibraries()) {
            Set<String> libName = getBundleResources(lib, destination);
            if (libName.size() != 1)
                throw new RuntimeException(
                        "Something wrong with this code. Refactor it! And the same with libraries");

            File library = new File(libName.toArray(new String[0])[0]);
            if (library.exists()) {
                libContent.add(library.getPath());

                int libNameIdx = library.getName().lastIndexOf(".");
                if (libNameIdx >= 0) {
                    String libShortName = library.getName().substring(0, libNameIdx);
                    ldFlags = ldFlags + "-l"
                            + (libShortName.startsWith("lib") ? libShortName.substring(3) : libShortName) + ";";
                }
            }
        }

        executor = new ThirdPartyLibraryLinkExecutor(packageName, libContent.toArray(new String[0]), javaSource,
                headersContent.toArray(new String[0]), bundleRes, outputJar, ldFlags);
    }
    return executor;
}

From source file:org.springframework.cloud.stream.app.tensorflow.util.ModelExtractor.java

/**
 * Detect the Archive and the Compressor from the file extension
 *
 * @param fileName File name with extension
 * @return Returns a tuple of the detected (Archive, Compressor). Null stands for not available archive or detector.
 * The (null, null) response stands for no Archive or Compressor discovered.
 *///from www.j  av  a 2  s  .  c  o  m
private String[] detectArchiveAndCompressor(String fileName) {

    String normalizedFileName = fileName.trim().toLowerCase();

    if (normalizedFileName.endsWith(".tar.gz") || normalizedFileName.endsWith(".tgz")
            || normalizedFileName.endsWith(".taz")) {
        return new String[] { ArchiveStreamFactory.TAR, CompressorStreamFactory.GZIP };
    } else if (normalizedFileName.endsWith(".tar.bz2") || normalizedFileName.endsWith(".tbz2")
            || normalizedFileName.endsWith(".tbz")) {
        return new String[] { ArchiveStreamFactory.TAR, CompressorStreamFactory.BZIP2 };
    } else if (normalizedFileName.endsWith(".cpgz")) {
        return new String[] { ArchiveStreamFactory.CPIO, CompressorStreamFactory.GZIP };
    } else if (hasArchive(normalizedFileName)) {
        return new String[] { findArchive(normalizedFileName).get(), null };
    } else if (hasCompressor(normalizedFileName)) {
        return new String[] { null, findCompressor(normalizedFileName).get() };
    } else if (normalizedFileName.endsWith(".gzip")) {
        return new String[] { null, CompressorStreamFactory.GZIP };
    } else if (normalizedFileName.endsWith(".bz2") || normalizedFileName.endsWith(".bz")) {
        return new String[] { null, CompressorStreamFactory.BZIP2 };
    }

    // No archived/compressed
    return new String[] { null, null };
}

From source file:rv.comm.rcssserver.TarBz2ZipUtil.java

/**
 * Creates the reader used for sequential reading
 * /*www. j a  va 2s.  com*/
 * @return the reader used for sequential reading
 * @throws FileNotFoundException
 *             if the logsrc is not found
 */
public static BufferedReader createBufferedReader(File file) throws FileNotFoundException {
    Reader reader = null;
    if (isTarBZ2Ending(file)) {
        reader = getTarBZ2InputStream(file);

    } else if (isBZ2Ending(file)) {
        reader = getCompressedInputStream(file, CompressorStreamFactory.BZIP2);

    } else if (isGZipEnding(file)) {
        reader = getCompressedInputStream(file, CompressorStreamFactory.GZIP);

    } else if (isZIPEnding(file)) {
        reader = getZipStream(file);
    } else {
        reader = new FileReader(file);
    }
    return new BufferedReader(reader);
}

From source file:rv.comm.rcssserver.TarBz2ZipUtil.java

public static Reader getTarBZ2InputStream(File file) {
    try {/*from   ww  w .ja  v a2s .  com*/
        // only works for the current layout of tar.bz2 files
        InputStream zStream = new BufferedInputStream(new FileInputStream(file));
        CompressorInputStream bz2InputStream = new CompressorStreamFactory()
                .createCompressorInputStream(CompressorStreamFactory.BZIP2, zStream);
        TarArchiveInputStream tarStream = new TarArchiveInputStream(bz2InputStream);
        TarArchiveEntry entry = tarStream.getNextTarEntry();

        // step into deepest directory
        while (entry != null && entry.isDirectory()) {
            TarArchiveEntry[] entries = entry.getDirectoryEntries();
            if (entries.length > 0) {
                entry = entries[0];
            } else {
                // empty directory
                entry = tarStream.getNextTarEntry();
            }
        }
        if (entry == null) {
            System.out.println("tar file does not contain logfile");
            return null;
        }

        // search for proper file
        while (entry != null && !entry.getName().endsWith("sparkmonitor.log")) {
            entry = tarStream.getNextTarEntry();
        }

        if (entry == null) {
            System.out.println("tar file does not contain logfile");
            return null;
        }

        // we have reached the proper position
        return new InputStreamReader(tarStream);

    } catch (IOException e) {
        // not a bz2 file
        System.out.println("File has bz2 ending, but seems to be not bz2");
        e.printStackTrace();
    } catch (CompressorException e) {
        e.printStackTrace();
    }
    return null;
}