View Javadoc
1   package org.codehaus.plexus.archiver.zip;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.FileNotFoundException;
6   import java.io.IOException;
7   import java.io.InputStream;
8   import org.apache.commons.compress.archivers.zip.UnixStat;
9   import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
10  import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
11  import org.apache.commons.compress.parallel.InputStreamSupplier;
12  import org.apache.commons.compress.utils.IOUtils;
13  import org.codehaus.plexus.util.DirectoryScanner;
14  import org.junit.Ignore;
15  import org.junit.Test;
16  
17  @SuppressWarnings( "ResultOfMethodCallIgnored" )
18  @Ignore
19  public class ConcurrentJarCreatorTest
20  {
21  
22      @Test
23      public void concurrent()
24          throws Exception
25      {
26          File home = new File( System.getProperty( "user.home" ) );
27          File result = new File( home, "multiStream2-parallel.zip" );
28          ConcurrentJarCreator zipCreator = new ConcurrentJarCreator( Runtime.getRuntime().availableProcessors() );
29  
30          final File file1 = new File( home, "lsrc/plexus" );
31          doAddAll( file1.getPath(), zipCreator );
32  
33          ZipArchiveOutputStream zos = createZipARchiveOutputStream( result );
34          zipCreator.writeTo( zos );
35          zos.close();
36          System.out.println( "Concurrent:" + zipCreator.getStatisticsMessage() );
37      }
38  
39      @Test
40      public void concurrent2() throws Exception
41      {
42          concurrent();
43      }
44  
45      @Test
46      @Ignore
47      public void classic()
48          throws Exception
49      {
50          long startAt = System.currentTimeMillis();
51          File home = new File( System.getProperty( "user.home" ) );
52          File result = new File( home, "multiStream2-classic.zip" );
53  
54          final File file1 = new File( home, "lsrc/plexus" );
55          ZipArchiveOutputStream zos = createZipARchiveOutputStream( result );
56          doAddAll( file1.getPath(), zos );
57          zos.close();
58          System.out.println( "linear:" + ( System.currentTimeMillis() - startAt ) + "ms" );
59  
60      }
61  
62      private ZipArchiveOutputStream createZipARchiveOutputStream( File result ) throws IOException
63      {
64          ZipArchiveOutputStream zos = new ZipArchiveOutputStream( result );
65          zos.setEncoding( "UTF-8" );
66          return zos;
67      }
68  
69      private void doAddAll( String base, ConcurrentJarCreator mos ) throws IOException
70      {
71  
72          DirectoryScanner ds = getIncludedFiles( base );
73  
74          for ( String fileName : ds.getIncludedFiles() )
75          {
76              final File file = new File( base, fileName );
77              ZipArchiveEntry za = createZipArchiveEntry( file, fileName );
78  
79              mos.addArchiveEntry( za, new InputStreamSupplier()
80              {
81  
82                  public InputStream get()
83                  {
84                      try
85                      {
86                          return file.isFile() ? new FileInputStream( file ) : null;
87                      }
88                      catch ( FileNotFoundException e )
89                      {
90                          throw new RuntimeException( e );
91                      }
92                  }
93  
94              }, true );
95          }
96  
97      }
98  
99      private DirectoryScanner getIncludedFiles( String base )
100     {
101         DirectoryScanner ds = new DirectoryScanner();
102         ds.setBasedir( base );
103         ds.scan();
104         return ds;
105     }
106 
107     private void doAddAll( String base, ZipArchiveOutputStream mos ) throws IOException
108     {
109         DirectoryScanner ds = getIncludedFiles( base );
110 
111         for ( String fileName : ds.getIncludedFiles() )
112         {
113             final File file = new File( base, fileName );
114             ZipArchiveEntry za = createZipArchiveEntry( file, fileName );
115 
116             mos.putArchiveEntry( za );
117             if ( file.isFile() )
118             {
119                 FileInputStream input = new FileInputStream( file );
120                 IOUtils.copy( input, mos );
121                 input.close();
122             }
123             mos.closeArchiveEntry();
124         }
125 
126     }
127 
128     @SuppressWarnings( "OctalInteger" )
129     private ZipArchiveEntry createZipArchiveEntry( File file, String name )
130     {
131         ZipArchiveEntry za = new ZipArchiveEntry( file, name );
132         if ( file.isDirectory() )
133         {
134             za.setMethod( ZipArchiveEntry.STORED );
135             za.setSize( 0 );
136             za.setUnixMode( UnixStat.DIR_FLAG | 0664 );
137         }
138         else
139         {
140             za.setMethod( ZipArchiveEntry.DEFLATED );
141             za.setSize( file.length() );
142             za.setUnixMode( UnixStat.FILE_FLAG | 0664 );
143         }
144         za.setTime( file.lastModified() );
145         return za;
146     }
147 
148 }