private boolean handleRpmFile(String innerDir, String archiveFile) {
        boolean success = true;
        File rpmFile = new File(archiveFile);
        FileInputStream rpmFIS = null;
        try {
            rpmFIS = new FileInputStream(rpmFile.getPath());
        } catch (FileNotFoundException e) {
            success = false;
            logger.warn("File not found: {}", archiveFile);
        }

        Format format = null;
        ReadableByteChannel channel = Channels.newChannel(rpmFIS);
        ReadableChannelWrapper channelWrapper = new ReadableChannelWrapper(channel);
        try {
            format = new org.redline_rpm.Scanner().run(channelWrapper);
        } catch (IOException e) {
            success = false;
            logger.warn("Error reading RPM file {}: {}", archiveFile, e.getCause());
        }

        if (format != null) {
            Header header = format.getHeader();
            FileOutputStream cpioOS = null;
            FileOutputStream cpioEntryOutputStream = null;
            CpioArchiveInputStream cpioIn = null;
            File cpioFile = null;
            try {
                // extract all .cpio file
                // get input stream according to payload compressor type
                InputStream inputStream;
                AbstractHeader.Entry pcEntry = header.getEntry(Header.HeaderTag.PAYLOADCOMPRESSOR);
                String[] pc = (String[]) pcEntry.getValues();
                if (pc[0].equals(LZMA)) {
                    try {
                        inputStream = new LZMACompressorInputStream(rpmFIS);
                    } catch (Exception e) {
                        throw new IOException("Failed to load LZMA compression stream", e);
                    }
                } else {
                    inputStream = Util.openPayloadStream(header, rpmFIS);
                }

                cpioFile = new File(rpmFile.getPath() + CPIO);
                cpioOS = new FileOutputStream(cpioFile);
                IOUtils.copy(inputStream, cpioOS);
                // extract files from .cpio
                File extractDestination = new File(innerDir);
                extractDestination.mkdirs();
                cpioIn = new CpioArchiveInputStream(new FileInputStream(cpioFile));

                CpioArchiveEntry cpioEntry;
                while ((cpioEntry = (CpioArchiveEntry) cpioIn.getNextEntry()) != null) {
                    String entryName = cpioEntry.getName();
                    String lowercaseName = entryName.toLowerCase();
                    File file = new File(extractDestination, getFileName(entryName));
                    cpioEntryOutputStream = new FileOutputStream(file);
                    IOUtils.copy(cpioIn, cpioEntryOutputStream);
                    String innerExtractionDir;
                    if (lowercaseName.matches(TAR_EXTENSION_PATTERN)) {
                        innerExtractionDir = innerDir + File.separator + entryName + this.randomString;
                        unTar(file.getName(), innerExtractionDir, file.getPath());
                    } else if (lowercaseName.matches(ZIP_EXTENSION_PATTERN)) {
                        innerExtractionDir = innerDir + File.separator + entryName + this.randomString;
                        unZip(innerExtractionDir, file.getPath());
                    }
                    // close
                    closeResource(cpioEntryOutputStream);
                }
            } catch (IOException e) {
                logger.error("Error unpacking rpm file {}: {}", rpmFile.getName(), e.getMessage());
            } finally {
                closeResource(cpioEntryOutputStream);
                closeResource(cpioIn);
                closeResource(cpioOS);
                deleteFile(cpioFile);
            }
        }
        return success;
    }
private boolean handleRpmFile ( String innerDir , String archiveFile ) { boolean success = true ; File rpmFile = new File ( archiveFile ) ; FileInputStream rpmFIS = null ; try { rpmFIS = new FileInputStream ( rpmFile . getPath ( ) ) ; } catch ( FileNotFoundException e ) { success = false ; logger . warn ( "File not found: {}" , archiveFile ) ; } Format format = null ; ReadableByteChannel channel = Channels . newChannel ( rpmFIS ) ; ReadableChannelWrapper channelWrapper = new ReadableChannelWrapper ( channel ) ; try { format = new org . redline_rpm . Scanner ( ) . run ( channelWrapper ) ; } catch ( IOException e ) { success = false ; logger . warn ( "Error reading RPM file {}: {}" , archiveFile , e . getCause ( ) ) ; } if ( format != null ) { Header header = format . getHeader ( ) ; FileOutputStream cpioOS = null ; FileOutputStream cpioEntryOutputStream = null ; CpioArchiveInputStream cpioIn = null ; File cpioFile = null ; try { InputStream inputStream ; AbstractHeader . Entry pcEntry = header . getEntry ( Header . HeaderTag . PAYLOADCOMPRESSOR ) ; String [ ] pc = ( String [ ] ) pcEntry . getValues ( ) ; if ( pc [ 0 ] . equals ( LZMA ) ) { try { inputStream = new LZMACompressorInputStream ( rpmFIS ) ; } catch ( Exception e ) { throw new IOException ( "Failed to load LZMA compression stream" , e ) ; } } else { inputStream = Util . openPayloadStream ( header , rpmFIS ) ; } cpioFile = new File ( rpmFile . getPath ( ) + CPIO ) ; cpioOS = new FileOutputStream ( cpioFile ) ; IOUtils . copy ( inputStream , cpioOS ) ; File extractDestination = new File ( innerDir ) ; extractDestination . mkdirs ( ) ; cpioIn = new CpioArchiveInputStream ( new FileInputStream ( cpioFile ) ) ; CpioArchiveEntry cpioEntry ; while ( ( cpioEntry = ( CpioArchiveEntry ) cpioIn . getNextEntry ( ) ) != null ) { String entryName = cpioEntry . getName ( ) ; String lowercaseName = entryName . toLowerCase ( ) ; File file = new File ( extractDestination , getFileName ( entryName ) ) ; cpioEntryOutputStream = new FileOutputStream ( file ) ; IOUtils . copy ( cpioIn , cpioEntryOutputStream ) ; String innerExtractionDir ; if ( lowercaseName . matches ( TAR_EXTENSION_PATTERN ) ) { innerExtractionDir = innerDir + File . separator + entryName + this . randomString ; unTar ( file . getName ( ) , innerExtractionDir , file . getPath ( ) ) ; } else if ( lowercaseName . matches ( ZIP_EXTENSION_PATTERN ) ) { innerExtractionDir = innerDir + File . separator + entryName + this . randomString ; unZip ( innerExtractionDir , file . getPath ( ) ) ; } closeResource ( cpioEntryOutputStream ) ; } } catch ( IOException e ) { logger . error ( "Error unpacking rpm file {}: {}" , rpmFile . getName ( ) , e . getMessage ( ) ) ; } finally { closeResource ( cpioEntryOutputStream ) ; closeResource ( cpioIn ) ; closeResource ( cpioOS ) ; deleteFile ( cpioFile ) ; } } return success ; }
Open and extract data from rpm files