Skip to content

Commit

Permalink
Add zstd (un)archiver support
Browse files Browse the repository at this point in the history
  • Loading branch information
pleeplop authored and plamentotev committed Sep 9, 2022
1 parent 8656ee0 commit 5ba0d19
Show file tree
Hide file tree
Showing 14 changed files with 707 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@
<version>1.9</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<version>1.5.2-3</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ String getFileExtention( @Nonnull File file )
if ( "gz".equals( archiveExt )
|| "bz2".equals( archiveExt )
|| "xz".equals( archiveExt )
|| "zst".equals( archiveExt )
|| "snappy".equals( archiveExt ) )
{
String[] tokens = StringUtils.split( path, "." );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2022 The Apache Software Foundation
*
* 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 javax.inject.Named;
import java.io.File;

/**
* Alias for {@link PlexusIoTarFileResourceCollection}
*/
@Named( "tar.zst" )
public class PlexusIoTarZstdFileResourceCollection extends PlexusIoTarFileResourceCollection
{

public PlexusIoTarZstdFileResourceCollection()
{
}

@Override
protected TarFile newTarFile( File file )
{
return new ZstdTarFile( file );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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.apache.commons.compress.compressors.zstandard.ZstdCompressorOutputStream;
import org.codehaus.plexus.archiver.AbstractArchiver;
import org.codehaus.plexus.archiver.ArchiveEntry;
import org.codehaus.plexus.archiver.ArchiverException;
Expand Down Expand Up @@ -484,7 +485,8 @@ public enum TarCompressionMethod
gzip,
bzip2,
snappy,
xz
xz,
zstd

}

Expand All @@ -507,6 +509,10 @@ else if ( TarCompressionMethod.xz.equals( tarCompressionMethod ) )
{
return new XZCompressorOutputStream( bufferedOutputStream( ostream ) );
}
else if ( TarCompressionMethod.zstd.equals( tarCompressionMethod ) )
{
return new ZstdCompressorOutputStream( bufferedOutputStream( ostream ) );
}

return ostream;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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.apache.commons.compress.compressors.zstandard.ZstdCompressorInputStream;
import org.codehaus.plexus.archiver.AbstractUnArchiver;
import org.codehaus.plexus.archiver.ArchiverException;
import org.codehaus.plexus.archiver.util.Streams;
Expand Down Expand Up @@ -159,6 +160,10 @@ else if ( compression == UntarCompressionMethod.XZ )
{
return new XZCompressorInputStream( istream );
}
else if ( compression == UntarCompressionMethod.ZSTD )
{
return new ZstdCompressorInputStream( istream );
}
return istream;
}

Expand All @@ -172,7 +177,8 @@ public enum UntarCompressionMethod
GZIP( "gzip" ),
BZIP2( "bzip2" ),
SNAPPY( "snappy" ),
XZ( "xz" );
XZ( "xz" ),
ZSTD( "zstd" );

final String value;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2022 The Apache Software Foundation
*
* 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 javax.inject.Named;
import java.io.File;

/**
* Extract files in tar with zstd compression
*/
@Named( "tar.zst" )
public class TarZstdUnArchiver extends TarUnArchiver
{

public TarZstdUnArchiver()
{
setupCompressionMethod();
}

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

setupCompressionMethod();
}

private final void setupCompressionMethod()
{
setCompression( UntarCompressionMethod.ZSTD );
}

}
41 changes: 41 additions & 0 deletions src/main/java/org/codehaus/plexus/archiver/tar/ZstdTarFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2022 The Apache Software Foundation
*
* 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 org.codehaus.plexus.archiver.zstd.ZstdUnArchiver;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
* Extension of {@link org.codehaus.plexus.archiver.tar.TarFile} for zst compressed files.
*/
public class ZstdTarFile extends TarFile
{

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

@Override
protected InputStream getInputStream( File file ) throws IOException
{
return ZstdUnArchiver.getZstdInputStream( super.getInputStream( file ) );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2022 The Apache Software Foundation
*
* 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.zstd;

import javax.inject.Named;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import org.codehaus.plexus.archiver.util.Streams;
import org.codehaus.plexus.components.io.attributes.FileAttributes;
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
import org.codehaus.plexus.components.io.resources.PlexusIoCompressedFileResourceCollection;

/**
* Implementation of {@link org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection} for
* zstd compressed files.
*/
@Named( "zst" )
public class PlexusIoZstdResourceCollection extends PlexusIoCompressedFileResourceCollection
{

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

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

@Override
protected InputStream getInputStream( File file ) throws IOException
{
return ZstdUnArchiver.getZstdInputStream( Streams.fileInputStream( file ) );
}

}
90 changes: 90 additions & 0 deletions src/main/java/org/codehaus/plexus/archiver/zstd/ZstdArchiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2022 The Apache Software Foundation
*
* 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.zstd;

import javax.inject.Named;

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;
import org.codehaus.plexus.archiver.exceptions.EmptyArchiveException;

/**
* Zstd archiver.
*/
@Named( "zst" )
public class ZstdArchiver extends AbstractArchiver
{

private final ZstdCompressor compressor = new ZstdCompressor();

public ZstdArchiver()
{
}

/**
* Set compression level
*/
public void setLevel( Integer level )
throws ArchiverException
{
compressor.setLevel( level );
}

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

ResourceIterator iter = getResources();
if ( !iter.hasNext() )
{
throw new EmptyArchiveException( "archive cannot be empty" );
}
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 "zstd";
}

}
Loading

0 comments on commit 5ba0d19

Please sign in to comment.