Skip to content

Instantly share code, notes, and snippets.

@niteria
Created October 5, 2011 15:59
Show Gist options
  • Save niteria/1264812 to your computer and use it in GitHub Desktop.
Save niteria/1264812 to your computer and use it in GitHub Desktop.
import java.io.File
/** A wrapper around file, allowing iteration either on direct children
or on directory tree */
class RichFile(file: File) {
def children = new Iterable[File] {
def elements =
if (file.isDirectory) file.listFiles.elements else Iterator.empty;
}
def andTree : Iterable[File] = (
Seq.single(file)
++ children.flatMap(child => new RichFile(child).andTree))
}
/** implicitely enrich java.io.File with methods of RichFile */
object RichFile {
implicit def toRichFile(file: File) = new RichFile(file)
}
import RichFile.toRichFile
// ============= //
import java.util.zip.CRC32
import java.io.FileInputStream
def crc32(file: File): Long = {
val fs = new FileInputStream(file)
var ch = fs.read
var crc = new CRC32();
while (ch != -1) {
crc.update(ch)
ch = fs.read
}
crc.getValue
}
if (args.length < 2) {
println("supply 2 params")
exit
}
val first = args(0)
val second = args(1)
println("comparing " + first + " with " + second)
def genList(f: File) =
(f.andTree filter (x => !x.isDirectory) map {
(x:File) => (x, crc32(x))
})//.toList()//.sortBy(_._2) //((a, b) => a._2 < b._2)
val fst = genList(new File(first)).toList
val snd = genList(new File(second))
println(fst)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment