use of org.sagebionetworks.bridge.util.ZipOverflowException 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);
}
}
Aggregations