当前位置: 首页>>代码示例>>Scala>>正文


Scala ZipInputStream类代码示例

本文整理汇总了Scala中java.util.zip.ZipInputStream的典型用法代码示例。如果您正苦于以下问题:Scala ZipInputStream类的具体用法?Scala ZipInputStream怎么用?Scala ZipInputStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了ZipInputStream类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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)
  }
} 
开发者ID:UKHomeOffice,项目名称:drt-passenger-splits,代码行数:50,代码来源:ZipUtils.scala

示例2: Sentiment140Downloader

//设置package包名称以及导入依赖的类
package com.aluxian.tweeather.scripts

import java.net.URL
import java.util.zip.ZipInputStream

import org.apache.hadoop.fs.Path
import org.apache.spark.Logging


object Sentiment140Downloader extends Script with Logging {

  val downloadUrl = "http://cs.stanford.edu/people/alecmgo/trainingandtestdata.zip"

  override def main(args: Array[String]) {
    super.main(args)

    logInfo(s"Downloading sentiment140 dataset from $downloadUrl")
    val zip = new ZipInputStream(new URL(downloadUrl).openStream())
    val buffer = new Array[Byte](4 * 1024)

    Stream.continually(zip.getNextEntry)
      .takeWhile(_ != null)
      .foreach { entry =>
        val fileName = entry.getName
        val out = hdfs.create(new Path(s"tw/sentiment/140/downloaded/$fileName"))
        logInfo(s"Downloading $fileName")

        Stream.continually(zip.read(buffer))
          .takeWhile(_ != -1)
          .foreach { count =>
            out.write(buffer, 0, count)
          }

        out.close()
      }

    zip.close()
    logInfo("Downloading finished")
    sc.stop()
  }

} 
开发者ID:cnajeefa,项目名称:Tourism-Sentiment-Analysis,代码行数:43,代码来源:Sentiment140Downloader.scala

示例3: EmailParser

//设置package包名称以及导入依赖的类
package uk.pkerrigan.dmarcparser

import java.io.ByteArrayInputStream
import java.nio.charset.CodingErrorAction
import java.util.Properties
import java.util.zip.{GZIPInputStream, ZipInputStream}
import javax.activation.DataSource
import javax.mail.Session
import javax.mail.internet.MimeMessage
import scala.collection.JavaConverters._

import org.apache.commons.mail.util.MimeMessageParser
import uk.pkerrigan.dmarcparser.report.Feedback

import scala.io._

class EmailParser(parser: ParserTrait = new Parser()) extends EmailParserTrait{

  implicit val codec = Codec("UTF-8")
  codec.onMalformedInput(CodingErrorAction.REPLACE)
  codec.onUnmappableCharacter(CodingErrorAction.REPLACE)

  def parseEmail(email: String): Option[Feedback] = {
    val s = Session.getDefaultInstance(new Properties())
    val is = new ByteArrayInputStream(email.getBytes)
    val message = new MimeMessage(s, is)
    val messageParser = new MimeMessageParser(message).parse()

    messageParser.getAttachmentList.asScala.headOption.flatMap(extract)
  }

  private def extract(a: DataSource): Option[Feedback] = a match {
    case `a` if a.getContentType.equals("application/gzip") => extractGzip(a)
    case `a` if a.getContentType.equals("application/x-gzip") => extractGzip(a)
    case `a` if a.getContentType.equals("application/zip") => extractZip(a)
    case `a` if a.getContentType.equals("application/x-zip-compressed") => extractZip(a)
    case _ => None
  }

  private def extractZip(a: DataSource): Option[Feedback] = {
    val zip = new ZipInputStream(a.getInputStream)
    zip.getNextEntry
    val rawXml = Source.fromInputStream(zip).mkString
    if (rawXml == "") None else Some(parser.parse(rawXml))
  }
  private def extractGzip(a: DataSource): Option[Feedback] = {
    val zip = new GZIPInputStream(a.getInputStream)
    val rawXml = Source.fromInputStream(zip).mkString
    if (rawXml == "") None else Some(parser.parse(rawXml))
  }
} 
开发者ID:patrickkerrigan,项目名称:dmarc-parser,代码行数:52,代码来源:EmailParser.scala

示例4: 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)
		}
	}
} 
开发者ID:arguslab,项目名称:Argus-SAF,代码行数:39,代码来源:AndroidXMLParser.scala

示例5: 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
} 
开发者ID:ErrorsAndGlitches,项目名称:S3DropboxLambda,代码行数:50,代码来源:ZipFileIterator.scala

示例6: 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)
  }
} 
开发者ID:UKHomeOffice,项目名称:drt-scalajs-spa-exploration,代码行数:50,代码来源:ZipUtils.scala

示例7: UnzipUtility

//设置package包名称以及导入依赖的类
package it.milczarek.gpwquoter.file

import java.io.{BufferedOutputStream, File, FileInputStream, FileOutputStream}
import java.util.zip.ZipInputStream


object UnzipUtility {

  val bufferSize = 4096

  def unzip(zipFilePath: File, destDirectory: String) {
    def extractFile(zipIn: ZipInputStream, filePath: String) {
      val bos = new BufferedOutputStream(new FileOutputStream(filePath))
      val bytesIn = new Array[Byte](bufferSize)
      var read = zipIn.read(bytesIn)
      while (read != -1) {
        bos.write(bytesIn, 0, read)
        read = zipIn.read(bytesIn)
      }
      bos.close()
    }

    val destDir = new File(destDirectory)
    if (!destDir.exists()) destDir.mkdir()

    val zipIn = new ZipInputStream(new FileInputStream(zipFilePath))

    var entry = zipIn.getNextEntry
    while (entry != null) {
      val filePath = destDirectory + File.separator + entry.getName
      if (!entry.isDirectory) {
        extractFile(zipIn, filePath)
      } else {
        val dir = new File(filePath)
        dir.mkdir()
      }
      zipIn.closeEntry()
      entry = zipIn.getNextEntry
    }
    zipIn.close()
  }
} 
开发者ID:milczarekIT,项目名称:gpw-quoter,代码行数:43,代码来源:UnzipUtility.scala

示例8: 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
  }
} 
开发者ID:cristiprg,项目名称:BDAPRO.GlobalStateML,代码行数:24,代码来源:package.scala

示例9: unzip

//设置package包名称以及导入依赖的类
package flaky

import java.io.File

trait Unzip {

  def unzip(zipped:File, unzipDir:File): Unit = {
    import java.io.{FileInputStream, FileOutputStream}
    import java.util.zip.ZipInputStream
    val fis = new FileInputStream(zipped)
    val zis = new ZipInputStream(fis)

    unzipDir.mkdirs()
    Stream
      .continually(zis.getNextEntry)
      .takeWhile(_ != null)
      .foreach { file =>
        if (file.isDirectory) {
          val dir = new File(unzipDir, file.getName)
          dir.mkdirs()
          dir.deleteOnExit()
        } else {
          val file1 = new File(unzipDir, file.getName)
          file1.deleteOnExit()
          val fout = new FileOutputStream(file1)
          val buffer = new Array[Byte](1024)
          Stream.continually(zis.read(buffer)).takeWhile(_ != -1).foreach(fout.write(buffer, 0, _))
        }
      }
  }
} 
开发者ID:otrebski,项目名称:sbt-flaky,代码行数:32,代码来源:Unzip.scala


注:本文中的java.util.zip.ZipInputStream类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。