Repository: commons-compress Updated Branches: refs/heads/master 1a31dec12 -> 7117039b1
COMPRESS-373 support for writing legacy lzma streams Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/7117039b Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/7117039b Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/7117039b Branch: refs/heads/master Commit: 7117039b146155d81857ec29cc48b64324e5f19c Parents: 1a31dec Author: Stefan Bodewig <bode...@apache.org> Authored: Tue Nov 29 21:32:08 2016 +0100 Committer: Stefan Bodewig <bode...@apache.org> Committed: Tue Nov 29 21:32:08 2016 +0100 ---------------------------------------------------------------------- src/changes/changes.xml | 4 + .../compressors/CompressorStreamFactory.java | 7 +- .../lzma/LZMACompressorOutputStream.java | 80 ++++++++++++++++++++ src/site/xdoc/examples.xml | 2 +- src/site/xdoc/index.xml | 2 + src/site/xdoc/limitations.xml | 3 +- .../compress/compressors/LZMATestCase.java | 24 ++++++ 7 files changed, 119 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7117039b/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 45941a2..4bafa28 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -72,6 +72,10 @@ The <action> type attribute can be add,update,fix,remove. <action issue="COMPRESS-369" type="add" date="2016-11-15"> Allow archive extensions through a standard JRE ServiceLoader. </action> + <action issue="COMPRESS-369" type="add" date="2016-11-29"> + Add write support for the legacy LZMA format, this requires XZ + for Java 1.6. + </action> </release> <release version="1.12" date="2016-06-21" description="Release 1.12 - API compatible to 1.11 but requires Java 6 at runtime. http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7117039b/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java b/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java index 03baa1d..6a46a06 100644 --- a/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java +++ b/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java @@ -38,6 +38,7 @@ import org.apache.commons.compress.compressors.deflate.DeflateCompressorOutputSt import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream; import org.apache.commons.compress.compressors.lzma.LZMACompressorInputStream; +import org.apache.commons.compress.compressors.lzma.LZMACompressorOutputStream; import org.apache.commons.compress.compressors.lzma.LZMAUtils; import org.apache.commons.compress.compressors.pack200.Pack200CompressorInputStream; import org.apache.commons.compress.compressors.pack200.Pack200CompressorOutputStream; @@ -530,6 +531,10 @@ public class CompressorStreamFactory implements CompressorStreamProvider { return new Pack200CompressorOutputStream(out); } + if (LZMA.equalsIgnoreCase(name)) { + return new LZMACompressorOutputStream(out); + } + if (DEFLATE.equalsIgnoreCase(name)) { return new DeflateCompressorOutputStream(out); } @@ -576,7 +581,7 @@ public class CompressorStreamFactory implements CompressorStreamProvider { @Override public Set<String> getOutputStreamCompressorNames() { - return Sets.newHashSet(GZIP, BZIP2, XZ, PACK200, DEFLATE); + return Sets.newHashSet(GZIP, BZIP2, XZ, LZMA, PACK200, DEFLATE); } /** http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7117039b/src/main/java/org/apache/commons/compress/compressors/lzma/LZMACompressorOutputStream.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/compressors/lzma/LZMACompressorOutputStream.java b/src/main/java/org/apache/commons/compress/compressors/lzma/LZMACompressorOutputStream.java new file mode 100644 index 0000000..6669262 --- /dev/null +++ b/src/main/java/org/apache/commons/compress/compressors/lzma/LZMACompressorOutputStream.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.compress.compressors.lzma; + +import java.io.IOException; +import java.io.OutputStream; +import org.tukaani.xz.LZMA2Options; +import org.tukaani.xz.LZMAOutputStream; + +import org.apache.commons.compress.compressors.CompressorOutputStream; + +/** + * LZMA compressor. + * @since 1.13 + */ +public class LZMACompressorOutputStream extends CompressorOutputStream { + private final LZMAOutputStream out; + + /** + * Creates a LZMA compressor. + * + * @param outputStream the stream to wrap + * @throws IOException on error + */ + public LZMACompressorOutputStream(final OutputStream outputStream) + throws IOException { + out = new LZMAOutputStream(outputStream, new LZMA2Options(), -1); + } + + /** {@inheritDoc} */ + @Override + public void write(final int b) throws IOException { + out.write(b); + } + + /** {@inheritDoc} */ + @Override + public void write(final byte[] buf, final int off, final int len) throws IOException { + out.write(buf, off, len); + } + + /** + * Doesn't do anything as {@link LZMAOutputStream} doesn't support flushing. + */ + @Override + public void flush() throws IOException { + out.flush(); + } + + /** + * Finishes compression without closing the underlying stream. + * No more data can be written to this stream after finishing. + * @throws IOException on error + */ + public void finish() throws IOException { + out.finish(); + } + + /** {@inheritDoc} */ + @Override + public void close() throws IOException { + out.close(); + } +} http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7117039b/src/site/xdoc/examples.xml ---------------------------------------------------------------------- diff --git a/src/site/xdoc/examples.xml b/src/site/xdoc/examples.xml index 38ebe28..a0b1059 100644 --- a/src/site/xdoc/examples.xml +++ b/src/site/xdoc/examples.xml @@ -36,7 +36,7 @@ cpio, dump, tar and zip. Pack200 is a special case as it can only compress JAR files.</p> - <p>We currently only provide read support for lzma, arj, + <p>We currently only provide read support for arj, dump and Z. arj can only read uncompressed archives, 7z can read archives with many compression and encryption algorithms supported by 7z but doesn't support encryption when writing http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7117039b/src/site/xdoc/index.xml ---------------------------------------------------------------------- diff --git a/src/site/xdoc/index.xml b/src/site/xdoc/index.xml index 6ed0c33..6e8b626 100644 --- a/src/site/xdoc/index.xml +++ b/src/site/xdoc/index.xml @@ -79,6 +79,8 @@ needed. This allows archives to be read from inputs and written to outputs that are seekable but are not represented by <code>File</code>s.</li> + <li>Added support for writing the legacy LZMA format - + this requires XZ for Java 1.6.</li> </ul> </subsection> </section> http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7117039b/src/site/xdoc/limitations.xml ---------------------------------------------------------------------- diff --git a/src/site/xdoc/limitations.xml b/src/site/xdoc/limitations.xml index 5a661fd..bd60293 100644 --- a/src/site/xdoc/limitations.xml +++ b/src/site/xdoc/limitations.xml @@ -110,7 +110,8 @@ <li>the format requires the otherwise optional <a href="http://tukaani.org/xz/java.html">XZ for Java</a> library.</li> - <li>read-only support</li> + <li>Commons Compress 1.12 and earlier only support reading + the format</li> </ul> </section> <section name="PACK200"> http://git-wip-us.apache.org/repos/asf/commons-compress/blob/7117039b/src/test/java/org/apache/commons/compress/compressors/LZMATestCase.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/compressors/LZMATestCase.java b/src/test/java/org/apache/commons/compress/compressors/LZMATestCase.java index a880cf3..622449d 100644 --- a/src/test/java/org/apache/commons/compress/compressors/LZMATestCase.java +++ b/src/test/java/org/apache/commons/compress/compressors/LZMATestCase.java @@ -24,15 +24,39 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import org.apache.commons.compress.AbstractTestCase; import org.apache.commons.compress.compressors.lzma.LZMACompressorInputStream; import org.apache.commons.compress.utils.IOUtils; +import org.junit.Assert; import org.junit.Test; public final class LZMATestCase extends AbstractTestCase { @Test + public void lzmaRoundtrip() throws Exception { + final File input = getFile("test1.xml"); + final File compressed = new File(dir, "test1.xml.xz"); + try (OutputStream out = new FileOutputStream(compressed)) { + try (CompressorOutputStream cos = new CompressorStreamFactory() + .createCompressorOutputStream("lzma", out)) { + IOUtils.copy(new FileInputStream(input), cos); + } + } + byte[] orig; + try (InputStream is = new FileInputStream(input)) { + orig = IOUtils.toByteArray(is); + } + byte[] uncompressed; + try (InputStream is = new FileInputStream(compressed); + CompressorInputStream in = new LZMACompressorInputStream(is)) { + uncompressed = IOUtils.toByteArray(in); + } + Assert.assertArrayEquals(orig, uncompressed); + } + + @Test public void testLZMAUnarchive() throws Exception { final File input = getFile("bla.tar.lzma"); final File output = new File(dir, "bla.tar");