diff options
author | Youssef Elghareeb <ghareeb@google.com> | 2021-01-22 15:30:25 +0100 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2021-01-28 02:57:22 -0500 |
commit | 6f82690aaf2be783be6d77f0903788ff0832472a (patch) | |
tree | 375f47dab22823d2dc3fe54b762178c7bbbba6bf /org.eclipse.jgit.archive/src/org/eclipse/jgit/archive | |
parent | c29ec3447d3339e57a46e02423c44ba3638a197e (diff) | |
download | jgit-6f82690aaf2be783be6d77f0903788ff0832472a.tar.gz jgit-6f82690aaf2be783be6d77f0903788ff0832472a.zip |
Add the "compression-level" option to all ArchiveCommand formats
Different archive formats support a compression level in the range
[0-9]. The value 0 is for lowest compressions and 9 for highest. Highest
levels produce output files of smaller sizes but require more memory to
do the compression.
This change allows passing a "compression-level" option to the git
archive command and implements using it for different file formats.
Change-Id: I5758f691c37ba630dbac24db67bb7da827bbc8e1
Signed-off-by: Youssef Elghareeb <ghareeb@google.com>
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit.archive/src/org/eclipse/jgit/archive')
6 files changed, 67 insertions, 4 deletions
diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/BaseFormat.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/BaseFormat.java index 27f001e220..0ebac77228 100644 --- a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/BaseFormat.java +++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/BaseFormat.java @@ -25,6 +25,11 @@ import org.eclipse.jgit.util.StringUtils; * @since 4.0 */ public class BaseFormat { + /** + * Compression-level for the archive file. Only values in [0-9] are allowed. + * @since 5.11 + */ + protected static final String COMPRESSION_LEVEL = "compression-level"; //$NON-NLS-1$ /** * Apply options to archive output stream @@ -40,6 +45,9 @@ public class BaseFormat { Map<String, Object> o) throws IOException { for (Map.Entry<String, Object> p : o.entrySet()) { try { + if (p.getKey().equals(COMPRESSION_LEVEL)) { + continue; + } new Statement(s, "set" + StringUtils.capitalize(p.getKey()), //$NON-NLS-1$ new Object[] { p.getValue() }).execute(); } catch (Exception e) { @@ -49,4 +57,32 @@ public class BaseFormat { } return s; } + + /** + * Removes and returns the {@link #COMPRESSION_LEVEL} key from the input map + * parameter if it exists, or -1 if this key does not exist. + * + * @param o + * options map + * @return The compression level if it exists in the map, or -1 instead. + * @throws IllegalArgumentException + * if the {@link #COMPRESSION_LEVEL} option does not parse to an + * Integer. + * @since 5.11 + */ + protected int getCompressionLevel(Map<String, Object> o) { + if (!o.containsKey(COMPRESSION_LEVEL)) { + return -1; + } + Object option = o.get(COMPRESSION_LEVEL); + try { + Integer compressionLevel = (Integer) option; + return compressionLevel.intValue(); + } catch (ClassCastException e) { + throw new IllegalArgumentException( + MessageFormat.format( + ArchiveText.get().invalidCompressionLevel, option), + e); + } + } } diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/Tbz2Format.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/Tbz2Format.java index e880f5ec56..940dafd40f 100644 --- a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/Tbz2Format.java +++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/Tbz2Format.java @@ -45,7 +45,13 @@ public final class Tbz2Format extends BaseFormat implements @Override public ArchiveOutputStream createArchiveOutputStream(OutputStream s, Map<String, Object> o) throws IOException { - BZip2CompressorOutputStream out = new BZip2CompressorOutputStream(s); + BZip2CompressorOutputStream out; + int compressionLevel = getCompressionLevel(o); + if (compressionLevel != -1) { + out = new BZip2CompressorOutputStream(s, compressionLevel); + } else { + out = new BZip2CompressorOutputStream(s); + } return tarFormat.createArchiveOutputStream(out, o); } diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TgzFormat.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TgzFormat.java index 859a59d095..72e2439f68 100644 --- a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TgzFormat.java +++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TgzFormat.java @@ -18,6 +18,7 @@ import java.util.Map; import org.apache.commons.compress.archivers.ArchiveOutputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; +import org.apache.commons.compress.compressors.gzip.GzipParameters; import org.eclipse.jgit.api.ArchiveCommand; import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.ObjectId; @@ -45,7 +46,15 @@ public final class TgzFormat extends BaseFormat implements @Override public ArchiveOutputStream createArchiveOutputStream(OutputStream s, Map<String, Object> o) throws IOException { - GzipCompressorOutputStream out = new GzipCompressorOutputStream(s); + GzipCompressorOutputStream out; + int compressionLevel = getCompressionLevel(o); + if (compressionLevel != -1) { + GzipParameters parameters = new GzipParameters(); + parameters.setCompressionLevel(compressionLevel); + out = new GzipCompressorOutputStream(s, parameters); + } else { + out = new GzipCompressorOutputStream(s); + } return tarFormat.createArchiveOutputStream(out, o); } diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TxzFormat.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TxzFormat.java index 484ab5775c..b16fb6dcbd 100644 --- a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TxzFormat.java +++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/TxzFormat.java @@ -45,7 +45,13 @@ public final class TxzFormat extends BaseFormat implements @Override public ArchiveOutputStream createArchiveOutputStream(OutputStream s, Map<String, Object> o) throws IOException { - XZCompressorOutputStream out = new XZCompressorOutputStream(s); + XZCompressorOutputStream out; + int compressionLevel = getCompressionLevel(o); + if (compressionLevel != -1) { + out = new XZCompressorOutputStream(s, compressionLevel); + } else { + out = new XZCompressorOutputStream(s); + } return tarFormat.createArchiveOutputStream(out, o); } diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/ZipFormat.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/ZipFormat.java index 59a9765f28..97a24c75cb 100644 --- a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/ZipFormat.java +++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/ZipFormat.java @@ -47,7 +47,12 @@ public final class ZipFormat extends BaseFormat implements @Override public ArchiveOutputStream createArchiveOutputStream(OutputStream s, Map<String, Object> o) throws IOException { - return applyFormatOptions(new ZipArchiveOutputStream(s), o); + ZipArchiveOutputStream out = new ZipArchiveOutputStream(s); + int compressionLevel = getCompressionLevel(o); + if (compressionLevel != -1) { + out.setLevel(compressionLevel); + } + return applyFormatOptions(out, o); } /** {@inheritDoc} */ diff --git a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/internal/ArchiveText.java b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/internal/ArchiveText.java index 45f96fa4c1..551646bcdc 100644 --- a/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/internal/ArchiveText.java +++ b/org.eclipse.jgit.archive/src/org/eclipse/jgit/archive/internal/ArchiveText.java @@ -28,6 +28,7 @@ public class ArchiveText extends TranslationBundle { // @formatter:off /***/ public String cannotSetOption; + /***/ public String invalidCompressionLevel; /***/ public String pathDoesNotMatchMode; /***/ public String unsupportedMode; } |