本文整理汇总了Scala中java.util.zip.ZipOutputStream类的典型用法代码示例。如果您正苦于以下问题:Scala ZipOutputStream类的具体用法?Scala ZipOutputStream怎么用?Scala ZipOutputStream使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ZipOutputStream类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: S3Operations
//设置package包名称以及导入依赖的类
package aws.s3
import java.io._
import java.util.zip.{ZipEntry, ZipOutputStream}
import aws.Interpreter.ErrorsOr
import cats.data.NonEmptyList
import cats.instances.either._
import cats.instances.list._
import cats.syntax.traverse._
import com.ovoenergy.comms.model.CommManifest
import logic.TemplateOp._
object S3Operations {
def downloadTemplateFiles(s3ClientWrapper: AmazonS3ClientWrapper,
commManifest: CommManifest,
bucketName: String): Either[String, TemplateFiles] = {
val prefix = buildPrefix(commManifest)
val fileKeys = s3ClientWrapper.listFiles(bucketName, prefix).right
fileKeys.flatMap { keys =>
val result = keys.toList.traverseU { absKey =>
val file = s3ClientWrapper.downloadFile(bucketName, absKey).right
val pair: Either[String, (String, Array[Byte])] = file map { f =>
val strippedKey = absKey.stripPrefix(prefix).dropWhile(_ == '/')
(strippedKey, f)
}
pair
}
result.right.map(_.toMap)
}
}
private def buildPrefix(commManifest: CommManifest) = {
s"${commManifest.commType.toString.toLowerCase}/${commManifest.name}/${commManifest.version}"
}
type ByteArray = Array[Byte]
def compressFiles(templateFiles: TemplateFiles): ErrorsOr[ByteArray] = {
val baos = new ByteArrayOutputStream()
try {
val zip = new ZipOutputStream(baos)
try {
templateFiles.foreach {
case (fileName, file) =>
zip.putNextEntry(new ZipEntry(fileName))
zip.write(file)
zip.closeEntry()
}
} finally {
zip.close()
}
val result = baos.toByteArray
Right(result)
} catch {
case e: Throwable => Left(NonEmptyList.of(s"Failed to compress template files: ${e.getMessage}"))
}
}
}
示例2: ZipFileIteratorSpec
//设置package包名称以及导入依赖的类
package com.s3dropbox.lambda
import java.io.{File, FileInputStream, FileOutputStream}
import java.util.zip.{ZipEntry, ZipOutputStream}
import com.s3dropbox.lambda.ZipFileIterator.ZipFileEntry
class ZipFileIteratorSpec extends UnitSpec {
describe("When given a valid zip file with more than one compressed file") {
it("should iterate over the compressed files and provide their file name and contents") {
val entries: Array[(String, Array[Byte])] =
(1 to 5)
.map((fileNum: Int) => {
(s"file_$fileNum", s"This is the contents of file $fileNum".getBytes)
})
.toArray
runZipFileTest(entries)
}
}
describe("when given a zip file containing a single large 1MB file") {
it("should decompress the single large file") {
val entries: Array[(String, Array[Byte])] = Array(
("some_file", (1 to 1024 * 1024).map(_.toByte).toArray)
)
runZipFileTest(entries)
}
}
def runZipFileTest(entries: Array[(String, Array[Byte])]): Unit = {
var index: Int = 0
val zipFileIter: ZipFileIterator = new ZipFileIterator(new FileInputStream(zipFile(entries)))
zipFileIter.foreach((zipFileEntry: ZipFileEntry) => {
assert(zipFileEntry.filename == entries(index)._1)
assert(zipFileEntry.data sameElements entries(index)._2)
index += 1
})
zipFileIter.close()
}
def zipFile(entries: Array[(String, Array[Byte])]): File = {
val temp: File = File.createTempFile("temp-zip-file", ".zip")
temp.deleteOnExit()
val zos: ZipOutputStream = new ZipOutputStream(new FileOutputStream(temp))
entries.foreach((entry: (String, Array[Byte])) => {
val filename: String = entry._1
val contents: Array[Byte] = entry._2
val zentry: ZipEntry = new ZipEntry(filename)
zos.putNextEntry(zentry)
zos.write(contents)
zos.closeEntry()
})
zos.close()
temp
}
}
示例3: ZipImplicits
//设置package包名称以及导入依赖的类
package anchorman.docx
import anchorman.media.MediaFile
import java.io.OutputStreamWriter
import java.util.zip.{ZipEntry, ZipOutputStream}
import scala.xml.{MinimizeMode, NodeSeq, XML}
object ZipImplicits {
implicit class ZipOutputStreamOps(out: ZipOutputStream) {
def writeXmlFile(path: String, nodes: NodeSeq): Unit = {
out.putNextEntry(new ZipEntry(path))
val writer = new OutputStreamWriter(out)
try XML.write(writer, nodes.head, "UTF-8", true, null, MinimizeMode.Always)
finally writer.flush()
}
def writeMediaFile(path: String, file: MediaFile): Unit = {
out.putNextEntry(new ZipEntry(path))
try out.write(file.content)
finally out.flush()
}
}
}
示例4: Zip
//设置package包名称以及导入依赖的类
package flaky.history
import java.io.{File, FileInputStream, FileOutputStream}
import java.util.zip.{ZipEntry, ZipOutputStream}
object Zip {
def compressFolder(zipFilePath: File, folder: File): Unit = {
import java.io.File
def recursiveListFiles(f: File): Array[File] = {
val these = f.listFiles
these.filter(_.isFile) ++ these.filter(_.isDirectory).flatMap(recursiveListFiles)
}
val toList = recursiveListFiles(folder).toList
compress(zipFilePath, folder, toList)
}
def compress(zipFilePath: File, root: File, files: List[File]): Unit = {
val zip = new ZipOutputStream(new FileOutputStream(zipFilePath))
val rootPath = root.getAbsolutePath
try {
for (file <- files) {
zip.putNextEntry(new ZipEntry(file.getAbsolutePath.substring(rootPath.length)))
val in = new FileInputStream(file)
try {
Iterator
.continually(in.read())
.takeWhile(_ > -1)
.foreach(zip.write)
} finally {
in.close()
}
zip.closeEntry()
}
}
finally {
zip.close()
}
}
}