Skip to content

Instantly share code, notes, and snippets.

@joshlemer
Created November 11, 2016 22:11
Show Gist options
  • Save joshlemer/70a4fa356807a1626a2852680ee60021 to your computer and use it in GitHub Desktop.
Save joshlemer/70a4fa356807a1626a2852680ee60021 to your computer and use it in GitHub Desktop.
import java.nio.file.{Path, Paths}
import better.files.File.OpenOptions
import org.apache.commons.compress
import better.files._
import org.apache.commons.compress.archivers.ArchiveStreamFactory
import org.apache.commons.compress.archivers.tar.{TarArchiveEntry, TarArchiveInputStream}
object TarFile {
val mb = 1024 * 1024
val batchSize = 512 * mb
}
sealed trait TarEntry
class TarFileEntry extends TarEntry
class TarDirectoryEntry extends TarEntry
class GzipFile(file: File) {
}
class TarFile(file: File, gzipped: Boolean = false) {
def inputStream = {
val is = file.newInputStream
val gz = if(gzipped) is.gzipped else is
new TarArchiveInputStream(gz)
}
def nameWithoutExtension = {
val suffix = if(gzipped) ".tar.gz" else ".gz"
file.name.stripSuffix(suffix)
}
def unTar(): File = unTarTo(nameWithoutExtension.toFile)
def unTarTo(destination: File): File = {
destination.createDirectory()
val is = inputStream
var entry = is.getNextTarEntry
while (entry != null) {
val newFile = destination / entry.getName
if (entry.isFile) {
newFile.createIfNotExists(asDirectory = false, createParents = true)
val arr = new Array[Byte](entry.getSize.toInt)
is.read(arr, 0, entry.getSize.toInt)
newFile.append(arr)(OpenOptions.append)
} else {
newFile.createIfNotExists(asDirectory = true, createParents = true)
}
entry = is.getNextTarEntry
}
destination
}
// var entry = inputStream.getNextTarEntry
// while (entry != null) {
// if(!entry.isDirectory) {
// var size = 0
// while(size < entry.getSize) {
// print(inputStream.read().toChar)
// size += 1
// }
// }
//
// entry = inputStream.getNextTarEntry
// }
}
object CompressFiles extends App {
implicit class TarFileOps(string: String) {
def toTarFile = toTarFile(false)
def toTarFile(gzipped: Boolean = false) = new TarFile(string.toFile, gzipped)
}
// val file = "badge_rules.tar.gz".toFile.newInputStream.gzipped
// val input = new ArchiveStreamFactory().createArchiveInputStream(file)
// new TarFile("badge_rules.tar.gz".toFile).unzipTo("badge_rules".toFile)//.unzipTo("badge_rules".toFile)
"badge_rules.tar.gz".toTarFile(gzipped = true)
// val arch = new TarArchiveInputStream(file)//.getNextTarEntry().getFile()
// val arch = new TarArchiveInputStream(file).getNextTarEntry()//.getFile().toScala.lines
// def toFile(entry: TarArchiveEntry): File = {
// if (entry.isFile) entry.getFile.toScala
// else {
// entry.getName.t
// entry.getDirectoryEntries().map(_.getFile.toScala)
// entry.getDirectoryEntries()
// }
// }
// def printLines(entry: TarArchiveEntry, prefix: String = ""): String = {
// prefix + entry.getName + "\n" +
// (if (entry.isFile) entry.getFile.toScala.lines.toList
// else entry.getDirectoryEntries.toList.flatMap(e => printLines(e, prefix + " ").lines.toList)).mkString(prefix + "\n")
// }
val directory = {
// for (_ <- 1 to 100) println(Option(is.getNextTarEntry()).map(_.getName))
// val it = new Iterator[TarArchiveEntry] {
// var current = is.getNextTarEntry
// def next() = {
// val temp = current
// current = is.getNextTarEntry
// temp
// }
// def hasNext() = current != null
// }
//
// val files = new Iterator[File] {
// var current = is.getNextTarEntry
// def next() = {
// val temp = current
// current = is.getNextTarEntry
// temp
// }
// def hasNext() = current != null
// }
// it.toList.filter(_.isFile).map{ f =>
//
// val content = new Array[Byte](f.getSize.toInt)
// new TarArchiveInputStream(file).read(content, )
// }
// println(Option(is.getNextTarEntry()).map(_.getName))
}
println(directory)
// input.
// file.lines.foreach(println)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment