Skip to content

Instantly share code, notes, and snippets.

@kirked
Last active January 29, 2023 23:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirked/412b5156f94419e71ce4a84ec1d54761 to your computer and use it in GitHub Desktop.
Save kirked/412b5156f94419e71ce4a84ec1d54761 to your computer and use it in GitHub Desktop.
Create a zip file on-the-fly using Play framework (nonblocking, iteratee, stream, S3, future, low memory use)
/*------------------------------------------------------------------------------
* MIT License
*
* Copyright (c) 2016 Doug Kirk
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*----------------------------------------------------------------------------*/
import com.typesafe.scalalogging.slf4j.StrictLogging
import java.io.{ByteArrayOutputStream, InputStream, IOException}
import java.util.zip.{ZipEntry, ZipOutputStream}
import play.api.libs.iteratee.{Enumeratee, Enumerator}
import scala.concurrent.{Future, ExecutionContext}
/**
* Play iteratee-based reactive zip-file generation.
*/
object ZipEnumerator extends StrictLogging {
/**
* A source to zip.
*
* @param filepath The zip-file path at which to store the data.
* @param stream The data stream provider.
*/
case class Source(filepath: String, stream: () => Future[Option[InputStream]])
/**
* Given sources, returns an Enumerator that feeds a zip-file of the source contents.
*/
def apply(sources: Iterable[Source])(implicit ec: ExecutionContext): Enumerator[Array[Byte]] = {
val resolveSources: Enumerator[ResolvedSource] = Enumerator.unfoldM(sources) { sources =>
sources.headOption match {
case None => Future(None)
case Some(Source(filepath, futureStream)) =>
futureStream().map { _.map(stream => (sources.tail, ResolvedSource(filepath, stream)) ) }
}
}
val buffer = new ZipBuffer(8192)
val writeCentralDirectory = Enumerator.generateM(Future {
if (buffer.isClosed) None
else {
buffer.close
Some(buffer.bytes)
}
})
resolveSources &> zipeach(buffer) andThen writeCentralDirectory
}
private def zipeach(buffer: ZipBuffer)(implicit ec: ExecutionContext): Enumeratee[ResolvedSource, Array[Byte]] = {
Enumeratee.mapConcat[ResolvedSource] { source =>
buffer.zipStream.putNextEntry(new ZipEntry(source.filepath))
var done = false
def entryDone: Unit = {
done = true
buffer.zipStream.closeEntry
source.stream.close
}
def restOfStream: Stream[Array[Byte]] = {
if (done) Stream.empty
else {
while (!done && !buffer.full) {
try {
val byte = source.stream.read
if (byte == -1) entryDone
else buffer.zipStream.write(byte)
}
catch {
case e: IOException =>
logger.error(s"reading/zipping stream [${source.filepath}]", e)
entryDone
}
}
buffer.bytes #:: restOfStream
}
}
restOfStream
}
}
private case class ResolvedSource(filepath: String, stream: InputStream)
private class ZipBuffer(capacity: Int) {
private val buf = new ByteArrayOutputStream(capacity)
private var closed = false
val zipStream = new ZipOutputStream(buf)
def close(): Unit = {
if (!closed) {
closed = true
reset
zipStream.close // writes central directory
}
}
def isClosed = closed
def reset: Unit = buf.reset
def full: Boolean = buf.size >= capacity
def bytes: Array[Byte] = {
val result = buf.toByteArray
reset
result
}
}
}
@kirked
Copy link
Author

kirked commented May 25, 2016

Sample usage:

  val s3 = ...
  val sources = items.map(item => ZipEnumerator.Source(item.filename, { () => s3.getInputStream(item.storagePath) }))
  Ok.chunked(ZipEnumerator(sources))(play.api.http.Writeable.wBytes).withHeaders(
              CONTENT_TYPE -> "application/zip",
              CONTENT_DISPOSITION -> s"attachment; filename=MyBasket.zip; filename*=UTF-8''My%20Basket.zip"
            )

@ObjectiveTruth
Copy link

This just saved my day thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment