geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jinmeil...@apache.org
Subject [10/19] geode git commit: GEODE-2430: Refactor ZipUtils
Date Tue, 07 Feb 2017 19:03:31 GMT
GEODE-2430: Refactor ZipUtils


Project: http://git-wip-us.apache.org/repos/asf/geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/50aebcc8
Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/50aebcc8
Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/50aebcc8

Branch: refs/heads/feature/GEODE-2267
Commit: 50aebcc859da9c2456ef142ff7ec4c1620c11900
Parents: 9a77486
Author: Jared Stewart <jstewart@pivotal.io>
Authored: Fri Feb 3 12:53:38 2017 -0800
Committer: Kirk Lund <klund@apache.org>
Committed: Mon Feb 6 15:21:47 2017 -0800

----------------------------------------------------------------------
 ...xportImportClusterConfigurationCommands.java |  3 +-
 .../internal/configuration/utils/ZipUtils.java  | 62 ++++++++------------
 .../configuration/ZipUtilsJUnitTest.java        |  2 +-
 3 files changed, 27 insertions(+), 40 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geode/blob/50aebcc8/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/ExportImportClusterConfigurationCommands.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/ExportImportClusterConfigurationCommands.java
b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/ExportImportClusterConfigurationCommands.java
index 1ee22db..f822c67 100644
--- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/ExportImportClusterConfigurationCommands.java
+++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/ExportImportClusterConfigurationCommands.java
@@ -98,8 +98,7 @@ public class ExportImportClusterConfigurationCommands extends AbstractCommandsSu
       for (Configuration config : sc.getEntireConfiguration().values()) {
         sc.writeConfigToFile(config);
       }
-
-      ZipUtils.zip(sc.getSharedConfigurationDirPath(), zipFile.getCanonicalPath());
+      ZipUtils.zipDirectory(sc.getSharedConfigurationDirPath(), zipFile.getCanonicalPath());
 
       InfoResultData infoData = ResultBuilder.createInfoResultData();
       byte[] byteData = FileUtils.readFileToByteArray(zipFile);

http://git-wip-us.apache.org/repos/asf/geode/blob/50aebcc8/geode-core/src/main/java/org/apache/geode/management/internal/configuration/utils/ZipUtils.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/configuration/utils/ZipUtils.java
b/geode-core/src/main/java/org/apache/geode/management/internal/configuration/utils/ZipUtils.java
index 8944194..81161d6 100644
--- a/geode-core/src/main/java/org/apache/geode/management/internal/configuration/utils/ZipUtils.java
+++ b/geode-core/src/main/java/org/apache/geode/management/internal/configuration/utils/ZipUtils.java
@@ -22,6 +22,9 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URI;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.Enumeration;
 import java.util.Stack;
 import java.util.zip.ZipEntry;
@@ -32,51 +35,36 @@ import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
 
 /****
- * Utilities class to zip/unzip folder
+ * Utilities class to zip/unzip directory
  *
  */
 public class ZipUtils {
 
-  public static void zip(String sourceFolderPath, String targetFilePath) throws Exception
{
-    File sourceFolder = new File(sourceFolderPath);
-    File targetFile = new File(targetFilePath);
-
-    if (!sourceFolder.exists()) {
-      throw new Exception("Source folder does not exist");
-    }
-
-    FileOutputStream fos = new FileOutputStream(targetFile);
-    ZipOutputStream zos = new ZipOutputStream(fos);
-    URI baseURI = sourceFolder.toURI();
-
-    Stack<File> fileStack = new Stack<File>();
-    fileStack.push(sourceFolder);
-
-    while (!fileStack.isEmpty()) {
-      File directory = fileStack.pop();
-
-      for (File child : directory.listFiles()) {
-        String name = baseURI.relativize(child.toURI()).getPath();
-
-        if (child.isDirectory()) {
-          fileStack.push(child);
-          zos.putNextEntry(new ZipEntry(name));
-        } else {
-          if (!name.endsWith("zip")) {
-            ZipEntry zipEntry = new ZipEntry(name);
-            zos.putNextEntry(zipEntry);
-            InputStream in = new FileInputStream(child);
-            IOUtils.copy(in, zos);
-            IOUtils.closeQuietly(in);
-          }
+  public static void zipDirectory(Path sourceDirectory, Path targetFile) throws IOException
{
+    Path p = Files.createFile(targetFile);
+    try (ZipOutputStream zs = new ZipOutputStream(Files.newOutputStream(p))) {
+      Files.walk(sourceDirectory).filter(path -> !Files.isDirectory(path)).forEach(path
-> {
+        ZipEntry zipEntry = new ZipEntry(sourceDirectory.relativize(path).toString());
+        try {
+          zs.putNextEntry(zipEntry);
+          zs.write(Files.readAllBytes(path));
+          zs.closeEntry();
+        } catch (Exception e) {
+          throw new RuntimeException("Unable to write zip file", e);
         }
-      }
+      });
     }
-    IOUtils.closeQuietly(zos);
   }
 
+  public static void zipDirectory(String sourceDirectoryPath, String targetFilePath)
+      throws IOException {
+    Path sourceDirectory = Paths.get(sourceDirectoryPath);
+    Path targetFile = Paths.get(targetFilePath);
+
+    zipDirectory(sourceDirectory, targetFile);
+  }
 
-  public static void unzip(String zipFilePath, String outputFolderPath) throws IOException
{
+  public static void unzip(String zipFilePath, String outputDirectoryPath) throws IOException
{
     ZipFile zipFile = new ZipFile(zipFilePath);
     @SuppressWarnings("unchecked")
     Enumeration<ZipEntry> zipEntries = (Enumeration<ZipEntry>) zipFile.entries();
@@ -84,7 +72,7 @@ public class ZipUtils {
     try {
       while (zipEntries.hasMoreElements()) {
         ZipEntry zipEntry = zipEntries.nextElement();
-        String fileName = outputFolderPath + File.separator + zipEntry.getName();
+        String fileName = outputDirectoryPath + File.separator + zipEntry.getName();
 
         if (zipEntry.isDirectory()) {
           FileUtils.forceMkdir(new File(fileName));

http://git-wip-us.apache.org/repos/asf/geode/blob/50aebcc8/geode-core/src/test/java/org/apache/geode/management/internal/configuration/ZipUtilsJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/configuration/ZipUtilsJUnitTest.java
b/geode-core/src/test/java/org/apache/geode/management/internal/configuration/ZipUtilsJUnitTest.java
index 4e34dd5..1791574 100755
--- a/geode-core/src/test/java/org/apache/geode/management/internal/configuration/ZipUtilsJUnitTest.java
+++ b/geode-core/src/test/java/org/apache/geode/management/internal/configuration/ZipUtilsJUnitTest.java
@@ -75,7 +75,7 @@ public class ZipUtilsJUnitTest {
     assertFalse(zipFile.exists());
     assertFalse(zipFile.isFile());
 
-    ZipUtils.zip(sourceFolder.getCanonicalPath(), zipFile.getCanonicalPath());
+    ZipUtils.zipDirectory(sourceFolder.getCanonicalPath(), zipFile.getCanonicalPath());
     assertTrue(zipFile.exists());
     assertTrue(zipFile.isFile());
 


Mime
View raw message