Last active
February 8, 2018 06:04
-
-
Save dineshr93/ae2daa4e58faf6aef22d1b2334636e02 to your computer and use it in GitHub Desktop.
calculate MD5 files and folders using commons-codec-1.11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.codec.digest.DigestUtils; | |
import java.io.*; | |
import java.nio.file.Files; | |
import java.util.Arrays; | |
import java.util.Comparator; | |
import java.util.List; | |
import java.util.Vector; | |
//https://stackoverflow.com/a/46899517/2018343 | |
public class Hashing | |
{ | |
public static String hashDirectory(String directoryPath, boolean includeHiddenFiles) throws IOException | |
{ | |
File directory = new File(directoryPath); | |
if (!directory.isDirectory()) | |
{ | |
throw new IllegalArgumentException("Not a directory"); | |
} | |
Vector<FileInputStream> fileStreams = new Vector<>(); | |
collectFiles(directory, fileStreams, includeHiddenFiles); | |
try (SequenceInputStream sequenceInputStream = new SequenceInputStream(fileStreams.elements())) | |
{ | |
return DigestUtils.md5Hex(sequenceInputStream); | |
} | |
} | |
private static void collectFiles(File directory, | |
List<FileInputStream> fileInputStreams, | |
boolean includeHiddenFiles) throws IOException | |
{ | |
File[] files = directory.listFiles(); | |
if (files != null) | |
{ | |
Arrays.sort(files, Comparator.comparing(File::getName)); | |
for (File file : files) | |
{ | |
if (includeHiddenFiles || !Files.isHidden(file.toPath())) | |
{ | |
if (file.isDirectory()) | |
{ | |
collectFiles(file, fileInputStreams, includeHiddenFiles); | |
} else | |
{ | |
fileInputStreams.add(new FileInputStream(file)); | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.text.DecimalFormat; | |
import javax.xml.bind.DatatypeConverter; | |
//https://stackoverflow.com/a/26231444 | |
public class MD5 { | |
public static void main(String[] args) throws NoSuchAlgorithmException, IOException { | |
//byte[] b = Files.readAllBytes(Paths.get("D:\\ztestfile\\PDMDLL.dll"));commons-codec-1.11.jar | |
byte[] b = Files.readAllBytes(Paths.get("D:\\ztestfile\\commons-codec-1.11.jar")); | |
byte[] hash = MessageDigest.getInstance("MD5").digest(b); | |
System.out.println(DatatypeConverter.printHexBinary(hash)); | |
FileInputStream fis = new FileInputStream(new File("D:\\ztestfile\\commons-codec-1.11.jar")); | |
String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis); | |
fis.close(); | |
System.out.println(md5); | |
String hashval=Hashing.hashDirectory("D:\\ztestfile\\commons-codec-1.11", true);//1st input is path of folder, 2nd input is include hidden files or not | |
System.out.println(hashval); | |
/* File f = new File("C:\\Users\\rad9kor\\Desktop\\FileListerExcel.zip"); | |
DecimalFormat df2 = new DecimalFormat(".##"); | |
double bytes = f.length(); | |
double kb = (bytes / 1024); | |
double mb = (kb / 1024); | |
double gigabytes = (mb / 1024); | |
double terabytes = (gigabytes / 1024); | |
double petabytes = (terabytes / 1024); | |
double exabytes = (petabytes / 1024); | |
double zettabytes = (exabytes / 1024); | |
double yottabytes = (zettabytes / 1024); | |
System.out.println("bytes : " + bytes); | |
System.out.println("kilobytes : " + kb); | |
System.out.println("megabytes : " + df2.format(mb)); | |
System.out.println("gigabytes : " + gigabytes); | |
System.out.println("terabytes : " + terabytes); | |
System.out.println("petabytes : " + petabytes); | |
System.out.println("exabytes : " + exabytes); | |
System.out.println("zettabytes : " + zettabytes); | |
System.out.println("yottabytes : " + yottabytes);*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment