2

Im not sure what im doing wrong, but what i need is to create a zip file with all files and folders ( empty or not ) of a given directory. So, at the moment i have this simple code that "works" but it doens't add empty folders :|

content of c:\temp\trashtests

c:\temp\trashtests\a\a.txt
c:\temp\trashtests\b\b.txt
c:\temp\trashtests\c
c:\temp\trashtests\d

Current code:

class ZipTools:
    """
    Zip tools
    """
    def __init__(self, target_path=None, zip_name=None):
        self.path = target_path
        self.zip_name = zip_name

    def create_zip(self):
        shutil.make_archive(self.zip_name, format='zip', root_dir=self.path)

execution:

ab = self.ZipTools('c:\temp\trashtests', 'c:\test\a.zip') ab.create_zip()

The output is a zip file only with:

\a\a.txt
\b\b.txt

So, how can i create a zip file with all contents of a given directory using shutils? If not, how could i do it using zipfile?

Thanks in advance.

EDIT:

As sugested by J.F. Sebastian, i tried the solution of this question but it didn't worked since it created a zip file with the following structure:

File: a.zip Content:

c:
a\a.txt
b\b.txt

I'm still trying to figure it out a solution :)

Community
  • 1
  • 1
thclpr
  • 5,778
  • 10
  • 54
  • 87
  • possible duplicate of [How do I create a zip file of a file path using Python, including empty directories?](http://stackoverflow.com/questions/11751822/how-do-i-create-a-zip-file-of-a-file-path-using-python-including-empty-director) – jfs Aug 25 '14 at 15:29
  • @J.F.Sebastian , Hi, This could be considered as duplicate if i opened the question asking for a solution using zipfile module :) But my intention here is primary discover if it is possible to do it using shutils.make_archive. – thclpr Aug 25 '14 at 15:37
  • nothing in the title suggests that you must use zipfile module. The question I've linked mentions `shutil.make_archive()` *explicitly*. If your question is not a duplicate then you should specify how exactly the existing answers do not work for you – jfs Aug 25 '14 at 15:40
  • @J.F.Sebastian , Thanks for the advice, im doing that right now :) – thclpr Aug 25 '14 at 15:41

1 Answers1

0

I was able to solve this problem using using this code:

An important note, this code works for what i need, which is:

Zip an existing folder with the same name of the zip file to create.

import os
import sys
import zipfile


class ZipTools:
    """
    Zip tools
    """
    def __init__(self, folder_path=None, pkg_zip=None):
        self.path = folder_path
        self.zip_name = pkg_zip
        self.temp_dir = 'c:\\temp'
        self.archive = '{p}\\{z}'.format(p=self.temp_dir, z='a.zip')

    def zip(self):
        parent_folder = os.path.dirname(self.path)
        # Retrieve the paths of the folder contents.
        contents = os.walk(self.path)
        try:
            zip_file = zipfile.ZipFile(self.archive, 'w', zipfile.ZIP_DEFLATED)
            for root, folders, files in contents:
                # Include all subfolders, including empty ones.
                for folder_name in folders:
                    ab_path = os.path.join(root, folder_name)
                    rel_path = ab_path.replace(parent_folder + '\\' + self.zip_name, '')
                    print rel_path
                    zip_file.write(ab_path, rel_path)
                for file_name in files:
                    ab_path = os.path.join(root, file_name)
                    rel_path = ab_path.replace(parent_folder + '\\' + self.zip_name, '')
                    zip_file.write(ab_path, rel_path)
        except zipfile.BadZipfile, message:
            print message
            sys.exit(1)
        finally:
            zip_file.close()


if __name__ == '__main__':
    ab = ZipTools('c:\\temp\\trashtests', 'a.zip')
    ab.zip()
thclpr
  • 5,778
  • 10
  • 54
  • 87