Skip to content

Add zstd (un)archiver support #226

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 1 commit into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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