Skip to content

Instantly share code, notes, and snippets.

@sasaki-shigeo
Last active August 26, 2019 14:06
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save sasaki-shigeo/8431252 to your computer and use it in GitHub Desktop.
Save sasaki-shigeo/8431252 to your computer and use it in GitHub Desktop.
A sample code of gzip in Scala. The API of gzip is defined in the package java.util.zip that provides GZIPOutputStream and GZIPInputStream, the subclasses of DeflatterOutputStream and InflatterInputStream, respectively. GZIPOutputStream compresses data into a given output stream and GZIPInputStream decompresses data from an input one.
import java.io._
import java.util.zip._
val pi = new PipedInputStream
val po = new PipedOutputStream(pi)
val zo = new GZIPOutputStream(po)
val zi = new GZIPInputStream(pi)
val w = new PrintWriter(zo)
val r = new BufferedReader(new InputStreamReader(zi))
pw.println("Hello, world.")
pw.close
println(r.readLine)
import java.io._
import java.util.zip._
/** Gzcat
*/
object gzcat extends App {
private val buf = new Array[Byte](1024)
try {
for (path <- args) {
try {
var in = new GZIPInputStream(new FileInputStream(path))
var n = in.read(buf)
while (n >= 0) {
System.out.write(buf, 0, n)
n = in.read(buf)
}
}
catch {
case _:FileNotFoundException =>
System.err.printf("File Not Found: %s", path)
case _:SecurityException =>
System.err.printf("Permission Denied: %s", path)
}
}
}
finally {
System.out.flush
}
}
/** gzip
*/
object gzip {
private val buf = new Array[Byte](1024)
def gzip(path: String) {
val src = new File(path)
val dst = new File(path ++ ".gz")
try {
val in = new BufferedInputStream(new FileInputStream(src))
try {
val out = new GZIPOutputStream(new FileOutputStream(dst))
try {
var n = in.read(buf)
while (n >= 0) {
out.write(buf, 0, n)
n = in.read(buf)
}
}
finally {
out.flush
}
} catch {
case _:FileNotFoundException =>
System.err.printf("Permission Denied: %s", path ++ ".gz")
case _:SecurityException =>
System.err.printf("Permission Denied: %s", path ++ ".gz")
}
} catch {
case _: FileNotFoundException =>
System.err.printf("File Not Found: %s", path)
case _: SecurityException =>
System.err.printf("Permission Denied: %s", path)
}
}
def main(filenames: Array[String]) {
if (filenames.length == 0) {
System.err.println("Usage: scala gzip file...")
System.exit(1)
}
else {
filenames.foreach(gzip(_))
}
}
}
object gunzip {
private val buf = new Array[Byte](1024)
/**
* gunzip
*
* @param PATH: the path name without the extentson ".gz"
* You have to check if there is no file named as PATH
* before you invode this method, or the file is going
* to be overwritten.
*/
def gunzip(path: String) {
val src = new File(path ++ ".gz")
val dst = new File(path)
try {
val in = new GZIPInputStream(new FileInputStream(src))
try {
val out = new BufferedOutputStream(new FileOutputStream(dst))
try {
var n = in.read(buf)
while (n >= 0) {
out.write(buf, 0, n)
n = in.read(buf)
}
}
finally {
out.flush
}
} catch {
case _:FileNotFoundException =>
System.err.printf("Permission Denied: %s", path)
case _:SecurityException =>
System.err.printf("Permission Denied: %s", path)
}
} catch {
case _: FileNotFoundException =>
System.err.printf("File Not Found: %s", path ++ ".gz")
case _: SecurityException =>
System.err.printf("Permission Denied: %s", path ++ ".gz")
}
}
def main(filenames: Array[String]) {
if (filenames.length == 0) {
System.err.println("Usage: scala gunzip gz-file...")
System.exit(1)
}
else {
val eitherNames =
for (name <- filenames)
yield if (name.endsWith(".gz")) {
val noExtension = name.drop(3)
val src = new File(name)
val dst = new File(noExtension)
if (! src.exists)
Left(new FileNotFoundException(name))
else if (dst.exists)
Left(new IOException("file already exists: " + noExtension))
else
Right(noExtension)
} else {
Left(new IOException("file is not gzip: " + name))
}
if (eitherNames.forall {_.isRight}) {
for (Right(noExt) <- eitherNames) {
gunzip(noExt)
}
}
else {
for (Left(e) <- eitherNames)
throw e
}
}
}
}
@califano
Copy link

Hi thanks for your example. I would like to point out that you should add after the line 54 out.close if not the compressed file is not written properly.

@SIVAPRAKASHGITHUB
Copy link

Very Good Example. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment