本文整理汇总了Scala中java.util.zip.ZipEntry类的典型用法代码示例。如果您正苦于以下问题:Scala ZipEntry类的具体用法?Scala ZipEntry怎么用?Scala ZipEntry使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ZipEntry类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ZipUtils
//设置package包名称以及导入依赖的类
package core
import java.io.Closeable
import java.nio.charset.StandardCharsets._
import java.util.zip.{ZipEntry, ZipInputStream}
import scala.collection.JavaConversions._
import scala.collection.mutable.ArrayBuffer
object ZipUtils {
case class UnzippedFileContent(filename: String, content: String)
def usingZip[R <: Closeable, T](unzippedStream: R)(f: (R) => T) = {
try {
f(unzippedStream)
} finally {
unzippedStream.close()
}
}
def unzipAllFilesInStream(unzippedStream: ZipInputStream): Stream[UnzippedFileContent] = {
unzipAllFilesInStream(unzippedStream, Option(unzippedStream.getNextEntry))
}
def unzipAllFilesInStream(unzippedStream: ZipInputStream, ze: Option[ZipEntry]): Stream[UnzippedFileContent] = {
ze match {
case None => Stream.empty
case Some(ze) =>
val name: String = ze.getName
val entry: String = ZipUtils.getZipEntry(unzippedStream)
val maybeEntry1: Option[ZipEntry] = Option(unzippedStream.getNextEntry)
UnzippedFileContent(name, entry) #::
unzipAllFilesInStream(unzippedStream, maybeEntry1)
}
}
def getZipEntry(zis: ZipInputStream): String = {
val buffer = new Array[Byte](4096)
val stringBuffer = new ArrayBuffer[Byte]()
var len: Int = zis.read(buffer)
while (len > 0) {
stringBuffer ++= buffer.take(len)
len = zis.read(buffer)
}
val content: String = new String(stringBuffer.toArray, UTF_8)
(content)
}
}
示例2: handleAndroidXMLFiles
//设置package包名称以及导入依赖的类
package org.argus.amandroid.core.parser
import java.io.File
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import java.io.FileInputStream
def handleAndroidXMLFiles(apk: File, fileNameFilter: Set[String],
handler: AndroidXMLHandler) = {
try {
var archive: ZipInputStream = null
try {
archive = new ZipInputStream(new FileInputStream(apk))
var entry: ZipEntry = null
entry = archive.getNextEntry()
while (entry != null) {
val entryName = entry.getName()
handler.handleXMLFile(entryName, fileNameFilter, archive)
entry = archive.getNextEntry()
}
}
finally {
if (archive != null)
archive.close()
}
}
catch {
case e: Exception =>
e.printStackTrace()
if (e.isInstanceOf[RuntimeException])
throw e.asInstanceOf[RuntimeException]
else
throw new RuntimeException(e)
}
}
}
示例3: ZipUtil
//设置package包名称以及导入依赖的类
package org.argus.jawa.core.util
import java.io.{File, FileOutputStream, InputStream, OutputStream}
import java.util.zip.{ZipEntry, ZipFile}
import scala.collection.JavaConverters._
object ZipUtil {
val BUFSIZE = 4096
val buffer = new Array[Byte](BUFSIZE)
def unZip(source: String, targetFolder: String): Boolean = {
val zipFile = new ZipFile(source)
unzipAllFile(zipFile.entries.asScala.toList, getZipEntryInputStream(zipFile), new File(targetFolder))
}
def getZipEntryInputStream(zipFile: ZipFile)(entry: ZipEntry): InputStream = zipFile.getInputStream(entry)
def unzipAllFile(entryList: List[ZipEntry], inputGetter: (ZipEntry) => InputStream, targetFolder: File): Boolean = {
entryList match {
case entry :: entries =>
if (entry.isDirectory)
new File(targetFolder, entry.getName).mkdirs
else
saveFile(inputGetter(entry), new FileOutputStream(new File(targetFolder, entry.getName)))
unzipAllFile(entries, inputGetter, targetFolder)
case _ =>
true
}
}
def saveFile(fis: InputStream, fos: OutputStream): Unit = {
writeToFile(bufferReader(fis), fos)
fis.close()
fos.close()
}
def bufferReader(fis: InputStream)(buffer: Array[Byte]): (Int, Array[Byte]) = (fis.read(buffer), buffer)
def writeToFile(reader: (Array[Byte]) => ((Int, Array[Byte])), fos: OutputStream): Boolean = {
val (length, data) = reader(buffer)
if (length >= 0) {
fos.write(data, 0, length)
writeToFile(reader, fos)
} else
true
}
}
示例4: 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}"))
}
}
}
示例5: LoadingFormalPreferences
//设置package包名称以及导入依赖的类
package au.id.tmm.senatedb.core.rawdata.download
import java.io.InputStream
import java.nio.file.Path
import java.util.zip.{ZipEntry, ZipFile}
import au.id.tmm.senatedb.core.model.SenateElection
import au.id.tmm.senatedb.core.rawdata.download.StorageUtils.findRawDataWithIntegrityCheckFor
import au.id.tmm.senatedb.core.rawdata.resources.FormalPreferencesResource
import au.id.tmm.utilities.geo.australia.State
import au.id.tmm.utilities.io.ZipFileUtils.{ImprovedPath, ImprovedZipFile}
import au.id.tmm.utilities.option.OptionUtils.ImprovedOption
import scala.io.Source
import scala.util.Try
object LoadingFormalPreferences {
def csvLinesOf(dataDir: Path, election: SenateElection, state: State): Try[Source] = {
for {
matchingResource <- resourceMatching(election, state)
dataFilePath <- findRawDataWithIntegrityCheckFor(dataDir, matchingResource)
inputStream <- csvInputStreamFrom(matchingResource, dataFilePath)
source <- Try(Source.fromInputStream(inputStream, "UTF-8"))
} yield source
}
private def resourceMatching(election: SenateElection, state: State): Try[FormalPreferencesResource] =
FormalPreferencesResource.of(election, state)
.failIfAbsent(new UnsupportedOperationException(s"Could not find raw data for $state at $election"))
private def csvInputStreamFrom(resource: FormalPreferencesResource, zipFilePath: Path): Try[InputStream] =
for {
zipFile <- Try(zipFilePath.asZipFile)
zipEntry <- findMatchingZipEntry(resource, zipFilePath, zipFile)
inputStream <- Try(zipFile.getInputStream(zipEntry))
} yield inputStream
private def findMatchingZipEntry(resource: FormalPreferencesResource,
zipFilePath: Path,
zipFile: ZipFile): Try[ZipEntry] = {
zipFile
.entryWithName(resource.zipEntryName)
.failIfAbsent(throw new IllegalStateException(s"Could not find expected zip file '${resource.zipEntryName}'" +
s" in file at $zipFilePath"))
}
}
示例6: LoadingDistributionsOfPreferences
//设置package包名称以及导入依赖的类
package au.id.tmm.senatedb.core.rawdata.download
import java.io.InputStream
import java.nio.file.Path
import java.util.zip.{ZipEntry, ZipFile}
import au.id.tmm.senatedb.core.model.SenateElection
import au.id.tmm.senatedb.core.rawdata.download.StorageUtils.findRawDataWithIntegrityCheckFor
import au.id.tmm.senatedb.core.rawdata.resources.DistributionOfPreferencesResource
import au.id.tmm.utilities.geo.australia.State
import au.id.tmm.utilities.io.ZipFileUtils.{ImprovedPath, ImprovedZipFile}
import au.id.tmm.utilities.option.OptionUtils.ImprovedOption
import scala.io.Source
import scala.util.Try
object LoadingDistributionsOfPreferences {
def csvLinesOf(dataDir: Path, election: SenateElection, state: State): Try[Source] = {
for {
matchingResource <- resourceMatching(election)
dataFilePath <- findRawDataWithIntegrityCheckFor(dataDir, matchingResource)
inputStream <- csvInputStreamFrom(matchingResource, dataFilePath, state)
source <- Try(Source.fromInputStream(inputStream, "UTF-8"))
} yield source
}
private def resourceMatching(election: SenateElection): Try[DistributionOfPreferencesResource] =
DistributionOfPreferencesResource.of(election)
.failIfAbsent(new UnsupportedOperationException(s"Could not find a distribution of preferences for $election"))
private def csvInputStreamFrom(resource: DistributionOfPreferencesResource, zipFilePath: Path, state: State): Try[InputStream] =
for {
zipFile <- Try(zipFilePath.asZipFile)
zipEntry <- findMatchingZipEntry(resource, state, zipFilePath, zipFile)
inputStream <- Try(zipFile.getInputStream(zipEntry))
} yield inputStream
private def findMatchingZipEntry(resource: DistributionOfPreferencesResource,
state: State,
zipFilePath: Path,
zipFile: ZipFile): Try[ZipEntry] = {
val zipEntryName = resource.zipEntryNameOf(state)
zipFile
.entryWithName(zipEntryName)
.failIfAbsent(throw new IllegalStateException(s"Could not find expected zip file '${zipEntryName}'" +
s" in file at $zipFilePath"))
}
}
示例7: ZipFileIterator
//设置package包名称以及导入依赖的类
package com.s3dropbox.lambda
import java.io.{ByteArrayOutputStream, InputStream}
import java.util.zip.{ZipEntry, ZipInputStream}
import com.s3dropbox.lambda.ZipFileIterator.{END_OF_FILE, ONE_MB, PAGE_SIZE, ZipFileEntry}
class ZipFileIterator(istream: InputStream) extends Iterator[ZipFileEntry] {
private val zis: ZipInputStream = new ZipInputStream(istream)
private var zentryOpt: Option[ZipEntry] = None
override def hasNext: Boolean = {
zentryOpt = Option(zis.getNextEntry)
zentryOpt.isDefined
}
override def next(): ZipFileEntry = {
val zentry: ZipEntry = zentryOpt.getOrElse(throw new Exception("Calling next() when there are no ZipEntry's to process"))
// sometimes the size cannot be determined, in which case, the buffer is read until no more data is read
val bosSize: Int = if (zentry.getSize > 0) zentry.getSize.toInt else ONE_MB
val bos: ByteArrayOutputStream = new ByteArrayOutputStream(bosSize)
// even though we may know the exact size of the file, ZipInputStream.read() may return a value < the known file size
val readBuffer: Array[Byte] = new Array[Byte](PAGE_SIZE)
Iterator
.continually(zis.read(readBuffer, 0, PAGE_SIZE))
.takeWhile((bytesRead: Int) => bytesRead != END_OF_FILE)
.foreach((bytesRead: Int) => bos.write(readBuffer, 0, bytesRead))
ZipFileEntry(zentry.getName, bos.toByteArray)
}
def close(): Unit = {
zis.closeEntry()
zis.close()
}
}
object ZipFileIterator {
case class ZipFileEntry(filename: String, data: Array[Byte])
private[ZipFileIterator] val ONE_MB: Int = 1024 * 1024
private[ZipFileIterator] val END_OF_FILE: Int = -1
private[ZipFileIterator] val PAGE_SIZE: Int = 1024
}
示例8: 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
}
}
示例9: 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()
}
}
}
示例10: ZipUtils
//设置package包名称以及导入依赖的类
package passengersplits.core
import java.io.Closeable
import java.nio.charset.StandardCharsets._
import java.util.zip.{ZipEntry, ZipInputStream}
import scala.collection.JavaConversions._
import scala.collection.mutable.ArrayBuffer
object ZipUtils {
case class UnzippedFileContent( filename: String, content: String, zipFilename: Option[String] = None)
def usingZip[R <: Closeable, T](unzippedStream: R)(f: (R) => T) = {
try {
f(unzippedStream)
} finally {
unzippedStream.close()
}
}
def unzipAllFilesInStream(unzippedStream: ZipInputStream): Stream[UnzippedFileContent] = {
unzipAllFilesInStream(unzippedStream, Option(unzippedStream.getNextEntry))
}
def unzipAllFilesInStream(unzippedStream: ZipInputStream, ze: Option[ZipEntry]): Stream[UnzippedFileContent] = {
ze match {
case None => Stream.empty
case Some(ze) =>
val name: String = ze.getName
val entry: String = ZipUtils.getZipEntry(unzippedStream)
val maybeEntry1: Option[ZipEntry] = Option(unzippedStream.getNextEntry)
UnzippedFileContent(name, entry) #::
unzipAllFilesInStream(unzippedStream, maybeEntry1)
}
}
def getZipEntry(zis: ZipInputStream): String = {
val buffer = new Array[Byte](4096)
val stringBuffer = new ArrayBuffer[Byte]()
var len: Int = zis.read(buffer)
while (len > 0) {
stringBuffer ++= buffer.take(len)
len = zis.read(buffer)
}
val content: String = new String(stringBuffer.toArray, UTF_8)
(content)
}
}
示例11: ZipArchive
//设置package包名称以及导入依赖的类
package com.cloudlabhk.CiMarking
import java.io.{File, FileOutputStream, InputStream, OutputStream}
import java.util.zip.{ZipEntry, ZipFile}
import scala.collection.JavaConversions._
//https://gist.github.com/Swind/2568527
class ZipArchive {
val BUFSIZE = 4096
val buffer = new Array[Byte](BUFSIZE)
def unZip(source: String, targetFolder: String) = {
val zipFile = new ZipFile(source)
unzipAllFile(zipFile.entries.toList, getZipEntryInputStream(zipFile) _, new File(targetFolder))
}
def getZipEntryInputStream(zipFile: ZipFile)(entry: ZipEntry) = zipFile.getInputStream(entry)
def unzipAllFile(entryList: List[ZipEntry], inputGetter: (ZipEntry) => InputStream, targetFolder: File): Boolean = {
entryList match {
case entry :: entries =>
if (entry.isDirectory)
new File(targetFolder, entry.getName).mkdirs
else
saveFile(inputGetter(entry), new FileOutputStream(new File(targetFolder, entry.getName)))
unzipAllFile(entries, inputGetter, targetFolder)
case _ =>
true
}
}
def saveFile(fis: InputStream, fos: OutputStream) = {
writeToFile(bufferReader(fis) _, fos)
fis.close
fos.close
}
def bufferReader(fis: InputStream)(buffer: Array[Byte]) = (fis.read(buffer), buffer)
def writeToFile(reader: (Array[Byte]) => Tuple2[Int, Array[Byte]], fos: OutputStream): Boolean = {
val (length, data) = reader(buffer)
if (length >= 0) {
fos.write(data, 0, length)
writeToFile(reader, fos)
} else
true
}
}
示例12: getUsers
//设置package包名称以及导入依赖的类
import scala.collection.mutable.ArrayBuffer
import java.io.FileInputStream
import java.util.zip.{ZipEntry, ZipInputStream}
package object config {
def getUsers(pkg: String, className: String): Seq[String] = {
val f = s"apps/bdapro-ws1617-flink-jobs-1.0-SNAPSHOT.jar"
val zip = new ZipInputStream(new FileInputStream(f))
var entry: ZipEntry = zip.getNextEntry()
val users = new ArrayBuffer[String]()
while (entry != null) {
if (!entry.isDirectory && entry.getName.endsWith(".class")) {
val pattern = ("""de/tu_berlin/dima/bdapro/flink/""" + pkg + """/([^/]*)/""" + className + """\.class""").r
val matches = pattern.findAllIn(entry.getName).matchData foreach {
m => users += m.group(1)
}
}
entry = zip.getNextEntry()
}
users
}
}
示例13: 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()
}
}
}
示例14: ZipUtils
//设置package包名称以及导入依赖的类
package com.socialthingy.plusf.wos
import java.io.File
import java.nio.file.Files
import java.nio.file.StandardCopyOption.REPLACE_EXISTING
import java.util.{Collections, Optional}
import java.util.zip.{ZipEntry, ZipFile}
import scala.collection.mutable.ListBuffer
import scala.util.Try
import scala.util.control.NonFatal
import scala.collection.JavaConverters._
object ZipUtils {
def isZipFile(file: File): Boolean = {
val isZip = Try {
val zf = new ZipFile(file)
zf.close()
}
isZip.isSuccess
}
def findFiles(file: File): java.util.List[ZipEntry] = withZipFile(file)(Collections.emptyList[ZipEntry]()) { zf =>
val entriesInFile = zf.entries()
val matchingEntries = ListBuffer[ZipEntry]()
while (entriesInFile.hasMoreElements) {
val next = entriesInFile.nextElement
val name = next.getName.toLowerCase()
if (name.endsWith(".tap") || name.endsWith(".tzx")) {
matchingEntries.append(next)
}
}
matchingEntries.asJava
}
def unzipFile(zipFile: File, entry: ZipEntry): Optional[File] = withZipFile(zipFile)(Optional.empty[File]) { zf =>
val unzipped = File.createTempFile("plusf", entry.getName)
unzipped.deleteOnExit()
val stream = zf.getInputStream(entry)
try {
Files.copy(stream, unzipped.toPath, REPLACE_EXISTING)
Optional.of(unzipped)
} finally {
stream.close()
}
}
private def withZipFile[T](file: File)(onError: => T)(fn: ZipFile => T): T = try {
val zf = new ZipFile(file)
try {
fn(zf)
} finally {
zf.close()
}
} catch {
case NonFatal(e) => onError
}
}