Example usage for java.util.zip ZipException ZipException

List of usage examples for java.util.zip ZipException ZipException

Introduction

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

Prototype


public ZipException(String s) 

Source Link

Document

Constructs a ZipException with the specified detail message.

Usage

From source file:org.bonitasoft.web.designer.controller.utils.Unzipper.java

public Path unzipInTempDir(InputStream is, String tempDirPrefix) throws IOException {
    Path tempDirectory = Files.createTempDirectory(temporaryZipPath, tempDirPrefix);
    Path zipFile = writeInDir(is, tempDirectory);
    try {//from  w w w  .j av a2s .c om
        ZipUtil.unpack(zipFile.toFile(), tempDirectory.toFile());
    } catch (org.zeroturnaround.zip.ZipException e) {
        throw new ZipException(e.getMessage());
    } finally {
        FileUtils.deleteQuietly(zipFile.toFile());
    }
    return tempDirectory;
}

From source file:org.codice.ddf.configuration.migration.MigrationZipFile.java

private Cipher initCipher() throws ZipException {
    try {//  w  w  w  .jav  a 2 s . co  m
        Cipher iCipher = Cipher.getInstance(MigrationZipConstants.CIPHER_ALGORITHM);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(MigrationZipConstants.CIPHER_IV);
        SecretKey key = initKey(keyPath);
        iCipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
        return iCipher;
    } catch (InvalidKeyException | NoSuchPaddingException | NoSuchAlgorithmException
            | InvalidAlgorithmParameterException e) {
        String message = String.format(MigrationZipConstants.KEY_INVALID_ERROR, keyPath, e);
        LOGGER.error(message);
        throw new ZipException(message);
    }
}

From source file:org.codice.ddf.configuration.migration.MigrationZipFile.java

@SuppressWarnings({
        "squid:S2093", /* try-with-resource will throw IOException with InputStream and we do not care to get that exception */
        "squid:S2095" /* stream is closed in the finally clause */
})
private SecretKey initKey(Path keyPath) throws ZipException {
    BufferedInputStream bif = null;
    try {/*from   ww w  .  j av a2 s  .c  om*/
        bif = new BufferedInputStream(new FileInputStream(keyPath.toFile()));
        String keyData = IOUtils.toString(bif, StandardCharsets.UTF_8);
        byte[] keyBytes = decodeHex(keyData.toCharArray());
        return new SecretKeySpec(keyBytes, MigrationZipConstants.KEY_ALGORITHM);
    } catch (IOException e) {
        String message = String.format(MigrationZipConstants.FILE_IO_ERROR, keyPath, e);
        LOGGER.warn(message);
        throw new ZipException(message);
    } catch (DecoderException e) {
        String message = String.format(MigrationZipConstants.KEY_DECODE_ERROR, keyPath, e);
        LOGGER.warn(message);
        throw new ZipException(message);
    } finally {
        IOUtils.closeQuietly(bif);
    }
}

From source file:org.codice.ddf.configuration.migration.MigrationZipFile.java

private boolean validateChecksum(Path zipPath, Path checksumPath) throws ZipException {
    String actualHash = getChecksumFor(zipPath);
    String storedHash = null;/*from  w  ww . j a v a 2  s . com*/
    try {
        storedHash = readFileToString(checksumPath.toFile(), Charsets.UTF_8);
    } catch (IOException e) {
        String message = String.format(MigrationZipConstants.FILE_IO_ERROR, checksumPath.toString(), e);
        LOGGER.warn(message);
        throw new ZipException(message);
    }
    return actualHash.equals(storedHash);
}

From source file:org.codice.ddf.configuration.migration.MigrationZipFile.java

private static String getChecksumFor(Path path) throws ZipException {
    try (FileInputStream fis = new FileInputStream(path.toFile())) {
        MessageDigest digest = MessageDigest.getInstance(MigrationZipConstants.CHECKSUM_DIGEST_ALGORITHM);
        byte[] fileBytes = IOUtils.toByteArray(fis);
        digest.update(fileBytes);/*  www  . ja  va2s  . co  m*/
        return new String(encodeHex(digest.digest()));
    } catch (FileNotFoundException e) {
        String message = String.format(MigrationZipConstants.FILE_NOT_EXIST, path.toString(), e);
        LOGGER.warn(message);
        throw new ZipException(message);
    } catch (IOException e) {
        String message = String.format(MigrationZipConstants.FILE_IO_ERROR, path.toString(), e);
        LOGGER.warn(message);
        throw new ZipException(message);
    } catch (NoSuchAlgorithmException e) {
        String message = String.format(MigrationZipConstants.CHECKSUM_INVALID_ALGORITHM_ERROR,
                MigrationZipConstants.CHECKSUM_DIGEST_ALGORITHM, e);
        LOGGER.warn(message);
        throw new ZipException(message);
    }
}

From source file:org.commoncrawl.util.StreamingArcFileReader.java

private int readInflatedBytes(byte[] b, int off, int len) throws IOException {
    if (b == null) {
        throw new NullPointerException();
    } else if (off < 0 || len < 0 || len > b.length - off) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return 0;
    }//w  w  w . j av  a  2s .c o  m
    try {
        //try to output some bytes from the inflater 
        int n;
        while ((n = _inflater.inflate(b, off, len)) == 0) {
            if (_inflater.finished() || _inflater.needsDictionary()) {
                // these are EOS conditions 

                //first reclaim any remaining data from the inflater ... 
                if (_inflater.getRemaining() != 0) {
                    if (_activeInputBuffer == null) {
                        throw new RuntimeException("Bad State");
                    } else {
                        // increment bytes available ... 
                        synchronized (this) {
                            _bytesAvailable += _inflater.getRemaining();
                            _streamPos -= _inflater.getRemaining();
                        }
                        // and reposition cursor ...
                        _activeInputBuffer.position(_activeInputBuffer.position() - _inflater.getRemaining());
                    }
                }
                // b
                return -1;
            }
            // we produced no output .. check to see if have more input to add 
            if (_inflater.needsInput()) {
                if (_activeInputBuffer == null || _activeInputBuffer.remaining() == 0) {

                    _activeInputBuffer = null;

                    if (_consumerQueue.size() != 0) {
                        BufferItem nextItem = null;
                        try {
                            nextItem = _consumerQueue.take();
                        } catch (InterruptedException e) {
                            LOG.error(StringUtils.stringifyException(e));
                        }
                        if (nextItem._buffer == null) {
                            throw new EOFException();
                        } else {
                            _activeInputBuffer = nextItem._buffer;
                        }
                    }
                }
                if (_activeInputBuffer == null) {
                    return 0;
                } else {
                    // feed the buffer to the inflater ...
                    _inflater.setInput(_activeInputBuffer.array(), _activeInputBuffer.position(),
                            _activeInputBuffer.remaining());
                    // decrement bytes available ... 
                    synchronized (this) {
                        _bytesAvailable -= _activeInputBuffer.remaining();
                        _streamPos += _activeInputBuffer.remaining();
                    }
                    // and advance its position so 
                    _activeInputBuffer.position(_activeInputBuffer.position() + _activeInputBuffer.remaining());
                }
            }
        }

        return n;
    } catch (DataFormatException e) {
        String s = e.getMessage();
        throw new ZipException(s != null ? s : "Invalid ZLIB data format");
    }
}

From source file:org.jahia.utils.zip.legacy.ZipInputStream.java

/**
 * Reads from the current ZIP entry into an array of bytes. Blocks until
 * some input is available.// ww w  .  j a  va2 s.com
 * @param b the buffer into which the data is read
 * @param off the start offset of the data
 * @param len the maximum number of bytes read
 * @return the actual number of bytes read, or -1 if the end of the
 *         entry is reached
 * @exception java.util.zip.ZipException if a ZIP file error has occurred
 * @exception IOException if an I/O error has occurred
 */
public int read(byte[] b, int off, int len) throws IOException {
    ensureOpen();
    if (off < 0 || len < 0 || off > b.length - len) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return 0;
    }

    if (entry == null) {
        return -1;
    }
    switch (entry.method) {
    case DEFLATED:
        len = super.read(b, off, len);
        if (len == -1) {
            readEnd(entry);
            entryEOF = true;
            entry = null;
        } else {
            crc.update(b, off, len);
        }
        return len;
    case STORED:
        if (remaining <= 0) {
            entryEOF = true;
            entry = null;
            return -1;
        }
        if (len > remaining) {
            len = (int) remaining;
        }
        len = in.read(b, off, len);
        if (len == -1) {
            throw new ZipException("unexpected EOF");
        }
        crc.update(b, off, len);
        remaining -= len;
        return len;
    default:
        throw new InternalError("invalid compression method");
    }
}

From source file:org.jahia.utils.zip.legacy.ZipInputStream.java

private ZipEntry readLOC() throws IOException {
    try {//  www . j  a  va2 s .  co  m
        readFully(tmpbuf, 0, LOCHDR);
    } catch (EOFException e) {
        return null;
    }
    if (get32(tmpbuf, 0) != LOCSIG) {
        return null;
    }
    // get the entry name and create the ZipEntry first
    int len = get16(tmpbuf, LOCNAM);
    if (len == 0) {
        throw new ZipException("missing entry name");
    }
    int blen = b.length;
    if (len > blen) {
        do
            blen = blen * 2;
        while (len > blen);
        b = new byte[blen];
    }
    readFully(b, 0, len);
    ZipEntry e = createZipEntry(getUTF8String(b, 0, len));
    // now get the remaining fields for the entry
    e.version = get16(tmpbuf, LOCVER);
    e.flag = get16(tmpbuf, LOCFLG);
    if ((e.flag & 1) == 1) {
        throw new ZipException("encrypted ZIP entry not supported");
    }
    e.method = get16(tmpbuf, LOCHOW);
    e.time = get32(tmpbuf, LOCTIM);
    if ((e.flag & 8) == 8) {
        /* EXT descriptor present */
        if (e.method != DEFLATED) {
            throw new ZipException("only DEFLATED entries can have EXT descriptor");
        }
    } else {
        e.crc = get32(tmpbuf, LOCCRC);
        e.csize = get32(tmpbuf, LOCSIZ);
        e.size = get32(tmpbuf, LOCLEN);
    }
    len = get16(tmpbuf, LOCEXT);
    if (len > 0) {
        byte[] bb = new byte[len];
        readFully(bb, 0, len);
        e.extra = bb;
    }
    return e;
}

From source file:org.jahia.utils.zip.legacy.ZipInputStream.java

private void readEnd(ZipEntry e) throws IOException {
    int n = inf.getRemaining();
    if (n > 0) {
        ((PushbackInputStream) in).unread(buf, len - n, n);
    }//  w  w w . ja v a  2s  .  c  o m
    if ((e.flag & 8) == 8) {
        /* EXT descriptor present */
        readFully(tmpbuf, 0, EXTHDR);
        long sig = get32(tmpbuf, 0);
        if (sig != EXTSIG) { // no EXTSIG present
            e.crc = sig;
            e.csize = get32(tmpbuf, EXTSIZ - EXTCRC);
            e.size = get32(tmpbuf, EXTLEN - EXTCRC);
            ((PushbackInputStream) in).unread(tmpbuf, EXTHDR - EXTCRC - 1, EXTCRC);
        } else {
            e.crc = get32(tmpbuf, EXTCRC);
            e.csize = get32(tmpbuf, EXTSIZ);
            e.size = get32(tmpbuf, EXTLEN);
        }
    }
    if (e.size != inf.getTotalOut()) {
        throw new ZipException(
                "invalid entry size (expected " + e.size + " but got " + inf.getTotalOut() + " bytes)");
    }
    if (e.csize != inf.getTotalIn()) {
        throw new ZipException("invalid entry compressed size (expected " + e.csize + " but got "
                + inf.getTotalIn() + " bytes)");
    }
    if (e.crc != crc.getValue()) {
        throw new ZipException("invalid entry CRC (expected 0x" + Long.toHexString(e.crc) + " but got 0x"
                + Long.toHexString(crc.getValue()) + ")");
    }
}

From source file:org.opencms.importexport.A_CmsImport.java

/**
 * Returns a byte array containing the content of the file.<p>
 *
 * @param filename the name of the file to read
 * @return a byte array containing the content of the file
 *///  w w  w . j  ava  2s  . co  m
protected byte[] getFileBytes(String filename) {

    try {
        // is this a zip-file?
        if (m_importZip != null) {
            // yes
            ZipEntry entry = m_importZip.getEntry(filename);

            // path to file might be relative, too
            if ((entry == null) && filename.startsWith("/")) {
                entry = m_importZip.getEntry(filename.substring(1));
            }
            if (entry == null) {
                throw new ZipException(Messages.get().getBundle()
                        .key(Messages.LOG_IMPORTEXPORT_FILE_NOT_FOUND_IN_ZIP_1, filename));
            }

            InputStream stream = m_importZip.getInputStream(entry);
            int size = new Long(entry.getSize()).intValue();
            return CmsFileUtil.readFully(stream, size);
        } else {
            // no - use directory
            File file = new File(m_importResource, filename);
            return CmsFileUtil.readFile(file);
        }
    } catch (FileNotFoundException fnfe) {
        if (LOG.isErrorEnabled()) {
            LOG.error(Messages.get().getBundle().key(Messages.ERR_IMPORTEXPORT_FILE_NOT_FOUND_1, filename),
                    fnfe);
        }
        m_report.println(fnfe);
    } catch (IOException ioe) {
        if (LOG.isErrorEnabled()) {
            LOG.error(Messages.get().getBundle().key(Messages.ERR_IMPORTEXPORT_ERROR_READING_FILE_1, filename),
                    ioe);
        }
        m_report.println(ioe);
    }
    // this will only be returned in case there was an exception
    return "".getBytes();
}