<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://k2.ixota.com/index.php?action=history&amp;feed=atom&amp;title=Python%2FZip</id>
	<title>Python/Zip - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://k2.ixota.com/index.php?action=history&amp;feed=atom&amp;title=Python%2FZip"/>
	<link rel="alternate" type="text/html" href="https://k2.ixota.com/index.php?title=Python/Zip&amp;action=history"/>
	<updated>2026-06-26T11:00:33Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.33.1</generator>
	<entry>
		<id>https://k2.ixota.com/index.php?title=Python/Zip&amp;diff=4430&amp;oldid=prev</id>
		<title>Kenneth: /* pkzip */</title>
		<link rel="alternate" type="text/html" href="https://k2.ixota.com/index.php?title=Python/Zip&amp;diff=4430&amp;oldid=prev"/>
		<updated>2017-08-23T21:28:04Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;pkzip&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=== pkzip ===&lt;br /&gt;
&lt;br /&gt;
Test if zip file is valid&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# test if the file is a valid pkzip file&lt;br /&gt;
if zipfile.is_zipfile(zfilename):&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Extract all:&lt;br /&gt;
import zipfile&lt;br /&gt;
zfile = zipfile.ZipFile( zfilename, &amp;quot;r&amp;quot; )&lt;br /&gt;
zfile.extractall()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# open a zip file and show information&lt;br /&gt;
import zipfile&lt;br /&gt;
zfile = zipfile.ZipFile( zfilename, &amp;quot;r&amp;quot; )&lt;br /&gt;
print &amp;quot;Simple file information retrieval:&amp;quot;&lt;br /&gt;
zfile.printdir()&lt;br /&gt;
zfile.close()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# read the zipped file back in ...&lt;br /&gt;
zin = zipfile.ZipFile(zfilename, &amp;quot;r&amp;quot;)&lt;br /&gt;
strz = zin.read(arc_name)&lt;br /&gt;
zin.close()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Show archive information:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import datetime&lt;br /&gt;
import zipfile&lt;br /&gt;
&lt;br /&gt;
def print_info(archive_name):&lt;br /&gt;
    zf = zipfile.ZipFile(archive_name)&lt;br /&gt;
    for info in zf.infolist():&lt;br /&gt;
        print info.filename&lt;br /&gt;
        print &amp;#039;\tComment:\t&amp;#039;, info.comment&lt;br /&gt;
        print &amp;#039;\tModified:\t&amp;#039;, datetime.datetime(*info.date_time)&lt;br /&gt;
        print &amp;#039;\tSystem:\t\t&amp;#039;, info.create_system, &amp;#039;(0 = Windows, 3 = Unix)&amp;#039;&lt;br /&gt;
        print &amp;#039;\tZIP version:\t&amp;#039;, info.create_version&lt;br /&gt;
        print &amp;#039;\tCompressed:\t&amp;#039;, info.compress_size, &amp;#039;bytes&amp;#039;&lt;br /&gt;
        print &amp;#039;\tUncompressed:\t&amp;#039;, info.file_size, &amp;#039;bytes&amp;#039;&lt;br /&gt;
        print&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;#039;__main__&amp;#039;:&lt;br /&gt;
    print_info(&amp;#039;example.zip&amp;#039;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
* Compress Files with Zip (Python) | DaniWeb - http://www.daniweb.com/software-development/python/code/216859/compress-files-with-zip-python&lt;br /&gt;
* zipfile – Read and write ZIP archive files - Python Module of the Week - http://www.doughellmann.com/PyMOTW/zipfile/&lt;br /&gt;
&lt;br /&gt;
==== Extract ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import zipfile&lt;br /&gt;
import os.path&lt;br /&gt;
zfile = zipfile.ZipFile(&amp;quot;test.zip&amp;quot;)&lt;br /&gt;
for name in zfile.namelist():&lt;br /&gt;
  (dirname, filename) = os.path.split(name)&lt;br /&gt;
  print &amp;quot;Decompressing &amp;quot; + filename + &amp;quot; on &amp;quot; + dirname&lt;br /&gt;
  if not os.path.exists(dirname):&lt;br /&gt;
    os.mkdir(dirname)&lt;br /&gt;
  fd = open(name,&amp;quot;w&amp;quot;)&lt;br /&gt;
  fd.write(zfile.read(name))&lt;br /&gt;
  fd.close()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
My modifications:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import zipfile&lt;br /&gt;
import os.path&lt;br /&gt;
zfile = zipfile.ZipFile(&amp;quot;test.zip&amp;quot;)&lt;br /&gt;
for name in zfile.namelist():&lt;br /&gt;
  (dirname, filename) = os.path.split(name)&lt;br /&gt;
  print &amp;quot;Decompressing &amp;quot; + name&lt;br /&gt;
  if dirname and not os.path.exists(dirname):&lt;br /&gt;
    os.mkdir(dirname)&lt;br /&gt;
  if filename:&lt;br /&gt;
    fd = open(name,&amp;quot;w&amp;quot;)&lt;br /&gt;
    fd.write(zfile.read(name))&lt;br /&gt;
    fd.close()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import os&lt;br /&gt;
from zipfile import ZipFile, ZipInfo&lt;br /&gt;
&lt;br /&gt;
class ZipCompat(ZipFile):&lt;br /&gt;
    def __init__(self, *args, **kwargs):&lt;br /&gt;
        ZipFile.__init__(self, *args, **kwargs)&lt;br /&gt;
&lt;br /&gt;
    def extract(self, member, path=None, pwd=None):&lt;br /&gt;
        if not isinstance(member, ZipInfo):&lt;br /&gt;
            member = self.getinfo(member)&lt;br /&gt;
        if path is None:&lt;br /&gt;
            path = os.getcwd()&lt;br /&gt;
        return self._extract_member(member, path)&lt;br /&gt;
&lt;br /&gt;
    def extractall(self, path=None, members=None, pwd=None):&lt;br /&gt;
        if members is None:&lt;br /&gt;
            members = self.namelist()&lt;br /&gt;
        for zipinfo in members:&lt;br /&gt;
            self.extract(zipinfo, path)&lt;br /&gt;
&lt;br /&gt;
    def _extract_member(self, member, targetpath):&lt;br /&gt;
        if (targetpath[-1:] in (os.path.sep, os.path.altsep)&lt;br /&gt;
            and len(os.path.splitdrive(targetpath)[1]) &amp;gt; 1):&lt;br /&gt;
            targetpath = targetpath[:-1]&lt;br /&gt;
        if member.filename[0] == &amp;#039;/&amp;#039;:&lt;br /&gt;
            targetpath = os.path.join(targetpath, member.filename[1:])&lt;br /&gt;
        else:&lt;br /&gt;
            targetpath = os.path.join(targetpath, member.filename)&lt;br /&gt;
        targetpath = os.path.normpath(targetpath)&lt;br /&gt;
        upperdirs = os.path.dirname(targetpath)&lt;br /&gt;
        if upperdirs and not os.path.exists(upperdirs):&lt;br /&gt;
            os.makedirs(upperdirs)&lt;br /&gt;
        if member.filename[-1] == &amp;#039;/&amp;#039;:&lt;br /&gt;
            if not os.path.isdir(targetpath):&lt;br /&gt;
                os.mkdir(targetpath)&lt;br /&gt;
            return targetpath&lt;br /&gt;
        target = file(targetpath, &amp;quot;wb&amp;quot;)&lt;br /&gt;
        try:&lt;br /&gt;
            target.write(self.read(member.filename))&lt;br /&gt;
        finally:&lt;br /&gt;
            target.close()&lt;br /&gt;
        return targetpath&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# unzip a file&lt;br /&gt;
def unzip(path):&lt;br /&gt;
    zfile = zipfile.ZipFile(path)&lt;br /&gt;
    for name in zfile.namelist():&lt;br /&gt;
        (dirname, filename) = os.path.split(name)&lt;br /&gt;
        if filename == &amp;#039;&amp;#039;:&lt;br /&gt;
            # directory&lt;br /&gt;
            if not os.path.exists(dirname):&lt;br /&gt;
                os.mkdir(dirname)&lt;br /&gt;
        else:&lt;br /&gt;
            # file&lt;br /&gt;
            fd = open(name, &amp;#039;w&amp;#039;)&lt;br /&gt;
            fd.write(zfile.read(name))&lt;br /&gt;
            fd.close()&lt;br /&gt;
    zfile.close()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
* How to unzip a zip file with Python 2.4? - Stack Overflow - http://stackoverflow.com/questions/7806563/how-to-unzip-a-zip-file-with-python-2-4&lt;br /&gt;
&lt;br /&gt;
==== Compress ====&lt;br /&gt;
&lt;br /&gt;
help:&lt;br /&gt;
 pydoc zipfile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import zipfile&lt;br /&gt;
str1 = &amp;quot;&amp;quot;&amp;quot;There is no ham in hamburger.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
arc_name = &amp;quot;English101.txt&amp;quot;&lt;br /&gt;
zfilename = &amp;quot;English101.zip&amp;quot;&lt;br /&gt;
zout = zipfile.ZipFile(zfilename, &amp;quot;w&amp;quot;)&lt;br /&gt;
zout.writestr(arc_name, str1)&lt;br /&gt;
zout.close()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# append file:&lt;br /&gt;
# zf = zipfile.ZipFile(&amp;#039;zipfile_append.zip&amp;#039;, mode=&amp;#039;a&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
zf = zipfile.ZipFile(&amp;#039;zipfile_write.zip&amp;#039;, mode=&amp;#039;w&amp;#039;)&lt;br /&gt;
try:&lt;br /&gt;
    print &amp;#039;adding README.txt&amp;#039;&lt;br /&gt;
    zf.write(&amp;#039;README.txt&amp;#039;)&lt;br /&gt;
    # alternate name:&lt;br /&gt;
    # zf.write(&amp;#039;README.txt&amp;#039;, arcname=&amp;#039;NOT_README.txt&amp;#039;)&lt;br /&gt;
finally:&lt;br /&gt;
    print &amp;#039;closing&amp;#039;&lt;br /&gt;
    zf.close()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
WARNING: By default, the contents of the archive are not compressed!  To add compression, the zlib module is required. If zlib is available, you can set the compression mode for individual files or for the archive as a whole using zipfile.ZIP_DEFLATED. The default compression mode is zipfile.ZIP_STORED.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from zipfile_infolist import print_info&lt;br /&gt;
import zipfile&lt;br /&gt;
try:&lt;br /&gt;
    import zlib&lt;br /&gt;
    compression = zipfile.ZIP_DEFLATED&lt;br /&gt;
except:&lt;br /&gt;
    compression = zipfile.ZIP_STORED&lt;br /&gt;
&lt;br /&gt;
modes = { zipfile.ZIP_DEFLATED: &amp;#039;deflated&amp;#039;,&lt;br /&gt;
          zipfile.ZIP_STORED:   &amp;#039;stored&amp;#039;,&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
print &amp;#039;creating archive&amp;#039;&lt;br /&gt;
zf = zipfile.ZipFile(&amp;#039;zipfile_write_compression.zip&amp;#039;, mode=&amp;#039;w&amp;#039;)&lt;br /&gt;
try:&lt;br /&gt;
    print &amp;#039;adding README.txt with compression mode&amp;#039;, modes[compression]&lt;br /&gt;
    zf.write(&amp;#039;README.txt&amp;#039;, compress_type=compression)&lt;br /&gt;
finally:&lt;br /&gt;
    print &amp;#039;closing&amp;#039;&lt;br /&gt;
    zf.close()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
* Compress Files with Zip (Python) | DaniWeb - http://www.daniweb.com/software-development/python/code/216859/compress-files-with-zip-python&lt;br /&gt;
* zipfile – Read and write ZIP archive files - Python Module of the Week - http://www.doughellmann.com/PyMOTW/zipfile/&lt;/div&gt;</summary>
		<author><name>Kenneth</name></author>
		
	</entry>
</feed>