{"id":659,"date":"2012-11-02T22:15:35","date_gmt":"2012-11-02T22:15:35","guid":{"rendered":"http:\/\/tech.avant.net\/q\/?p=659"},"modified":"2012-12-25T22:38:32","modified_gmt":"2012-12-25T22:38:32","slug":"zip-archive-in-python","status":"publish","type":"post","link":"https:\/\/tech.avant.net\/q\/zip-archive-in-python\/","title":{"rendered":"zip archive in python"},"content":{"rendered":"<p>I would like to create zip archives within a python batch script. I would like to compress individual files or entire directories of files.<\/p>\n<p>You can use the built-in zipfile module, and create a ZipFile as you would a normal File object, e.g.,<\/p>\n<pre class=\"sh_python\">\r\n&gt;&gt;&gt; \r\n&gt;&gt;&gt; foo = zipfile.ZipFile('foo.zip', mode='w')\r\n&gt;&gt;&gt; foo.write('foo.txt')\r\n&gt;&gt;&gt; \r\n<\/pre>\n<p>Unfortunately, by default the zipfile is uncompressed. You can add multiple files and directories to your zipfile, which can be useful for archival, but they will not be compressed. In order to compress the files, you&#8217;ll need to have the zlib library installed (it should already be installed in newer versions of python, 2.5 and greater). Simply use the ZIP_DEFLATED flag as follows,<\/p>\n<pre class=\"sh_python\">\r\n&gt;&gt;&gt; \r\n&gt;&gt;&gt; foo = zipfile.ZipFile('foo.zip', mode='w')\r\n&gt;&gt;&gt; foo.write('foo.txt', <strong>compress_type=zipfile.ZIP_DEFLATED<\/strong>)\r\n&gt;&gt;&gt; \r\n<\/pre>\n<p>In order to archive an entire directory (and all its contents) you can use the os.walk function. This function will return a list of all files and subdirectories as a triple (root, dirs, files). You can iterate through the returned files as follows,<\/p>\n<pre class=\"sh_python\">\r\n&gt;&gt;&gt; \r\n&gt;&gt;&gt; foo = zipfile.ZipFile('foo.zip', mode='w')\r\n&gt;&gt;&gt; for root, dirs, files in os.walk('\/path\/to\/foo'):\r\n...     for name in files:\r\n...         file_to_zip = os.path.join(root, name)\r\n...         foo.write(file_to_zip, compress_type=zipfile.ZIP_DEFLATED)\r\n...         \r\n&gt;&gt;&gt; \r\n<\/pre>\n<p>We can put this all together into a handy utility function that creates a compressed zipfile for any file or directory. This is also available in the following <a href=\"https:\/\/github.com\/timwarnock\/ziparchive.py\">github repo<\/a>.<\/p>\n<pre class=\"sh_python\">\r\ndef ziparchive(filepath, zfile=None):\r\n    ''' create\/overwrite a zip archive\r\n\r\n        can be a file or directory, and always overwrites the output zipfile if one already exists\r\n\r\n        An optional second argument can be provided to specify a zipfile name, \r\n        by default the basename will be used with a .zip extension\r\n\r\n        &gt;&gt;&gt;\r\n        &gt;&gt;&gt; ziparchive('foo\/data\/')\r\n        &gt;&gt;&gt; zf = zipfile.ZipFile('data.zip', 'r')\r\n        &gt;&gt;&gt; \r\n\r\n        &gt;&gt;&gt; \r\n        &gt;&gt;&gt; ziparchive('foo\/data\/', 'foo\/eggs.zip')\r\n        &gt;&gt;&gt; zf = zipfile.ZipFile('foo\/eggs.zip', 'r')\r\n        &gt;&gt;&gt; \r\n    '''\r\n    if zfile is None:\r\n        zfile = os.path.basename(filepath.strip('\/')) + '.zip'\r\n    filepath = filepath.rstrip('\/')\r\n    zf = zipfile.ZipFile(zfile, mode='w')\r\n    if os.path.isfile(filepath):\r\n        zf.write(filepath, filepath[len(os.path.dirname(filepath)):].strip('\/'), compress_type=zipfile.ZIP_DEFLATED)\r\n    else:\r\n        for root, dirs, files in os.walk(filepath):\r\n            for name in files:\r\n                file_to_zip = os.path.join(root, name)\r\n                arcname = file_to_zip[len(os.path.dirname(filepath)):].strip('\/')\r\n                zf.write(file_to_zip, arcname, compress_type=zipfile.ZIP_DEFLATED)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I would like to create zip archives within a python batch script. I would like to compress individual files or entire directories of files. You can use the built-in zipfile module, and create a ZipFile as you would a normal File object, e.g., &gt;&gt;&gt; &gt;&gt;&gt; foo = zipfile.ZipFile(&#8216;foo.zip&#8217;, mode=&#8217;w&#8217;) &gt;&gt;&gt; foo.write(&#8216;foo.txt&#8217;) &gt;&gt;&gt; Unfortunately, by default [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,14],"tags":[],"_links":{"self":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/659"}],"collection":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/comments?post=659"}],"version-history":[{"count":7,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/659\/revisions"}],"predecessor-version":[{"id":690,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/posts\/659\/revisions\/690"}],"wp:attachment":[{"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/media?parent=659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/categories?post=659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech.avant.net\/q\/wp-json\/wp\/v2\/tags?post=659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}