Reading a .bz2 file

I have a directory full of files which have been compressed and now have a .cnf.bz2 extension. Of course I could uncompress each into in the file system into a .cnf file (e.g., into /tmp), before reading it into my scale program. However, is there a more streamline way. I.e., someway to open the file, and effectively read lines from the uncompressed file, without having to physically create the file on disk?

Search the web for “scala bz2”. If this comes up empty, search for “java bz2” instead. Likely first match: commons-compress. Search for “commons-compress maven”, add to your build.sbt:

libraryDependencies += "org.apache.commons" % "commons-compress" % "1.18"

The uncompress functionality is implemented via an InputStream adapter, so you can just throw it at a Source, for example:

import java.nio.file._
import org.apache.commons.compress.compressors.bzip2._
import resource._
// ...
val bz2Path = Paths.get(bz2PathStr)
for(src <- managed(Source.fromInputStream(new BZip2CompressorInputStream(Files.newInputStream(bz2Path))))) {
1 Like