Last active
May 8, 2019 07:28
-
-
Save kamalbanga/a2a6675b213e4a22a5cd to your computer and use it in GitHub Desktop.
Zlib Compression in Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.io._ | |
import java.util.zip.{Inflater, Deflater} | |
import java.io.{File, FileOutputStream} | |
object App { | |
def compress(inData: Array[Byte]): Array[Byte] = { | |
var deflater: Deflater = new Deflater() | |
deflater.setInput(inData) | |
deflater.finish | |
val compressedData = new Array[Byte](inData.size * 2) // compressed data can be larger than original data | |
val count: Int = deflater.deflate(compressedData) | |
return compressedData.take(count) | |
} | |
def main(args: Array[String]) = { | |
val inFile = "/Users/kamal/rubycodes/data.txt" | |
val outFile = "/Users/kamal/zlib" | |
var lines = Source.fromFile(inFile).getLines.toIterable | |
var count = 0 | |
while(lines.size > 0) { | |
var compressedBytes = compress(lines.take(10).mkString("\n").getBytes) | |
lines = lines.drop(10) | |
count += 1 | |
(new FileOutputStream(new File(outFile + count))).write(compressedBytes) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.zip.{Inflater, Deflater} // Zlib library | |
import java.nio.file.{Files, Paths} | |
import java.io.{File, FileOutputStream} | |
object Inf { | |
def compress(inData: Array[Byte]): Array[Byte] = { | |
var deflater: Deflater = new Deflater() | |
deflater.setInput(inData) | |
deflater.finish | |
val compressedData = new Array[Byte](inData.size * 2) // compressed data can be larger than original data | |
val count: Int = deflater.deflate(compressedData) | |
return compressedData.take(count) | |
} | |
def decompress(inData: Array[Byte]): Array[Byte] = { | |
val inflater = new Inflater() | |
inflater.setInput(inData) | |
val decompressedData = new Array[Byte](inData.size * 2) | |
var count = inflater.inflate(decompressedData) | |
var finalData = decompressedData.take(count) | |
while (count > 0) { | |
count = inflater.inflate(decompressedData) | |
finalData = finalData ++ decompressedData.take(count) | |
} | |
return finalData | |
} | |
def main(args: Array[String]) { | |
val path = args(0) | |
val byteArray = Files.readAllBytes(Paths.get(path)) | |
val compressedFilePath = "/Users/kamal/compressed" | |
val decompressedFilePath = "/Users/kamal/decompressed" | |
val cmpFile = new FileOutputStream(new File(compressedFilePath)) | |
val dmpFile = new FileOutputStream(new File(decompressedFilePath)) | |
val compressedData = compress(byteArray) | |
val decompressedData = decompress(compressedData) | |
cmpFile.write(compressedData) | |
dmpFile.write(decompressedData) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Be carefull about leaks: Deflater and Inflater should be carefully ended.