Skip to content

Instantly share code, notes, and snippets.

@talenguyen
Created April 24, 2019 02:57
Show Gist options
  • Save talenguyen/98ca7f68101b049039c4295b8567a7d6 to your computer and use it in GitHub Desktop.
Save talenguyen/98ca7f68101b049039c4295b8567a7d6 to your computer and use it in GitHub Desktop.
File utilities that support generate hash, unzip, etc.
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.Okio;
public final class FileUtils {
private static final char[] hexCode = "0123456789abcdef".toCharArray();
private FileUtils() {
//no instance
}
public static String sha256(File file) throws IOException, NoSuchAlgorithmException {
return computeChecksum("SHA-256", file);
}
private static String computeChecksum(String algorithm, File file) throws
NoSuchAlgorithmException, IOException {
BufferedSource bufferedSource = null;
try {
bufferedSource = Okio.buffer(Okio.source(file));
//Create byte array to read data in chunks
byte[] buffer = new byte[2048];
int count;
final MessageDigest digest = MessageDigest.getInstance(algorithm);
//Read file data and update in message digest
while ((count = bufferedSource.read(buffer)) != -1) {
digest.update(buffer, 0, count);
}
return printHexBinary(digest.digest());
} finally {
closeQuietly(bufferedSource);
}
}
private static String printHexBinary(byte[] data) {
StringBuilder r = new StringBuilder(data.length * 2);
for (byte b : data) {
r.append(hexCode[(b >> 4) & 0xF]);
r.append(hexCode[(b & 0xF)]);
}
return r.toString();
}
@SuppressWarnings("EmptyCatchBlock") static void closeQuietly(Closeable closeable) {
if (closeable == null) {
return;
}
try {
closeable.close();
} catch (IOException __) {
}
}
public static String hash(String s) throws NoSuchAlgorithmException {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
messageDigest.update(s.getBytes());
return printHexBinary(messageDigest.digest());
}
/**
* Extracts a zip file specified by the zipFilePath to a directory specified by
* destDirectory (will be created if does not exists)
*
* @throws IOException throw during i/o operation
*/
@SuppressWarnings("ResultOfMethodCallIgnored") public static void unzip(
String zipFilePath,
String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn = null;
try {
zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
// iterates over entries in the zip file
while (entry != null) {
try {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
extractFile(zipIn, filePath);
} else {
// if the entry is a directory, make2h the directory
File dir = new File(filePath);
dir.mkdir();
}
} finally {
zipIn.closeEntry();
}
entry = zipIn.getNextEntry();
}
} finally {
closeQuietly(zipIn);
}
}
/**
* Extracts a zip entry (file entry)
*
* @throws IOException throw during i/o operation
*/
private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
BufferedSink sink = null;
try {
final BufferedSource source = Okio.buffer(Okio.source(zipIn));
sink = Okio.buffer(Okio.sink(new File(filePath)));
sink.writeAll(source);
sink.flush();
} finally {
closeQuietly(sink);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment