1 package org.codehaus.plexus.archiver.tar;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
22 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
23 import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
24 import org.codehaus.plexus.archiver.AbstractArchiveContentLister;
25 import org.codehaus.plexus.archiver.ArchiveContentEntry;
26 import org.codehaus.plexus.archiver.ArchiverException;
27 import org.codehaus.plexus.util.IOUtil;
28
29
30
31 import java.io.BufferedInputStream;
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.util.zip.GZIPInputStream;
37 import java.util.List;
38 import java.util.ArrayList;
39
40
41
42
43
44
45 public class TarArchiveContentLister
46 extends AbstractArchiveContentLister
47 {
48 public TarArchiveContentLister()
49 {
50 }
51
52 public TarArchiveContentLister( File sourceFile )
53 {
54 super( sourceFile );
55 }
56
57
58
59
60 private UntarCompressionMethod compression = UntarCompressionMethod.NONE;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public void setCompression( UntarCompressionMethod method )
76 {
77 compression = method;
78 }
79
80
81
82
83 public void setEncoding( String encoding )
84 {
85 getLogger().warn( "The TarArchiveContentLister doesn't support the encoding attribute" );
86 }
87
88 private static class MyTarResource
89 extends TarResource
90 {
91 public MyTarResource( TarFile tarFile, TarArchiveEntry entry )
92 {
93 super( tarFile, entry );
94 }
95
96 public ArchiveContentEntry asArchiveContentEntry()
97 {
98 if ( isSymbolicLink() )
99 {
100 return ArchiveContentEntry.createSymlinkEntry( getName(), this, -1 );
101 }
102 else if ( isDirectory() )
103 {
104 return ArchiveContentEntry.createDirectoryEntry( getName(), this, -1 );
105 }
106 else
107 {
108 return ArchiveContentEntry.createFileEntry( getName(), this, -1 );
109 }
110 }
111 };
112
113 protected List<ArchiveContentEntry> execute()
114 throws ArchiverException
115 {
116 ArrayList<ArchiveContentEntry> archiveContentList = new ArrayList<ArchiveContentEntry>();
117 getLogger().debug( "listing: " + getSourceFile() );
118 TarArchiveInputStream tis = null;
119 try
120 {
121 TarFile tarFile = new TarFile( getSourceFile() );
122 tis = new TarArchiveInputStream(
123 decompress( compression, getSourceFile(),
124 new BufferedInputStream( new FileInputStream( getSourceFile() ) ) ) );
125 TarArchiveEntry te;
126 while ( ( te = tis.getNextTarEntry() ) != null )
127 {
128 MyTarResource fileInfo = new MyTarResource( tarFile, te );
129 if ( isSelected( te.getName(), fileInfo ) )
130 {
131 ArchiveContentEntry ae = fileInfo.asArchiveContentEntry();
132 archiveContentList.add( ae );
133 }
134 }
135 getLogger().debug( "listing complete" );
136 }
137 catch ( final IOException ioe )
138 {
139 throw new ArchiverException( "Error while listing " + getSourceFile().getAbsolutePath(), ioe );
140 }
141 finally
142 {
143 IOUtil.close( tis );
144 }
145 return archiveContentList;
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204 private InputStream decompress( UntarCompressionMethod aiCompression, final File file, final InputStream istream )
205 throws IOException, ArchiverException
206 {
207 if ( aiCompression == UntarCompressionMethod.GZIP )
208 {
209 return new GZIPInputStream( istream );
210 }
211 else if ( aiCompression == UntarCompressionMethod.BZIP2 )
212 {
213 return new BZip2CompressorInputStream( istream );
214 }
215 else if ( aiCompression == UntarCompressionMethod.SNAPPY )
216 {
217
218
219 }
220 return istream;
221 }
222
223
224
225
226 public static enum UntarCompressionMethod
227 {
228 NONE( "none" ), GZIP( "gzip" ), BZIP2( "bzip2" ), SNAPPY( "snappy" );
229
230 final String value;
231
232
233
234
235 UntarCompressionMethod( String value )
236 {
237 this.value = value;
238 }
239 }
240 }