View Javadoc
1   package com.simpligility.maven.plugins.android.common;
2   
3   import org.apache.maven.plugin.MojoExecutionException;
4   import org.apache.maven.plugin.logging.Log;
5   import org.codehaus.plexus.archiver.ArchiverException;
6   import org.codehaus.plexus.archiver.UnArchiver;
7   import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
8   import org.codehaus.plexus.components.io.fileselectors.FileInfo;
9   import org.codehaus.plexus.components.io.fileselectors.FileSelector;
10  import org.codehaus.plexus.logging.Logger;
11  
12  import java.io.File;
13  import java.io.IOException;
14  
15  /**
16   * Extract an archive to a given location.
17   */
18  public final class ZipExtractor
19  {
20      private final Log log;
21  
22      public ZipExtractor( Log log )
23      {
24          this.log = log;
25      }
26  
27      public void extract( File zipFile, File targetFolder, final String suffixToExclude ) throws MojoExecutionException
28      {
29          final UnArchiver unArchiver = new ZipUnArchiver( zipFile )
30          {
31              @Override
32              protected Logger getLogger()
33              {
34                  return new MavenToPlexusLogAdapter( log );
35              }
36          };
37  
38          targetFolder.mkdirs();
39  
40          final FileSelector exclusionFilter = new FileSelector()
41          {
42              @Override
43              public boolean isSelected( FileInfo fileInfo ) throws IOException
44              {
45                  return !fileInfo.getName().endsWith( suffixToExclude );
46              }
47          };
48  
49          unArchiver.setDestDirectory( targetFolder );
50          unArchiver.setFileSelectors( new FileSelector[] { exclusionFilter } );
51  
52          log.debug( "Extracting archive to " + targetFolder );
53          try
54          {
55              unArchiver.extract();
56          }
57          catch ( ArchiverException e )
58          {
59              throw new MojoExecutionException( "ArchiverException while extracting " + zipFile.getAbsolutePath()
60                      + ". Message: " + e.getLocalizedMessage(), e );
61          }
62      }
63  }