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
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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;
import org.codehaus.plexus.util.IOUtil;

/**
*
* @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 );

try
{
final InputStream result = XZUnArchiver.getXZInputStream( fileIs );

Copy link
Contributor

Choose a reason for hiding this comment

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

You need to set fileIs to null here so that the finally clause does not close the stream.

return result;
}
finally
{
IOUtil.close(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;
}
}
80 changes: 80 additions & 0 deletions src/main/java/org/codehaus/plexus/archiver/xz/XZUnArchiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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.IOException;
import java.io.InputStream;
import javax.annotation.Nonnull;
import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
import org.codehaus.plexus.archiver.AbstractUnArchiver;
import org.codehaus.plexus.archiver.ArchiverException;

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

/**
*
* @author philip.lourandos
*/
public class XZUnArchiver extends AbstractUnArchiver
{
private static final String OPERATION_XZ = "xz";

public XZUnArchiver()
{
}

public XZUnArchiver( File source )
{
super( source );
}

@Override
protected void execute() throws ArchiverException
{
if ( getSourceFile().lastModified() > getDestFile().lastModified() )
{
getLogger().info( "Expanding " + getSourceFile().getAbsolutePath() + " to "
+ getDestFile().getAbsolutePath() );

copyFully( getXZInputStream( bufferedInputStream( fileInputStream( getSourceFile(), OPERATION_XZ) ) ),
bufferedOutputStream( fileOutputStream( getDestFile(), OPERATION_XZ) ), OPERATION_XZ );
}
}

public static @Nonnull XZCompressorInputStream getXZInputStream( InputStream in )
throws ArchiverException
{
try
{
return new XZCompressorInputStream(in);
}
catch (IOException ioe)
{
throw new ArchiverException( "Trouble creating BZIP2 compressor, invalid file ?", ioe );
}
}

@Override
protected void execute(String path, File outputDirectory) throws ArchiverException {
throw new UnsupportedOperationException("Targeted execution not supported in xz format");

}
}