1
0

Use stream instead of zip file

This commit is contained in:
Hubert Van De Walle 2020-09-11 17:58:20 +02:00
parent 0d47b51f3f
commit 8a08520267
3 changed files with 19 additions and 28 deletions

View File

@ -3,22 +3,19 @@ package starter
import starter.templates.Template import starter.templates.Template
import starter.utils.ZipOutput import starter.utils.ZipOutput
import starter.utils.sanitizeFilename import starter.utils.sanitizeFilename
import java.io.ByteArrayOutputStream
class ProjectZip(private val templates: List<Template>) { class ProjectZip(private val templates: List<Template>) {
fun createZip(project: Project): String { fun createZip(project: Project): ByteArrayOutputStream {
val name: String
val projectName = sanitizeFilename(project.name) val projectName = sanitizeFilename(project.name)
ZipOutput(projectName).use { zip -> val zipOutput = ZipOutput()
name = zip.name zipOutput.use { zip ->
templates.filter { it.enabled(project) }.forEach { template -> templates.filter { it.enabled(project) }.forEach { template ->
zip.write(projectName + "/" + template.path(project), template.render(project)) zip.write(projectName + "/" + template.path(project), template.render(project))
} }
} }
return zipOutput.outputStream
return name
} }
} }

View File

@ -2,7 +2,7 @@ package starter
import io.javalin.Javalin import io.javalin.Javalin
import starter.utils.sanitizeFilename import starter.utils.sanitizeFilename
import java.io.File import java.io.ByteArrayInputStream
class Server( class Server(
private val views: Views, private val views: Views,
@ -47,8 +47,9 @@ class Server(
ctx.contentType("application/zip") ctx.contentType("application/zip")
ctx.header("Content-Disposition", "attachment; filename=\"${sanitizeFilename(projectName)}.zip\"") ctx.header("Content-Disposition", "attachment; filename=\"${sanitizeFilename(projectName)}.zip\"")
val zipFile = projectZip.createZip(project)
ctx.result(File(zipFile).readBytes()) val outputStream = projectZip.createZip(project)
ctx.result(ByteArrayInputStream(outputStream.toByteArray()))
} }
} }
} }

View File

@ -2,28 +2,21 @@ package starter.utils
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
import java.io.File import java.io.ByteArrayOutputStream
import java.nio.file.Paths
class ZipOutput(name: String) : AutoCloseable { class ZipOutput : AutoCloseable {
private val baseDir = File(".").canonicalFile val outputStream = ByteArrayOutputStream()
private val zipPath = Paths.get(baseDir.path, "$name.zip") private val zipOutputStream = ZipArchiveOutputStream(outputStream)
private val zipFile = zipPath.toAbsolutePath().normalize().toFile().apply {
createNewFile()
}
private val outputStream = ZipArchiveOutputStream(zipFile.outputStream())
val name: String
get() = zipFile.name
fun write(path: String, content: String) { fun write(path: String, content: String) {
val entry = ZipArchiveEntry(path) val entry = ZipArchiveEntry(path)
outputStream.putArchiveEntry(entry) zipOutputStream.putArchiveEntry(entry)
outputStream.write(content.toByteArray()) zipOutputStream.write(content.toByteArray())
outputStream.closeArchiveEntry() zipOutputStream.closeArchiveEntry()
} }
override fun close() { override fun close() {
outputStream.finish() zipOutputStream.finish()
outputStream.close() zipOutputStream.close()
} }
} }