Skip to content

Commit 5ba0d19

Browse files
pleeplopplamentotev
authored andcommitted
Add zstd (un)archiver support
1 parent 8656ee0 commit 5ba0d19

14 files changed

+707
-2
lines changed

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@
9797
<version>1.9</version>
9898
<scope>runtime</scope>
9999
</dependency>
100+
<dependency>
101+
<groupId>com.github.luben</groupId>
102+
<artifactId>zstd-jni</artifactId>
103+
<version>1.5.2-3</version>
104+
<scope>runtime</scope>
105+
</dependency>
100106
<dependency>
101107
<groupId>com.google.code.findbugs</groupId>
102108
<artifactId>jsr305</artifactId>

src/main/java/org/codehaus/plexus/archiver/manager/DefaultArchiverManager.java

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ String getFileExtention( @Nonnull File file )
110110
if ( "gz".equals( archiveExt )
111111
|| "bz2".equals( archiveExt )
112112
|| "xz".equals( archiveExt )
113+
|| "zst".equals( archiveExt )
113114
|| "snappy".equals( archiveExt ) )
114115
{
115116
String[] tokens = StringUtils.split( path, "." );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2022 The Apache Software Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.codehaus.plexus.archiver.tar;
17+
18+
import javax.inject.Named;
19+
import java.io.File;
20+
21+
/**
22+
* Alias for {@link PlexusIoTarFileResourceCollection}
23+
*/
24+
@Named( "tar.zst" )
25+
public class PlexusIoTarZstdFileResourceCollection extends PlexusIoTarFileResourceCollection
26+
{
27+
28+
public PlexusIoTarZstdFileResourceCollection()
29+
{
30+
}
31+
32+
@Override
33+
protected TarFile newTarFile( File file )
34+
{
35+
return new ZstdTarFile( file );
36+
}
37+
38+
}

src/main/java/org/codehaus/plexus/archiver/tar/TarArchiver.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
3232
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
3333
import org.apache.commons.compress.compressors.xz.XZCompressorOutputStream;
34+
import org.apache.commons.compress.compressors.zstandard.ZstdCompressorOutputStream;
3435
import org.codehaus.plexus.archiver.AbstractArchiver;
3536
import org.codehaus.plexus.archiver.ArchiveEntry;
3637
import org.codehaus.plexus.archiver.ArchiverException;
@@ -484,7 +485,8 @@ public enum TarCompressionMethod
484485
gzip,
485486
bzip2,
486487
snappy,
487-
xz
488+
xz,
489+
zstd
488490

489491
}
490492

@@ -507,6 +509,10 @@ else if ( TarCompressionMethod.xz.equals( tarCompressionMethod ) )
507509
{
508510
return new XZCompressorOutputStream( bufferedOutputStream( ostream ) );
509511
}
512+
else if ( TarCompressionMethod.zstd.equals( tarCompressionMethod ) )
513+
{
514+
return new ZstdCompressorOutputStream( bufferedOutputStream( ostream ) );
515+
}
510516

511517
return ostream;
512518
}

src/main/java/org/codehaus/plexus/archiver/tar/TarUnArchiver.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
3131
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
3232
import org.apache.commons.compress.compressors.xz.XZCompressorInputStream;
33+
import org.apache.commons.compress.compressors.zstandard.ZstdCompressorInputStream;
3334
import org.codehaus.plexus.archiver.AbstractUnArchiver;
3435
import org.codehaus.plexus.archiver.ArchiverException;
3536
import org.codehaus.plexus.archiver.util.Streams;
@@ -159,6 +160,10 @@ else if ( compression == UntarCompressionMethod.XZ )
159160
{
160161
return new XZCompressorInputStream( istream );
161162
}
163+
else if ( compression == UntarCompressionMethod.ZSTD )
164+
{
165+
return new ZstdCompressorInputStream( istream );
166+
}
162167
return istream;
163168
}
164169

@@ -172,7 +177,8 @@ public enum UntarCompressionMethod
172177
GZIP( "gzip" ),
173178
BZIP2( "bzip2" ),
174179
SNAPPY( "snappy" ),
175-
XZ( "xz" );
180+
XZ( "xz" ),
181+
ZSTD( "zstd" );
176182

177183
final String value;
178184

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2022 The Apache Software Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.codehaus.plexus.archiver.tar;
17+
18+
import javax.inject.Named;
19+
import java.io.File;
20+
21+
/**
22+
* Extract files in tar with zstd compression
23+
*/
24+
@Named( "tar.zst" )
25+
public class TarZstdUnArchiver extends TarUnArchiver
26+
{
27+
28+
public TarZstdUnArchiver()
29+
{
30+
setupCompressionMethod();
31+
}
32+
33+
public TarZstdUnArchiver( File sourceFile )
34+
{
35+
super( sourceFile );
36+
37+
setupCompressionMethod();
38+
}
39+
40+
private final void setupCompressionMethod()
41+
{
42+
setCompression( UntarCompressionMethod.ZSTD );
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2022 The Apache Software Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.codehaus.plexus.archiver.tar;
17+
18+
import org.codehaus.plexus.archiver.zstd.ZstdUnArchiver;
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
24+
/**
25+
* Extension of {@link org.codehaus.plexus.archiver.tar.TarFile} for zst compressed files.
26+
*/
27+
public class ZstdTarFile extends TarFile
28+
{
29+
30+
public ZstdTarFile( File file )
31+
{
32+
super( file );
33+
}
34+
35+
@Override
36+
protected InputStream getInputStream( File file ) throws IOException
37+
{
38+
return ZstdUnArchiver.getZstdInputStream( super.getInputStream( file ) );
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2022 The Apache Software Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.codehaus.plexus.archiver.zstd;
17+
18+
import javax.inject.Named;
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
import java.util.HashMap;
24+
25+
import org.codehaus.plexus.archiver.util.Streams;
26+
import org.codehaus.plexus.components.io.attributes.FileAttributes;
27+
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes;
28+
import org.codehaus.plexus.components.io.resources.PlexusIoCompressedFileResourceCollection;
29+
30+
/**
31+
* Implementation of {@link org.codehaus.plexus.components.io.resources.PlexusIoResourceCollection} for
32+
* zstd compressed files.
33+
*/
34+
@Named( "zst" )
35+
public class PlexusIoZstdResourceCollection extends PlexusIoCompressedFileResourceCollection
36+
{
37+
38+
@Override
39+
protected PlexusIoResourceAttributes getAttributes( File file ) throws IOException
40+
{
41+
return new FileAttributes( file, new HashMap<Integer, String>(), new HashMap<Integer, String>() );
42+
}
43+
44+
@Override
45+
protected String getDefaultExtension()
46+
{
47+
return ".zst";
48+
}
49+
50+
@Override
51+
protected InputStream getInputStream( File file ) throws IOException
52+
{
53+
return ZstdUnArchiver.getZstdInputStream( Streams.fileInputStream( file ) );
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2022 The Apache Software Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.codehaus.plexus.archiver.zstd;
17+
18+
import javax.inject.Named;
19+
20+
import java.io.IOException;
21+
import org.codehaus.plexus.archiver.AbstractArchiver;
22+
import org.codehaus.plexus.archiver.ArchiveEntry;
23+
import org.codehaus.plexus.archiver.ArchiverException;
24+
import org.codehaus.plexus.archiver.ResourceIterator;
25+
import org.codehaus.plexus.archiver.exceptions.EmptyArchiveException;
26+
27+
/**
28+
* Zstd archiver.
29+
*/
30+
@Named( "zst" )
31+
public class ZstdArchiver extends AbstractArchiver
32+
{
33+
34+
private final ZstdCompressor compressor = new ZstdCompressor();
35+
36+
public ZstdArchiver()
37+
{
38+
}
39+
40+
/**
41+
* Set compression level
42+
*/
43+
public void setLevel( Integer level )
44+
throws ArchiverException
45+
{
46+
compressor.setLevel( level );
47+
}
48+
49+
@Override
50+
protected void execute() throws ArchiverException, IOException
51+
{
52+
if ( !checkForced() )
53+
{
54+
return;
55+
}
56+
57+
ResourceIterator iter = getResources();
58+
if ( !iter.hasNext() )
59+
{
60+
throw new EmptyArchiveException( "archive cannot be empty" );
61+
}
62+
ArchiveEntry entry = iter.next();
63+
if ( iter.hasNext() )
64+
{
65+
throw new ArchiverException( "There is more than one file in input." );
66+
}
67+
compressor.setSource( entry.getResource() );
68+
compressor.setDestFile( getDestFile() );
69+
compressor.compress();
70+
}
71+
72+
@Override
73+
public boolean isSupportingForced()
74+
{
75+
return true;
76+
}
77+
78+
@Override
79+
protected void close() throws IOException
80+
{
81+
compressor.close();
82+
}
83+
84+
@Override
85+
protected String getArchiveType()
86+
{
87+
return "zstd";
88+
}
89+
90+
}

0 commit comments

Comments
 (0)