View Javadoc

1   package com.eviware.soapui.tools;
2   
3   import java.io.File;
4   import java.io.FileFilter;
5   import java.io.FileInputStream;
6   import java.io.FileOutputStream;
7   import java.io.IOException;
8   import java.util.jar.JarEntry;
9   import java.util.jar.JarOutputStream;
10  import java.util.jar.Manifest;
11  
12  import org.apache.log4j.Logger;
13  
14  public class JarPackager
15  {
16  	public static int BUFFER_SIZE = 10240;
17  	static Logger log = Logger.getLogger( JarPackager.class );
18  
19  	public static void copyFileToDir( File fromFile, File toDir )
20  	{
21  		File toFile = new File( toDir, fromFile.getName() );
22  		try
23  		{
24  			copyFile( fromFile, toFile );
25  		}
26  		catch( IOException e )
27  		{
28  			log.error( e.getMessage(), e );
29  		}
30  
31  	}
32  
33  	public static void copyAllFromTo( File fromDir, File toDir, FileFilter filter )
34  	{
35  		if( fromDir.isDirectory() & toDir.isDirectory() )
36  		{
37  			log.info( "Coping files from " + fromDir.getAbsolutePath() + " to " + toDir.getAbsolutePath() );
38  			File[] fromFiles = filter == null ? fromDir.listFiles() : fromDir.listFiles( filter );
39  			for( File file : fromFiles )
40  			{
41  				File toFile = new File( toDir, file.getName() );
42  				try
43  				{
44  					copyFile( file, toFile );
45  				}
46  				catch( IOException e )
47  				{
48  					log.error( e.getMessage(), e );
49  				}
50  			}
51  		}
52  		else
53  		{
54  			log.error( fromDir.getAbsolutePath() + " or " + toDir.getAbsolutePath() + " is not directory!" );
55  		}
56  	}
57  
58  	private static void copyFile( File fromFile, File toFile ) throws IOException
59  	{
60  		FileInputStream from = null;
61  		FileOutputStream to = null;
62  		try
63  		{
64  			from = new FileInputStream( fromFile );
65  			to = new FileOutputStream( toFile );
66  			byte[] buffer = new byte[4096];
67  			int bytesRead;
68  
69  			while( ( bytesRead = from.read( buffer ) ) != -1 )
70  				to.write( buffer, 0, bytesRead ); // write
71  		}
72  		catch( Exception e )
73  		{
74  			log.error( e );
75  		}
76  		finally
77  		{
78  			if( from != null )
79  				try
80  				{
81  					from.close();
82  				}
83  				catch( IOException e )
84  				{
85  					throw e;
86  				}
87  			if( to != null )
88  				try
89  				{
90  					to.close();
91  				}
92  				catch( IOException e )
93  				{
94  					throw e;
95  				}
96  		}
97  
98  	}
99  
100 	public static void createJarArchive( File archiveFile, File root, File... tobeJared )
101 	{
102 		try
103 		{
104 			byte buffer[] = new byte[BUFFER_SIZE];
105 			// Open archive file
106 			FileOutputStream stream = new FileOutputStream( archiveFile );
107 			JarOutputStream out = new JarOutputStream( stream, new Manifest() );
108 
109 			for( int i = 0; i < tobeJared.length; i++ )
110 			{
111 				if( tobeJared[i] == null || !tobeJared[i].exists() )
112 					continue; // Just in case...
113 
114 				// Add archive entry
115 				String jarName = tobeJared[i].isDirectory() ? tobeJared[i].getAbsolutePath() + "/" : tobeJared[i]
116 						.getAbsolutePath();
117 				jarName = jarName.replace( root.getAbsolutePath(), "" );
118 				JarEntry jarAdd = new JarEntry( jarName );
119 				log.info( "Adding " + jarName );
120 				jarAdd.setTime( tobeJared[i].lastModified() );
121 				out.putNextEntry( jarAdd );
122 
123 				if( jarAdd.isDirectory() )
124 					continue;
125 
126 				// Write file to archive
127 				FileInputStream in = new FileInputStream( tobeJared[i] );
128 				while( true )
129 				{
130 					int nRead = in.read( buffer, 0, buffer.length );
131 					if( nRead <= 0 )
132 						break;
133 					out.write( buffer, 0, nRead );
134 				}
135 				in.close();
136 			}
137 
138 			out.close();
139 			stream.close();
140 			log.info( "Adding completed OK" );
141 		}
142 		catch( Exception ex )
143 		{
144 			log.error( ex.getMessage(), ex );
145 		}
146 	}
147 
148 }