<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Berthou.com &#187; zip</title>
	<atom:link href="http://www.berthou.com/us/tag/zip/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.berthou.com/us</link>
	<description>A french developper blog</description>
	<lastBuildDate>Mon, 09 Aug 2010 08:55:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>Servlet : zipservlet</title>
		<link>http://www.berthou.com/us/2007/09/29/test-english/</link>
		<comments>http://www.berthou.com/us/2007/09/29/test-english/#comments</comments>
		<pubDate>Sat, 29 Sep 2007 09:48:08 +0000</pubDate>
		<dc:creator>rberthou</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[exemple]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[zip]]></category>

		<guid isPermaLink="false">http://www.berthou.com/us/?p=5</guid>
		<description><![CDATA[This article aims at presenting how to create a servlet that sends a zip file to the user. We create here a html page with a form where you can enter an url. The servlet will get the content of this url (only the html page, not the images or other frames), create a zip [...]]]></description>
				<content:encoded><![CDATA[<p>This article aims at presenting how to create a servlet that sends a zip file to the user. We create here a html page with a form where you can enter an url. The servlet will get the content of this url (only the html page, not the images or other frames), create a zip file with it, and send you the file.<br/><br />
This example can be used as a basis to do various things.</p>
<ul>
<li>Sending a file with a servlet<br/><br />
This involves two headers: Content-Type and Content-Disposition. The content type is set to &#8220;application/zip&#8221;. The other one is used to specify the filename used by the browser when it starts downloading the file.</p>
<pre class="alertCode">
res.setContentType("application/zip");
res.setHeader("Content-Disposition","inline; filename=output.zip;");
</pre>
<p>This header replace &#8220;res.setHeader(&#8220;Content-Disposition&#8221;,&#8221;attachment; filename=output.zip;&#8221;);&#8221; (not work with IE5)</p>
<p>In this example, we create a zip file. We&#8217;ll discuss later on how to create such files. As soon as we get a byte array with the zip file, we are ready to send it.<br />
We simply call out.println(zip) to send the file.
</li>
<p>li>Creating a zip file Have you ever noticed the java.util.zip package ?<br/><br />
Basically, a zip file is created by adding ZipEntries to a ZipOutputStream.<br />
Once we have create a ZipEntry with a file name, we are ready to write an array of byte to the ZipOutputStream. Then we close the entry, finish the work with a call to finish(). We finally get a String containing the zipped file.</p>
<pre class="alertCode">
zout.putNextEntry(new ZipEntry("file.html"));
zout.write(b,0,size);
zout.closeEntry();
zout.finish();
String zip=bout.toString();
</pre>
<p><span id="more-5"></span></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">javaside.servlet.sample</span> <span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
* zipservlet.java
*
*  Main class for sample servlet
*
*  @author : R. BERTHOU
*  URL    : http://www.berthou.com/
* -----------------------------------------------------------
* Ver  * Author     *  DATE    * Description
* ....................DD/MM/YY...............................
* 1.00 * R.BERTHOU  * 12/01/01 *
**********************************************************/</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.zip.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> zipservlet <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet <span style="color: #009900;">&#123;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doGet <span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse res<span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span>
     <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">byte</span> b<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">300000</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #003399;">URL</span> url<span style="color: #339933;">;</span>
          <span style="color: #003399;">String</span> temp<span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #003399;">ByteArrayOutputStream</span> bout<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ByteArrayOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003399;">ZipOutputStream</span> zout<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ZipOutputStream</span><span style="color: #009900;">&#40;</span>bout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          ServletOutputStream out <span style="color: #339933;">=</span> res.<span style="color: #006633;">getOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          ServletContext servletContext <span style="color: #339933;">=</span> getServletContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>temp<span style="color: #339933;">=</span>req.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;url&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
          <span style="color: #009900;">&#123;</span>
               <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                    url<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span>temp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #003399;">URLConnection</span> uc <span style="color: #339933;">=</span> url.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                    <span style="color: #003399;">DataInputStream</span> input <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">DataInputStream</span><span style="color: #009900;">&#40;</span>uc.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                    <span style="color: #000066; font-weight: bold;">int</span> numRead<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
                    <span style="color: #000066; font-weight: bold;">int</span> size<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
                    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>numRead <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                         numRead <span style="color: #339933;">=</span> input.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>b,size,<span style="color: #cc66cc;">20000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                         size<span style="color: #339933;">+=</span>numRead<span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
&nbsp;
                    zout.<span style="color: #006633;">putNextEntry</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ZipEntry</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;file.html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    zout.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>b,<span style="color: #cc66cc;">0</span>,size<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    zout.<span style="color: #006633;">closeEntry</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    zout.<span style="color: #006633;">finish</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #003399;">String</span> zip<span style="color: #339933;">=</span>bout.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                    res.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/zip&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    res.<span style="color: #006633;">setHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Disposition&quot;</span>,<span style="color: #0000ff;">&quot;inline; filename=output.zip;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                    out.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>zip<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    out.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
               <span style="color: #009900;">&#125;</span>
               <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span>
               <span style="color: #009900;">&#123;</span>
                    res.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    out.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;html&gt;&lt;head&gt;&lt;/head&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    out.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;body&gt;&lt;b&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    out.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;An error has occured while processing &quot;</span><span style="color: #339933;">+</span>temp<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    out.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Here is the exception: &quot;</span><span style="color: #339933;">+</span>e<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PrintWriter</span><span style="color: #009900;">&#40;</span>out<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    out.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;/body&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    out.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;/html&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #009900;">&#125;</span>
          <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.berthou.com/us/2007/09/29/test-english/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
