Avatar of rnicholus
rnicholus

asked on 

Creating files on the fly with JSP

I want to build a JSP app where user is returned zip files on the fly.
These zip files will contain one or more XML files.

Is it possible to create both the XML files on the fly and then the zip files (also on the fly)?

My understanding is that it's not possible. I need to create XML files put it on hard drive (not on the fly). Then I can create the zip file on the fly.

If yes, please give me example.
If no, please give me a clear explanation why.

Thanks =)
JavaJSP

Avatar of undefined
Last Comment
rnicholus
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

No - you need to create a zip file then add entries to it then write the zip file to the outputstream of the page:

http://www.exampledepot.com/egs/java.util.zip/CreateZip.html
Avatar of rnicholus
rnicholus

ASKER

So you're saying

1. The XML files need to be available on hard drive.
2. Then the zip file need to be available on hard drive.
3. Then I can stream the original zip file into user?

I think I'm pretty sure zip file on the fly (#2) can be done?
Something like this probably: http://www.javaworld.com/javaforums/showflat.php?Cat=2&Number=89959&an=0&page=3
Avatar of Mick Barry
Mick Barry
Flag of Australia image

can't see why it can't be done on the fly

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

But don't set the transfer encoding to chunked as in that example - it isn't chunked
Avatar of rnicholus
rnicholus

ASKER

>> can't see why it can't be done on the fly
I don't see how. Could you please give me some pointers?
Sorry if it seems to be a newbie question.

1. Let say I create XML file (where this needs to be saved before it is being zipped on the fly) --> hard disk. Is it possible to keep this XML somewhere not on hard disk??
2. Then it will zip it on the fly too. So no file would be on hard disk?

This is possible??
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of rnicholus
rnicholus

ASKER

I'm not sure if I'm doing #3 and #4 correctly. I got the message: "The Compressed (zipped) Folder is invalid or corrupted"

<%@ page import="java.util.*, java.sql.*, java.text.*, java.io.*, java.util.zip.*" %>
 
<%!
	public void copyContentsToOutput(OutputStream zipOutputStream) throws Exception
	{
		Writer writer
   			= new BufferedWriter(new OutputStreamWriter(zipOutputStream));
	
		writer.write("<lalala>", 0, 8);
		writer.write("<item>book</item>, 0, 17");
		writer.write("</lalala>", 0, 8);
		
		writer.flush(); 
		writer.close();
	} // end copyContentsToOutput()
%>
 
<%
	response.setContentType("application/zip");
	response.setHeader ("Content-Disposition", "attachment; filename=\"test.zip\""); %>
	
	ZipOutputStream zipOutputStream = 
		new ZipOutputStream(response.getOutputStream());
		
	// Add ZIP entry to output stream.
	zipOutputStream.putNextEntry(new ZipEntry("test1.xml"));
	copyContentsToOutput(zipOutputStream);
	zipOutputStream.closeEntry();
	zipOutputStream.flush();
	zipOutputStream.close();
%>

Open in new window

SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of rnicholus
rnicholus

ASKER

CEHJ, it's still not good.

If I add this: response.setHeader("Transfer-Encoding", "chunked");"
I no longer get: The Compressed (zipped) Folder is invalid or corrupted"

But test.zip doesn't have anything. It's empty.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

The thing is, i'm not sure if what you're doing is quite right. That technique *would* be correct for chunked encoding (or at least along the right lines), but what you want to do is to send a zip *file* to the user. If think you're going to have to use a .. errr.. ZipFile to do that. The will mean a temp zip on disk
Avatar of rnicholus
rnicholus

ASKER

I saw the explanation of chunked encoding and this is what I want to do:
==================
Chunked encoding is useful when a large amount of data is being returned to the client and the total size of the response may not be known until the request has been fully processed.
==================

The zip file will contain one or more XML file(s) that depend on user input and what's available in database (dynamic).
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Your objective, i think, is to return a zip file? If so, chunked encoding is not really relevant
Avatar of rnicholus
rnicholus

ASKER

My objective is to return zip file that can contain one or more XML file(s). One ore more XML file(s), what's the size of the XML file(s) will be varied based on user requests.

What I want to figure out to do is to create everything on the fly. No putting data onto hard disk involved.
Possible?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>Possible?

Don't think so
Avatar of rnicholus
rnicholus

ASKER

Apparently, it can =)
Using my method or your method in the code snippet below.

I'm not sure why the tomcat in my system doesn't detect a bug that is so clear. Please see my original code snippet then you can see the bug on this line:
response.setHeader ("Content-Disposition", "attachment; filename=\"test.zip\""); %>

I close "%>" before I'm done with everything.







public void copyContentsToOutput(OutputStream zipOutputStream) throws Exception
{
Writer writer
= new BufferedWriter(new OutputStreamWriter(zipOutputStream));
        
writer.write("<lalala>", 0, 8);
writer.write("<item>chicken</item>, 0, 17");
writer.write("</lalala>", 0, 8);
                
 writer.flush(); 
 writer.close();
} // end copyContentsToOutput()   
                
public void copyContentsToOutput(OutputStream zipOutputStream) throws Exception
{
zipOutputStream.write("<lalala>".getBytes());
zipOutputStream.write("<item>book</item>".getBytes());
zipOutputStream.write("</lalala>".getBytes());            
zipOutputStream.flush(); 
} // end copyContentsToOutput()

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

So it's working now then?
Avatar of rnicholus
rnicholus

ASKER

Yes, it is. Thanks for the help guys! ;D
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo