简体   繁体   English

scalaz-stream的膨胀的用法示例

[英]Usage example of scalaz-stream's inflate

In the following usage example of scalaz-stream (taken from the documentation ), what do I need to change if the input and/or output is a gzipped file? 在以下scalaz-stream使用示例中(取自文档 ),如果输入和/或输出是gzip压缩文件,我需要更改什么? In other words, how do I use compress ? 换句话说,我该如何使用compress

import scalaz.stream._
import scalaz.concurrent.Task

val converter: Task[Unit] =
  io.linesR("testdata/fahrenheit.txt")
    .filter(s => !s.trim.isEmpty && !s.startsWith("//"))
    .map(line => fahrenheitToCelsius(line.toDouble).toString)
    .intersperse("\n")
    .pipe(text.utf8Encode)
    .to(io.fileChunkW("testdata/celsius.txt"))
    .run

// at the end of the universe...
val u: Unit = converter.run

Compressing the output is easy. 压缩输出很容易。 Since compress.deflate() is a Process1[ByteVector, ByteVector] you need to plug it into your pipeline where you are emitting ByteVector s (that is right after text.utf8Encode which is a Process1[String, ByteVector] ): 由于compress.deflate()是一个Process1[ByteVector, ByteVector]你需要将它插入到你发出ByteVector的管道中(就在text.utf8Encode之后,它是一个Process1[String, ByteVector] ):

val converter: Task[Unit] =
  io.linesR("testdata/fahrenheit.txt")
    .filter(s => !s.trim.isEmpty && !s.startsWith("//"))
    .map(line => fahrenheitToCelsius(line.toDouble).toString)
    .intersperse("\n")
    .pipe(text.utf8Encode)
    .pipe(compress.deflate())
    .to(io.fileChunkW("testdata/celsius.zip"))
    .run

For inflate you can't use io.linesR to read the compressed file. 对于inflate不能使用io.linesR读取压缩文件。 You need a process that produces ByteVector s instead of String s in order to pipe them into inflate . 你需要产生一个过程ByteVector !而非String S IN为了管他们进入inflate (You could use io.fileChunkR for that.) The next step would be decoding the uncompressed data to String s (with text.utf8Decode for example) and then using text.lines() to emit the text line by line. (你可以使用io.fileChunkR了点。)下一步将未压缩数据将被解码String (其中s text.utf8Decode例如),然后使用text.lines()以发射由行中的文本行。 Something like this should do the trick: 像这样的东西应该做的伎俩:

val converter: Task[Unit] =
  Process.constant(4096).toSource
    .through(io.fileChunkR("testdata/fahrenheit.zip"))
    .pipe(compress.inflate())
    .pipe(text.utf8Decode)
    .pipe(text.lines())
    .filter(s => !s.trim.isEmpty && !s.startsWith("//"))
    .map(line => fahrenheitToCelsius(line.toDouble).toString)
    .intersperse("\n")
    .pipe(text.utf8Encode)
    .to(io.fileChunkW("testdata/celsius.txt"))
    .run

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM