Skip to content

Added xz compression support #27

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 14 commits into from
May 6, 2016
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>1.5</version>
<scope>runtime</scope>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2016 Codehaus.
*
* Licensed 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.codehaus.plexus.archiver.tar;

import java.io.File;

/**
*
* @author philip.lourandos
*/
public class PlexusIoTarXZFileResourceCollection extends
PlexusIoTarFileResourceCollection
{

public PlexusIoTarXZFileResourceCollection()
{
}

@Override
protected TarFile newTarFile(File file)
{
return new XZTarFile( file );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
import org.codehaus.plexus.archiver.AbstractArchiver;
import org.codehaus.plexus.archiver.ArchiveEntry;
import org.codehaus.plexus.archiver.ArchiverException;
Expand Down Expand Up @@ -465,7 +466,8 @@ public static enum TarCompressionMethod
none,
gzip,
bzip2,
snappy
snappy,
xz

}

Expand All @@ -484,6 +486,11 @@ else if ( TarCompressionMethod.snappy.equals( tarCompressionMethod ) )
{
return new SnappyOutputStream( bufferedOutputStream( ostream ) );
}
else if (TarCompressionMethod.xz.equals( tarCompressionMethod ) )
{
return new XZCompressorOutputStream( bufferedOutputStream( ostream ) );
}

return ostream;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
import org.codehaus.plexus.archiver.AbstractUnArchiver;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.util.Streams;
Expand Down Expand Up @@ -63,6 +64,7 @@ public TarUnArchiver( File sourceFile )
* <li>gzip - Gzip compression</li>
* <li>bzip2 - Bzip2 compression</li>
* <li>snappy - Snappy compression</li>
* <li>xz - Xz compression</li>
* </ul>
*
* @param method compression method
Expand Down Expand Up @@ -149,6 +151,10 @@ else if ( compression == UntarCompressionMethod.BZIP2 )
else if ( compression == UntarCompressionMethod.SNAPPY )
{
return new SnappyInputStream( istream, true );
}
else if (compression == UntarCompressionMethod.XZ)
{
return new XZCompressorInputStream(istream);
}
return istream;
}
Expand All @@ -161,7 +167,8 @@ public static enum UntarCompressionMethod
NONE( "none" ),
GZIP( "gzip" ),
BZIP2( "bzip2" ),
SNAPPY( "snappy" );
SNAPPY( "snappy" ),
XZ("xz");

final String value;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2016 Codehaus.
*
* Licensed 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.codehaus.plexus.archiver.tar;

import java.io.File;

/**
*
* @author philip.lourandos
*/
public class TarXZUnArchiver extends TarUnArchiver
{
public TarXZUnArchiver() {
setupCompressionMethod();
}

public TarXZUnArchiver(File sourceFile) {
super(sourceFile);

setupCompressionMethod();
}

private final void setupCompressionMethod()
{
setCompression(TarUnArchiver.UntarCompressionMethod.XZ);
}
}
40 changes: 40 additions & 0 deletions src/main/java/org/codehaus/plexus/archiver/tar/XZTarFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2016 Codehaus.
*
* Licensed 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.codehaus.plexus.archiver.tar;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.codehaus.plexus.archiver.xz.XZUnArchiver;

/**
*
* @author philip.lourandos
*/
public class XZTarFile extends TarFile
{

public XZTarFile ( File file )
{
super(file);
}

@Override
protected InputStream getInputStream( File file ) throws IOException
{
return XZUnArchiver.getXZInputStream( super.getInputStream( file ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2016 Codehaus.
*
* Licensed 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.codehaus.plexus.archiver.xz;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import org.codehaus.plexus.components.io.attributes.Java7FileAttributes;
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
import org.codehaus.plexus.components.io.resources.PlexusIoCompressedFileResourceCollection;

/**
*
* @author lore
*/
public class PlexusIoXZResourceCollection extends PlexusIoCompressedFileResourceCollection {

@Override
protected PlexusIoResourceAttributes getAttributes(File file) throws IOException {
return new Java7FileAttributes(file, new HashMap<Integer, String>(), new HashMap<Integer, String>());
}

@Override
protected String getDefaultExtension() {
return ".xz";
}

@Override
protected InputStream getInputStream(File file) throws IOException {
FileInputStream fileIs = new FileInputStream(file);

return XZUnArchiver.getXZInputStream(fileIs);

}
}
72 changes: 72 additions & 0 deletions src/main/java/org/codehaus/plexus/archiver/xz/XZArchiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2016 Codehaus.
*
* Licensed 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.codehaus.plexus.archiver.xz;

import java.io.IOException;
import org.codehaus.plexus.archiver.AbstractArchiver;
import org.codehaus.plexus.archiver.ArchiveEntry;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.ResourceIterator;

/**
*
* @author philiplourandos
*/
public class XZArchiver extends AbstractArchiver {

private final XZCompressor compressor = new XZCompressor();

public XZArchiver()
{
}

@Override
protected void execute() throws ArchiverException, IOException
{
if ( !checkForced())
{
return;
}

ResourceIterator iter = getResources();
ArchiveEntry entry = iter.next();
if ( iter.hasNext() )
{
throw new ArchiverException( "There is more than one file in input." );
}
compressor.setSource( entry.getResource() );
compressor.setDestFile( getDestFile() );
compressor.compress();
}

@Override
public boolean isSupportingForced()
{
return true;
}

@Override
protected void close() throws IOException
{
compressor.close();
}

@Override
protected String getArchiveType()
{
return "xz";
}
}
60 changes: 60 additions & 0 deletions src/main/java/org/codehaus/plexus/archiver/xz/XZCompressor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2016 Codehaus.
*
* Licensed 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.codehaus.plexus.archiver.xz;

import java.io.IOException;
import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.util.Compressor;
import org.codehaus.plexus.util.IOUtil;

import static org.codehaus.plexus.archiver.util.Streams.bufferedOutputStream;
import static org.codehaus.plexus.archiver.util.Streams.fileOutputStream;

/**
*
* @author philip.lourandos
*/
public class XZCompressor extends Compressor
{
private XZCompressorOutputStream xzOut;

public XZCompressor()
{
}

@Override
public void compress() throws ArchiverException
{
try
{
xzOut = new XZCompressorOutputStream( bufferedOutputStream( fileOutputStream( getDestFile() ) ) );

compress( getSource(), xzOut);
}
catch ( IOException ioe )
{
throw new ArchiverException( "Problem creating xz " + ioe.getMessage(), ioe);
}
}

@Override
public void close()
{
IOUtil.close( xzOut );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to throw an ArchiverException in case the close throws an IOException.

xzOut = null;
}
}
Loading