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


Scala ZipFile类代码示例

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


在下文中一共展示了ZipFile类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: validJar

//设置package包名称以及导入依赖的类
package uk.co.telegraph.sbt.resolver

import java.util.zip.ZipFile

import sbt._
import scala.concurrent.duration.Duration
import scala.util._

trait ArtifactDeploy {

  private def validJar(file:File) = Try(new ZipFile(file)).isSuccess

  def apply(
    moduleId:ModuleID,
    targetDir:File,
    downloadIfOrderThan:Duration,
    logger: Logger
  )(implicit scalaVersion:String, repository:Repository):File = {

    def isStale(file:File) = moduleId.revision == LatestVersionLabel && System.currentTimeMillis - file.lastModified > downloadIfOrderThan.toMillis

    val localJar = new File(targetDir, s"${moduleId.name}.jar")

    if(!targetDir.exists()){
      logger.info(s"Creating target directory $targetDir")
      targetDir.mkdirs()
    }

    if(!localJar.exists() || isStale(localJar) || !validJar(localJar) ){
      Try{
        val remoteJar = moduleId.resolveVersion().remoteJar

        logger.info(s"Downloading jar from [$remoteJar] to [${localJar.getAbsolutePath}]")
        ( remoteJar #> localJar ).!!
      } match {
        case Success(_) =>
        case Failure(e) =>
          sys.error(s"Fail to download artifact [$moduleId]. Reason: ${e.getMessage}")
      }
    }
    if (!validJar(localJar)) {
      sys.error(s"Invalid jar file at [${localJar.getAbsolutePath}]")
    }
    localJar
  }
}

object ArtifactDeploy extends ArtifactDeploy 
开发者ID:telegraph,项目名称:sbt-wiremock,代码行数:49,代码来源:ArtifactDeploy.scala

示例2: JarClassSource

//设置package包名称以及导入依赖的类
package eu.tznvy.jancy.transpiler.discovery

import java.io.File
import java.net.URLClassLoader
import java.util.zip.ZipFile
import java.io.Closeable
import scala.collection.JavaConverters._


class JarClassSource(file: File) extends Closeable {

  lazy val zipFile = new ZipFile(file.getPath)

  override def close(): Unit = {
    zipFile.close()
  }

  def iterate: Iterator[Class[_]] = {
    val classLoader = new URLClassLoader(Array(file.toURI.toURL))

    zipFile
      .stream
      .iterator
      .asScala
      .map(_.getName)
      .filter(_.endsWith(".class"))
      .map(_.replace(".class", "").replace('/', '.'))
      .map(classLoader.loadClass)
  }
} 
开发者ID:brthanmathwoag,项目名称:jancy,代码行数:31,代码来源:JarClassSource.scala

示例3: ContentFilesExtractor

//设置package包名称以及导入依赖的类
package eu.tznvy.jancy.transpiler.discovery

import java.io.File
import java.nio.file.Path
import java.util.zip.ZipFile
import eu.tznvy.jancy.transpiler.helpers.Filesystem
import resource.managed


class ContentFilesExtractor(filesystem: Filesystem) {

  def extract(files: Seq[ContentFile], jar: File, root: Path): Seq[Path] = {

    managed(new ZipFile(jar.getPath))
      .map({ z =>
        files.flatMap({ f =>
          managed(z.getInputStream(z.getEntry(f.source)))
            .map({ in =>
              val outputPath = root.resolve(f.destination)
              filesystem.createDirectories(outputPath.getParent)
              filesystem.copy(in, outputPath)
              outputPath
            }).opt.map(Seq(_)).getOrElse(Seq())
        })
    }).opt.getOrElse(Seq())
  }
} 
开发者ID:brthanmathwoag,项目名称:jancy,代码行数:28,代码来源:ContentFilesExtractor.scala

示例4: ContentFilesDiscoverer

//设置package包名称以及导入依赖的类
package eu.tznvy.jancy.transpiler.discovery

import java.io.File
import java.util.zip.ZipFile
import resource.managed
import scala.collection.JavaConverters._


object ContentFilesDiscoverer {

  def discover(file: File, configurationName: String): Seq[ContentFile] = {
    managed(new ZipFile(file.getPath))
      .map(_
        .stream
        .iterator
        .asScala
        .filter(!_.isDirectory)
        .map(_.getName)
        .filter(_.startsWith(configurationName + "/"))
        .map({ source =>
          val destination = source.substring(configurationName.length + 1)
          ContentFile(source, destination)
        })
        .toList)
      .opt
      .get
  }
} 
开发者ID:brthanmathwoag,项目名称:jancy,代码行数:29,代码来源:ContentFilesDiscoverer.scala

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

示例6: 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"))
  }
} 
开发者ID:tmccarthy,项目名称:SenateDB,代码行数:48,代码来源:LoadingFormalPreferences.scala

示例7: 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"))
  }
} 
开发者ID:tmccarthy,项目名称:SenateDB,代码行数:51,代码来源:LoadingDistributionsOfPreferences.scala

示例8: 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
  }
} 
开发者ID:wongcyrus,项目名称:awslambdamavenbuild,代码行数:56,代码来源:ZipArchive.scala

示例9: ZippedFileImporter

//设置package包名称以及导入依赖的类
package com.twitter.diffy.scrooge

import com.twitter.scrooge.frontend.{DirImporter, Importer}
import java.io.File
import java.nio.file.Files
import java.util.zip.ZipFile
import scala.collection.JavaConversions._
import scala.io.Source

object ZippedFileImporter {
  def apply(zipFiles: Seq[ZipFile]): Importer = {
    val thriftDir = Files.createTempDirectory("thrift-")
    thriftDir.toFile.deleteOnExit()

    zipFiles foreach { zipFile =>
      zipFile.entries.toList.collect {
        case zipEntry if !zipEntry.isDirectory && zipEntry.getName.endsWith(".thrift") =>
          val data = Source.fromInputStream(zipFile.getInputStream(zipEntry), "UTF-8").mkString

          val newFile = new File(thriftDir.toString + File.separator + zipEntry.getName)
          new File(newFile.getParent).mkdirs()

          Files.write(newFile.toPath, data.getBytes)
      }
    }

    DirImporter(thriftDir.toFile)
  }
}

object FileImporter {
  def apply(files: Seq[File]): Importer = {
    val thriftDir = Files.createTempDirectory("thrift-")
    thriftDir.toFile.deleteOnExit()

    files foreach { file =>
      val newFile = new File(thriftDir.toString + File.separator + file.getName)
      Files.copy(file.toPath, newFile.toPath)
    }

    DirImporter(thriftDir.toFile)
  }
} 
开发者ID:sachinmanchanda,项目名称:diffy_unicast,代码行数:44,代码来源:ThriftImporter.scala

示例10: DownloadS3Proxy

//设置package包名称以及导入依赖的类
package com.localytics.sbt.s3

import java.io.File
import java.net.URL
import java.util.zip.ZipFile

import sbt.Keys.TaskStreams

import scala.sys.process._
import scala.util.Try

object DownloadS3Proxy {

  private[s3] def validJar(file: File): Boolean = Try(new ZipFile(file)).isSuccess

  def apply(ver: String, url: String, dir: File, file: String, streamz: TaskStreams): File = {
    val outputFile = new File(dir, file)
    if (!dir.exists()) {
      streamz.log.info(s"Creating S3Proxy directory $dir")
      dir.mkdirs()
    }
    if (!outputFile.exists()) {
      streamz.log.info(s"Downloading S3Proxy from [$url] to [${outputFile.getAbsolutePath}]")
      (new URL(url) #> outputFile).!!
    }
    if (!validJar(outputFile)) sys.error(s"Invalid jar file at [${outputFile.getAbsolutePath}]")
    outputFile
  }
} 
开发者ID:localytics,项目名称:sbt-s3,代码行数:30,代码来源:DownloadS3Proxy.scala

示例11: 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
  }
} 
开发者ID:alangibson27,项目名称:plus-f,代码行数:60,代码来源:ZipUtils.scala

示例12: DownloadElasticMQ

//设置package包名称以及导入依赖的类
package com.localytics.sbt.sqs

import java.net.URL
import java.util.zip.ZipFile

import sbt.File
import sbt.Keys._

import scala.sys.process._
import scala.util.Try

object DownloadElasticMQ {

  private[sqs] def validJar(file: File): Boolean = Try(new ZipFile(file)).isSuccess

  def apply(version: String, url: String, dir: File, file: String, streamz: TaskStreams): File = {
    val outputFile = new File(dir, file)
    if (!dir.exists()) {
      streamz.log.info(s"Creating ElasticMQ directory $dir")
      dir.mkdirs()
    }
    if (!outputFile.exists() || !validJar(outputFile)) {
      streamz.log.info(s"Downloading ElasticMQ from [$url] to [${outputFile.getAbsolutePath}]")
      (new URL(url) #> outputFile).!!
    }
    if (!validJar(outputFile)) sys.error(s"Invalid jar file at [${outputFile.getAbsolutePath}]")
    outputFile
  }

} 
开发者ID:localytics,项目名称:sbt-sqs,代码行数:31,代码来源:DownloadElasticMQ.scala


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