Skip to content

Commit 85024ec

Browse files
Merge pull request #27 from philiplourandos/master
Added xz compression support
2 parents a2f9c26 + 394c008 commit 85024ec

File tree

13 files changed

+653
-2
lines changed

13 files changed

+653
-2
lines changed

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@
8686
<version>3.0.0</version>
8787
<scope>provided</scope>
8888
</dependency>
89+
<dependency>
90+
<groupId>org.tukaani</groupId>
91+
<artifactId>xz</artifactId>
92+
<version>1.5</version>
93+
<scope>runtime</scope>
94+
</dependency>
8995
</dependencies>
9096

9197

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2016 Codehaus.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.codehaus.plexus.archiver.tar;
17+
18+
import java.io.File;
19+
20+
/**
21+
*
22+
* @author philip.lourandos
23+
*/
24+
public class PlexusIoTarXZFileResourceCollection extends
25+
PlexusIoTarFileResourceCollection
26+
{
27+
28+
public PlexusIoTarXZFileResourceCollection()
29+
{
30+
}
31+
32+
@Override
33+
protected TarFile newTarFile(File file)
34+
{
35+
return new XZTarFile( file );
36+
}
37+
}

src/main/java/org/codehaus/plexus/archiver/tar/TarArchiver.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
2121
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
2222
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
23+
import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
2324
import org.codehaus.plexus.archiver.AbstractArchiver;
2425
import org.codehaus.plexus.archiver.ArchiveEntry;
2526
import org.codehaus.plexus.archiver.ArchiverException;
@@ -466,7 +467,8 @@ public static enum TarCompressionMethod
466467
none,
467468
gzip,
468469
bzip2,
469-
snappy
470+
snappy,
471+
xz
470472

471473
}
472474

@@ -485,6 +487,11 @@ else if ( TarCompressionMethod.snappy.equals( tarCompressionMethod ) )
485487
{
486488
return new SnappyOutputStream( bufferedOutputStream( ostream ) );
487489
}
490+
else if (TarCompressionMethod.xz.equals( tarCompressionMethod ) )
491+
{
492+
return new XZCompressorOutputStream( bufferedOutputStream( ostream ) );
493+
}
494+
488495
return ostream;
489496
}
490497

src/main/java/org/codehaus/plexus/archiver/tar/TarUnArchiver.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
2121
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
2222
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
23+
import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
2324
import org.codehaus.plexus.archiver.AbstractUnArchiver;
2425
import org.codehaus.plexus.archiver.ArchiverException;
2526
import org.codehaus.plexus.archiver.util.Streams;
@@ -63,6 +64,7 @@ public TarUnArchiver( File sourceFile )
6364
* <li>gzip - Gzip compression</li>
6465
* <li>bzip2 - Bzip2 compression</li>
6566
* <li>snappy - Snappy compression</li>
67+
* <li>xz - Xz compression</li>
6668
* </ul>
6769
*
6870
* @param method compression method
@@ -150,6 +152,10 @@ else if ( compression == UntarCompressionMethod.BZIP2 )
150152
else if ( compression == UntarCompressionMethod.SNAPPY )
151153
{
152154
return new SnappyInputStream( istream, true );
155+
}
156+
else if (compression == UntarCompressionMethod.XZ)
157+
{
158+
return new XZCompressorInputStream(istream);
153159
}
154160
return istream;
155161
}
@@ -162,7 +168,8 @@ public static enum UntarCompressionMethod
162168
NONE( "none" ),
163169
GZIP( "gzip" ),
164170
BZIP2( "bzip2" ),
165-
SNAPPY( "snappy" );
171+
SNAPPY( "snappy" ),
172+
XZ("xz");
166173

167174
final String value;
168175

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2016 Codehaus.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.codehaus.plexus.archiver.tar;
17+
18+
import java.io.File;
19+
20+
/**
21+
*
22+
* @author philip.lourandos
23+
*/
24+
public class TarXZUnArchiver extends TarUnArchiver
25+
{
26+
public TarXZUnArchiver() {
27+
setupCompressionMethod();
28+
}
29+
30+
public TarXZUnArchiver(File sourceFile) {
31+
super(sourceFile);
32+
33+
setupCompressionMethod();
34+
}
35+
36+
private final void setupCompressionMethod()
37+
{
38+
setCompression(TarUnArchiver.UntarCompressionMethod.XZ);
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2016 Codehaus.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.codehaus.plexus.archiver.tar;
17+
18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import org.codehaus.plexus.archiver.xz.XZUnArchiver;
22+
23+
/**
24+
*
25+
* @author philip.lourandos
26+
*/
27+
public class XZTarFile extends TarFile
28+
{
29+
30+
public XZTarFile ( File file )
31+
{
32+
super(file);
33+
}
34+
35+
@Override
36+
protected InputStream getInputStream( File file ) throws IOException
37+
{
38+
return XZUnArchiver.getXZInputStream( super.getInputStream( file ) );
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2016 Codehaus.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.codehaus.plexus.archiver.xz;
17+
18+
import java.io.File;
19+
import java.io.FileInputStream;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.util.HashMap;
23+
import org.codehaus.plexus.components.io.attributes.Java7FileAttributes;
24+
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
25+
import org.codehaus.plexus.components.io.resources.PlexusIoCompressedFileResourceCollection;
26+
27+
/**
28+
*
29+
* @author lore
30+
*/
31+
public class PlexusIoXZResourceCollection extends PlexusIoCompressedFileResourceCollection {
32+
33+
@Override
34+
protected PlexusIoResourceAttributes getAttributes(File file) throws IOException {
35+
return new Java7FileAttributes(file, new HashMap<Integer, String>(), new HashMap<Integer, String>());
36+
}
37+
38+
@Override
39+
protected String getDefaultExtension() {
40+
return ".xz";
41+
}
42+
43+
@Override
44+
protected InputStream getInputStream(File file) throws IOException {
45+
FileInputStream fileIs = new FileInputStream(file);
46+
47+
return XZUnArchiver.getXZInputStream(fileIs);
48+
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2016 Codehaus.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.codehaus.plexus.archiver.xz;
17+
18+
import java.io.IOException;
19+
import org.codehaus.plexus.archiver.AbstractArchiver;
20+
import org.codehaus.plexus.archiver.ArchiveEntry;
21+
import org.codehaus.plexus.archiver.ArchiverException;
22+
import org.codehaus.plexus.archiver.ResourceIterator;
23+
24+
/**
25+
*
26+
* @author philiplourandos
27+
*/
28+
public class XZArchiver extends AbstractArchiver {
29+
30+
private final XZCompressor compressor = new XZCompressor();
31+
32+
public XZArchiver()
33+
{
34+
}
35+
36+
@Override
37+
protected void execute() throws ArchiverException, IOException
38+
{
39+
if ( !checkForced())
40+
{
41+
return;
42+
}
43+
44+
ResourceIterator iter = getResources();
45+
ArchiveEntry entry = iter.next();
46+
if ( iter.hasNext() )
47+
{
48+
throw new ArchiverException( "There is more than one file in input." );
49+
}
50+
compressor.setSource( entry.getResource() );
51+
compressor.setDestFile( getDestFile() );
52+
compressor.compress();
53+
}
54+
55+
@Override
56+
public boolean isSupportingForced()
57+
{
58+
return true;
59+
}
60+
61+
@Override
62+
protected void close() throws IOException
63+
{
64+
compressor.close();
65+
}
66+
67+
@Override
68+
protected String getArchiveType()
69+
{
70+
return "xz";
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2016 Codehaus.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.codehaus.plexus.archiver.xz;
17+
18+
import java.io.IOException;
19+
import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
20+
import org.codehaus.plexus.archiver.ArchiverException;
21+
import org.codehaus.plexus.archiver.util.Compressor;
22+
import org.codehaus.plexus.util.IOUtil;
23+
24+
import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
25+
import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;
26+
27+
/**
28+
*
29+
* @author philip.lourandos
30+
*/
31+
public class XZCompressor extends Compressor
32+
{
33+
private XZCompressorOutputStream xzOut;
34+
35+
public XZCompressor()
36+
{
37+
}
38+
39+
@Override
40+
public void compress() throws ArchiverException
41+
{
42+
try
43+
{
44+
xzOut = new XZCompressorOutputStream( bufferedOutputStream( fileOutputStream( getDestFile() ) ) );
45+
46+
compress( getSource(), xzOut);
47+
}
48+
catch ( IOException ioe )
49+
{
50+
throw new ArchiverException( "Problem creating xz " + ioe.getMessage(), ioe);
51+
}
52+
}
53+
54+
@Override
55+
public void close()
56+
{
57+
IOUtil.close( xzOut );
58+
xzOut = null;
59+
}
60+
}

0 commit comments

Comments
 (0)