Skip to content

Commit a2a45f8

Browse files
author
lore
committed
Added xz package to support xz archiving and un-archiving
1 parent ccb7128 commit a2a45f8

File tree

4 files changed

+275
-0
lines changed

4 files changed

+275
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
import org.codehaus.plexus.util.IOUtil;
27+
28+
/**
29+
*
30+
* @author lore
31+
*/
32+
public class PlexusIOXZResourceCollection extends PlexusIoCompressedFileResourceCollection
33+
{
34+
35+
@Override
36+
protected PlexusIoResourceAttributes getAttributes( File file ) throws IOException
37+
{
38+
return new Java7FileAttributes(file, new HashMap<Integer, String>(), new HashMap<Integer, String>());
39+
}
40+
41+
@Override
42+
protected String getDefaultExtension()
43+
{
44+
return ".xz";
45+
}
46+
47+
@Override
48+
protected InputStream getInputStream( File file ) throws IOException
49+
{
50+
FileInputStream fileIs = new FileInputStream( file );
51+
52+
try
53+
{
54+
final InputStream result = XZUnArchiver.getXZInputStream( fileIs );
55+
56+
return result;
57+
}
58+
finally
59+
{
60+
IOUtil.close(fileIs);
61+
}
62+
}
63+
}
Lines changed: 72 additions & 0 deletions
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+
}
Lines changed: 60 additions & 0 deletions
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+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.IOException;
20+
import java.io.InputStream;
21+
import javax.annotation.Nonnull;
22+
import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
23+
import org.codehaus.plexus.archiver.AbstractUnArchiver;
24+
import org.codehaus.plexus.archiver.ArchiverException;
25+
26+
import static org.codehaus.plexus.archiver.util.Streams.bufferedInputStream;
27+
import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
28+
import static org.codehaus.plexus.archiver.util.Streams.copyFully;
29+
import static org.codehaus.plexus.archiver.util.Streams.fileInputStream;
30+
import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;
31+
32+
/**
33+
*
34+
* @author philip.lourandos
35+
*/
36+
public class XZUnArchiver extends AbstractUnArchiver
37+
{
38+
private static final String OPERATION_XZ = "xz";
39+
40+
public XZUnArchiver()
41+
{
42+
}
43+
44+
public XZUnArchiver( File source )
45+
{
46+
super( source );
47+
}
48+
49+
@Override
50+
protected void execute() throws ArchiverException
51+
{
52+
if ( getSourceFile().lastModified() > getDestFile().lastModified() )
53+
{
54+
getLogger().info( "Expanding " + getSourceFile().getAbsolutePath() + " to "
55+
+ getDestFile().getAbsolutePath() );
56+
57+
copyFully( getXZInputStream( bufferedInputStream( fileInputStream( getSourceFile(), OPERATION_XZ) ) ),
58+
bufferedOutputStream( fileOutputStream( getDestFile(), OPERATION_XZ) ), OPERATION_XZ );
59+
}
60+
}
61+
62+
public static @Nonnull XZCompressorInputStream getXZInputStream( InputStream in )
63+
throws ArchiverException
64+
{
65+
try
66+
{
67+
return new XZCompressorInputStream(in);
68+
}
69+
catch (IOException ioe)
70+
{
71+
throw new ArchiverException( "Trouble creating BZIP2 compressor, invalid file ?", ioe );
72+
}
73+
}
74+
75+
@Override
76+
protected void execute(String path, File outputDirectory) throws ArchiverException {
77+
throw new UnsupportedOperationException("Targeted execution not supported in xz format");
78+
79+
}
80+
}

0 commit comments

Comments
 (0)