sjno
asked on
Running Zipping Program from within JSP
Ok, here is the situation. We have a website linked to a massive database (USDA). Through the website the user can pair down this data to get a more refined data set. For example, The user can see the reported losses report by corn farmers in Kansas for 2000 through 2003. We want to allow the user to no only see the data in the browser, but to be able to save and download the data as a Comma Seperated Value (CSV) file. Saving the file is failry simple and we have that worked out. These files are huge, so we want to zip them after they are created to make downloads faster. The only problem is I can't get teyh command line version of any zipping program (winzip, winrar, etc..) to run from inside JSP code. Here is the code, please help if you have any ideas.
// the file is already created
Runtime rt = Runtime.getRuntime();
Process p = null;
try {
String[] cmd = {"winrar.exe A", "d:\\tomcat4\\webapps\\das hboard\\do wn.zip d:\\tomcat4\\webapps\\dash board\\dat a.csv"};
p = rt.exec(cmd);
p.waitFor();
} catch (Exception e)
{
e.printStackTrace();
}
// the file is already created
Runtime rt = Runtime.getRuntime();
Process p = null;
try {
String[] cmd = {"winrar.exe A", "d:\\tomcat4\\webapps\\das
p = rt.exec(cmd);
p.waitFor();
} catch (Exception e)
{
e.printStackTrace();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
If I knew how to split points I would but since dyanet answered first they gets the points I guess.
You can play around with the 'BUFFER' size for performance.
import java.util.ArrayList;
import java.util.zip.*;
import java.io.*;
static final int BUFFER = 2048;
byte data[] = new byte[BUFFER];
FileOutputStream dest = new FileOutputStream("c:\\myda
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)
BufferedInputStream origin;
ArrayList filenameList = new ArrayList();
filenameList.add("mycsvfil
filenameList.add("mycsvfil
//etc..
for (int counter=0; counter<filenameList.size(
FileInputStream fi = new FileInputStream((String)fi
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry((String)filenameL
entry.setMethod(ZipOutputS
out.putNextEntry(entry); //adds entry to zip file
//write data to zip file
int count;
while((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}//end while
origin.close();
}//end for
out.close();
Hey presto you have a compressed zip file that you can spit back to your user.
Hope this helps,
Damian