From 9b75bda9eabab609dfce6bccc27d61a35dcb86f1 Mon Sep 17 00:00:00 2001 From: Shibly Meeran Date: Tue, 15 Jan 2019 20:19:48 +0530 Subject: [PATCH] adding zip util to selectively unzip files --- README.md | 2 +- pom.xml | 15 +------ src/me/shib/java/lib/utils/FileUtils.java | 44 +++++++++++++++++++ .../shib/java/lib/utils/LocalFileCache.java | 2 +- 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index f02ea08..01472ae 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,6 @@ Add to your `pom.xml` me.shib.java.lib utils - 0.0.1 + 0.0.2 ``` diff --git a/pom.xml b/pom.xml index 9fb58d3..ed273a7 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ me.shib.java.lib utils - 0.0.1 + 0.0.2 Utils Commonly used custom utils https://github.com/shibme/utils @@ -75,19 +75,6 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.10.4 - - - attach-javadocs - - jar - - - - org.apache.maven.plugins maven-source-plugin diff --git a/src/me/shib/java/lib/utils/FileUtils.java b/src/me/shib/java/lib/utils/FileUtils.java index 799c1e7..a1adf42 100644 --- a/src/me/shib/java/lib/utils/FileUtils.java +++ b/src/me/shib/java/lib/utils/FileUtils.java @@ -4,6 +4,8 @@ import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.ArrayList; +import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -43,6 +45,48 @@ public static boolean unZip(File zipFile, File outputDirectory) { } } + public static boolean unZip(File zipFile, String fileToExtract, File outputDirectory) { + List filesToExtract = new ArrayList<>(); + filesToExtract.add(fileToExtract); + return unZip(zipFile, filesToExtract, outputDirectory); + } + + public static boolean unZip(File zipFile, List filesToExtract, File outputDirectory) { + byte[] buffer = new byte[1024]; + try { + if (!outputDirectory.exists()) { + outputDirectory.mkdir(); + } + ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); + ZipEntry ze = zis.getNextEntry(); + while (ze != null) { + String fileName = ze.getName(); + File newFile = new File(outputDirectory.getAbsolutePath() + File.separator + fileName); + if (filesToExtract.contains(newFile.getName())) { + System.out.println("Extracting : " + newFile.getAbsoluteFile()); + if (ze.isDirectory()) { + newFile.mkdirs(); + } else { + new File(newFile.getParent()).mkdirs(); + FileOutputStream fos = new FileOutputStream(newFile); + int len; + while ((len = zis.read(buffer)) > 0) { + fos.write(buffer, 0, len); + } + fos.close(); + } + } + ze = zis.getNextEntry(); + } + zis.closeEntry(); + zis.close(); + System.out.println("Extracted " + zipFile.getName() + " successfully!"); + return true; + } catch (IOException ex) { + return false; + } + } + private static String calculateChecksum(File file, String hashType) { InputStream is = null; try { diff --git a/src/me/shib/java/lib/utils/LocalFileCache.java b/src/me/shib/java/lib/utils/LocalFileCache.java index d88f3e2..4b255df 100644 --- a/src/me/shib/java/lib/utils/LocalFileCache.java +++ b/src/me/shib/java/lib/utils/LocalFileCache.java @@ -104,7 +104,7 @@ public String[] getKeys(String type) { if (keyDir.exists()) { String[] encodedKeys = keyDir.list(); ArrayList keyList = new ArrayList<>(); - if(encodedKeys != null) { + if (encodedKeys != null) { for (String enKey : encodedKeys) { String key = decodeKeyToName(enKey.replace(".json", "")); if (key != null) {