Groovy 에서도 100MB 넘는 파일 중에서 중복된 파일을 찾는 프로그램을 만들어보았다.
모든 언어를 공부하면서 만들어보는 것인데, MD5 Checksum 이 같으면 중복된 파일로 인식하고, 카운트하여 정렬하고 출력하도록 하였다.
파일에 대한 MD5 Checksum 기능이 없어서 직접 구현되었고, 이 부분을 제외하면 Ruby 와 거의 비슷하다.
// -----------------------------------------------------------------------------
// 100MB 이상의 파일중에서 중복된 파일 찾기
// -----------------------------------------------------------------------------
import java.security.MessageDigest
final TARGET_DIR = "C:\\"
final LIMIT_SIZE = 100000000
def md5sum(final file) {
MessageDigest digest = MessageDigest.getInstance("MD5")
file.withInputStream() { is ->
byte[] buffer = new byte[8192]
int read = 0
while( (read = is.read(buffer)) > 0) {
digest.update(buffer, 0, read);
}
}
byte[] md5 = digest.digest()
BigInteger bigInt = new BigInteger(1, md5)
return bigInt.toString(16).padLeft(32, '0')
}
def file_list = []
def distinct_list = [:]
def startDate = new Date().format('yyyy/MM/dd HH:mm:ss')
new File(TARGET_DIR).eachFileRecurse { file ->
if( file.size() > LIMIT_SIZE ) {
def md5 = null
try {
md5 = md5sum(file)
} catch(ex) {
md5 = null
}
if (md5 != null) {
if(distinct_list[md5] == null)
distinct_list[md5] = 1
else
distinct_list[md5] += 1
file_list << (md5+"|"+file)
}
}
}
distinct_list = distinct_list.sort { a, b -> b.value <=> a.value }
distinct_list.each { md5, cnt ->
if( cnt > 1) {
println "\n[ $md5 ]"
file_list.each { file ->
def (md5_2, filename) = file.split(/\|/)
if(md5 == md5_2) { println filename }
}
}
}
def endDate = new Date().format('yyyy/MM/dd HH:mm:ss')
println "\n\n\n"
println "================================================================================"
println "Start Time : $startDate"
println "End Time : $endDate"
println "================================================================================"