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

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

Introduction

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

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads some number of bytes from the input stream and stores them into the buffer array b.

Usage

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)
 * /*  w  w  w .j  a va2s  .c om*/
 * @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:com.espringtran.compressor4j.processor.CpioProcessor.java

/**
 * Read from compressed file/*from   ww  w.j av  a2 s .  com*/
 * 
 * @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());
}