Skip to content

Commit 3e66c11

Browse files
committed
Made TarUnArchiver not subclass AbstractZipUnArchiver
Refactoring suggested by Viktor Sadovnikov
1 parent adacddc commit 3e66c11

File tree

3 files changed

+84
-69
lines changed

3 files changed

+84
-69
lines changed

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,22 @@
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

2528
import java.io.File;
29+
import java.io.FileNotFoundException;
30+
import java.io.FileOutputStream;
2631
import java.io.IOException;
2732
import java.io.InputStream;
33+
import java.io.OutputStream;
2834
import java.util.ArrayList;
35+
import java.util.Date;
2936
import java.util.Iterator;
3037
import java.util.List;
3138

@@ -281,4 +288,57 @@ public void setIgnorePermissions( final boolean ignorePermissions )
281288
this.ignorePermissions = ignorePermissions;
282289
}
283290

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

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
2121
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
2222
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
23+
import org.codehaus.plexus.archiver.AbstractUnArchiver;
2324
import org.codehaus.plexus.archiver.ArchiverException;
2425
import org.codehaus.plexus.archiver.util.EnumeratedAttribute;
25-
import org.codehaus.plexus.archiver.zip.AbstractZipUnArchiver;
2626
import org.codehaus.plexus.util.IOUtil;
2727

2828
import java.io.BufferedInputStream;
@@ -37,7 +37,7 @@
3737
* @version $Revision$ $Date$
3838
*/
3939
public class TarUnArchiver
40-
extends AbstractZipUnArchiver
40+
extends AbstractUnArchiver
4141
{
4242
public TarUnArchiver()
4343
{
@@ -80,34 +80,42 @@ public void setEncoding( String encoding )
8080

8181
protected void execute()
8282
throws ArchiverException
83+
{
84+
execute( getSourceFile(), getDestDirectory() );
85+
}
86+
87+
protected void execute( String path, File outputDirectory )
88+
{
89+
execute( new File( path ), getDestDirectory() );
90+
}
91+
92+
protected void execute( File sourceFile, File destDirectory )
93+
throws ArchiverException
8394
{
8495
TarArchiveInputStream tis = null;
8596
try
8697
{
87-
getLogger().info( "Expanding: " + getSourceFile() + " into " + getDestDirectory() );
88-
File sourceFile = getSourceFile();
98+
getLogger().info( "Expanding: " + sourceFile + " into " + destDirectory );
8999
TarFile tarFile = new TarFile( sourceFile );
90-
tis = new TarArchiveInputStream( compression.decompress( sourceFile,
91-
new BufferedInputStream(
92-
new FileInputStream( sourceFile ) ) ) );
100+
tis = new TarArchiveInputStream(
101+
compression.decompress( sourceFile, new BufferedInputStream( new FileInputStream( sourceFile ) ) ) );
93102
TarArchiveEntry te;
94103
while ( ( te = tis.getNextTarEntry() ) != null )
95104
{
96-
97-
TarResource fileInfo = new TarResource( tarFile, te);
98-
if (!isSelected( te.getName(), fileInfo )) {
99-
continue;
105+
TarResource fileInfo = new TarResource( tarFile, te );
106+
if ( isSelected( te.getName(), fileInfo ) )
107+
{
108+
extractFile( sourceFile, destDirectory, tis, te.getName(), te.getModTime(), te.isDirectory(),
109+
te.getMode() != 0 ? te.getMode() : null );
100110
}
101111

102-
extractFile(getSourceFile(), getDestDirectory(), tis, te.getName(), te.getModTime(),
103-
te.isDirectory(), te.getMode() != 0 ? te.getMode() : null);
104112
}
105113
getLogger().debug( "expand complete" );
106114

107115
}
108116
catch ( IOException ioe )
109117
{
110-
throw new ArchiverException( "Error while expanding " + getSourceFile().getAbsolutePath(), ioe );
118+
throw new ArchiverException( "Error while expanding " + sourceFile.getAbsolutePath(), ioe );
111119
}
112120
finally
113121
{
@@ -179,7 +187,7 @@ public UntarCompressionMethod( String method )
179187
*/
180188
public String[] getValues()
181189
{
182-
return new String[]{NONE, GZIP, BZIP2};
190+
return new String[]{ NONE, GZIP, BZIP2 };
183191
}
184192

185193
/**
@@ -189,7 +197,7 @@ public String[] getValues()
189197
* @param file provides location information for BuildException
190198
* @param istream input stream
191199
* @return input stream with on-the-fly decompression
192-
* @throws IOException thrown by GZIPInputStream constructor
200+
* @throws IOException thrown by GZIPInputStream constructor
193201
*/
194202
private InputStream decompress( final File file, final InputStream istream )
195203
throws IOException, ArchiverException

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

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

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

0 commit comments

Comments
 (0)