Solved

Running Zipping Program from within JSP

Posted on 2003-11-13
3
217 Views
Last Modified: 2010-04-01
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\\dashboard\\down.zip d:\\tomcat4\\webapps\\dashboard\\data.csv"};
 p = rt.exec(cmd);
p.waitFor();
} catch (Exception e)
{
e.printStackTrace();
}
0
Comment
Question by:sjno
3 Comments
 
LVL 2

Accepted Solution

by:
dyanet earned 250 total points
ID: 9740753
Two other solutions for you:
1. use java.util.zip package to compress the files. Use FileInputStream to read the file in and ZipOutputStream to write it the output file.  You can probably do that while you are creating the CSV file for the first time.
2. If you are running behind Apache, investigate and see if mod_gzip will solve your problem.  Most modern browsers support compressed content and Apache will automatically compress the file to achieve the desired goal - less use of bandwidth.
0
 

Expert Comment

by:u9707118
ID: 9741619
Here is a pure java solution that I have written for my project.
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:\\mydata\\myZipFile.zip");
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
BufferedInputStream origin;
ArrayList filenameList = new ArrayList();

filenameList.add("mycsvfile1.csv");
filenameList.add("mycsvfile2.csv");
//etc..

for (int counter=0; counter<filenameList.size(); counter++) {
            FileInputStream fi = new FileInputStream((String)filenameList.get(counter));
            origin = new BufferedInputStream(fi, BUFFER);
            ZipEntry entry = new ZipEntry((String)filenameList.get(counter)); //creates entry containing file
               
            entry.setMethod(ZipOutputStream.DEFLATED); //sets method to compressed
            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
0
 

Author Comment

by:sjno
ID: 9742085
If I knew how to split points I would but since dyanet answered first they gets the points I guess.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Stuck in Bootstrap WysiHtml Editor 3 37
jersey maven project error 7 137
authenticate preemptively soap ui option 3 123
struts spring hibernate example 12 105
Note: This is the third blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   We’ve been talki…
Cloud-based technologies and services will continue to grow in popularity in 2017 thanks to the simple, scalable and cost-effective solutions they deliver. Here are three areas where cloud adoption is poised to really take off.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now