-
Notifications
You must be signed in to change notification settings - Fork 3.8k
CompressionUtils: Add support for decompressing xz, bz2, zip. #5586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,14 +28,18 @@ | |
| import com.google.common.io.Files; | ||
| import io.druid.java.util.common.io.NativeIO; | ||
| import io.druid.java.util.common.logger.Logger; | ||
| import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; | ||
| import org.apache.commons.compress.compressors.xz.XZCompressorInputStream; | ||
|
|
||
| import java.io.BufferedInputStream; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.File; | ||
| import java.io.FileOutputStream; | ||
| import java.io.FilterInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Enumeration; | ||
| import java.util.zip.GZIPInputStream; | ||
| import java.util.zip.GZIPOutputStream; | ||
|
|
@@ -48,7 +52,9 @@ public class CompressionUtils | |
| { | ||
| private static final Logger log = new Logger(CompressionUtils.class); | ||
| private static final int DEFAULT_RETRY_COUNT = 3; | ||
| private static final String BZ2_SUFFIX = ".bz2"; | ||
| private static final String GZ_SUFFIX = ".gz"; | ||
| private static final String XZ_SUFFIX = ".xz"; | ||
| private static final String ZIP_SUFFIX = ".zip"; | ||
|
|
||
| /** | ||
|
|
@@ -313,7 +319,7 @@ public static FileUtils.FileCopyResult gunzip(InputStream in, File outFile) thro | |
| * | ||
| * @return A GZIPInputStream that can handle concatenated gzip streams in the input | ||
| */ | ||
| public static GZIPInputStream gzipInputStream(final InputStream in) throws IOException | ||
| private static GZIPInputStream gzipInputStream(final InputStream in) throws IOException | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can break extensions that depend on it. Can it be deprecated for a version or two instead? |
||
| { | ||
| return new GZIPInputStream( | ||
| new FilterInputStream(in) | ||
|
|
@@ -516,4 +522,42 @@ public static String getGzBaseName(String fname) | |
| } | ||
| throw new IAE("[%s] is not a valid gz file name", fname); | ||
| } | ||
|
|
||
| /** | ||
| * Decompress an input stream from a file, based on the filename. | ||
| */ | ||
| public static InputStream decompress(final InputStream in, final String fileName) throws IOException | ||
| { | ||
| if (fileName.endsWith(GZ_SUFFIX)) { | ||
| return gzipInputStream(in); | ||
| } else if (fileName.endsWith(BZ2_SUFFIX)) { | ||
| return new BZip2CompressorInputStream(in, true); | ||
| } else if (fileName.endsWith(XZ_SUFFIX)) { | ||
| return new XZCompressorInputStream(in, true); | ||
| } else if (fileName.endsWith(ZIP_SUFFIX)) { | ||
| // This reads the first file in the archive. | ||
| final ZipInputStream zipIn = new ZipInputStream(in, StandardCharsets.UTF_8); | ||
| try { | ||
| final ZipEntry nextEntry = zipIn.getNextEntry(); | ||
| if (nextEntry == null) { | ||
| zipIn.close(); | ||
|
|
||
| // No files in the archive - return an empty stream. | ||
| return new ByteArrayInputStream(new byte[0]); | ||
| } | ||
| return zipIn; | ||
| } | ||
| catch (IOException e) { | ||
| try { | ||
| zipIn.close(); | ||
| } | ||
| catch (IOException e2) { | ||
| e.addSuppressed(e2); | ||
| } | ||
| throw e; | ||
| } | ||
| } else { | ||
| return in; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These constants wouldn't happen to be in the apache libs anywhere would they?