Search in sources :

Example 1 with SwordBookMetaData

use of org.crosswire.jsword.book.sword.SwordBookMetaData in project step by STEPBible.

the class DirectoryListingInstaller method extraConfFile.

/**
 * Extracts a single conf file to read its details
 *
 * @param zipFile the zip file in question
 */
private void extraConfFile(BookDriver fakeDriver, File zipFile) {
    InputStream in = null;
    ZipInputStream zin = null;
    try {
        ConfigEntry.resetStatistics();
        in = NetUtil.getInputStream(zipFile.toURI());
        zin = new ZipInputStream(in);
        while (true) {
            ZipEntry entry = zin.getNextEntry();
            if (entry == null) {
                break;
            }
            String internal = entry.getName();
            if (internal.endsWith(SwordConstants.EXTENSION_CONF)) {
                LOGGER.trace("Reading a conf file [{}]", entry.getName());
                try {
                    int size = (int) entry.getSize();
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    byte[] bytes = new byte[1024];
                    int read;
                    while ((read = zin.read(bytes)) > 0) {
                        os.write(bytes, 0, read);
                    }
                    internal = internal.substring(0, internal.length() - 5);
                    if (internal.startsWith(SwordConstants.DIR_CONF + '/')) {
                        internal = internal.substring(7);
                    }
                    SwordBookMetaData sbmd = new SwordBookMetaData(os.toByteArray(), internal);
                    sbmd.setDriver(fakeDriver);
                    Book book = new SwordBook(sbmd, new NullBackend());
                    entries.put(book.getName(), book);
                    // assume 1 conf file per zip file
                    return;
                } catch (Exception ex) {
                    LOGGER.error("Failed to load config for entry: {}", internal, ex);
                }
            }
        }
        ConfigEntry.dumpStatistics();
    } catch (IOException ex) {
        LOGGER.error("Failed to configuration from [{}]", zipFile.getName());
        throw new StepInternalException("Error reading directory of zip files.", ex);
    } finally {
        IOUtil.close(zin);
        IOUtil.close(in);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SwordBookMetaData(org.crosswire.jsword.book.sword.SwordBookMetaData) NullBackend(org.crosswire.jsword.book.sword.NullBackend) IOException(java.io.IOException) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) InstallException(org.crosswire.jsword.book.install.InstallException) ZipInputStream(java.util.zip.ZipInputStream) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) SwordBook(org.crosswire.jsword.book.sword.SwordBook) Book(org.crosswire.jsword.book.Book) SwordBook(org.crosswire.jsword.book.sword.SwordBook)

Aggregations

StepInternalException (com.tyndalehouse.step.core.exceptions.StepInternalException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 Book (org.crosswire.jsword.book.Book)1 InstallException (org.crosswire.jsword.book.install.InstallException)1 NullBackend (org.crosswire.jsword.book.sword.NullBackend)1 SwordBook (org.crosswire.jsword.book.sword.SwordBook)1 SwordBookMetaData (org.crosswire.jsword.book.sword.SwordBookMetaData)1