<?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>schmichael&#039;s blog &#187; zip</title>
	<atom:link href="http://blog.schmichael.com/tag/zip/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.schmichael.com</link>
	<description>good good study, day day up</description>
	<lastBuildDate>Mon, 02 Aug 2010 17:28:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>I Love Python: ZipFile Edition</title>
		<link>http://blog.schmichael.com/2009/07/08/i-love-python-zipfile-edition/</link>
		<comments>http://blog.schmichael.com/2009/07/08/i-love-python-zipfile-edition/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 18:42:49 +0000</pubDate>
		<dc:creator>Michael Schurter</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[zip]]></category>

		<guid isPermaLink="false">http://michael.susens-schurter.com/blog/?p=664</guid>
		<description><![CDATA[For a client web project I needed to create a zip file containing a number of generated XML files. This isn&#8217;t something I need to do very often, so I briefly considered just writing the XML files to disk and &#8230; <a href="http://blog.schmichael.com/2009/07/08/i-love-python-zipfile-edition/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For a client web project I needed to create a zip file containing a number of generated XML files.  This isn&#8217;t something I need to do very often, so I briefly considered just writing the XML files to disk and running a zip command.  Ugly, but surely trying to dig up a pleasant Python zip library would be more work?</p>
<p>Turns out Python has had a wonderful zip library in its standard library since 1.6!  The <code><a href="http://docs.python.org/library/zipfile.html">zipfile</a></code> module makes creating zip files a breeze:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">zipfile</span> <span style="color: #ff7700;font-weight:bold;">import</span> ZipFile, ZIP_DEFLATED
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">template</span>.<span style="color: black;">defaultfilters</span> <span style="color: #ff7700;font-weight:bold;">import</span> slugify
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> somewhere_else <span style="color: #ff7700;font-weight:bold;">import</span> render_spam_xml, render_egg_xml
&nbsp;
ZIP_PATH = <span style="color: #483d8b;">&quot;/some/system/path/for/zips&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> create_zip<span style="color: black;">&#40;</span>spam<span style="color: black;">&#41;</span>:
    spam_slug = slugify<span style="color: black;">&#40;</span>spam.<span style="color: black;">name</span><span style="color: black;">&#41;</span>
    filename = <span style="color: #483d8b;">&quot;%s.zip&quot;</span> <span style="color: #66cc66;">%</span> spam_slug
    abspath = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>ZIP_PATH, filename<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Create zip</span>
    z = ZipFile<span style="color: black;">&#40;</span>abspath, <span style="color: #483d8b;">&quot;w&quot;</span>, ZIP_DEFLATED<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Write spam xml directly to zip</span>
    z.<span style="color: black;">writestr</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%s.xml&quot;</span> <span style="color: #66cc66;">%</span> spam_slug, render_spam_xml<span style="color: black;">&#40;</span>spam<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Write xml files to zip</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> egg <span style="color: #ff7700;font-weight:bold;">in</span> spam.<span style="color: black;">egg_set</span>.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        egg_slug = slugify<span style="color: black;">&#40;</span>egg.<span style="color: black;">name</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Renders the egg object to an xml string</span>
        <span style="color: #dc143c;">xml</span> = render_egg_xml<span style="color: black;">&#40;</span>egg<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Note how easy it is to specify paths in the zip file:</span>
        z.<span style="color: black;">writestr</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;eggs/%s.xml&quot;</span> <span style="color: #66cc66;">%</span> egg_slug, <span style="color: #dc143c;">xml</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Zip file must be closed to be valid</span>
    z.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> abspath</pre></div></div>

<p><small>(Sorry for the Django bits in there, but they should be easy to replace.)</small></p>
<p>My favorite part is that you can use either the <code>ZipFile.write</code> method to add files to the zip or the <code>ZipFile.writestr</code> method to write bytes (strings in my case) directly to the zip file.</p>
<p>At any rate, just wanted to blog about it, so when I need to do it again in a few years I don&#8217;t do something stupid like running the zip command.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schmichael.com/2009/07/08/i-love-python-zipfile-edition/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
