Example usage for org.apache.commons.compress.archivers.zip ZipArchiveInputStream read

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveInputStream read

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.zip ZipArchiveInputStream 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.glaf.core.util.ZipUtils.java

/**
 * /*from w ww .ja  v  a  2 s. c  o  m*/
 * zip
 * 
 * @param zipFilePath
 *            zip,  "/var/data/aa.zip"
 * 
 * @param saveFileDir
 *            ?, "/var/test/"
 */
public static void decompressZip(String zipFilePath, String saveFileDir) {
    if (isEndsWithZip(zipFilePath)) {
        File file = new File(zipFilePath);
        if (file.exists() && file.isFile()) {
            InputStream inputStream = null;
            ZipArchiveInputStream zais = null;
            try {
                inputStream = new FileInputStream(file);
                zais = new ZipArchiveInputStream(inputStream);
                ArchiveEntry archiveEntry = null;
                while ((archiveEntry = zais.getNextEntry()) != null) {
                    String entryFileName = archiveEntry.getName();
                    String entryFilePath = saveFileDir + entryFileName;
                    byte[] content = new byte[(int) archiveEntry.getSize()];
                    zais.read(content);
                    OutputStream os = null;
                    try {
                        File entryFile = new File(entryFilePath);
                        os = new BufferedOutputStream(new FileOutputStream(entryFile));
                        os.write(content);
                    } catch (IOException e) {
                        throw new IOException(e);
                    } finally {
                        IOUtils.closeStream(os);
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                IOUtils.closeStream(zais);
                IOUtils.closeStream(inputStream);
            }
        }
    }
}

From source file:io.github.runassudo.gtfs.ZipStreamGTFSFile.java

public FlatGTFSFile toFlatFile() throws IOException {
    File destBase = new File(
            new File(GTFSCollection.cacheDir,
                    Hashing.sha256().hashString(parentFile.getName(), StandardCharsets.UTF_8).toString()),
            Hashing.sha256().hashString(zipEntry.getName(), StandardCharsets.UTF_8).toString());

    if (!destBase.exists()) {
        ZipArchiveInputStream gtfsStream = new ZipArchiveInputStream(parentFile.getInputStream(zipEntry));
        ZipArchiveEntry contentEntry;/*from   w w w . j a  v  a 2s.  co  m*/
        while ((contentEntry = gtfsStream.getNextZipEntry()) != null) {
            // Copy this file to cache
            File dest = new File(destBase, contentEntry.getName());
            dest.getParentFile().mkdirs();
            FileOutputStream os = new FileOutputStream(dest);
            byte[] buf = new byte[4096];
            int len;
            while ((len = gtfsStream.read(buf)) > 0) {
                os.write(buf, 0, len);
            }
            os.close();
        }
        gtfsStream.close();
    }

    return new FlatGTFSFile(destBase);
}

From source file:com.alcatel_lucent.nz.wnmsextract.reader.FileSelector.java

/**
 * Top unzip method. extract tarfile to constituent parts processing gzips 
 * along the way e.g. yyyyMMdd.zip->/yyyyMMdd/INode-CH_RNC01/A2010...zip
 *///from   w  w w.j  a v a  2s.co  m
protected void unzip1(File zipfile) throws FileNotFoundException {

    try {
        ZipArchiveInputStream zais = new ZipArchiveInputStream(new FileInputStream(zipfile));
        ZipArchiveEntry z1 = null;
        while ((z1 = zais.getNextZipEntry()) != null) {
            if (z1.isDirectory()) {
                /*hack to add vcc identifier because fucking ops cant rename a simple file*/
                if (z1.getName().contains("account"))
                    identifier = ".vcc";
                else
                    identifier = "";
            } else {
                String fn = z1.getName().substring(z1.getName().lastIndexOf("/"));
                File f = new File(getCalTempPath() + fn);
                FileOutputStream fos = new FileOutputStream(f);
                BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

                int n = 0;
                byte[] content = new byte[BUFFER];
                while (-1 != (n = zais.read(content))) {
                    fos.write(content, 0, n);
                }

                bos.flush();
                bos.close();
                fos.close();

                File unz = null;
                if (f.getName().endsWith("zip"))
                    unz = unzip3(f);
                else
                    unz = ungzip(f);

                if (unz != null)
                    allfiles.add(unz);
                f.delete();
            }
        }
        zais.close();
    } catch (IOException ioe) {
        jlog.fatal("IO read error :: " + ioe);
    }

}

From source file:ee.sk.digidoc.factory.SAXDigiDocFactory.java

/**
 * Reads in a DigiDoc file. One of fname or isSdoc must be given.
 * @param fname signed doc filename//from   w ww  .j  a  v  a  2s .  com
 * @param isSdoc opened stream with DigiDoc data
 * The user must open and close it.
 * @param errs list of errors to fill with parsing errors. If given
 * then attempt is made to continue parsing on errors and return them in this list.
 * If not given (null) then the first error found will be thrown.
 * @return signed document object if successfully parsed
 */
private SignedDoc readSignedDocOfType(String fname, InputStream isSdoc, boolean isBdoc, List errs)
        throws DigiDocException {
    // Use an instance of ourselves as the SAX event handler
    SAXDigiDocFactory handler = this;
    m_errs = errs;
    DigiDocVerifyFactory.initProvider();
    SAXParserFactory factory = SAXParserFactory.newInstance();
    if (m_logger.isDebugEnabled())
        m_logger.debug("Start reading ddoc/bdoc " + ((fname != null) ? "from file: " + fname : "from stream")
                + " bdoc: " + isBdoc);
    if (fname == null && isSdoc == null) {
        throw new DigiDocException(DigiDocException.ERR_READ_FILE, "No input file", null);
    }
    if (fname != null) {
        File inFile = new File(fname);
        if (!inFile.canRead() || inFile.length() == 0) {
            throw new DigiDocException(DigiDocException.ERR_READ_FILE, "Empty or unreadable input file", null);
        }
    }
    ZipFile zf = null;
    ZipArchiveInputStream zis = null;
    ZipArchiveEntry ze = null;
    InputStream isEntry = null;
    File fTmp = null;
    try {
        factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        if (isBdoc) { // bdoc parsing
            // must be a bdoc document ?
            m_doc = new SignedDoc();
            m_doc.setVersion(SignedDoc.BDOC_VERSION_1_0);
            m_doc.setFormat(SignedDoc.FORMAT_BDOC);
            Enumeration eFiles = null;
            if (fname != null) {
                zf = new ZipFile(fname, "UTF-8");
                eFiles = zf.getEntries();
            } else if (isSdoc != null) {
                zis = new ZipArchiveInputStream(isSdoc, "UTF-8", true, true);
            }
            ArrayList lSigFnames = new ArrayList();
            ArrayList lDataFnames = new ArrayList();
            // read all entries
            boolean bHasMimetype = false, bManifest1 = false;
            int nFil = 0;
            while ((zf != null && eFiles.hasMoreElements())
                    || (zis != null && ((ze = zis.getNextZipEntry()) != null))) {
                nFil++;

                // read entry
                if (zf != null) { // ZipFile
                    ze = (ZipArchiveEntry) eFiles.nextElement();
                    isEntry = zf.getInputStream(ze);
                } else { // ZipArchiveInputStream
                    int n = 0, nTot = 0;
                    if ((ze.getName().equals(FILE_MIMETYPE) || ze.getName().equals(FILE_MANIFEST)
                            || (ze.getName().startsWith(FILE_SIGNATURES) && ze.getName().endsWith(".xml")))
                            || (nMaxBdocFilCached <= 0
                                    || (ze.getSize() < nMaxBdocFilCached && ze.getSize() >= 0))) {
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        byte[] data = new byte[2048];
                        while ((n = zis.read(data)) > 0) {
                            bos.write(data, 0, n);
                            nTot += n;
                        }
                        if (m_logger.isDebugEnabled())
                            m_logger.debug("Read: " + nTot + " bytes from zip");
                        data = bos.toByteArray();
                        bos = null;
                        isEntry = new ByteArrayInputStream(data);
                    } else {
                        File fCacheDir = new File(ConfigManager.instance().getStringProperty(
                                "DIGIDOC_DF_CACHE_DIR", System.getProperty("java.io.tmpdir")));
                        fTmp = File.createTempFile("bdoc-data", ".tmp", fCacheDir);
                        FileOutputStream fos = new FileOutputStream(fTmp);
                        byte[] data = new byte[2048];
                        while ((n = zis.read(data)) > 0) {
                            fos.write(data, 0, n);
                            nTot += n;
                        }
                        if (m_logger.isDebugEnabled())
                            m_logger.debug("Read: " + nTot + " bytes from zip to: " + fTmp.getAbsolutePath());
                        fos.close();
                        isEntry = new FileInputStream(fTmp);
                    }
                }
                if (m_logger.isDebugEnabled())
                    m_logger.debug("Entry: " + ze.getName() + " nlen: " + ze.getName().length() + " size: "
                            + ze.getSize() + " dir: " + ze.isDirectory() + " comp-size: "
                            + ze.getCompressedSize());
                // mimetype file
                if (ze.getName().equals(FILE_MIMETYPE)) {
                    if (m_logger.isDebugEnabled())
                        m_logger.debug("Check mimetype!");
                    checkBdocMimetype(isEntry);
                    bHasMimetype = true;
                    m_doc.setComment(ze.getComment());
                    if (nFil != 1) {
                        m_logger.error("mimetype file is " + nFil + " file but must be first");
                        handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML,
                                "mimetype file is not first zip entry", null));
                    }
                } else if (ze.getName().equals(FILE_MANIFEST)) { // manifest.xml file
                    if (m_logger.isDebugEnabled())
                        m_logger.debug("Read manifest");
                    if (!bManifest1 && isEntry != null) {
                        bManifest1 = true;
                        BdocManifestParser mfparser = new BdocManifestParser(m_doc);
                        mfparser.readManifest(isEntry);
                    } else {
                        m_logger.error("Found multiple manifest.xml files!");
                        throw new DigiDocException(DigiDocException.ERR_MULTIPLE_MANIFEST_FILES,
                                "Found multiple manifest.xml files!", null);
                    }
                } else if (ze.getName().startsWith(FILE_SIGNATURES) && ze.getName().endsWith(".xml")) { // some signature
                    m_fileName = ze.getName();
                    if (m_logger.isDebugEnabled())
                        m_logger.debug("Reading bdoc siganture: " + m_fileName);
                    boolean bExists = false;
                    for (int j = 0; j < lSigFnames.size(); j++) {
                        String s1 = (String) lSigFnames.get(j);
                        if (s1.equals(m_fileName))
                            bExists = true;
                    }
                    if (bExists) {
                        m_logger.error("Duplicate signature filename: " + m_fileName);
                        handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML,
                                "Duplicate signature filename: " + m_fileName, null));
                    } else
                        lSigFnames.add(m_fileName);
                    SAXParser saxParser = factory.newSAXParser();
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    int n = 0;
                    byte[] data = new byte[2048];
                    while ((n = isEntry.read(data)) > 0)
                        bos.write(data, 0, n);
                    data = bos.toByteArray();
                    bos = null;
                    if (m_logger.isDebugEnabled())
                        m_logger.debug(
                                "Parsing bdoc: " + m_fileName + " size: " + ((data != null) ? data.length : 0));
                    saxParser.parse(new SignatureInputStream(new ByteArrayInputStream(data)), this);
                    if (m_logger.isDebugEnabled())
                        m_logger.debug("Parsed bdoc: " + m_fileName);
                    Signature sig1 = m_doc.getLastSignature();
                    m_sigComment = ze.getComment();
                    if (sig1 != null) {
                        sig1.setPath(m_fileName);
                        sig1.setComment(ze.getComment());
                    }
                } else { // probably a data file
                    if (m_logger.isDebugEnabled())
                        m_logger.debug("Read data file: " + ze.getName());
                    if (!ze.isDirectory()) {
                        boolean bExists = false;
                        for (int j = 0; j < lDataFnames.size(); j++) {
                            String s1 = (String) lDataFnames.get(j);
                            if (s1.equals(ze.getName()))
                                bExists = true;
                        }
                        if (bExists) {
                            m_logger.error("Duplicate datafile filename: " + ze.getName());
                            handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML,
                                    "Duplicate datafile filename: " + ze.getName(), null));
                        } else
                            lDataFnames.add(ze.getName());
                        DataFile df = m_doc.findDataFileById(ze.getName());
                        if (df != null) {
                            if (ze.getSize() > 0)
                                df.setSize(ze.getSize());
                            df.setContentType(DataFile.CONTENT_BINARY);
                            df.setFileName(ze.getName());
                        } else {
                            df = new DataFile(ze.getName(), DataFile.CONTENT_BINARY, ze.getName(),
                                    "application/binary", m_doc);
                            if (m_doc.getDataFiles() == null)
                                m_doc.setDataFiles(new ArrayList());
                            m_doc.getDataFiles().add(df);
                            //m_doc.addDataFile(df); // this does some intiailization work unnecessary here
                        }
                        // enable caching if requested
                        if (isEntry != null)
                            df.setOrCacheBodyAndCalcHashes(isEntry);
                        df.setComment(ze.getComment());
                        df.setLastModDt(new Date(ze.getTime()));
                        // fix mime type according to DataObjectFormat
                        Signature sig1 = m_doc.getLastSignature();
                        if (sig1 != null) {
                            Reference dRef = sig1.getSignedInfo().getReferenceForDataFile(df);
                            if (dRef != null) {
                                DataObjectFormat dof = sig1.getSignedInfo()
                                        .getDataObjectFormatForReference(dRef);
                                if (dof != null) {
                                    df.setMimeType(dof.getMimeType());
                                }
                            }
                        }
                    }
                }
                if (fTmp != null) {
                    fTmp.delete();
                    fTmp = null;
                }
            } // while zip entries
            if (!bHasMimetype) {
                m_logger.error("No mimetype file");
                handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML,
                        "Not a BDOC format file! No mimetype file!", null));
            }
            // if no signatures exist then copy mime-type from manifest.xml to DataFile -s
            if (m_doc.countSignatures() == 0) {
                for (int i = 0; i < m_doc.countDataFiles(); i++) {
                    DataFile df = m_doc.getDataFile(i);
                    if (m_doc.getManifest() != null) {
                        for (int j = 0; j < m_doc.getManifest().getNumFileEntries(); j++) {
                            ManifestFileEntry mfe = m_doc.getManifest().getFileEntry(j);
                            if (mfe.getFullPath() != null && mfe.getFullPath().equals(df.getFileName())) {
                                df.setMimeType(mfe.getMediaType());
                            } // if fullpath
                        } // for
                    } // if
                } // for i
            }
        } else { // ddoc parsing
            if (m_logger.isDebugEnabled())
                m_logger.debug("Reading ddoc: " + fname + " file: " + m_fileName);
            m_fileName = fname;
            SAXParser saxParser = factory.newSAXParser();
            if (fname != null)
                saxParser.parse(new SignatureInputStream(new FileInputStream(fname)), this);
            else if (isSdoc != null)
                saxParser.parse(isSdoc, this);
        }
    } catch (org.xml.sax.SAXParseException ex) {
        m_logger.error("SAX Error: " + ex);
        handleError(ex);

    } catch (Exception ex) {
        m_logger.error("Error reading3: " + ex);
        ex.printStackTrace();
        /*if(ex instanceof DigiDocException){
           DigiDocException dex = (DigiDocException)ex;
           m_logger.error("Dex: " + ex);
           if(dex.getNestedException() != null) {
              dex.getNestedException().printStackTrace();
              m_logger.error("Trace: "); 
           }
        }*/
        handleError(ex);
    } finally { // cleanup
        try {
            if (isEntry != null) {
                isEntry.close();
                isEntry = null;
            }
            if (zis != null)
                zis.close();
            if (zf != null)
                zf.close();
            if (fTmp != null) {
                fTmp.delete();
                fTmp = null;
            }
        } catch (Exception ex) {
            m_logger.error("Error closing streams and files: " + ex);
        }
    }
    // compare Manifest and DataFiles
    boolean bErrList = (errs != null);
    if (errs == null)
        errs = new ArrayList();
    boolean bOk = DigiDocVerifyFactory.verifyManifestEntries(m_doc, errs);
    if (m_doc == null) {
        m_logger.error("Error reading4: doc == null");
        handleError(new DigiDocException(DigiDocException.ERR_DIGIDOC_BADXML,
                "This document is not in ddoc or bdoc format", null));
    }
    if (!bErrList && errs.size() > 0) { // if error list was not used then we have to throw exception. So we will throw the first one since we can only do it once
        DigiDocException ex = (DigiDocException) errs.get(0);
        throw ex;
    }
    return m_doc;
}

From source file:nz.co.kakariki.networkutils.reader.ExtractArchive.java

/**
 * Top unzip method./*from   w ww .  jav  a2s . co m*/
 */
protected static void unzip(File zipfile) throws FileNotFoundException {
    File path = zipfile.getParentFile();
    try {
        ZipArchiveInputStream zais = new ZipArchiveInputStream(new FileInputStream(zipfile));
        ZipArchiveEntry z1 = null;
        while ((z1 = zais.getNextZipEntry()) != null) {
            String fn = z1.getName();
            if (fn.contains("/")) {
                fn = fn.substring(z1.getName().lastIndexOf("/"));
            }
            File f = new File(path + File.separator + fn);
            FileOutputStream fos = new FileOutputStream(f);
            BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);

            int n = 0;
            byte[] content = new byte[BUFFER];
            while (-1 != (n = zais.read(content))) {
                fos.write(content, 0, n);
            }

            bos.flush();
            bos.close();
            fos.close();
        }

        zais.close();
        zipfile.delete();
    } catch (IOException ioe) {
        jlog.fatal("IO read error :: " + ioe);
    }

}

From source file:org.ngrinder.common.util.CompressionUtil.java

/**
 * Unzip the given input stream into destination directory with the given character set.
 * //from ww  w . j  a v a2  s  .  co m
 * @param is
 *            input stream
 * @param destDir
 *            destination directory
 * @param charsetName
 *            character set name
 */
public static void unzip(InputStream is, File destDir, String charsetName) {
    ZipArchiveInputStream zis = null;
    try {
        ZipArchiveEntry entry;
        String name;
        File target;
        int nWritten = 0;
        BufferedOutputStream bos;
        byte[] buf = new byte[1024 * 8];
        zis = new ZipArchiveInputStream(is, charsetName, false);
        while ((entry = zis.getNextZipEntry()) != null) {
            name = entry.getName();
            target = new File(destDir, name);
            if (entry.isDirectory()) {
                target.mkdirs(); /* does it always work? */
            } else {
                target.createNewFile();
                bos = new BufferedOutputStream(new FileOutputStream(target));
                while ((nWritten = zis.read(buf)) >= 0) {
                    bos.write(buf, 0, nWritten);
                }
                bos.close();
            }
        }
    } catch (Exception e) {
        throw new NGrinderRuntimeException(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(zis);
    }
}

From source file:org.ngrinder.script.util.CompressionUtil.java

public void unzip(InputStream is, File destDir, String charsetName) throws IOException {
    ZipArchiveInputStream zis;
    ZipArchiveEntry entry;/*from w w w.  ja  v a  2  s . c om*/
    String name;
    File target;
    int nWritten = 0;
    BufferedOutputStream bos;
    byte[] buf = new byte[1024 * 8];

    zis = new ZipArchiveInputStream(is, charsetName, false);
    while ((entry = zis.getNextZipEntry()) != null) {
        name = entry.getName();
        target = new File(destDir, name);
        if (entry.isDirectory()) {
            target.mkdirs(); /* does it always work? */
        } else {
            target.createNewFile();
            bos = new BufferedOutputStream(new FileOutputStream(target));
            while ((nWritten = zis.read(buf)) >= 0) {
                bos.write(buf, 0, nWritten);
            }
            bos.close();
        }
    }
    zis.close();
}

From source file:org.panbox.core.pairing.file.PanboxFilePairingUtils.java

public static PanboxFilePairingLoadReturnContainer loadPairingFile(File inputFile, char[] password)
        throws IOException, NoSuchAlgorithmException, CertificateException, KeyStoreException,
        UnrecoverableKeyException, IllegalArgumentException {
    ZipArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(inputFile));
    try {/*from w w w .j  a  va  2s  .  co m*/
        byte[] buffer = new byte[1048576]; //1MB

        ArchiveEntry entry;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int len = 0;

        // ENTRY 1: devicename
        entry = in.getNextEntry();

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for device name.");
            throw new IllegalArgumentException("Could not find entry for device name.");
        }

        baos = new ByteArrayOutputStream();
        len = 0;
        while ((len = in.read(buffer)) > 0) {
            baos.write(buffer, 0, len);
        }

        String devicename = new String(baos.toByteArray());

        // ENTRY 2: eMail
        entry = in.getNextEntry();

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for eMail.");
            throw new IllegalArgumentException("Could not find entry for eMail.");
        }

        baos = new ByteArrayOutputStream();
        len = 0;
        while ((len = in.read(buffer)) > 0) {
            baos.write(buffer, 0, len);
        }

        String eMail = new String(baos.toByteArray());

        // ENTRY 3: firstName
        entry = in.getNextEntry();

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for first name.");
            throw new IllegalArgumentException("Could not find entry for first name.");
        }

        baos = new ByteArrayOutputStream();
        len = 0;
        while ((len = in.read(buffer)) > 0) {
            baos.write(buffer, 0, len);
        }

        String firstName = new String(baos.toByteArray());

        // ENTRY 4: lastName
        entry = in.getNextEntry();

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for last name.");
            throw new IllegalArgumentException("Could not find entry for last name.");
        }

        baos = new ByteArrayOutputStream();
        len = 0;
        while ((len = in.read(buffer)) > 0) {
            baos.write(buffer, 0, len);
        }

        String lastName = new String(baos.toByteArray());

        // ENTRY 5: devKeyStore.p12
        entry = in.getNextEntry();

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for device key store.");
            throw new IllegalArgumentException("Could not find entry for device key store.");
        }

        KeyStore devKeyStore = KeyStore.getInstance("PKCS12");
        devKeyStore.load(in, password);
        PrivateKey devPKey = (PrivateKey) devKeyStore.getKey(devicename.toLowerCase(), password);
        Certificate[] devCert = devKeyStore.getCertificateChain(devicename.toLowerCase());

        // ENTRY 6: knownDevices.list/knownDevices.bks
        entry = in.getNextEntry(); // knownDevices.list

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for knownDevices.list.");
            throw new IllegalArgumentException("Could not find entry for knownDevices.list.");
        }

        Map<String, X509Certificate> devices = new HashMap<String, X509Certificate>();

        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        Map<String, String> deviceNames = new HashMap<String, String>();

        String line;
        while ((line = br.readLine()) != null) {
            String[] values = line.split(DELIMITER);
            deviceNames.put(values[0], values[1]);
        }

        entry = in.getNextEntry(); // knownDevices.bks

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for knownDevices.bks.");
            throw new IllegalArgumentException("Could not find entry for knownDevices.bks.");
        }

        KeyStore devicesStore = KeyStore.getInstance("BKS");
        devicesStore.load(in, password);

        for (Entry<String, String> device : deviceNames.entrySet()) {
            X509Certificate deviceCert = (X509Certificate) devicesStore.getCertificate(device.getKey());
            devices.put(device.getValue(), deviceCert);
        }

        // ENTRY 7: contacts.vcard
        entry = in.getNextEntry();

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for contacts.");
            throw new IllegalArgumentException("Could not find entry for contacts.");
        }

        File contacts = File.createTempFile("panbox" + (new Random().nextInt(65536) - 32768), null);
        FileOutputStream fos = new FileOutputStream(contacts);
        len = 0;
        while ((len = in.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }
        fos.flush();
        fos.close();

        // ENTRY 8: ownerKeyStore/ownerCertStore.jks
        entry = in.getNextEntry();

        ByteArrayOutputStream tmp = new ByteArrayOutputStream();
        IOUtils.copy(in, tmp);
        ByteArrayInputStream buf = new ByteArrayInputStream(tmp.toByteArray());

        if (entry == null) {
            logger.error("PanboxClient : loadPairingFile : Could not find entry for owner key store.");
            throw new IllegalArgumentException("Could not find entry for owner key store.");
        }

        KeyStore ownerKeyStore = null;
        try {
            // Check if pairing is MASTER
            ownerKeyStore = KeyStore.getInstance("PKCS12");
            ownerKeyStore.load(buf, password);
            // At this point we know it's a PKCS11 file!
            PrivateKey ownerEncKey = (PrivateKey) ownerKeyStore.getKey("ownerEncKey", password);
            Certificate[] ownerEncCert = ownerKeyStore.getCertificateChain("ownerEncKey");
            PrivateKey ownerSignKey = (PrivateKey) ownerKeyStore.getKey("ownerSignKey", password);
            Certificate[] ownerSignCert = ownerKeyStore.getCertificateChain("ownerSignKey");
            in.close();
            removeInputFile(inputFile);

            return new PanboxFilePairingLoadReturnContainer(eMail, firstName, lastName, password, devicename,
                    devPKey, devCert[0], ownerSignKey, ownerSignCert[0], ownerEncKey, ownerEncCert[0], devices,
                    contacts);
        } catch (Exception e) {
            // SLAVE
            try {
                buf = new ByteArrayInputStream(tmp.toByteArray());
                ownerKeyStore = KeyStore.getInstance("BKS");
                ownerKeyStore.load(buf, password);
                Certificate ownerEncCert = ownerKeyStore.getCertificate("ownerEncCert");
                Certificate ownerSignCert = ownerKeyStore.getCertificate("ownerSignCert");
                in.close();
                removeInputFile(inputFile);

                return new PanboxFilePairingLoadReturnContainer(eMail, firstName, lastName, password,
                        devicename, devPKey, devCert[0], null, ownerSignCert, null, ownerEncCert, devices,
                        contacts);
            } catch (Exception ex) {
                logger.error(
                        "PanboxClient : loadPairingFile : Could not determine if pairing file was master or slave.");
                throw new IllegalArgumentException("Pairing type was unknown. Broken file?");
            }
        }
    } catch (IOException | NoSuchAlgorithmException | CertificateException | KeyStoreException
            | UnrecoverableKeyException | IllegalArgumentException e) {
        in.close();
        throw e;
    }

}