In my project, i need generate an agent for users, and the agent include some jars(about 22M),some source class(about 200k) and a XML. The server will generate different xml for defferent user,so I have to generate the agent dynamically. I used Runtime.getRuntime().exec("tar...")
to tar the files to generate agents.
When I run the unit test , the generation costs a lot of time, about 2min for every agent. I cann't let the user wait a page for 2min...so do it exists any other way to make this program more efficiency, or there is another way to generate the agent fast and smoothly?? appreciate!
/**
* tar the agents
* @param inputFiles agent files
* @param outputFile agent tar
* @param baseDir the directory path to run "tar" command
*/
public static void tarFile(String[] inputFiles,String outputFile,String baseDir){
String cmd="tar -zcf "+outputFile+" ";
for (int i = 0; i < inputFiles.length; i++) {
cmd+=inputFiles[i]+" ";
}
System.out.println(cmd);
try {
Process process=Runtime.getRuntime().exec(cmd, null, new File(baseDir));
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(process.getInputStream()));
String s;
while ((s=bufferedReader.readLine())!=null) {
System.out.println(s);
}
process.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this is my tar class, the param--basedir is to set the command's execution path
Use Apache Commons Compress to create the
tar
archive directly in Java.See TarArchiveOutputStream.
Here a complete servlet that creates that
tar
output on the fly:The client will start receiving the archive instantly, without having to wait for the
tar
file to be created in advance. Also this does not require writing the archive to disc.