Example usage for org.apache.commons.compress.archivers.cpio CpioArchiveInputStream CpioArchiveInputStream

List of usage examples for org.apache.commons.compress.archivers.cpio CpioArchiveInputStream CpioArchiveInputStream

Introduction

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

Prototype

public CpioArchiveInputStream(final InputStream in) 

Source Link

Document

Construct the cpio input stream

Usage

From source file:ch.ledcom.jpreseed.InitrdRepackerTest.java

private static CpioArchiveInputStream gzippedCpio(ByteArrayOutputStream stream) throws IOException {
    return new CpioArchiveInputStream(new GZIPInputStream(new ByteArrayInputStream(stream.toByteArray())));
}

From source file:ch.ledcom.assertj.CpioArchiveInputStreamAssertTest.java

private CpioArchiveInputStream createCpioArchiveInputStream() {
    return new CpioArchiveInputStream(getClass().getResourceAsStream("/test-archive.cpio"));
}

From source file:ch.ledcom.jpreseed.InitrdRepacker.java

public final void repack(OutputStream out) throws IOException {
    // start new archive
    try (CpioArchiveInputStream cpioIn = new CpioArchiveInputStream(new GZIPInputStream(initrdGz));
            CpioArchiveOutputStream cpioOut = new CpioArchiveOutputStream(new GZIPOutputStream(out))) {
        CpioArchiveEntry cpioEntry;/* w w  w.j a va  2  s  . c  o m*/

        // add files from base archive
        while ((cpioEntry = cpioIn.getNextCPIOEntry()) != null) {
            if (!additionalFiles.keySet().contains(cpioEntry.getName())) {
                logger.info("Repacking [{}]", cpioEntry.getName());
                cpioOut.putArchiveEntry(cpioEntry);
                long bytesCopied = copy(cpioIn, cpioOut);
                cpioOut.closeArchiveEntry();
                logger.debug("Copied [{}] bytes", bytesCopied);
            }
        }

        // additional files
        for (Map.Entry<String, File> entry : additionalFiles.entrySet()) {
            logger.info("Packing new file [{}]", entry.getKey());
            ArchiveEntry additionalEntry = cpioOut.createArchiveEntry(entry.getValue(), entry.getKey());
            cpioOut.putArchiveEntry(additionalEntry);
            try (InputStream in = new FileInputStream(entry.getValue())) {
                copy(in, cpioOut);
            }
            cpioOut.closeArchiveEntry();
        }
    }
}

From source file:de.dentrassi.rpm.RpmInputStream.java

protected void ensureInit() throws IOException {
    if (this.lead == null) {
        this.lead = readLead();
    }/* w ww  . j av  a2 s .c om*/

    if (this.signatureHeader == null) {
        this.signatureHeader = readHeader(true);
    }

    if (this.payloadHeader == null) {
        this.payloadHeader = readHeader(false);
    }

    // set up content stream

    if (this.payloadStream == null) {
        this.payloadStream = setupPayloadStream();
        this.cpioStream = new CpioArchiveInputStream(this.payloadStream); // we did ensure that we only support CPIO before
    }
}

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

/**
 * Read from compressed file// w  w  w  .java2  s  .  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);
    CpioArchiveInputStream ais = new CpioArchiveInputStream(bais);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        CpioArchiveEntry entry = ais.getNextCPIOEntry();
        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.getNextCPIOEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        baos.close();
        ais.close();
        bais.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}

From source file:ch.ledcom.jpreseed.UsbCreatorTest.java

private CpioArchiveInputStream getCpioArchiveInputStream(FsDirectoryEntry newInitRdGzEntry) throws IOException {
    FsFile initrdGzFile = newInitRdGzEntry.getFile();
    ByteBuffer initRdBuffer = ByteBuffer.allocate((int) initrdGzFile.getLength());
    initrdGzFile.read(0, initRdBuffer);//from   w  w  w .  ja v  a  2 s  .c om
    initRdBuffer.rewind();
    return new CpioArchiveInputStream(new GZIPInputStream(new ByteBufferDataInputStream(initRdBuffer)));
}

From source file:com.pclinuxos.rpm.util.FileUtils.java

/**
 * The method extracts a srpm into the default directory used by the program (/home/<user>/RCEB/srpms/tmp)
 * /*from   ww  w.  j  a  v a 2 s .  c  o  m*/
 * @param srpm the srpm to extract
 * @return 0 if the extraction was successfully, -1 if a IOException occurred, -2 if a InterruptException
 *  occurred. Values > 0 for return codes of the rpm2cpio command.
 */
public static int extractSrpm(String srpm) {

    int returnCode = 0;

    try {

        Process extractProcess = Runtime.getRuntime()
                .exec("rpm2cpio " + FileConstants.SRCSRPMS.getAbsolutePath() + "/" + srpm);
        // 64kb buffer
        byte[] buffer = new byte[0xFFFF];
        InputStream inread = extractProcess.getInputStream();

        FileOutputStream out = new FileOutputStream(new File(FileConstants.F4SRPMEX + "archive.cpio"));

        while (inread.read(buffer) != -1) {

            out.write(buffer);
        }

        returnCode = extractProcess.waitFor();

        if (returnCode == 0) {

            CpioArchiveInputStream cpioIn = new CpioArchiveInputStream(
                    new FileInputStream(FileConstants.F4SRPMEX + "archive.cpio"));
            CpioArchiveEntry cpEntry;

            while ((cpEntry = cpioIn.getNextCPIOEntry()) != null) {

                FileOutputStream fOut = new FileOutputStream(FileConstants.F4SRPMEX + cpEntry.getName());
                // Do not make this buffer bigger it breaks the cpio decompression
                byte[] buffer2 = new byte[1];
                ArrayList<Byte> buf = new ArrayList<Byte>();

                while (cpioIn.read(buffer2) != -1) {

                    buf.add(buffer2[0]);
                }

                byte[] file = new byte[buf.size()];

                for (int i = 0; i < buf.size(); i++) {

                    file[i] = buf.get(i);
                }

                fOut.write(file);

                fOut.flush();
                fOut.close();
            }

            cpioIn.close();
        }
    } catch (IOException e) {

        returnCode = -1;
    } catch (InterruptedException e) {

        returnCode = -2;
    }

    new File(FileConstants.F4SRPMEX + "archive.cpio").delete();

    return returnCode;
}

From source file:org.apache.tika.parser.pkg.PackageExtractor.java

public void parse(InputStream stream) throws IOException, SAXException, TikaException {
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();//from  w  ww.ja  v  a  2  s.  c om

    // At the end we want to close the package/compression stream to
    // release any associated resources, but the underlying document
    // stream should not be closed
    stream = new CloseShieldInputStream(stream);

    // Capture two bytes to determine the packaging/compression format
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream);
    }
    stream.mark(2);
    int a = stream.read();
    int b = stream.read();
    stream.reset();

    // Select decompression or unpacking mechanism based on the two bytes
    if (a == 'B' && b == 'Z') {
        metadata.set(Metadata.CONTENT_TYPE, "application/x-bzip");
        decompress(new BZip2CompressorInputStream(stream), xhtml);
    } else if (a == 0x1f && b == 0x8b) {
        metadata.set(Metadata.CONTENT_TYPE, "application/x-gzip");
        decompress(new GZIPInputStream(stream), xhtml);
    } else if (a == 'P' && b == 'K') {
        metadata.set(Metadata.CONTENT_TYPE, "application/zip");
        unpack(new ZipArchiveInputStream(stream), xhtml);
    } else if ((a == '0' && b == '7') || (a == 0x71 && b == 0xc7) || (a == 0xc7 && b == 0x71)) {
        metadata.set(Metadata.CONTENT_TYPE, "application/x-cpio");
        unpack(new CpioArchiveInputStream(stream), xhtml);
    } else if (a == '=' && (b == '<' || b == '!')) {
        metadata.set(Metadata.CONTENT_TYPE, "application/x-archive");
        unpack(new ArArchiveInputStream(stream), xhtml);
    } else {
        metadata.set(Metadata.CONTENT_TYPE, "application/x-tar");
        unpack(new TarArchiveInputStream(stream), xhtml);
    }

    xhtml.endDocument();
}

From source file:org.apereo.portal.io.xml.JaxbPortalDataHandlerService.java

protected void importDataArchive(Resource archive, InputStream resourceStream, BatchImportOptions options) {
    BufferedInputStream bufferedResourceStream = null;
    try {/*from   w w  w.j ava 2  s . c o m*/
        //Make sure the stream is buffered
        if (resourceStream instanceof BufferedInputStream) {
            bufferedResourceStream = (BufferedInputStream) resourceStream;
        } else {
            bufferedResourceStream = new BufferedInputStream(resourceStream);
        }

        //Buffer up to 100MB, bad things will happen if we bust this buffer.
        //TODO see if there is a buffered stream that will write to a file once the buffer fills up
        bufferedResourceStream.mark(100 * 1024 * 1024);
        final MediaType type = getMediaType(bufferedResourceStream, archive.getFilename());

        if (MT_JAVA_ARCHIVE.equals(type)) {
            final ArchiveInputStream archiveStream = new JarArchiveInputStream(bufferedResourceStream);
            importDataArchive(archive, archiveStream, options);
        } else if (MediaType.APPLICATION_ZIP.equals(type)) {
            final ArchiveInputStream archiveStream = new ZipArchiveInputStream(bufferedResourceStream);
            importDataArchive(archive, archiveStream, options);
        } else if (MT_CPIO.equals(type)) {
            final ArchiveInputStream archiveStream = new CpioArchiveInputStream(bufferedResourceStream);
            importDataArchive(archive, archiveStream, options);
        } else if (MT_AR.equals(type)) {
            final ArchiveInputStream archiveStream = new ArArchiveInputStream(bufferedResourceStream);
            importDataArchive(archive, archiveStream, options);
        } else if (MT_TAR.equals(type)) {
            final ArchiveInputStream archiveStream = new TarArchiveInputStream(bufferedResourceStream);
            importDataArchive(archive, archiveStream, options);
        } else if (MT_BZIP2.equals(type)) {
            final CompressorInputStream compressedStream = new BZip2CompressorInputStream(
                    bufferedResourceStream);
            importDataArchive(archive, compressedStream, options);
        } else if (MT_GZIP.equals(type)) {
            final CompressorInputStream compressedStream = new GzipCompressorInputStream(
                    bufferedResourceStream);
            importDataArchive(archive, compressedStream, options);
        } else if (MT_PACK200.equals(type)) {
            final CompressorInputStream compressedStream = new Pack200CompressorInputStream(
                    bufferedResourceStream);
            importDataArchive(archive, compressedStream, options);
        } else if (MT_XZ.equals(type)) {
            final CompressorInputStream compressedStream = new XZCompressorInputStream(bufferedResourceStream);
            importDataArchive(archive, compressedStream, options);
        } else {
            throw new RuntimeException("Unrecognized archive media type: " + type);
        }
    } catch (IOException e) {
        throw new RuntimeException("Could not load InputStream for resource: " + archive, e);
    } finally {
        IOUtils.closeQuietly(bufferedResourceStream);
    }
}

From source file:org.ysb33r.groovy.vfsplugin.cpio.CpioFileSystem.java

protected CpioArchiveInputStream createCpioFile(final File file) throws FileSystemException {
    try {/*from  ww w  .ja v a 2 s . co  m*/
        if ("cpiogz".equalsIgnoreCase(getRootName().getScheme())) {
            return new CpioArchiveInputStream(new GZIPInputStream(new FileInputStream(file)));
        } else if ("cpiobz2".equalsIgnoreCase(getRootName().getScheme())) {
            return new CpioArchiveInputStream(
                    Bzip2FileObject.wrapInputStream(file.getAbsolutePath(), new FileInputStream(file)));
        }
        return new CpioArchiveInputStream(new FileInputStream(file));
    } catch (final IOException ioe) {
        throw new FileSystemException("vfs.provider.Cpio/open-Cpio-file.error", file, ioe);
    }
}