{"action":"created","comment":{"url":"https://api.github.com/repos/metamx/java-util/pulls/comments/26806583","id":26806583,"diff_hunk":"@@ -0,0 +1,388 @@\n+/*\n+ * Copyright 2015 Metamarkets Group Inc.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ *     http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+package com.metamx.common;\n+\n+import com.google.common.base.Predicate;\n+import com.google.common.base.Predicates;\n+import com.google.common.base.Strings;\n+import com.google.common.base.Throwables;\n+import com.google.common.io.ByteSink;\n+import com.google.common.io.ByteSource;\n+import com.google.common.io.ByteStreams;\n+import com.google.common.io.Files;\n+import com.metamx.common.guava.CloseQuietly;\n+import com.metamx.common.logger.Logger;\n+\n+import java.io.BufferedInputStream;\n+import java.io.File;\n+import java.io.FileOutputStream;\n+import java.io.FilterInputStream;\n+import java.io.IOException;\n+import java.io.InputStream;\n+import java.io.OutputStream;\n+import java.util.ArrayList;\n+import java.util.Enumeration;\n+import java.util.concurrent.Callable;\n+import java.util.zip.GZIPInputStream;\n+import java.util.zip.GZIPOutputStream;\n+import java.util.zip.ZipEntry;\n+import java.util.zip.ZipFile;\n+import java.util.zip.ZipInputStream;\n+import java.util.zip.ZipOutputStream;\n+\n+public class CompressionUtils\n+{\n+  private static final Logger log = new Logger(CompressionUtils.class);\n+  private static final int DEFAULT_RETRY_COUNT = 3;\n+\n+  public static final String GZ_SUFFIX = \".gz\";\n+  public static final String ZIP_SUFFIX = \".zip\";\n+\n+  public static long zip(File directory, File outputZipFile) throws IOException\n+  {\n+    if (!isZip(outputZipFile.getName())) {\n+      log.warn(\"No .zip suffix[%s], putting files from [%s] into it anyway.\", outputZipFile, directory);\n+    }\n+\n+    try (final FileOutputStream out = new FileOutputStream(outputZipFile)) {\n+      return zip(directory, out);\n+    }\n+  }\n+\n+  public static long zip(File directory, OutputStream out) throws IOException\n+  {\n+    if (!directory.isDirectory()) {\n+      throw new IOException(String.format(\"directory[%s] is not a directory\", directory));\n+    }\n+    final File[] files = directory.listFiles();\n+\n+    long totalSize = 0;\n+    try (final ZipOutputStream zipOut = new ZipOutputStream(out)) {\n+      for (File file : files) {\n+        log.info(\"Adding file[%s] with size[%,d].  Total size so far[%,d]\", file, file.length(), totalSize);\n+        if (file.length() >= Integer.MAX_VALUE) {\n+          zipOut.finish();\n+          throw new IOException(String.format(\"file[%s] too large [%,d]\", file, file.length()));\n+        }\n+        zipOut.putNextEntry(new ZipEntry(file.getName()));\n+        totalSize += Files.asByteSource(file).copyTo(zipOut);\n+      }\n+      zipOut.closeEntry();\n+    }\n+\n+    return totalSize;\n+  }\n+\n+  public static FileUtils.FileCopyResult unzip(\n+      final ByteSource byteSource,\n+      final File outDir,\n+      final Predicate<Throwable> shouldRetry,\n+      boolean cacheLocally\n+  ) throws IOException\n+  {\n+    if (!cacheLocally) {\n+      try {\n+        return RetryUtils.retry(\n+            new Callable<FileUtils.FileCopyResult>()\n+            {\n+              @Override\n+              public FileUtils.FileCopyResult call() throws Exception\n+              {\n+                return unzip(byteSource.openStream(), outDir);\n+              }\n+            },\n+            shouldRetry,\n+            DEFAULT_RETRY_COUNT\n+        );\n+      }\n+      catch (Exception e) {\n+        throw Throwables.propagate(e);\n+      }\n+    }\n+    final File tmpFile = File.createTempFile(\"compressionUtilZipCache\", ZIP_SUFFIX);","path":"src/main/java/com/metamx/common/CompressionUtils.java","position":null,"original_position":115,"commit_id":"8a22f18a4a2d08db7ca6f9c1adc3db0240c14a7e","original_commit_id":"c993a85a24611091184a2b65565097ae91268c0a","user":{"login":"drcrallen","id":8213081,"avatar_url":"https://avatars.githubusercontent.com/u/8213081?v=3","gravatar_id":"","url":"https://api.github.com/users/drcrallen","html_url":"https://github.com/drcrallen","followers_url":"https://api.github.com/users/drcrallen/followers","following_url":"https://api.github.com/users/drcrallen/following{/other_user}","gists_url":"https://api.github.com/users/drcrallen/gists{/gist_id}","starred_url":"https://api.github.com/users/drcrallen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drcrallen/subscriptions","organizations_url":"https://api.github.com/users/drcrallen/orgs","repos_url":"https://api.github.com/users/drcrallen/repos","events_url":"https://api.github.com/users/drcrallen/events{/privacy}","received_events_url":"https://api.github.com/users/drcrallen/received_events","type":"User","site_admin":false},"body":"fixed","created_at":"2015-03-19T23:02:55Z","updated_at":"2015-03-19T23:02:55Z","html_url":"https://github.com/metamx/java-util/pull/22#discussion_r26806583","pull_request_url":"https://api.github.com/repos/metamx/java-util/pulls/22","_links":{"self":{"href":"https://api.github.com/repos/metamx/java-util/pulls/comments/26806583"},"html":{"href":"https://github.com/metamx/java-util/pull/22#discussion_r26806583"},"pull_request":{"href":"https://api.github.com/repos/metamx/java-util/pulls/22"}}},"pull_request":{"url":"https://api.github.com/repos/metamx/java-util/pulls/22","id":30690312,"html_url":"https://github.com/metamx/java-util/pull/22","diff_url":"https://github.com/metamx/java-util/pull/22.diff","patch_url":"https://github.com/metamx/java-util/pull/22.patch","issue_url":"https://api.github.com/repos/metamx/java-util/issues/22","number":22,"state":"open","locked":false,"title":"Add Compression and File utilities for Zip and GZ handling","user":{"login":"drcrallen","id":8213081,"avatar_url":"https://avatars.githubusercontent.com/u/8213081?v=3","gravatar_id":"","url":"https://api.github.com/users/drcrallen","html_url":"https://github.com/drcrallen","followers_url":"https://api.github.com/users/drcrallen/followers","following_url":"https://api.github.com/users/drcrallen/following{/other_user}","gists_url":"https://api.github.com/users/drcrallen/gists{/gist_id}","starred_url":"https://api.github.com/users/drcrallen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/drcrallen/subscriptions","organizations_url":"https://api.github.com/users/drcrallen/orgs","repos_url":"https://api.github.com/users/drcrallen/repos","events_url":"https://api.github.com/users/drcrallen/events{/privacy}","received_events_url":"https://api.github.com/users/drcrallen/received_events","type":"User","site_admin":false},"body":"I moved these from druid-api and added / expanded the functionality.","created_at":"2015-03-06T23:50:45Z","updated_at":"2015-03-19T23:02:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":"70a8c3d93d96ce49c1f2e5da80527e2d13d6e214","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/metamx/java-util/pulls/22/commits","review_comments_url":"https://api.github.com/repos/metamx/java-util/pulls/22/comments","review_comment_url":"https://api.github.com/repos/metamx/java-util/pulls/comments{/number}","comments_url":"https://api.github.com/repos/metamx/java-util/issues/22/comments","statuses_url":"https://api.github.com/repos/metamx/java-util/statuses/8a22f18a4a2d08db7ca6f9c1adc3db0240c14a7e","head":{"label":"metamx:CompressionFileUtils","ref":"CompressionFileUtils","sha":"8a22f18a4a2d08db7ca6f9c1adc3db0240c14a7e","user":{"login":"metamx","id":299329,"avatar_url":"https://avatars.githubusercontent.com/u/299329?v=3","gravatar_id":"","url":"https://api.github.com/users/metamx","html_url":"https://github.com/metamx","followers_url":"https://api.github.com/users/metamx/followers","following_url":"https://api.github.com/users/metamx/following{/other_user}","gists_url":"https://api.github.com/users/metamx/gists{/gist_id}","starred_url":"https://api.github.com/users/metamx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/metamx/subscriptions","organizations_url":"https://api.github.com/users/metamx/orgs","repos_url":"https://api.github.com/users/metamx/repos","events_url":"https://api.github.com/users/metamx/events{/privacy}","received_events_url":"https://api.github.com/users/metamx/received_events","type":"Organization","site_admin":false},"repo":{"id":2622329,"name":"java-util","full_name":"metamx/java-util","owner":{"login":"metamx","id":299329,"avatar_url":"https://avatars.githubusercontent.com/u/299329?v=3","gravatar_id":"","url":"https://api.github.com/users/metamx","html_url":"https://github.com/metamx","followers_url":"https://api.github.com/users/metamx/followers","following_url":"https://api.github.com/users/metamx/following{/other_user}","gists_url":"https://api.github.com/users/metamx/gists{/gist_id}","starred_url":"https://api.github.com/users/metamx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/metamx/subscriptions","organizations_url":"https://api.github.com/users/metamx/orgs","repos_url":"https://api.github.com/users/metamx/repos","events_url":"https://api.github.com/users/metamx/events{/privacy}","received_events_url":"https://api.github.com/users/metamx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/metamx/java-util","description":"Utility code for java and jvm-based languages","fork":false,"url":"https://api.github.com/repos/metamx/java-util","forks_url":"https://api.github.com/repos/metamx/java-util/forks","keys_url":"https://api.github.com/repos/metamx/java-util/keys{/key_id}","collaborators_url":"https://api.github.com/repos/metamx/java-util/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/metamx/java-util/teams","hooks_url":"https://api.github.com/repos/metamx/java-util/hooks","issue_events_url":"https://api.github.com/repos/metamx/java-util/issues/events{/number}","events_url":"https://api.github.com/repos/metamx/java-util/events","assignees_url":"https://api.github.com/repos/metamx/java-util/assignees{/user}","branches_url":"https://api.github.com/repos/metamx/java-util/branches{/branch}","tags_url":"https://api.github.com/repos/metamx/java-util/tags","blobs_url":"https://api.github.com/repos/metamx/java-util/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/metamx/java-util/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/metamx/java-util/git/refs{/sha}","trees_url":"https://api.github.com/repos/metamx/java-util/git/trees{/sha}","statuses_url":"https://api.github.com/repos/metamx/java-util/statuses/{sha}","languages_url":"https://api.github.com/repos/metamx/java-util/languages","stargazers_url":"https://api.github.com/repos/metamx/java-util/stargazers","contributors_url":"https://api.github.com/repos/metamx/java-util/contributors","subscribers_url":"https://api.github.com/repos/metamx/java-util/subscribers","subscription_url":"https://api.github.com/repos/metamx/java-util/subscription","commits_url":"https://api.github.com/repos/metamx/java-util/commits{/sha}","git_commits_url":"https://api.github.com/repos/metamx/java-util/git/commits{/sha}","comments_url":"https://api.github.com/repos/metamx/java-util/comments{/number}","issue_comment_url":"https://api.github.com/repos/metamx/java-util/issues/comments{/number}","contents_url":"https://api.github.com/repos/metamx/java-util/contents/{+path}","compare_url":"https://api.github.com/repos/metamx/java-util/compare/{base}...{head}","merges_url":"https://api.github.com/repos/metamx/java-util/merges","archive_url":"https://api.github.com/repos/metamx/java-util/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/metamx/java-util/downloads","issues_url":"https://api.github.com/repos/metamx/java-util/issues{/number}","pulls_url":"https://api.github.com/repos/metamx/java-util/pulls{/number}","milestones_url":"https://api.github.com/repos/metamx/java-util/milestones{/number}","notifications_url":"https://api.github.com/repos/metamx/java-util/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/metamx/java-util/labels{/name}","releases_url":"https://api.github.com/repos/metamx/java-util/releases{/id}","created_at":"2011-10-21T18:41:10Z","updated_at":"2015-03-18T02:04:41Z","pushed_at":"2015-03-19T23:02:44Z","git_url":"git://github.com/metamx/java-util.git","ssh_url":"git@github.com:metamx/java-util.git","clone_url":"https://github.com/metamx/java-util.git","svn_url":"https://github.com/metamx/java-util","homepage":"","size":2654,"stargazers_count":13,"watchers_count":13,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":19,"mirror_url":null,"open_issues_count":1,"forks":19,"open_issues":1,"watchers":13,"default_branch":"master"}},"base":{"label":"metamx:master","ref":"master","sha":"b1fcd41b094e3bd653f7e86494ac76fad0a746ee","user":{"login":"metamx","id":299329,"avatar_url":"https://avatars.githubusercontent.com/u/299329?v=3","gravatar_id":"","url":"https://api.github.com/users/metamx","html_url":"https://github.com/metamx","followers_url":"https://api.github.com/users/metamx/followers","following_url":"https://api.github.com/users/metamx/following{/other_user}","gists_url":"https://api.github.com/users/metamx/gists{/gist_id}","starred_url":"https://api.github.com/users/metamx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/metamx/subscriptions","organizations_url":"https://api.github.com/users/metamx/orgs","repos_url":"https://api.github.com/users/metamx/repos","events_url":"https://api.github.com/users/metamx/events{/privacy}","received_events_url":"https://api.github.com/users/metamx/received_events","type":"Organization","site_admin":false},"repo":{"id":2622329,"name":"java-util","full_name":"metamx/java-util","owner":{"login":"metamx","id":299329,"avatar_url":"https://avatars.githubusercontent.com/u/299329?v=3","gravatar_id":"","url":"https://api.github.com/users/metamx","html_url":"https://github.com/metamx","followers_url":"https://api.github.com/users/metamx/followers","following_url":"https://api.github.com/users/metamx/following{/other_user}","gists_url":"https://api.github.com/users/metamx/gists{/gist_id}","starred_url":"https://api.github.com/users/metamx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/metamx/subscriptions","organizations_url":"https://api.github.com/users/metamx/orgs","repos_url":"https://api.github.com/users/metamx/repos","events_url":"https://api.github.com/users/metamx/events{/privacy}","received_events_url":"https://api.github.com/users/metamx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/metamx/java-util","description":"Utility code for java and jvm-based languages","fork":false,"url":"https://api.github.com/repos/metamx/java-util","forks_url":"https://api.github.com/repos/metamx/java-util/forks","keys_url":"https://api.github.com/repos/metamx/java-util/keys{/key_id}","collaborators_url":"https://api.github.com/repos/metamx/java-util/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/metamx/java-util/teams","hooks_url":"https://api.github.com/repos/metamx/java-util/hooks","issue_events_url":"https://api.github.com/repos/metamx/java-util/issues/events{/number}","events_url":"https://api.github.com/repos/metamx/java-util/events","assignees_url":"https://api.github.com/repos/metamx/java-util/assignees{/user}","branches_url":"https://api.github.com/repos/metamx/java-util/branches{/branch}","tags_url":"https://api.github.com/repos/metamx/java-util/tags","blobs_url":"https://api.github.com/repos/metamx/java-util/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/metamx/java-util/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/metamx/java-util/git/refs{/sha}","trees_url":"https://api.github.com/repos/metamx/java-util/git/trees{/sha}","statuses_url":"https://api.github.com/repos/metamx/java-util/statuses/{sha}","languages_url":"https://api.github.com/repos/metamx/java-util/languages","stargazers_url":"https://api.github.com/repos/metamx/java-util/stargazers","contributors_url":"https://api.github.com/repos/metamx/java-util/contributors","subscribers_url":"https://api.github.com/repos/metamx/java-util/subscribers","subscription_url":"https://api.github.com/repos/metamx/java-util/subscription","commits_url":"https://api.github.com/repos/metamx/java-util/commits{/sha}","git_commits_url":"https://api.github.com/repos/metamx/java-util/git/commits{/sha}","comments_url":"https://api.github.com/repos/metamx/java-util/comments{/number}","issue_comment_url":"https://api.github.com/repos/metamx/java-util/issues/comments{/number}","contents_url":"https://api.github.com/repos/metamx/java-util/contents/{+path}","compare_url":"https://api.github.com/repos/metamx/java-util/compare/{base}...{head}","merges_url":"https://api.github.com/repos/metamx/java-util/merges","archive_url":"https://api.github.com/repos/metamx/java-util/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/metamx/java-util/downloads","issues_url":"https://api.github.com/repos/metamx/java-util/issues{/number}","pulls_url":"https://api.github.com/repos/metamx/java-util/pulls{/number}","milestones_url":"https://api.github.com/repos/metamx/java-util/milestones{/number}","notifications_url":"https://api.github.com/repos/metamx/java-util/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/metamx/java-util/labels{/name}","releases_url":"https://api.github.com/repos/metamx/java-util/releases{/id}","created_at":"2011-10-21T18:41:10Z","updated_at":"2015-03-18T02:04:41Z","pushed_at":"2015-03-19T23:02:44Z","git_url":"git://github.com/metamx/java-util.git","ssh_url":"git@github.com:metamx/java-util.git","clone_url":"https://github.com/metamx/java-util.git","svn_url":"https://github.com/metamx/java-util","homepage":"","size":2654,"stargazers_count":13,"watchers_count":13,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":19,"mirror_url":null,"open_issues_count":1,"forks":19,"open_issues":1,"watchers":13,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/metamx/java-util/pulls/22"},"html":{"href":"https://github.com/metamx/java-util/pull/22"},"issue":{"href":"https://api.github.com/repos/metamx/java-util/issues/22"},"comments":{"href":"https://api.github.com/repos/metamx/java-util/issues/22/comments"},"review_comments":{"href":"https://api.github.com/repos/metamx/java-util/pulls/22/comments"},"review_comment":{"href":"https://api.github.com/repos/metamx/java-util/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/metamx/java-util/pulls/22/commits"},"statuses":{"href":"https://api.github.com/repos/metamx/java-util/statuses/8a22f18a4a2d08db7ca6f9c1adc3db0240c14a7e"}}}}