Java 从GZIPInputStream读取大量字节

Java 从GZIPInputStream读取大量字节,java,scala,gzip,gzipinputstream,Java,Scala,Gzip,Gzipinputstream,我正在通过GZIPInputStream读取一个gzipped文件。我想一次读取大量数据,但不管我让GZIPInputStream读取多少字节,它总是读取的字节数要少得多。比如说, val bArray = new Array[Byte](81920) val fis = new FileInputStream(new File(inputFileName)) val gis = new GZIPInputStream(fis) val bytesRead = gis.read(bArray)

我正在通过GZIPInputStream读取一个gzipped文件。我想一次读取大量数据,但不管我让GZIPInputStream读取多少字节,它总是读取的字节数要少得多。比如说,

val bArray = new Array[Byte](81920)
val fis = new FileInputStream(new File(inputFileName))
val gis = new GZIPInputStream(fis)
val bytesRead =  gis.read(bArray)

读取的字节总是在1800字节左右,而它应该几乎等于bArray的大小,在本例中为81920。为什么是这样?有没有办法解决这个问题,并真正有更多的字节数读取

如果您有大量数据,我会尝试使用akka streams

  implicit val system = ActorSystem()
  implicit val ec = system.dispatcher
  implicit val materializer = ActorMaterializer()

  val fis = new FileInputStream(new File(""))
  val gis = new GZIPInputStream(fis) 
  val bfs: BufferedSource = Source.fromInputStream(gis)
bfs
公开流处理的
Flow
api

您还可以从中获取流:

val ss: Stream[String] = bfs.bufferedReader().lines()

如果你有大量的数据,我会尝试使用akka流

  implicit val system = ActorSystem()
  implicit val ec = system.dispatcher
  implicit val materializer = ActorMaterializer()

  val fis = new FileInputStream(new File(""))
  val gis = new GZIPInputStream(fis) 
  val bfs: BufferedSource = Source.fromInputStream(gis)
bfs
公开流处理的
Flow
api

您还可以从中获取流:

val ss: Stream[String] = bfs.bufferedReader().lines()

好的,我找到了解决办法。GZIPInputStream有一个构造函数版本,它也采用缓冲区的大小。

好的,我找到了解决方案。GZIPInputStream的构造函数有一个版本,它也采用缓冲区的大小。

读取可能总是返回比您要求的更少的字节,因此通常您必须循环,读取任意数量的字节

换句话说,给
GZIPInputStream
一个大的缓冲区并不意味着它将在给定的请求中被填充

import java.util.zip.GZIPInputStream
import java.io.FileInputStream
import java.io.File
import java.io.InputStream
import java.io.FilterInputStream

object Unzipped extends App {
  val inputFileName = "/tmp/sss.gz"
  val bArray = new Array[Byte](80 * 1024)
  val fis = new FileInputStream(new File(inputFileName))
  val stingy = new StingyInputStream(fis)
  val gis = new GZIPInputStream(stingy, 80 * 1024)
  val bytesRead = gis.read(bArray, 0, bArray.length)
  println(bytesRead)
}

class StingyInputStream(is: InputStream) extends FilterInputStream(is) {
  override def read(b: Array[Byte], off: Int, len: Int) = {
    val n = len.min(1024)
    super.read(b, off, n)
  }
}
因此,与其发出一个读:

  import reflect.io.Streamable.Bytes
  val sb = new Bytes {
    override val length = 80 * 1024L
    override val inputStream = gis
  }
  val res = sb.toByteArray()
  println(res.length)  // your explicit length

我并不是说这就是要使用的API,只是为了演示。我懒得写循环。

读取可能总是返回比您要求的字节更少的字节,因此通常您必须始终循环,读取任意数量的字节

换句话说,给
GZIPInputStream
一个大的缓冲区并不意味着它将在给定的请求中被填充

import java.util.zip.GZIPInputStream
import java.io.FileInputStream
import java.io.File
import java.io.InputStream
import java.io.FilterInputStream

object Unzipped extends App {
  val inputFileName = "/tmp/sss.gz"
  val bArray = new Array[Byte](80 * 1024)
  val fis = new FileInputStream(new File(inputFileName))
  val stingy = new StingyInputStream(fis)
  val gis = new GZIPInputStream(stingy, 80 * 1024)
  val bytesRead = gis.read(bArray, 0, bArray.length)
  println(bytesRead)
}

class StingyInputStream(is: InputStream) extends FilterInputStream(is) {
  override def read(b: Array[Byte], off: Int, len: Int) = {
    val n = len.min(1024)
    super.read(b, off, n)
  }
}
因此,与其发出一个读:

  import reflect.io.Streamable.Bytes
  val sb = new Bytes {
    override val length = 80 * 1024L
    override val inputStream = gis
  }
  val res = sb.toByteArray()
  println(res.length)  // your explicit length

我并不是说这就是要使用的API,只是为了演示。我懒得写循环。

您正在读取的文件有多大?示例循环:@som snytt:您的示例没有使用gzip库。您正在读取的文件有多大?示例循环:@som snytt:您的示例没有使用gzip库。