Search in sources :

Example 1 with DuplicateZipEntryException

use of org.sagebionetworks.bridge.util.DuplicateZipEntryException in project BridgeServer2 by Sage-Bionetworks.

the class UploadArchiveService method unzip.

/**
 * <p>
 * Unzips the given stream. For each individual zip entry, this method will call entryNameToOutputStream, passing
 * in the zip entry file name and expecting an OutputStream in which to write the unzipped bytes. It will then call
 * outputStreamFinalizer, allowing the caller to finalize the stream, for example, closing the stream.
 * </p>
 * <p>
 * The caller is responsible for closing any and all streams involved.
 * </p>
 *
 * @param source
 *         input stream of zipped data to unzip
 * @param entryNameToOutpuStream
 *         arg is the zip entry file name, return value is the OutputStream in which to write the unzipped bytes
 * @param outputStreamFinalizer
 *         args are the zip entry file name and the corresponding OutputStream returned by entryNameToOutputStream;
 *         this is where you finalize the stream, eg closing the stream
 */
public void unzip(InputStream source, Function<String, OutputStream> entryNameToOutpuStream, BiConsumer<String, OutputStream> outputStreamFinalizer) {
    // Validate input
    checkNotNull(source);
    checkNotNull(entryNameToOutpuStream);
    checkNotNull(outputStreamFinalizer);
    // Unzip
    Set<String> zipEntryNameSet = new HashSet<>();
    try (ZipInputStream zis = new ZipInputStream(source)) {
        ZipEntry zipEntry = zis.getNextEntry();
        while (zipEntry != null) {
            if (zipEntryNameSet.size() >= maxNumZipEntries) {
                throw new ZipOverflowException("The number of zip entries is over the max allowed");
            }
            final String entryName = zipEntry.getName();
            if (zipEntryNameSet.contains(entryName)) {
                throw new DuplicateZipEntryException("Duplicate filename " + entryName);
            }
            final long entrySize = zipEntry.getSize();
            if (entrySize > maxZipEntrySize) {
                throw new ZipOverflowException("Zip entry size is over the max allowed size. The entry " + entryName + " has size " + entrySize + ". The max allowed size is" + maxZipEntrySize + ".");
            }
            zipEntryNameSet.add(entryName);
            OutputStream outputStream = entryNameToOutpuStream.apply(entryName);
            toByteArray(entryName, zis, outputStream);
            outputStreamFinalizer.accept(entryName, outputStream);
            zipEntry = zis.getNextEntry();
        }
    } catch (DuplicateZipEntryException | ZipOverflowException ex) {
        throw new BadRequestException(ex);
    } catch (IOException ex) {
        throw new BridgeServiceException(ex);
    }
}
Also used : ZipOverflowException(org.sagebionetworks.bridge.util.ZipOverflowException) ZipEntry(java.util.zip.ZipEntry) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) IOException(java.io.IOException) ZipInputStream(java.util.zip.ZipInputStream) DuplicateZipEntryException(org.sagebionetworks.bridge.util.DuplicateZipEntryException) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) HashSet(java.util.HashSet)

Aggregations

IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 HashSet (java.util.HashSet)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)1 BadRequestException (org.sagebionetworks.bridge.exceptions.BadRequestException)1 BridgeServiceException (org.sagebionetworks.bridge.exceptions.BridgeServiceException)1 DuplicateZipEntryException (org.sagebionetworks.bridge.util.DuplicateZipEntryException)1 ZipOverflowException (org.sagebionetworks.bridge.util.ZipOverflowException)1