Example usage for org.apache.commons.compress.archivers ArchiveStreamFactory CPIO

List of usage examples for org.apache.commons.compress.archivers ArchiveStreamFactory CPIO

Introduction

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

Prototype

String CPIO

To view the source code for org.apache.commons.compress.archivers ArchiveStreamFactory CPIO.

Click Source Link

Document

Constant used to identify the CPIO archive format.

Usage

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

private void addFilesToArchive(ArchiveOutputStream taos, File file) throws IOException {
    // Create an entry for the file
    //taos.putArchiveEntry(new TarArchiveEntry(file, file.getParentFile().toURI().relativize(file.toURI()).toString()));
    switch (archivingFormat) {
    case ArchiveStreamFactory.TAR:
        taos.putArchiveEntry(new TarArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())));
        break;/*  w  ww.  ja va 2s .  c o  m*/
    case ArchiveStreamFactory.ZIP:
        taos.putArchiveEntry(new ZipArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())));
        break;
    case ArchiveStreamFactory.JAR:
        taos.putArchiveEntry(new JarArchiveEntry(new ZipArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString()))));
        break;
    case ArchiveStreamFactory.AR:
        taos.putArchiveEntry(new ArArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())));
        break;
    case ArchiveStreamFactory.CPIO:
        taos.putArchiveEntry(new CpioArchiveEntry(file, FilenameUtils.separatorsToUnix(
                Paths.get(packageLocationDir.getPath()).relativize(Paths.get(file.getPath())).toString())));
        break;
    }
    if (file.isFile()) {
        // Add the file to the archive
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(bis, taos);
        taos.closeArchiveEntry();
        bis.close();
    } else if (file.isDirectory()) {
        // close the archive entry
        taos.closeArchiveEntry();
        // go through all the files in the directory and using recursion, add them to the archive
        for (File childFile : file.listFiles()) {
            addFilesToArchive(taos, childFile);
        }
    }
}

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

private void validateArchivingFormat() {
    if (!archivingFormat.equals(ArchiveStreamFactory.CPIO) && !archivingFormat.equals(ArchiveStreamFactory.TAR)
            && !archivingFormat.equals(ArchiveStreamFactory.JAR)
            && !archivingFormat.equals(ArchiveStreamFactory.AR)
            && !archivingFormat.equals(ArchiveStreamFactory.ZIP) && !archivingFormat.equals("exploded")) {
        throw new PackageToolException(PackagingToolReturnInfo.PKG_ASSEMBLER_INVALID_PARAMS,
                String.format(/*  ww  w  .  jav  a 2s  . co m*/
                        "Specified archiving format <%s> is not supported. The supported archiving "
                                + "formats are: %s, %s, %s, %s, %s, %s.",
                        archivingFormat, ArchiveStreamFactory.AR, ArchiveStreamFactory.CPIO,
                        ArchiveStreamFactory.JAR, ArchiveStreamFactory.TAR, ArchiveStreamFactory.ZIP,
                        "exploded"));
    }
}

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   w  ww . jav  a2  s  . c  om*/
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:org.structr.websocket.command.UnarchiveCommand.java

@Override
public void processMessage(WebSocketMessage webSocketData) {

    final Set<String> supportedByArchiveStreamFactory = new HashSet<>(
            Arrays.asList(new String[] { ArchiveStreamFactory.AR, ArchiveStreamFactory.ARJ,
                    ArchiveStreamFactory.CPIO, ArchiveStreamFactory.DUMP, ArchiveStreamFactory.JAR,
                    ArchiveStreamFactory.TAR, ArchiveStreamFactory.ZIP }));

    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final App app = StructrApp.getInstance(securityContext);

    try {//from  ww w.j a v a  2 s. c o m

        final String id = (String) webSocketData.getId();
        final File file;

        try (final Tx tx = app.tx()) {

            file = app.get(File.class, id);

            if (file == null) {
                getWebSocket().send(
                        MessageBuilder.status().code(400).message("File not found: ".concat(id)).build(), true);
                return;
            }

            final String fileExtension = StringUtils.substringAfterLast(file.getName(), ".");
            if (!supportedByArchiveStreamFactory.contains(fileExtension)) {

                getWebSocket().send(MessageBuilder.status().code(400)
                        .message("Unsupported archive format: ".concat(fileExtension)).build(), true);
                return;
            }

            tx.success();
        }

        // no transaction here since this is a bulk command
        unarchive(securityContext, file);

    } catch (Throwable t) {

        t.printStackTrace();

        String msg = t.toString();

        try (final Tx tx = app.tx()) {

            // return error message
            getWebSocket().send(
                    MessageBuilder.status().code(400)
                            .message("Could not unarchive file: ".concat((msg != null) ? msg : "")).build(),
                    true);

            tx.success();

        } catch (FrameworkException ignore) {
        }

    }
}