Skip to content

Commit 732496b

Browse files
committed
PLXCOPM-153, PLXCOMP-205, PLXCOMP-232: adding support for includes/excludes to TarUnArchiver
2 parents 32af216 + 63fbf8f commit 732496b

File tree

5 files changed

+147
-65
lines changed

5 files changed

+147
-65
lines changed

src/main/java/org/codehaus/plexus/archiver/AbstractUnArchiver.java

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@
1717
* limitations under the License.
1818
*/
1919

20+
import org.codehaus.plexus.archiver.util.ArchiveEntryUtils;
2021
import org.codehaus.plexus.archiver.util.FilterSupport;
2122
import org.codehaus.plexus.components.io.fileselectors.FileSelector;
2223
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
2324
import org.codehaus.plexus.logging.AbstractLogEnabled;
25+
import org.codehaus.plexus.util.FileUtils;
26+
import org.codehaus.plexus.util.IOUtil;
2427

25-
import java.io.File;
26-
import java.io.IOException;
27-
import java.io.InputStream;
28+
import java.io.*;
2829
import java.util.ArrayList;
30+
import java.util.Date;
2931
import java.util.Iterator;
3032
import java.util.List;
3133

@@ -287,4 +289,57 @@ public void setIgnorePermissions( final boolean ignorePermissions )
287289
this.ignorePermissions = ignorePermissions;
288290
}
289291

292+
protected void extractFile( final File srcF, final File dir, final InputStream compressedInputStream,
293+
final String entryName, final Date entryDate, final boolean isDirectory,
294+
final Integer mode )
295+
throws IOException, ArchiverException
296+
{
297+
final File f = FileUtils.resolveFile(dir, entryName);
298+
299+
try
300+
{
301+
if ( !isOverwrite() && f.exists() && ( f.lastModified() >= entryDate.getTime() ) )
302+
{
303+
return;
304+
}
305+
306+
// create intermediary directories - sometimes zip don't add them
307+
final File dirF = f.getParentFile();
308+
if ( dirF != null )
309+
{
310+
dirF.mkdirs();
311+
}
312+
313+
if ( isDirectory )
314+
{
315+
f.mkdirs();
316+
}
317+
else
318+
{
319+
OutputStream out = null;
320+
try
321+
{
322+
out = new FileOutputStream( f );
323+
324+
IOUtil.copy(compressedInputStream, out);
325+
}
326+
finally
327+
{
328+
IOUtil.close( out );
329+
}
330+
}
331+
332+
f.setLastModified( entryDate.getTime() );
333+
334+
if ( !isIgnorePermissions() && mode != null && !isDirectory)
335+
{
336+
ArchiveEntryUtils.chmod(f, mode, getLogger(), isUseJvmChmod());
337+
}
338+
}
339+
catch ( final FileNotFoundException ex )
340+
{
341+
getLogger().warn( "Unable to expand to file " + f.getPath() );
342+
}
343+
}
344+
290345
}

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
*/
1919

2020
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
21+
import org.codehaus.plexus.archiver.AbstractUnArchiver;
2122
import org.codehaus.plexus.archiver.ArchiverException;
2223
import org.codehaus.plexus.archiver.util.EnumeratedAttribute;
23-
import org.codehaus.plexus.archiver.zip.AbstractZipUnArchiver;
2424
import org.codehaus.plexus.util.IOUtil;
2525

2626
import java.io.BufferedInputStream;
@@ -35,7 +35,7 @@
3535
* @version $Revision$ $Date$
3636
*/
3737
public class TarUnArchiver
38-
extends AbstractZipUnArchiver
38+
extends AbstractUnArchiver
3939
{
4040
public TarUnArchiver()
4141
{
@@ -78,27 +78,37 @@ public void setEncoding( String encoding )
7878

7979
protected void execute()
8080
throws ArchiverException
81+
{
82+
execute(getSourceFile(), getDestDirectory());
83+
}
84+
85+
protected void execute( String path, File outputDirectory )
86+
{
87+
execute( new File(path), getDestDirectory());
88+
}
89+
90+
protected void execute( File sourceFile, File outputDirectory )
8191
{
8292
TarInputStream tis = null;
8393
try
8494
{
85-
getLogger().info( "Expanding: " + getSourceFile() + " into " + getDestDirectory() );
86-
File sourceFile = getSourceFile();
87-
TarFile tarFile = new TarFile( sourceFile );
95+
getLogger().info( "Expanding: " + sourceFile + " into " + outputDirectory );
96+
8897
tis = new TarInputStream( compression.decompress( sourceFile,
89-
new BufferedInputStream(
90-
new FileInputStream( sourceFile ) ) ) );
98+
new BufferedInputStream(
99+
new FileInputStream( sourceFile ) ) ) );
91100
TarEntry te;
101+
TarFile tarFile = new TarFile( sourceFile );
92102

93103
while ( ( te = tis.getNextEntry() ) != null )
94104
{
95-
96105
TarResource fileInfo = new TarResource( tarFile, te);
97106
if (!isSelected( te.getName(), fileInfo )) {
98107
continue;
99108
}
100109

101-
extractFile(getSourceFile(), getDestDirectory(), tis, te.getName(), te.getModTime(),
110+
extractFile( sourceFile, outputDirectory, tis, te.getName(), te.getModTime(),
111+
102112
te.isDirectory(), te.getMode() != 0 ? te.getMode() : null);
103113
}
104114
getLogger().debug( "expand complete" );
@@ -205,4 +215,5 @@ else if ( BZIP2.equals( value ) )
205215
return istream;
206216
}
207217
}
218+
208219
}

src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipUnArchiver.java

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -191,59 +191,6 @@ private void extractFileIfIncluded( final File sourceFile, final File destDirect
191191
}
192192
}
193193

194-
protected void extractFile( final File srcF, final File dir, final InputStream compressedInputStream,
195-
final String entryName, final Date entryDate, final boolean isDirectory,
196-
final Integer mode )
197-
throws IOException, ArchiverException
198-
{
199-
final File f = FileUtils.resolveFile( dir, entryName );
200-
201-
try
202-
{
203-
if ( !isOverwrite() && f.exists() && ( f.lastModified() >= entryDate.getTime() ) )
204-
{
205-
return;
206-
}
207-
208-
// create intermediary directories - sometimes zip don't add them
209-
final File dirF = f.getParentFile();
210-
if ( dirF != null )
211-
{
212-
dirF.mkdirs();
213-
}
214-
215-
if ( isDirectory )
216-
{
217-
f.mkdirs();
218-
}
219-
else
220-
{
221-
OutputStream out = null;
222-
try
223-
{
224-
out = new FileOutputStream( f );
225-
226-
IOUtil.copy( compressedInputStream, out );
227-
}
228-
finally
229-
{
230-
IOUtil.close( out );
231-
}
232-
}
233-
234-
f.setLastModified( entryDate.getTime() );
235-
236-
if ( !isIgnorePermissions() && mode != null && !isDirectory)
237-
{
238-
ArchiveEntryUtils.chmod( f, mode, getLogger(), isUseJvmChmod() );
239-
}
240-
}
241-
catch ( final FileNotFoundException ex )
242-
{
243-
getLogger().warn( "Unable to expand to file " + f.getPath() );
244-
}
245-
}
246-
247194
protected void execute( final String path, final File outputDirectory )
248195
throws ArchiverException
249196
{

src/test/jars/test.tar.gz

383 Bytes
Binary file not shown.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.codehaus.plexus.archiver.tar;
2+
3+
import org.codehaus.plexus.PlexusTestCase;
4+
import org.codehaus.plexus.archiver.UnArchiver;
5+
import org.codehaus.plexus.components.io.fileselectors.FileSelector;
6+
import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
7+
import org.codehaus.plexus.util.FileUtils;
8+
9+
import java.io.File;
10+
import java.lang.reflect.Method;
11+
12+
/**
13+
* @author <a href="mailto:[email protected]">Viktor Sadovnikov</a>
14+
* @version $Revision$ $Date$
15+
*/
16+
public class TarUnArchiverTest extends PlexusTestCase
17+
{
18+
19+
private void runUnarchiver( FileSelector[] selectors, boolean[] results )
20+
throws Exception
21+
{
22+
String s = "target/tar-unarchiver-tests";
23+
24+
File testJar = new File( getBasedir(), "src/test/jars/test.tar.gz" );
25+
26+
File outputDirectory = new File( getBasedir(), s );
27+
28+
TarUnArchiver tarUn = (TarUnArchiver) lookup( UnArchiver.ROLE, "tar.gz" );
29+
tarUn.setSourceFile(testJar);
30+
tarUn.setDestDirectory(outputDirectory);
31+
tarUn.setFileSelectors(selectors);
32+
33+
FileUtils.deleteDirectory( outputDirectory );
34+
35+
tarUn.extract( testJar.getAbsolutePath(), outputDirectory );
36+
37+
assertFileExistance( s, "/resources/artifactId/test.properties", results[0]);
38+
assertFileExistance( s, "/resources/artifactId/directory/test.properties", results[1]);
39+
assertFileExistance( s, "/META-INF/MANIFEST.MF", results[2]);
40+
41+
}
42+
43+
private void assertFileExistance( String s, String file, boolean exists ) {
44+
File f0 = new File( getBasedir(), s + file );
45+
assertEquals( String.format("Did %s expect to find %s file", exists ? "" : "NOT", f0.getAbsoluteFile()), exists, f0.exists() );
46+
}
47+
48+
public void testExtractingADirectory() throws Exception
49+
{
50+
runUnarchiver( null, new boolean[]{ true, true, true } );
51+
}
52+
53+
public void testSelectors()
54+
throws Exception
55+
{
56+
IncludeExcludeFileSelector fileSelector = new IncludeExcludeFileSelector();
57+
runUnarchiver( new FileSelector[]{ fileSelector }, new boolean[]{ true, true, true } );
58+
59+
fileSelector.setExcludes( new String[]{ "**/test.properties" } );
60+
runUnarchiver( new FileSelector[]{ fileSelector }, new boolean[]{ false, false, true } );
61+
62+
fileSelector.setIncludes( new String[]{ "**/test.properties" } );
63+
fileSelector.setExcludes( null );
64+
runUnarchiver( new FileSelector[]{ fileSelector }, new boolean[]{ true, true, false } );
65+
66+
fileSelector.setExcludes( new String[]{ "resources/artifactId/directory/test.properties" } );
67+
runUnarchiver( new FileSelector[]{ fileSelector }, new boolean[]{ true, false, false } );
68+
}
69+
}

0 commit comments

Comments
 (0)