diff --git a/pom.xml b/pom.xml index 50851e94d..ce66bf4aa 100644 --- a/pom.xml +++ b/pom.xml @@ -80,6 +80,12 @@ 3.0.0 provided + + org.tukaani + xz + 1.5 + runtime + diff --git a/src/main/java/org/codehaus/plexus/archiver/tar/PlexusIoTarXZFileResourceCollection.java b/src/main/java/org/codehaus/plexus/archiver/tar/PlexusIoTarXZFileResourceCollection.java new file mode 100644 index 000000000..a030d9949 --- /dev/null +++ b/src/main/java/org/codehaus/plexus/archiver/tar/PlexusIoTarXZFileResourceCollection.java @@ -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 ); + } +} diff --git a/src/main/java/org/codehaus/plexus/archiver/tar/TarArchiver.java b/src/main/java/org/codehaus/plexus/archiver/tar/TarArchiver.java index 47a2851cf..2425e0189 100644 --- a/src/main/java/org/codehaus/plexus/archiver/tar/TarArchiver.java +++ b/src/main/java/org/codehaus/plexus/archiver/tar/TarArchiver.java @@ -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; @@ -465,7 +466,8 @@ public static enum TarCompressionMethod none, gzip, bzip2, - snappy + snappy, + xz } @@ -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; } diff --git a/src/main/java/org/codehaus/plexus/archiver/tar/TarUnArchiver.java b/src/main/java/org/codehaus/plexus/archiver/tar/TarUnArchiver.java index 3de21e5d5..82dff777d 100644 --- a/src/main/java/org/codehaus/plexus/archiver/tar/TarUnArchiver.java +++ b/src/main/java/org/codehaus/plexus/archiver/tar/TarUnArchiver.java @@ -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; @@ -63,6 +64,7 @@ public TarUnArchiver( File sourceFile ) *
  • gzip - Gzip compression
  • *
  • bzip2 - Bzip2 compression
  • *
  • snappy - Snappy compression
  • + *
  • xz - Xz compression
  • * * * @param method compression method @@ -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; } @@ -161,7 +167,8 @@ public static enum UntarCompressionMethod NONE( "none" ), GZIP( "gzip" ), BZIP2( "bzip2" ), - SNAPPY( "snappy" ); + SNAPPY( "snappy" ), + XZ("xz"); final String value; diff --git a/src/main/java/org/codehaus/plexus/archiver/tar/TarXZUnArchiver.java b/src/main/java/org/codehaus/plexus/archiver/tar/TarXZUnArchiver.java new file mode 100644 index 000000000..ecc60ccfb --- /dev/null +++ b/src/main/java/org/codehaus/plexus/archiver/tar/TarXZUnArchiver.java @@ -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); + } +} diff --git a/src/main/java/org/codehaus/plexus/archiver/tar/XZTarFile.java b/src/main/java/org/codehaus/plexus/archiver/tar/XZTarFile.java new file mode 100644 index 000000000..5a700fead --- /dev/null +++ b/src/main/java/org/codehaus/plexus/archiver/tar/XZTarFile.java @@ -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 ) ); + } +} diff --git a/src/main/java/org/codehaus/plexus/archiver/xz/PlexusIoXZResourceCollection.java b/src/main/java/org/codehaus/plexus/archiver/xz/PlexusIoXZResourceCollection.java new file mode 100644 index 000000000..4b0c5599f --- /dev/null +++ b/src/main/java/org/codehaus/plexus/archiver/xz/PlexusIoXZResourceCollection.java @@ -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(), new HashMap()); + } + + @Override + protected String getDefaultExtension() { + return ".xz"; + } + + @Override + protected InputStream getInputStream(File file) throws IOException { + FileInputStream fileIs = new FileInputStream(file); + + return XZUnArchiver.getXZInputStream(fileIs); + + } +} diff --git a/src/main/java/org/codehaus/plexus/archiver/xz/XZArchiver.java b/src/main/java/org/codehaus/plexus/archiver/xz/XZArchiver.java new file mode 100644 index 000000000..1a84947ea --- /dev/null +++ b/src/main/java/org/codehaus/plexus/archiver/xz/XZArchiver.java @@ -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"; + } +} diff --git a/src/main/java/org/codehaus/plexus/archiver/xz/XZCompressor.java b/src/main/java/org/codehaus/plexus/archiver/xz/XZCompressor.java new file mode 100644 index 000000000..9797f95ef --- /dev/null +++ b/src/main/java/org/codehaus/plexus/archiver/xz/XZCompressor.java @@ -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 ); + xzOut = null; + } +} diff --git a/src/main/java/org/codehaus/plexus/archiver/xz/XZUnArchiver.java b/src/main/java/org/codehaus/plexus/archiver/xz/XZUnArchiver.java new file mode 100644 index 000000000..48e229966 --- /dev/null +++ b/src/main/java/org/codehaus/plexus/archiver/xz/XZUnArchiver.java @@ -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"); + + } +} diff --git a/src/main/resources/META-INF/plexus/components.xml b/src/main/resources/META-INF/plexus/components.xml index ee2406a2d..d19b7a89f 100644 --- a/src/main/resources/META-INF/plexus/components.xml +++ b/src/main/resources/META-INF/plexus/components.xml @@ -11,6 +11,12 @@ org.codehaus.plexus.archiver.bzip2.BZip2Archiver per-lookup + + org.codehaus.plexus.archiver.Archiver + xz + org.codehaus.plexus.archiver.xz.XZArchiver + per-lookup + org.codehaus.plexus.archiver.Archiver dir @@ -80,6 +86,12 @@ org.codehaus.plexus.archiver.bzip2.BZip2UnArchiver per-lookup + + org.codehaus.plexus.archiver.UnArchiver + xz + org.codehaus.plexus.archiver.xz.XZUnArchiver + per-lookup + org.codehaus.plexus.archiver.UnArchiver gzip @@ -201,6 +213,20 @@ org.codehaus.plexus.archiver.tar.TarBZip2UnArchiver per-lookup + + + org.codehaus.plexus.archiver.UnArchiver + txz + org.codehaus.plexus.archiver.tar.TarXZUnArchiver + per-lookup + + + + org.codehaus.plexus.archiver.UnArchiver + tar.xz + org.codehaus.plexus.archiver.tar.TarXZUnArchiver + per-lookup + org.codehaus.plexus.archiver.UnArchiver @@ -249,6 +275,12 @@ org.codehaus.plexus.archiver.bzip2.PlexusIoBzip2ResourceCollection per-lookup + + org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection + xz + org.codehaus.plexus.archiver.xz.PlexusIoXZResourceCollection + per-lookup + org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection gzip @@ -381,6 +413,18 @@ org.codehaus.plexus.archiver.tar.PlexusIoTarBZip2FileResourceCollection per-lookup + + org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection + txz + org.codehaus.plexus.archiver.tar.PlexusIoTarXZFileResourceCollection + per-lookup + + + org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection + tar.xz + org.codehaus.plexus.archiver.tar.PlexusIoTarXZFileResourceCollection + per-lookup + org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection diff --git a/src/test/java/org/codehaus/plexus/archiver/tar/TarXzUnArchiverTest.java b/src/test/java/org/codehaus/plexus/archiver/tar/TarXzUnArchiverTest.java new file mode 100644 index 000000000..51309641f --- /dev/null +++ b/src/test/java/org/codehaus/plexus/archiver/tar/TarXzUnArchiverTest.java @@ -0,0 +1,81 @@ +/* + * 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 static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertTrue; +import org.codehaus.plexus.PlexusTestCase; +import static org.codehaus.plexus.PlexusTestCase.getTestFile; +import org.codehaus.plexus.archiver.Archiver; +import org.codehaus.plexus.archiver.UnArchiver; +import org.codehaus.plexus.archiver.xz.XZArchiver; + +/** + * + * @author philip.lourandos + */ +public class TarXzUnArchiverTest extends PlexusTestCase { + + public void testExtract() + throws Exception + { + TarArchiver tarArchiver = (TarArchiver) lookup( Archiver.ROLE, "tar" ); + tarArchiver.setLongfile(TarLongFileMode.posix ); + + String fileName1 = "TarBZip2UnArchiverTest1.txt"; + String fileName2 = "TarBZip2UnArchiverTest2.txt"; + File file1InTar = getTestFile( "target/output/" + fileName1 ); + File file2InTar = getTestFile( "target/output/" + fileName2 ); + file1InTar.delete(); + file2InTar.delete(); + + assertFalse(file1InTar.exists()); + assertFalse(file2InTar.exists()); + + File testXZFile = getTestFile( "target/output/archive.tar.xz" ); + assertFalse(testXZFile.exists()); + + tarArchiver.addFile( getTestFile( "src/test/resources/manifests/manifest1.mf" ), fileName1 ); + tarArchiver.addFile( getTestFile( "src/test/resources/manifests/manifest2.mf" ), fileName2, 0664 ); + tarArchiver.setDestFile( getTestFile( "target/output/archive.tar" ) ); + tarArchiver.createArchive(); + + XZArchiver xzArchiver = (XZArchiver) lookup( Archiver.ROLE, "xz" ); + + xzArchiver.setDestFile( testXZFile ); + xzArchiver.addFile( getTestFile( "target/output/archive.tar" ), "dontcare" ); + xzArchiver.createArchive(); + + assertTrue(testXZFile.exists()); + + TarXZUnArchiver tarXZUnArchiver = (TarXZUnArchiver) lookup( UnArchiver.ROLE, "tar.xz" ); + + tarXZUnArchiver.setDestDirectory( getTestFile( "target/output" ) ); + tarXZUnArchiver.setSourceFile( testXZFile ); + tarXZUnArchiver.extract(); + + assertTrue( file1InTar.exists() ); + assertTrue( file2InTar.exists() ); + + assertEquals( testXZFile, tarXZUnArchiver.getSourceFile() ); + } + + public void testLookup() throws Exception + { + assertNotNull( lookup( UnArchiver.ROLE, "tar.xz" ) ); + } +} diff --git a/src/test/java/org/codehaus/plexus/archiver/xz/XzArchiverTest.java b/src/test/java/org/codehaus/plexus/archiver/xz/XzArchiverTest.java new file mode 100644 index 000000000..3aab6ab8c --- /dev/null +++ b/src/test/java/org/codehaus/plexus/archiver/xz/XzArchiverTest.java @@ -0,0 +1,127 @@ +/* + * 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.InputStream; +import java.util.Arrays; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertTrue; +import static org.codehaus.plexus.PlexusTestCase.getTestFile; +import org.codehaus.plexus.archiver.Archiver; +import org.codehaus.plexus.archiver.BasePlexusArchiverTest; +import org.codehaus.plexus.archiver.zip.ZipArchiver; +import org.codehaus.plexus.util.FileUtils; +import org.codehaus.plexus.util.IOUtil; + +/** + * + * @author philip.lourandos + */ +public class XzArchiverTest extends BasePlexusArchiverTest +{ + public void testCreateArchive() + throws Exception + { + ZipArchiver zipArchiver = (ZipArchiver) lookup( Archiver.ROLE, "zip" ); + zipArchiver.addDirectory( getTestFile( "src" ) ); + zipArchiver.setDestFile( getTestFile( "target/output/archiveForxz.zip" ) ); + zipArchiver.createArchive(); + + XZArchiver archiver = (XZArchiver) lookup( Archiver.ROLE, "xz" ); + String[] inputFiles = new String[ 1 ]; + inputFiles[ 0 ] = "archiveForxz.zip"; + + File targetOutputFile = getTestFile( "target/output/archive.xz" ); + assertFalse( targetOutputFile.exists() ); + + archiver.addDirectory( getTestFile( "target/output" ), inputFiles, null ); + archiver.setDestFile( targetOutputFile ); + archiver.createArchive(); + + assertTrue( targetOutputFile.exists() ); + } + + public void testCreateResourceCollection() throws Exception + { + final File pomFile = new File("pom.xml"); + final File xzFile = new File( "target/output/pom.xml.xz" ); + XZArchiver xzArchiver = (XZArchiver) lookup( Archiver.ROLE, "xz" ); + xzArchiver.setDestFile( xzFile ); + xzArchiver.addFile( pomFile, "pom.xml" ); + FileUtils.removePath( xzFile.getPath() ); + xzArchiver.createArchive(); + + System.out.println( "Created: " + xzFile.getAbsolutePath() ); + + final File zipFile = new File( "target/output/pomxz.zip" ); + ZipArchiver zipArchiver = (ZipArchiver) lookup( Archiver.ROLE, "zip" ); + zipArchiver.setDestFile( zipFile ); + zipArchiver.addArchivedFileSet( xzFile, "prfx/" ); + FileUtils.removePath( zipFile.getPath() ); + zipArchiver.createArchive(); + + final ZipFile juZipFile = new ZipFile( zipFile ); + final ZipEntry zipEntry = juZipFile.getEntry( "prfx/target/output/pom.xml" ); + final InputStream archivePom = juZipFile.getInputStream( zipEntry ); + final InputStream pom = new FileInputStream( pomFile ); + + assertTrue( Arrays.equals( IOUtil.toByteArray( pom ), IOUtil.toByteArray( archivePom ) ) ); + archivePom.close(); + pom.close(); + juZipFile.close(); + } + + /** + * Tests the .xz archiver is forced set to true, and after that + * tests the behavior when the forced is set to false. + * + * @throws Exception + */ + public void testXzIsForcedBehaviour() throws Exception + { + XZArchiver xzArchiver = (XZArchiver) createArchiver( "xz" ); + + assertTrue( xzArchiver.isSupportingForced() ); + xzArchiver.createArchive(); + + final long creationTime = xzArchiver.getDestFile().lastModified(); + + waitUntilNewTimestamp( xzArchiver.getDestFile(), creationTime ); + + xzArchiver = (XZArchiver) createArchiver( "xz" ); + + xzArchiver.setForced( true ); + xzArchiver.createArchive(); + + final long firstRunTime = xzArchiver.getDestFile().lastModified(); + + assertFalse( creationTime==firstRunTime ); + + xzArchiver = (XZArchiver) createArchiver( "xz" ); + + xzArchiver.setForced( false ); + xzArchiver.createArchive(); + + final long secondRunTime = xzArchiver.getDestFile().lastModified(); + + assertEquals( firstRunTime,secondRunTime ); + } +}