Example usage for java.util.zip ZipEntry getSize

List of usage examples for java.util.zip ZipEntry getSize

Introduction

In this page you can find the example usage for java.util.zip ZipEntry getSize.

Prototype

public long getSize() 

Source Link

Document

Returns the uncompressed size of the entry data.

Usage

From source file:org.codice.ddf.commands.catalog.ImportCommand.java

@Override
protected final Object executeWithSubject() throws Exception {
    int metacards = 0;
    int content = 0;
    int derivedContent = 0;
    File file = initImportFile(importFile);
    InputTransformer transformer = getServiceByFilter(InputTransformer.class,
            String.format("(%s=%s)", "id", DEFAULT_TRANSFORMER_ID))
                    .orElseThrow(() -> new CatalogCommandRuntimeException(
                            "Could not get " + DEFAULT_TRANSFORMER_ID + " input transformer"));

    if (unsafe) {
        if (!force) {
            String input = session.readLine(
                    "This will import data with no check to see if data is modified/corrupt. Do you wish to continue? (y/N) ",
                    null);/*ww w .  ja va 2  s.co  m*/
            if (!input.matches("^[yY][eE]?[sS]?$")) {
                console.println("ABORTED IMPORT.");
                return null;
            }
        }
        SecurityLogger.audit("Skipping validation check of imported data. There are no "
                + "guarantees of integrity or authenticity of the imported data." + "File being imported: {}",
                importFile);
    } else {
        if (StringUtils.isBlank(signatureFile)) {
            String message = "A signature file must be provided with import data";
            console.println(message);
            throw new CatalogCommandRuntimeException(message);
        }

        String alias = AccessController.doPrivileged(
                (PrivilegedAction<String>) () -> System.getProperty("org.codice.ddf.system.hostname"));

        try (FileInputStream fileIs = new FileInputStream(file);
                FileInputStream sigFileIs = new FileInputStream(signatureFile)) {
            if (!verifier.verifyDigitalSignature(fileIs, sigFileIs, alias)) {
                throw new CatalogCommandRuntimeException("The provided data could not be verified");
            }
        }
    }
    SecurityLogger.audit("Called catalog:import command on the file: {}", importFile);
    console.println("Importing file");
    Instant start = Instant.now();
    try (InputStream fis = new FileInputStream(file); ZipInputStream zipInputStream = new ZipInputStream(fis)) {
        ZipEntry entry = zipInputStream.getNextEntry();

        while (entry != null) {
            String filename = entry.getName();

            if (filename.startsWith("META-INF")) {
                entry = zipInputStream.getNextEntry();
                continue;
            }

            String[] pathParts = filename.split("\\" + File.separator);
            if (pathParts.length < 5) {
                console.println("Entry is not valid! " + filename);
                entry = zipInputStream.getNextEntry();
                continue;
            }
            String id = pathParts[ID];
            String type = pathParts[TYPE];

            switch (type) {
            case "metacard": {
                String metacardName = pathParts[NAME];
                Metacard metacard = null;
                try {
                    metacard = transformer.transform(new UncloseableBufferedInputStreamWrapper(zipInputStream),
                            id);
                } catch (IOException | CatalogTransformerException e) {
                    LOGGER.debug("Could not transform metacard: {}", id);
                    entry = zipInputStream.getNextEntry();
                    continue;
                }
                metacard = applyInjectors(metacard, attributeInjectors);
                catalogProvider.create(new CreateRequestImpl(metacard));
                metacards++;
                break;
            }
            case "content": {
                content++;
                String contentFilename = pathParts[NAME];
                ContentItem contentItem = new ContentItemImpl(id,
                        new ZipEntryByteSource(new UncloseableBufferedInputStreamWrapper(zipInputStream)), null,
                        contentFilename, entry.getSize(), null);
                CreateStorageRequestImpl createStorageRequest = new CreateStorageRequestImpl(
                        Collections.singletonList(contentItem), id, new HashMap<>());
                storageProvider.create(createStorageRequest);
                storageProvider.commit(createStorageRequest);
                break;
            }
            case "derived": {
                derivedContent++;
                String qualifier = pathParts[NAME];
                String derivedContentName = pathParts[DERIVED_NAME];
                ContentItem contentItem = new ContentItemImpl(id, qualifier,
                        new ZipEntryByteSource(new UncloseableBufferedInputStreamWrapper(zipInputStream)), null,
                        derivedContentName, entry.getSize(), null);
                CreateStorageRequestImpl createStorageRequest = new CreateStorageRequestImpl(
                        Collections.singletonList(contentItem), id, new HashMap<>());
                storageProvider.create(createStorageRequest);
                storageProvider.commit(createStorageRequest);
                break;
            }
            default: {
                LOGGER.debug("Cannot interpret type of {}", type);
            }
            }

            entry = zipInputStream.getNextEntry();
        }
    } catch (Exception e) {
        printErrorMessage(String.format(
                "Exception while importing metacards (%s)%nFor more information set the log level to INFO (log:set INFO org.codice.ddf.commands.catalog) ",
                e.getMessage()));
        LOGGER.info("Exception while importing metacards", e);
        throw e;
    }
    console.println("File imported successfully. Imported in: " + getFormattedDuration(start));
    console.println("Number of metacards imported: " + metacards);
    console.println("Number of content imported: " + content);
    console.println("Number of derived content imported: " + derivedContent);
    return null;
}

From source file:org.zeroturnaround.zip.ZipUtil.java

/**
 * Compares meta-data of two ZIP entries.
 * <p>/*from  w ww  .  j av  a 2 s .c om*/
 * Two entries are considered the same if
 * <ol>
 * <li>both entries exist,</li>
 * <li>both entries are either directories or files,</li>
 * <li>both entries have the same size,</li>
 * <li>both entries have the same CRC.</li>
 * </ol>
 *
 * @param path
 *          name of the entries.
 * @param e1
 *          first entry (required).
 * @param e2
 *          second entry (may be <code>null</code>).
 * @return <code>true</code> if no difference was found.
 */
private static boolean metaDataEquals(String path, ZipEntry e1, ZipEntry e2) throws IOException {
    // Check if the same entry exists in the second archive
    if (e2 == null) {
        log.debug("Entry '{}' removed.", path);
        return false;
    }

    // Check the directory flag
    if (e1.isDirectory()) {
        if (e2.isDirectory()) {
            return true; // Let's skip the directory as there is nothing to compare
        } else {
            log.debug("Entry '{}' not a directory any more.", path);
            return false;
        }
    } else if (e2.isDirectory()) {
        log.debug("Entry '{}' now a directory.", path);
        return false;
    }

    // Check the size
    long size1 = e1.getSize();
    long size2 = e2.getSize();
    if (size1 != -1 && size2 != -1 && size1 != size2) {
        log.debug("Entry '" + path + "' size changed (" + size1 + " vs " + size2 + ").");
        return false;
    }

    // Check the CRC
    long crc1 = e1.getCrc();
    long crc2 = e2.getCrc();
    if (crc1 != -1 && crc2 != -1 && crc1 != crc2) {
        log.debug("Entry '" + path + "' CRC changed (" + crc1 + " vs " + crc2 + ").");
        return false;
    }

    // Check the time (ignored, logging only)
    if (log.isTraceEnabled()) {
        long time1 = e1.getTime();
        long time2 = e2.getTime();
        if (time1 != -1 && time2 != -1 && time1 != time2) {
            log.trace(
                    "Entry '" + path + "' time changed (" + new Date(time1) + " vs " + new Date(time2) + ").");
        }
    }

    return true;
}

From source file:org.ejbca.ui.web.admin.configuration.SystemConfigMBean.java

private void importStatedump(byte[] zip, boolean lockdown) throws IOException, AuthorizationDeniedException {
    // Check that it's a ZIP file
    if (zip.length < 2 || zip[0] != 'P' || zip[1] != 'K') {
        throw new IOException("File is not a valid zip file.");
    }/*from ww w. j a  va  2s  . c  o m*/

    // Create temporary directory
    final Path tempdirPath = Files.createTempDirectory("ejbca_statedump_gui");
    final File tempdir = tempdirPath.toFile();
    log.info("Importing " + zip.length + " byte statedump zip file, using temporary directory " + tempdir);

    // Unpack the zip file
    try (final ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(zip))) {
        boolean empty = true;
        long limit = 100_000_000; // Maximum total uncompressed size is 100 MB
        while (true) {
            final ZipEntry entry = zipStream.getNextEntry();
            if (entry == null) {
                break;
            }
            if (entry.isDirectory()) {
                zipStream.closeEntry();
                continue;
            }

            final String name = entry.getName().replaceFirst("^.*/([^/]+)$", "$1");
            if (name.matches("([a-z0-9_-]+\\.xml|replacements.properties)")) {
                if (log.isDebugEnabled()) {
                    log.debug("Extracting zip file entry " + name + " into temporary directory");
                }

                if (entry.getSize() == 0) {
                    log.debug("Ignoring empty file");
                    zipStream.closeEntry();
                    continue;
                }

                // Create file exclusively (don't overwrite, and don't write to special devices or operating system special files)
                final Path filepath = Files.createFile(new File(tempdir, name).toPath());
                try (final FileOutputStream fos = new FileOutputStream(filepath.toFile())) {
                    try {
                        limit -= FileTools.streamCopyWithLimit(zipStream, fos, limit);
                    } catch (StreamSizeLimitExceededException ssle) {
                        throw new IOException("Zip file is larger than 100 MB. Aborting.");
                    }
                }
                zipStream.closeEntry();
                empty = false;
            } else if (log.isDebugEnabled()) {
                log.debug("Ignoring zip file entry " + name);
            }
        }

        if (empty) {
            throw new IOException("Zip file didn't contain any statedump xml files.");
        }

        // Import statedump
        importStatedump(tempdir, lockdown);

    } finally {
        // Clean up
        log.debug("Removing temporary directory for statedump XML files");
        FileUtils.deleteDirectory(tempdir);
    }
}

From source file:com.flexive.core.storage.GenericDivisionImporter.java

/**
 * Import data from a zip archive to a database table
 *
 * @param stmt               statement to use
 * @param zip                zip archive containing the zip entry
 * @param ze                 zip entry within the archive
 * @param xpath              xpath containing the entries to import
 * @param table              name of the table
 * @param executeInsertPhase execute the insert phase?
 * @param executeUpdatePhase execute the update phase?
 * @param updateColumns      columns that should be set to <code>null</code> in a first pass (insert)
 *                           and updated to the provided values in a second pass (update),
 *                           columns that should be used in the where clause have to be prefixed
 *                           with "KEY:", to assign a default value use the expression "columnname:default value",
 *                           if the default value is "@", it will be a negative counter starting at 0, decreasing.
 *                           If the default value starts with "%", it will be set to the column following the "%"
 *                           character in the first pass
 * @throws Exception on errors//from  w  w  w . j ava 2 s.  co  m
 */
protected void importTable(Statement stmt, final ZipFile zip, final ZipEntry ze, final String xpath,
        final String table, final boolean executeInsertPhase, final boolean executeUpdatePhase,
        final String... updateColumns) throws Exception {
    //analyze the table
    final ResultSet rs = stmt.executeQuery("SELECT * FROM " + table + " WHERE 1=2");
    StringBuilder sbInsert = new StringBuilder(500);
    StringBuilder sbUpdate = updateColumns.length > 0 ? new StringBuilder(500) : null;
    if (rs == null)
        throw new IllegalArgumentException("Can not analyze table [" + table + "]!");
    sbInsert.append("INSERT INTO ").append(table).append(" (");
    final ResultSetMetaData md = rs.getMetaData();
    final Map<String, ColumnInfo> updateClauseColumns = updateColumns.length > 0
            ? new HashMap<String, ColumnInfo>(md.getColumnCount())
            : null;
    final Map<String, ColumnInfo> updateSetColumns = updateColumns.length > 0
            ? new LinkedHashMap<String, ColumnInfo>(md.getColumnCount())
            : null;
    final Map<String, String> presetColumns = updateColumns.length > 0 ? new HashMap<String, String>(10) : null;
    //preset to a referenced column (%column syntax)
    final Map<String, String> presetRefColumns = updateColumns.length > 0 ? new HashMap<String, String>(10)
            : null;
    final Map<String, Integer> counters = updateColumns.length > 0 ? new HashMap<String, Integer>(10) : null;
    final Map<String, ColumnInfo> insertColumns = new HashMap<String, ColumnInfo>(
            md.getColumnCount() + (counters != null ? counters.size() : 0));
    int insertIndex = 1;
    int updateSetIndex = 1;
    int updateClauseIndex = 1;
    boolean first = true;
    for (int i = 0; i < md.getColumnCount(); i++) {
        final String currCol = md.getColumnName(i + 1).toLowerCase();
        if (updateColumns.length > 0) {
            boolean abort = false;
            for (String col : updateColumns) {
                if (col.indexOf(':') > 0 && !col.startsWith("KEY:")) {
                    String value = col.substring(col.indexOf(':') + 1);
                    col = col.substring(0, col.indexOf(':'));
                    if ("@".equals(value)) {
                        if (currCol.equalsIgnoreCase(col)) {
                            counters.put(col, 0);
                            insertColumns.put(col, new ColumnInfo(md.getColumnType(i + 1), insertIndex++));
                            sbInsert.append(',').append(currCol);
                        }
                    } else if (value.startsWith("%")) {
                        if (currCol.equalsIgnoreCase(col)) {
                            presetRefColumns.put(col, value.substring(1));
                            insertColumns.put(col, new ColumnInfo(md.getColumnType(i + 1), insertIndex++));
                            sbInsert.append(',').append(currCol);
                            //                                System.out.println("==> adding presetRefColumn "+col+" with value of "+value.substring(1));
                        }
                    } else if (!presetColumns.containsKey(col))
                        presetColumns.put(col, value);
                }
                if (currCol.equalsIgnoreCase(col)) {
                    abort = true;
                    updateSetColumns.put(currCol, new ColumnInfo(md.getColumnType(i + 1), updateSetIndex++));
                    break;
                }
            }
            if (abort)
                continue;
        }
        if (first) {
            first = false;
        } else
            sbInsert.append(',');
        sbInsert.append(currCol);
        insertColumns.put(currCol, new ColumnInfo(md.getColumnType(i + 1), insertIndex++));
    }
    if (updateColumns.length > 0 && executeUpdatePhase) {
        sbUpdate.append("UPDATE ").append(table).append(" SET ");
        int counter = 0;
        for (String updateColumn : updateSetColumns.keySet()) {
            if (counter++ > 0)
                sbUpdate.append(',');
            sbUpdate.append(updateColumn).append("=?");
        }
        sbUpdate.append(" WHERE ");
        boolean hasKeyColumn = false;
        for (String col : updateColumns) {
            if (!col.startsWith("KEY:"))
                continue;
            hasKeyColumn = true;
            String keyCol = col.substring(4);
            for (int i = 0; i < md.getColumnCount(); i++) {
                if (!md.getColumnName(i + 1).equalsIgnoreCase(keyCol))
                    continue;
                updateClauseColumns.put(keyCol, new ColumnInfo(md.getColumnType(i + 1), updateClauseIndex++));
                sbUpdate.append(keyCol).append("=? AND ");
                break;
            }

        }
        if (!hasKeyColumn)
            throw new IllegalArgumentException("Update columns require a KEY!");
        sbUpdate.delete(sbUpdate.length() - 5, sbUpdate.length()); //remove trailing " AND "
        //"shift" clause indices
        for (String col : updateClauseColumns.keySet()) {
            GenericDivisionImporter.ColumnInfo ci = updateClauseColumns.get(col);
            ci.index += (updateSetIndex - 1);
        }
    }
    if (presetColumns != null) {
        for (String key : presetColumns.keySet())
            sbInsert.append(',').append(key);
    }
    sbInsert.append(")VALUES(");
    for (int i = 0; i < insertColumns.size(); i++) {
        if (i > 0)
            sbInsert.append(',');
        sbInsert.append('?');
    }
    if (presetColumns != null) {
        for (String key : presetColumns.keySet())
            sbInsert.append(',').append(presetColumns.get(key));
    }
    sbInsert.append(')');
    if (DBG) {
        LOG.info("Insert statement:\n" + sbInsert.toString());
        if (updateColumns.length > 0)
            LOG.info("Update statement:\n" + sbUpdate.toString());
    }
    //build a map containing all nodes that require attributes
    //this allows for matching simple xpath queries like "flatstorages/storage[@name='FX_FLAT_STORAGE']/data"
    final Map<String, List<String>> queryAttributes = new HashMap<String, List<String>>(5);
    for (String pElem : xpath.split("/")) {
        if (!(pElem.indexOf('@') > 0 && pElem.indexOf('[') > 0))
            continue;
        List<String> att = new ArrayList<String>(5);
        for (String pAtt : pElem.split("@")) {
            if (!(pAtt.indexOf('=') > 0))
                continue;
            att.add(pAtt.substring(0, pAtt.indexOf('=')));
        }
        queryAttributes.put(pElem.substring(0, pElem.indexOf('[')), att);
    }
    final PreparedStatement psInsert = stmt.getConnection().prepareStatement(sbInsert.toString());
    final PreparedStatement psUpdate = updateColumns.length > 0 && executeUpdatePhase
            ? stmt.getConnection().prepareStatement(sbUpdate.toString())
            : null;
    try {
        final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        final DefaultHandler handler = new DefaultHandler() {
            private String currentElement = null;
            private Map<String, String> data = new HashMap<String, String>(10);
            private StringBuilder sbData = new StringBuilder(10000);
            boolean inTag = false;
            boolean inElement = false;
            int counter;
            List<String> path = new ArrayList<String>(10);
            StringBuilder currPath = new StringBuilder(100);
            boolean insertMode = true;

            /**
             * {@inheritDoc}
             */
            @Override
            public void startDocument() throws SAXException {
                counter = 0;
                inTag = false;
                inElement = false;
                path.clear();
                currPath.setLength(0);
                sbData.setLength(0);
                data.clear();
                currentElement = null;
            }

            /**
             * {@inheritDoc}
             */
            @Override
            public void processingInstruction(String target, String data) throws SAXException {
                if (target != null && target.startsWith("fx_")) {
                    if (target.equals("fx_mode"))
                        insertMode = "insert".equals(data);
                } else
                    super.processingInstruction(target, data);
            }

            /**
             * {@inheritDoc}
             */
            @Override
            public void endDocument() throws SAXException {
                if (insertMode)
                    LOG.info("Imported [" + counter + "] entries into [" + table + "] for xpath [" + xpath
                            + "]");
                else
                    LOG.info("Updated [" + counter + "] entries in [" + table + "] for xpath [" + xpath + "]");
            }

            /**
             * {@inheritDoc}
             */
            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes)
                    throws SAXException {
                pushPath(qName, attributes);
                if (currPath.toString().equals(xpath)) {
                    inTag = true;
                    data.clear();
                    for (int i = 0; i < attributes.getLength(); i++) {
                        String name = attributes.getLocalName(i);
                        if (StringUtils.isEmpty(name))
                            name = attributes.getQName(i);
                        data.put(name, attributes.getValue(i));
                    }
                } else {
                    currentElement = qName;
                }
                inElement = true;
                sbData.setLength(0);
            }

            /**
             * Push a path element from the stack
             *
             * @param qName element name to push
             * @param att attributes
             */
            private void pushPath(String qName, Attributes att) {
                if (att.getLength() > 0 && queryAttributes.containsKey(qName)) {
                    String curr = qName + "[";
                    boolean first = true;
                    final List<String> attList = queryAttributes.get(qName);
                    for (int i = 0; i < att.getLength(); i++) {
                        if (!attList.contains(att.getQName(i)))
                            continue;
                        if (first)
                            first = false;
                        else
                            curr += ',';
                        curr += "@" + att.getQName(i) + "='" + att.getValue(i) + "'";
                    }
                    curr += ']';
                    path.add(curr);
                } else
                    path.add(qName);
                buildPath();
            }

            /**
             * Pop the top path element from the stack
             */
            private void popPath() {
                path.remove(path.size() - 1);
                buildPath();
            }

            /**
             * Rebuild the current path
             */
            private synchronized void buildPath() {
                currPath.setLength(0);
                for (String s : path)
                    currPath.append(s).append('/');
                if (currPath.length() > 1)
                    currPath.delete(currPath.length() - 1, currPath.length());
                //                    System.out.println("currPath: " + currPath);
            }

            /**
             * {@inheritDoc}
             */
            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
                if (currPath.toString().equals(xpath)) {
                    if (DBG)
                        LOG.info("Insert [" + xpath + "]: [" + data + "]");
                    inTag = false;
                    try {
                        if (insertMode) {
                            if (executeInsertPhase) {
                                processColumnSet(insertColumns, psInsert);
                                counter += psInsert.executeUpdate();
                            }
                        } else {
                            if (executeUpdatePhase) {
                                if (processColumnSet(updateSetColumns, psUpdate)) {
                                    processColumnSet(updateClauseColumns, psUpdate);
                                    counter += psUpdate.executeUpdate();
                                }
                            }
                        }
                    } catch (SQLException e) {
                        throw new SAXException(e);
                    } catch (ParseException e) {
                        throw new SAXException(e);
                    }
                } else {
                    if (inTag) {
                        data.put(currentElement, sbData.toString());
                    }
                    currentElement = null;
                }
                popPath();
                inElement = false;
                sbData.setLength(0);
            }

            /**
             * Process a column set
             *
             * @param columns the columns to process
             * @param ps prepared statement to use
             * @return if data other than <code>null</code> has been set
             * @throws SQLException on errors
             * @throws ParseException on date/time conversion errors
             */
            private boolean processColumnSet(Map<String, ColumnInfo> columns, PreparedStatement ps)
                    throws SQLException, ParseException {
                boolean dataSet = false;
                for (String col : columns.keySet()) {
                    ColumnInfo ci = columns.get(col);
                    String value = data.get(col);
                    if (insertMode && counters != null && counters.get(col) != null) {
                        final int newVal = counters.get(col) - 1;
                        value = String.valueOf(newVal);
                        counters.put(col, newVal);
                        //                            System.out.println("new value for " + col + ": " + newVal);
                    }
                    if (insertMode && presetRefColumns != null && presetRefColumns.get(col) != null) {
                        value = data.get(presetRefColumns.get(col));
                        //                            System.out.println("Set presetRefColumn for "+col+" to ["+value+"] from column ["+presetRefColumns.get(col)+"]");
                    }

                    if (value == null)
                        ps.setNull(ci.index, ci.columnType);
                    else {
                        dataSet = true;
                        switch (ci.columnType) {
                        case Types.BIGINT:
                        case Types.NUMERIC:
                            if (DBG)
                                LOG.info("BigInt " + ci.index + "->" + new BigDecimal(value));
                            ps.setBigDecimal(ci.index, new BigDecimal(value));
                            break;
                        case java.sql.Types.DOUBLE:
                            if (DBG)
                                LOG.info("Double " + ci.index + "->" + Double.parseDouble(value));
                            ps.setDouble(ci.index, Double.parseDouble(value));
                            break;
                        case java.sql.Types.FLOAT:
                        case java.sql.Types.REAL:
                            if (DBG)
                                LOG.info("Float " + ci.index + "->" + Float.parseFloat(value));
                            ps.setFloat(ci.index, Float.parseFloat(value));
                            break;
                        case java.sql.Types.TIMESTAMP:
                        case java.sql.Types.DATE:
                            if (DBG)
                                LOG.info("Timestamp/Date " + ci.index + "->"
                                        + FxFormatUtils.getDateTimeFormat().parse(value));
                            ps.setTimestamp(ci.index,
                                    new Timestamp(FxFormatUtils.getDateTimeFormat().parse(value).getTime()));
                            break;
                        case Types.TINYINT:
                        case Types.SMALLINT:
                            if (DBG)
                                LOG.info("Integer " + ci.index + "->" + Integer.valueOf(value));
                            ps.setInt(ci.index, Integer.valueOf(value));
                            break;
                        case Types.INTEGER:
                        case Types.DECIMAL:
                            try {
                                if (DBG)
                                    LOG.info("Long " + ci.index + "->" + Long.valueOf(value));
                                ps.setLong(ci.index, Long.valueOf(value));
                            } catch (NumberFormatException e) {
                                //Fallback (temporary) for H2 if the reported long is a big decimal (tree...)
                                ps.setBigDecimal(ci.index, new BigDecimal(value));
                            }
                            break;
                        case Types.BIT:
                        case Types.CHAR:
                        case Types.BOOLEAN:
                            if (DBG)
                                LOG.info("Boolean " + ci.index + "->" + value);
                            if ("1".equals(value) || "true".equals(value))
                                ps.setBoolean(ci.index, true);
                            else
                                ps.setBoolean(ci.index, false);
                            break;
                        case Types.LONGVARBINARY:
                        case Types.VARBINARY:
                        case Types.BLOB:
                        case Types.BINARY:
                            ZipEntry bin = zip.getEntry(value);
                            if (bin == null) {
                                LOG.error("Failed to lookup binary [" + value + "]!");
                                ps.setNull(ci.index, ci.columnType);
                                break;
                            }
                            try {
                                ps.setBinaryStream(ci.index, zip.getInputStream(bin), (int) bin.getSize());
                            } catch (IOException e) {
                                LOG.error("IOException importing binary [" + value + "]: " + e.getMessage(), e);
                            }
                            break;
                        case Types.CLOB:
                        case Types.LONGVARCHAR:
                        case Types.VARCHAR:
                        case SQL_LONGNVARCHAR:
                        case SQL_NCHAR:
                        case SQL_NCLOB:
                        case SQL_NVARCHAR:
                            if (DBG)
                                LOG.info("String " + ci.index + "->" + value);
                            ps.setString(ci.index, value);
                            break;
                        default:
                            LOG.warn("Unhandled type [" + ci.columnType + "] for column [" + col + "]");
                        }
                    }
                }
                return dataSet;
            }

            /**
             * {@inheritDoc}
             */
            @Override
            public void characters(char[] ch, int start, int length) throws SAXException {
                if (inElement)
                    sbData.append(ch, start, length);
            }

        };
        handler.processingInstruction("fx_mode", "insert");
        parser.parse(zip.getInputStream(ze), handler);
        if (updateColumns.length > 0 && executeUpdatePhase) {
            handler.processingInstruction("fx_mode", "update");
            parser.parse(zip.getInputStream(ze), handler);
        }
    } finally {
        Database.closeObjects(GenericDivisionImporter.class, psInsert, psUpdate);
    }
}

From source file:de.mpg.mpdl.inge.pubman.web.sword.SwordUtil.java

/**
 * Converts a byte[] into a FileVO./*  w  w w.j  a  va 2s. c o  m*/
 * 
 * @param file
 * @param name
 * @param user
 * @return FileVO
 * @throws Exception
 */
private FileVO convertToFileAndAdd(InputStream zipinputstream, String name, AccountUserVO user,
        ZipEntry zipEntry) throws Exception {

    MdsFileVO mdSet = new MdsFileVO();
    FileVO fileVO = new FileVO();

    // ByteArrayInputStream in = new ByteArrayInputStream(file);
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String mimeType = fileNameMap.getContentTypeFor(name);

    // Hack: FileNameMap class does not know tei, bibtex and endnote
    if (name.endsWith(".tei")) {
        mimeType = "application/xml";
    }
    if (name.endsWith(".bib") || name.endsWith(".enl")) {
        mimeType = "text/plain";
    }

    URL fileURL = this.uploadFile(zipinputstream, mimeType, user.getHandle(), zipEntry);

    if (fileURL != null && !fileURL.toString().trim().equals("")) {

        if (this.currentDeposit.getContentDisposition() != null
                && !this.currentDeposit.getContentDisposition().equals("")) {
            name = this.currentDeposit.getContentDisposition();
        }

        fileVO.setStorage(FileVO.Storage.INTERNAL_MANAGED);
        fileVO.setVisibility(FileVO.Visibility.PUBLIC);
        fileVO.setDefaultMetadata(mdSet);
        fileVO.getDefaultMetadata().setTitle(name);
        fileVO.setMimeType(mimeType);
        fileVO.setName(name);

        FormatVO formatVO = new FormatVO();
        formatVO.setType("dcterms:IMT");
        formatVO.setValue(mimeType);
        fileVO.getDefaultMetadata().getFormats().add(formatVO);
        fileVO.setContent(fileURL.toString());
        fileVO.getDefaultMetadata().setSize((int) zipEntry.getSize());
        // This is the provided metadata file which we store as a component
        if (!name.endsWith(".pdf")) {
            String contentCategory = null;
            if (PubFileVOPresentation.getContentCategoryUri("SUPPLEMENTARY_MATERIAL") != null) {
                contentCategory = PubFileVOPresentation.getContentCategoryUri("SUPPLEMENTARY_MATERIAL");
            } else {
                Map<String, String> contentCategoryMap = PubFileVOPresentation.getContentCategoryMap();
                if (contentCategoryMap != null && !contentCategoryMap.entrySet().isEmpty()) {
                    contentCategory = contentCategoryMap.values().iterator().next();
                } else {
                    error("There is no content category available.");
                    Logger.getLogger(PubFileVOPresentation.class)
                            .warn("WARNING: no content-category has been defined in Genres.xml");
                }
            }
            fileVO.setContentCategory(contentCategory);
        } else {
            String contentCategory = null;
            if (PubFileVOPresentation.getContentCategoryUri("PUBLISHER_VERSION") != null) {
                contentCategory = PubFileVOPresentation.getContentCategoryUri("PUBLISHER_VERSION");
            } else {
                Map<String, String> contentCategoryMap = PubFileVOPresentation.getContentCategoryMap();
                if (contentCategoryMap != null && !contentCategoryMap.entrySet().isEmpty()) {
                    contentCategory = contentCategoryMap.values().iterator().next();
                } else {
                    error("There is no content category available.");
                    Logger.getLogger(PubFileVOPresentation.class)
                            .warn("WARNING: no content-category has been defined in Genres.xml");
                }
            }
            fileVO.setContentCategory(contentCategory);
        }

        // if escidoc item: check if it has already components with this filename. If true, use
        // existing file information.
        /*
         * if(this.currentDeposit.getFormatNamespace().equals(this.mdFormatEscidoc)) { for(FileVO
         * existingFile : itemVO.getFiles()) { if(existingFile.getName().replaceAll("/",
         * "_slsh_").equals(name)) { existingFile.setContent(fileURL.toString());
         * existingFile.getDefaultMetadata().setSize(file.length); existing = true;
         * if(existingFile.getVisibility().equals(Visibility.PRIVATE)) {
         * existingFile.setVisibility(Visibility.AUDIENCE); } } }
         * 
         * //If the file is the metadata file, do not add it for escidoc format
         * if(name.equals(depositXmlFileName)) { existing = true; }
         * 
         * 
         * }
         */
    }

    /*
     * if(!existing) { itemVO.getFiles().add(fileVO); }
     */
    return fileVO;
}

From source file:de.mpg.escidoc.pubman.sword.SwordUtil.java

/**
 * Converts a byte[] into a FileVO.//ww  w .j av  a  2s.  c  o  m
 * @param file
 * @param name
 * @param user
 * @return FileVO
 * @throws Exception
 */
private FileVO convertToFileAndAdd(InputStream zipinputstream, String name, AccountUserVO user,
        ZipEntry zipEntry) throws Exception {

    MdsFileVO mdSet = new MdsFileVO();
    FileVO fileVO = new FileVO();

    //ByteArrayInputStream in = new ByteArrayInputStream(file);
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String mimeType = fileNameMap.getContentTypeFor(name);

    //Hack: FileNameMap class does not know tei, bibtex and endnote
    if (name.endsWith(".tei")) {
        mimeType = "application/xml";
    }
    if (name.endsWith(".bib") || name.endsWith(".enl")) {
        mimeType = "text/plain";
    }

    URL fileURL = this.uploadFile(zipinputstream, mimeType, user.getHandle(), zipEntry);

    if (fileURL != null && !fileURL.toString().trim().equals("")) {

        if (this.currentDeposit.getContentDisposition() != null
                && !this.currentDeposit.getContentDisposition().equals("")) {
            name = this.currentDeposit.getContentDisposition();
        }

        fileVO.setStorage(FileVO.Storage.INTERNAL_MANAGED);
        fileVO.setVisibility(FileVO.Visibility.PUBLIC);
        fileVO.setDefaultMetadata(mdSet);
        fileVO.getDefaultMetadata().setTitle(new TextVO(name));
        fileVO.setMimeType(mimeType);
        fileVO.setName(name);

        FormatVO formatVO = new FormatVO();
        formatVO.setType("dcterms:IMT");
        formatVO.setValue(mimeType);
        fileVO.getDefaultMetadata().getFormats().add(formatVO);
        fileVO.setContent(fileURL.toString());
        fileVO.getDefaultMetadata().setSize((int) zipEntry.getSize());
        //This is the provided metadata file which we store as a component
        if (!name.endsWith(".pdf")) {
            String contentCategory = null;
            if (PubFileVOPresentation.getContentCategoryUri("SUPPLEMENTARY_MATERIAL") != null) {
                contentCategory = PubFileVOPresentation.getContentCategoryUri("SUPPLEMENTARY_MATERIAL");
            } else {
                Map<String, String> contentCategoryMap = PubFileVOPresentation.getContentCategoryMap();
                if (contentCategoryMap != null && !contentCategoryMap.entrySet().isEmpty()) {
                    contentCategory = contentCategoryMap.values().iterator().next();
                } else {
                    error("There is no content category available.");
                    Logger.getLogger(PubFileVOPresentation.class)
                            .warn("WARNING: no content-category has been defined in Genres.xml");
                }
            }
            fileVO.setContentCategory(contentCategory);
        } else {
            String contentCategory = null;
            if (PubFileVOPresentation.getContentCategoryUri("PUBLISHER_VERSION") != null) {
                contentCategory = PubFileVOPresentation.getContentCategoryUri("PUBLISHER_VERSION");
            } else {
                Map<String, String> contentCategoryMap = PubFileVOPresentation.getContentCategoryMap();
                if (contentCategoryMap != null && !contentCategoryMap.entrySet().isEmpty()) {
                    contentCategory = contentCategoryMap.values().iterator().next();
                } else {
                    error("There is no content category available.");
                    Logger.getLogger(PubFileVOPresentation.class)
                            .warn("WARNING: no content-category has been defined in Genres.xml");
                }
            }
            fileVO.setContentCategory(contentCategory);
        }

        //if escidoc item: check if it has already components with this filename. If true, use existing file information.
        /*
        if(this.currentDeposit.getFormatNamespace().equals(this.mdFormatEscidoc))
        {
        for(FileVO existingFile : itemVO.getFiles())
        {
            if(existingFile.getName().replaceAll("/", "_slsh_").equals(name))
            {
                existingFile.setContent(fileURL.toString());
                existingFile.getDefaultMetadata().setSize(file.length);
                existing = true;
                if(existingFile.getVisibility().equals(Visibility.PRIVATE))
                {
                    existingFile.setVisibility(Visibility.AUDIENCE);
                }
            }
        }
                
        //If the file is the metadata file, do not add it for escidoc format
        if(name.equals(depositXmlFileName))
        {
            existing = true;
        }
                
                
        }
        */
    }

    /*
    if(!existing)
    {
    itemVO.getFiles().add(fileVO);
    }
    */
    return fileVO;
}

From source file:fr.inria.atlanmod.neo4emf.neo4jresolver.runtimes.internal.Neo4jZippedInstaller.java

@Override
public void performInstall(IProgressMonitor monitor, IPath dirPath) throws IOException {
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }//w w  w .j a  va2 s .c om

    InputStream urlStream = null;
    try {
        URL url = getUrl();
        URLConnection connection = url.openConnection();
        connection.setConnectTimeout(TIMEOUT);
        connection.setReadTimeout(TIMEOUT);

        int length = connection.getContentLength();
        monitor.beginTask(NLS.bind("Reading from {1}", getVersion(), getUrl().toString()),
                length >= 0 ? length : IProgressMonitor.UNKNOWN);

        urlStream = connection.getInputStream();
        ZipInputStream zipStream = new ZipInputStream(urlStream);
        byte[] buffer = new byte[1024 * 8];
        long start = System.currentTimeMillis();
        int total = length;
        int totalRead = 0;
        ZipEntry entry;
        float kBps = -1;
        while ((entry = zipStream.getNextEntry()) != null) {
            if (monitor.isCanceled())
                break;
            String fullFilename = entry.getName();
            IPath fullFilenamePath = new Path(fullFilename);
            int secsRemaining = (int) ((total - totalRead) / 1024 / kBps);
            String textRemaining = secsToText(secsRemaining);
            monitor.subTask(NLS.bind("{0} remaining. Reading {1}",
                    textRemaining.length() > 0 ? textRemaining : "unknown time", StringUtils
                            .abbreviateMiddle(fullFilenamePath.removeFirstSegments(1).toString(), "...", 45)));
            int entrySize = (int) entry.getCompressedSize();
            OutputStream output = null;
            try {
                int len = 0;
                int read = 0;
                String action = null;
                if (jarFiles.contains(fullFilename)) {
                    action = "Copying";
                    String filename = FilenameUtils.getName(fullFilename);
                    output = new FileOutputStream(dirPath.append(filename).toOSString());
                } else {
                    action = "Skipping";
                    output = new NullOutputStream();
                }
                int secs = (int) ((System.currentTimeMillis() - start) / 1000);
                kBps = (float) totalRead / 1024 / secs;

                while ((len = zipStream.read(buffer)) > 0) {
                    if (monitor.isCanceled())
                        break;
                    read += len;
                    monitor.subTask(NLS.bind("{0} remaining. {1} {2} at {3}KB/s ({4}KB / {5}KB)",
                            new Object[] {
                                    String.format("%s",
                                            textRemaining.length() > 0 ? textRemaining : "unknown time"),
                                    action,
                                    StringUtils.abbreviateMiddle(
                                            fullFilenamePath.removeFirstSegments(1).toString(), "...", 45),
                                    String.format("%,.1f", kBps), String.format("%,.1f", (float) read / 1024),
                                    String.format("%,.1f", (float) entry.getSize() / 1024) }));
                    output.write(buffer, 0, len);
                }
                totalRead += entrySize;
                monitor.worked(entrySize);
            } finally {
                IOUtils.closeQuietly(output);
            }
        }
    } finally {
        IOUtils.closeQuietly(urlStream);
        monitor.done();
    }
}

From source file:org.opencms.workplace.tools.database.CmsHtmlImport.java

/**
 * This function reads the zip-file and saved the files and directories in a new 
 * temporary-folder.<p>/*from   ww  w .  ja  va2  s .  co  m*/
 * 
 * @return the temporary-folder where the files from the zip-file are saved
 */
private File unzipStream() {

    ZipInputStream importZip = null;
    File folder = null;
    try {
        // read the zip file
        importZip = new ZipInputStream(new FileInputStream(m_httpDir));
        // create a temporary-folder, where to unzip the zip file
        folder = createTempFolder("import_html");
        ZipEntry entry = null;
        byte[] buffer = null;
        while (true) {
            try {
                // get the next entry
                entry = importZip.getNextEntry();
                if (entry == null) {
                    break;
                }
                String name = entry.getName();
                // make a report for the user
                m_report.print(Messages.get().container(Messages.RPT_HTML_UNZIP_0), I_CmsReport.FORMAT_NOTE);
                m_report.print(org.opencms.report.Messages.get()
                        .container(org.opencms.report.Messages.RPT_ARGUMENT_1, name));
                m_report.print(
                        org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
                // replace the VFS separator with the separator of the file system
                name = name.replace('/', File.separatorChar);
                String path = folder + File.separator + name;
                if (entry.isDirectory()) {
                    // create a directory
                    File importFile = new File(path);
                    importFile.mkdirs();
                } else {
                    // create a file and read the content
                    int size = new Long(entry.getSize()).intValue();
                    if (size == -1) {
                        buffer = CmsFileUtil.readFully(importZip, false);
                    } else {
                        buffer = CmsFileUtil.readFully(importZip, size, false);
                    }
                    // create a new temporary file
                    File importFile = new File(path);
                    File parent = importFile.getParentFile();
                    if (parent != null) {
                        parent.mkdirs();
                    }
                    importFile.createNewFile();
                    // write the content in the file
                    FileOutputStream fileOutput = new FileOutputStream(importFile.getAbsoluteFile());
                    fileOutput.write(buffer);
                    fileOutput.close();
                }
                importZip.closeEntry();
                m_report.println(
                        org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
                        I_CmsReport.FORMAT_OK);
            } catch (Exception ex) {
                String name = (entry != null ? entry.getName() : "");
                if (LOG.isErrorEnabled()) {
                    LOG.error(Messages.get().getBundle().key(Messages.ERR_ZIPFILE_UNZIP_1, name), ex);
                }
                m_report.println(Messages.get().container(Messages.ERR_ZIPFILE_UNZIP_1, name),
                        I_CmsReport.FORMAT_ERROR);
            }
            entry = null;
        }
    } catch (Exception ex) {
        if (LOG.isErrorEnabled()) {
            LOG.error(Messages.get().getBundle().key(Messages.ERR_ZIPFILE_READ_1, m_httpDir), ex);
        }
        m_report.println(Messages.get().container(Messages.ERR_ZIPFILE_READ_1, m_httpDir),
                I_CmsReport.FORMAT_ERROR);
    } finally {
        closeStream(importZip);
    }
    return folder;

}

From source file:com.flexive.core.storage.GenericDivisionImporter.java

/**
 * Import database and filesystem binaries
 *
 * @param con an open and valid connection to store imported data
 * @param zip zip file containing the data
 * @throws Exception on errors// ww  w.  j av a2  s  .c  o m
 */
public void importBinaries(Connection con, ZipFile zip) throws Exception {
    ZipEntry ze = getZipEntry(zip, FILE_BINARIES);
    Statement stmt = con.createStatement();
    try {
        importTable(stmt, zip, ze, "binaries/binary", DatabaseConst.TBL_CONTENT_BINARY);
        ZipEntry curr;
        final String nodeBinDir = FxBinaryUtils.getBinaryDirectory();
        File binDir = new File(
                nodeBinDir + File.separatorChar + String.valueOf(FxContext.get().getDivisionId()));
        if (!binDir.exists())
            //noinspection ResultOfMethodCallIgnored
            binDir.mkdirs();
        if (!binDir.exists() && binDir.isDirectory()) {
            LOG.error("Failed to create binary directory [" + binDir.getAbsolutePath() + "]!");
            return;
        }
        int count = 0;
        for (Enumeration e = zip.entries(); e.hasMoreElements();) {
            curr = (ZipEntry) e.nextElement();
            if (curr.getName().startsWith(FOLDER_FS_BINARY) && !curr.isDirectory()) {
                File out = new File(binDir.getAbsolutePath() + File.separatorChar
                        + curr.getName().substring(FOLDER_FS_BINARY.length() + 1));
                String path = out.getAbsolutePath();
                path = path.replace('\\', '/'); //normalize separator chars
                path = path.replace('/', File.separatorChar);
                path = path.substring(0, out.getAbsolutePath().lastIndexOf(File.separatorChar));
                File fPath = new File(path);
                if (!fPath.exists()) {
                    if (!fPath.mkdirs()) {
                        LOG.error("Failed to create path [" + path + "!]");
                        continue;
                    }
                }
                if (!out.createNewFile()) {
                    LOG.error("Failed to create file [" + out.getAbsolutePath() + "]!");
                    continue;
                }
                if (FxFileUtils.copyStream2File(curr.getSize(), zip.getInputStream(curr), out))
                    count++;
                else
                    LOG.error("Failed to write zip stream to file [" + out.getAbsolutePath() + "]!");
            }
        }
        FxContext.get().runAsSystem();
        try {
            EJBLookup.getNodeConfigurationEngine().put(SystemParameters.NODE_BINARY_PATH, nodeBinDir);
        } finally {
            FxContext.get().stopRunAsSystem();
        }
        LOG.info("Imported [" + count + "] files to filesystem binary storage located at ["
                + binDir.getAbsolutePath() + "]");
    } finally {
        Database.closeObjects(GenericDivisionImporter.class, stmt);
    }
}

From source file:com.pari.pcb.zip.ZIPProcessor.java

public void process(boolean blocking) {
    tmpRoot = new File("TEMP" + File.separator + "ZIP_TMP_" + System.nanoTime());
    tmpRoot.mkdirs();//w ww . j av a 2 s .c  o  m
    long start = System.currentTimeMillis();
    if (!zipFile.exists()) {
        errMsg = "File " + fileName + " does not exist";
        setCompleted();
        return;
    }
    if (!zipFile.canRead()) {
        errMsg = "Unable to read the contents of the file " + fileName;
        setCompleted();
        return;
    }
    try {
        ZipFile inZip = new ZipFile(zipFile);
        numEntries = inZip.size();

        Enumeration<? extends ZipEntry> entryEnumeration = inZip.entries();
        if (numEntries == 0) {
            errMsg = "Empty ZIP file.";
            setCompleted();
            return;
        }
        while (entryEnumeration.hasMoreElements()) {
            ZipEntry entry = entryEnumeration.nextElement();
            currentEntryName = entry.getName();
            if (entry.isDirectory()) {
                if (numEntries > 1) {
                    numEntries--;
                }
                continue;
            }
            currentEntryIdx++;
            if (currentEntryIdx % 100 == 0) {
                System.gc();
                NCCMObserver.getInstance().logMemoryUsage(
                        "After handling " + currentEntryIdx + " files in Zip Processor, Memory Usage");
            }
            InputStream ipStream = inZip.getInputStream(entry);
            String fileName = entry.getName();
            System.err.println("Processing file: " + fileName);

            String line = null;
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(ipStream));
                line = br.readLine();
                CharsetDetector detector = new CharsetDetector();
                if (entry.getSize() == 0) {
                    logger.error("No Contents found in the file");
                    continue;
                } else {
                    detector.setText(line.getBytes());
                    charset = Charset.forName(detector.detect().getName());
                }
            } catch (Exception ex) {
                logger.error("Error while getting the charset encoding of the file");
            } finally {
                try {
                    ipStream.close();
                } catch (Exception e) {
                    logger.error("Exception while closing the stream");
                }
            }
            ipStream = inZip.getInputStream(entry);

            processZipEntry(ipStream, fileName, entry, inZip);
            if (progressIf != null) {
                progressIf.updateProgress(currentEntryName, currentEntryIdx, numEntries);
            }
            if (fileResultMap.get(fileName) == null) {
                ZIPFileResult fr = new ZIPFileResult(fileName);
                fr.fileType = ZIPFileType.UNKNOWN;
                fr.msg = "Unable to process the file.";
                fileResultMap.put(fileName, fr);
            }
            try {
                ipStream.close();
            } catch (Exception ex) {
                logger.warn("Unable to close the inputstream for entry " + entry.getName(), ex);
                ex.printStackTrace();
            }
            if (isCancelled) {
                break;
            }
            if (!blocking) {
                Thread.sleep(100 /* msec */);
            }
            if (isCancelled) {
                break;
            }
        }
        if (!isCancelled) {
            processCatOsVersionFiles(inZip);
            processFileResult();
        }
        inZip.close();
    } catch (ZipException e) {
        errMsg = "File is not a valid Zip file";
        logger.error("Exception while processing ZipFile: " + fileName, e);
    } catch (IOException e) {
        errMsg = "Unable to parse Zip file ";
        logger.error("IOException while processing ZipFile: " + fileName, e);
    } catch (InterruptedException e) {
        errMsg = "Zip processing Interrupted internally.";
        logger.error("Interrupted while processing ZipFile: " + fileName, e);
    } catch (Exception e) {
        errMsg = "Exception while processing zip file.";
        logger.error("Exception while processing ZipFile: " + fileName, e);
    }

    logger.debug("Done with : " + numEntries + " in " + (System.currentTimeMillis() - start) + " msecs");
    String[] devNames = getDeviceNames();
    if (devNames == null || devNames.length == 0) {
        addWarning("No valid devices found in the specified Zip file.");
    }
    setCompleted();
    return;
}