By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,596 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,596 IT Pros & Developers. It's quick & easy.

How to Zip a Directory with Python (using zipfile)

P: 3
I'm relatively new to python, and am trying to zip a directory containing several levels of files and folders. I can use the walk function to name each file in my directory, and I can use zipfile to zip a flat number of files in one folder, but I am having heck of a time trying to zip the whole directory. I want to zip it all to a file called "help.zip", and I want it to retain the original file structure.

Of my several tries, here is the latest:
Expand|Select|Wrap|Line Numbers
  1. import zipfile, os
  2.  
  3. def main():
  4.     zip = "help3.zip"
  5.     directory = "//groupstore/workgroups/documentation/test"
  6.     toZip(directory)
  7.  
  8.  
  9. def toZip(directory):
  10.     zippedHelp = zipfile.ZipFile(zip, "w", compression=zipfile.ZIP_DEFLATED )
  11.  
  12.     list = os.listdir(directory)
  13.  
  14.     for entity in list:
  15.         each = os.path.join(directory,entity)
  16.  
  17.         if os.path.isfile(each):
  18.             print each
  19.             zippedHelp.write(each,zipfile.ZIP_DEFLATED)
  20.         else:
  21.             addFolderToZip(zippedHelp,entity)
  22.  
  23.     zippedHelp.close()
  24.  
  25. #def addFolderToZip(zippedHelp,folder):
  26.  
  27.     for file in folder:
  28.             if os.path.isfile(file):
  29.                 zippedHelp.write(file, os.path.basename(file), zipfile.ZIP_DEFLATED)
  30.             elif os.path.isdir(file):
  31.                 addFolderToZip(zippedHelp,file)
  32. main()
  33.  
Nov 3 '08 #1
Share this Question
Share on Google+
10 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Here's another thread that may be relevant: http://bytes.com/forum/thread845051.html
Nov 4 '08 #2

bvdet
Expert Mod 2.5K+
P: 2,851
Here's a script that I use to backup the contents of a directory and it's subdirectories. It can easily be adjusted to backup only the directory contents or files with specific extensions. The file path is saved in the archive.
Expand|Select|Wrap|Line Numbers
  1. import zipfile, os
  2.  
  3. def makeArchive(fileList, archive):
  4.     """
  5.     'fileList' is a list of file names - full path each name
  6.     'archive' is the file name for the archive with a full path
  7.     """
  8.     try:
  9.         a = zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED)
  10.         for f in fileList:
  11.             print "archiving file %s" % (f)
  12.             a.write(f)
  13.         a.close()
  14.         return True
  15.     except: return False
  16.  
  17. def dirEntries(dir_name, subdir, *args):
  18.     '''Return a list of file names found in directory 'dir_name'
  19.     If 'subdir' is True, recursively access subdirectories under 'dir_name'.
  20.     Additional arguments, if any, are file extensions to match filenames. Matched
  21.         file names are added to the list.
  22.     If there are no additional arguments, all files found in the directory are
  23.         added to the list.
  24.     Example usage: fileList = dirEntries(r'H:\TEMP', False, 'txt', 'py')
  25.         Only files with 'txt' and 'py' extensions will be added to the list.
  26.     Example usage: fileList = dirEntries(r'H:\TEMP', True)
  27.         All files and all the files in subdirectories under H:\TEMP will be added
  28.         to the list.
  29.     '''
  30.     fileList = []
  31.     for file in os.listdir(dir_name):
  32.         dirfile = os.path.join(dir_name, file)
  33.         if os.path.isfile(dirfile):
  34.             if not args:
  35.                 fileList.append(dirfile)
  36.             else:
  37.                 if os.path.splitext(dirfile)[1][1:] in args:
  38.                     fileList.append(dirfile)
  39.         # recursively access file names in subdirectories
  40.         elif os.path.isdir(dirfile) and subdir:
  41.             print "Accessing directory:", dirfile
  42.             fileList.extend(dirEntries(dirfile, subdir, *args))
  43.     return fileList
  44.  
  45. if __name__ == '__main__':
  46.     folder = r'D:\Zip_Files\611 Lenox'
  47.     zipname = r'D:\Zip_Files\611 Lenox\test.zip'
  48.     makeArchive(dirEntries(folder, True), zipname)
  49.  
HTH
Nov 4 '08 #3

P: 3
Here's another thread that may be relevant: http://bytes.com/forum/thread845051.html

Thanks! I appreciate the help!
Nov 4 '08 #4

P: 3
Here's a script that I use to backup the contents of a directory and it's subdirectories. It can easily be adjusted to backup only the directory contents or files with specific extensions. The file path is saved in the archive.

Thank you!! Yay. I used your script to make mine work and I'm getting a better understanding of recursive functions. That was definitely where my hangup had been. Thanks again!!
Nov 4 '08 #5

P: 1
@bvdet
Thanks, this is just what i was looking for in my pys60 app :D. Thanks again for the help :)
Feb 27 '09 #6

P: 2
you can also do this, which is MUCH more concise:

Expand|Select|Wrap|Line Numbers
  1. def recursive_zip(zipf, directory, folder=None):
  2.     list = os.listdir(directory)
  3.  
  4.     for file in list:
  5.         if os.path.isfile(file):
  6.             zipf.write(file, folder, zipfile.ZIP_DEFLATED)
  7.         elif os.path.isdir(file):
  8.             recursive_zip(zipf, os.path.join(directory, file), file)
Mar 31 '10 #7

bvdet
Expert Mod 2.5K+
P: 2,851
Nakubu,

Thank you for your contribution. Please use code tags when posting code in the future.

It's not a good idea to use list and file as names of variables. The built-in functions list() and file() will be masked until the objects are deleted.

I have a question. Is zipf an open file object? It would be helpful to others reading this thread if you would post sample code that creates the file object, calls recursive_zip(), and closes the file object.

BV - Moderator
Mar 31 '10 #8

P: 2
here's a revised version:

Expand|Select|Wrap|Line Numbers
  1. def recursive_zip(zipf, directory, folder=None):
  2.     nodes = os.listdir(directory)
  3.  
  4.     for item in nodes:
  5.         if os.path.isfile(item):
  6.             zipf.write(item, folder, zipfile.ZIP_DEFLATED)
  7.         elif os.path.isdir(item):
  8.             recursive_zip(zipf, os.path.join(directory, item), item)
  9.  
  10.  
zipf is an opened zipfile.ZipFile instance. For example:

Expand|Select|Wrap|Line Numbers
  1. zipf = zipfile.ZipFile(zip, "w", compression=zipfile.ZIP_DEFLATED )
  2. path = '/Users/nakubu/some_folder'
  3. recursive_zip(zipf, path) //leave the first folder as None, as path is root.
  4. zipf.close()
  5.  
Mar 31 '10 #9

P: n/a
I found Nakubu's function helpful, but needed to modify it in several ways. Hope this helps someone:
Expand|Select|Wrap|Line Numbers
  1. def recursive_zip(zipf, directory, folder = ""):
  2.    for item in os.listdir(directory):
  3.       if os.path.isfile(os.path.join(directory, item)):
  4.          zipf.write(os.path.join(directory, item), folder + os.sep + item)
  5.       elif os.path.isdir(os.path.join(directory, item)):
  6.          recursive_zip(zipf, os.path.join(directory, item), folder + os.sep + item)
  7.  
Oct 17 '10 #10

P: 1
These are all good, but IMHO, os.walk rocks and does the hard stuff for you.
Jun 17 '11 #11

Post your reply

Sign in to post your reply or Sign up for a free account.