Home > database >  decompress data in scala using gzip
decompress data in scala using gzip

Time:03-23

when i try to decompress gzip file i get error:

my code:

val file_inp = new FileInputStream("Textfile.txt.gzip")
val file_out = new FileOutputStream("Textfromgzip.txt")
val gzInp = new GZIPInputStream(new BufferedInputStream(file_inp))
while (gzInp.available != -1) {
  file_out.write(gzInp.read)
}
file_out.close()

output :

scala:25: error: ambiguous reference to overloaded definition,
both method read in class GZIPInputStream of type (x$1: Array[Byte], x$2: Int, x$3: 
Int)Int
and  method read in class InflaterInputStream of type ()Int
match expected type ?
file_out.write(gzInp.read)
                     ^
one error found

if anyone knows about this error please help me.

CodePudding user response:

working model for gzip compress and decompress in scala :

//create a text file and write something ..
val file_txt = new FileOutputStream("Textfile1.txt")
file_txt.write("hello this is a sample text ".getBytes)
file_txt.write(10) // 10 -> \n (or) next Line
file_txt.write("hello it is  second line".getBytes)
file_txt.write(10)
file_txt.write("the good day".getBytes)
file_txt.close()

// gzip file 

val file_ip = new FileInputStream("Textfile1.txt")
val file_gzOut = new GZIPOutputStream(new FileOutputStream("Textfile1.txt.gz"))
file_gzOut.write(file_ip.readAllBytes)
file_gzOut.close

//extract gzip

val gzFile = new GZIPInputStream(new FileInputStream("Textfile1.txt.gz"))
val txtFile = new FileOutputStream("Textfrom_gz.txt")
txtFile.write(gzFile.readAllBytes)
gzFile.close

don't forgot to close the files it leads to "truncated gzip input"

  • Related