0

I am able to zip a folder but when extracting folder it's contains home directory folders also but i need only it's sub folders

ex : below folder i chosen to zip

"/home/sr1/Documents/MyFolder "

MyFolder also contains child's folders and those child folder consist files

I am able to zipped the MyFolder but when extracting the folder structure was

"/home/sr1/Documents/MyFolder/newDoc/file1.txt"

but it should be like

"MyFolder/newDoc/file1.txt"

Here is my code

def zip_folder(folder_path, output_path):
    """Zip the contents of an entire folder (with that folder included
    in the archive). Empty subfolders will be included in the archive
    as well.
    """
    parent_folder = os.path.dirname(folder_path)
    # Retrieve the paths of the folder contents.
    contents = os.walk(folder_path)
    zip_file = None
    try:
        zip_file = zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED)
    rootlen = len(folder_path) + 1
        for root, folders, files in contents:
            # Include all subfolders, including empty ones.
            for folder_name in folders:
        print " root ", root
        print " relative_path " ,folder_name 
                absolute_path = os.path.join(root, folder_name)
                relative_path = absolute_path.replace(parent_folder + '\\',
                                                      '')

                print "Adding '%s' to archive." % absolute_path
                zip_file.write(absolute_path, relative_path)
            for file_name in files:
                absolute_path = os.path.join(root, file_name)
                relative_path = absolute_path.replace(parent_folder + '\\',
                                                      '')
                print "Adding '%s' to archive." % absolute_path
                zip_file.write(absolute_path,absolute_path[rootlen:])
        print "'%s' created successfully." % output_path
    except IOError, message:
        print message
        sys.exit(1)
    except OSError, message:
        print message
        sys.exit(1)
    except zipfile.BadZipfile, message:
        print message
        sys.exit(1)
    finally:
    if zip_file :
            zip_file.close()


zip_folder('/home/sr1/Documents/MyFolder','MyFolder.zip')

Please help me how to over come this issue

Thanks in advance..

Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
suresh
  • 45
  • 1
  • 7
  • can you format your code properly please? – WhatsThePoint Apr 21 '17 at 10:22
  • Possibly related: http://stackoverflow.com/questions/27483431/python-zipfile-whole-path-is-in-file – Ilja Everilä Apr 21 '17 at 10:27
  • Hi @Ilja Everilä ,from above given link i am unable to get subfolders wen extracting the zip – suresh Apr 21 '17 at 12:16
  • Interesting. Tried the function given in the answer and it produced a working zip file, with leading path stripped, containing given path and subdirectories. I'm inclined to say that the error is at your end in the way you handle the produced zip file. – Ilja Everilä Apr 21 '17 at 12:29
  • Possible duplicate of [Python zipfile whole path is in file](http://stackoverflow.com/questions/27483431/python-zipfile-whole-path-is-in-file) – Ilja Everilä Apr 21 '17 at 12:35
  • yeah it's working when subdirectories contains files.but it's not working when subdirectories not contains files – suresh Apr 21 '17 at 12:54
  • See http://stackoverflow.com/questions/11751822/how-do-i-create-a-zip-file-of-a-file-path-using-python-including-empty-director. Apparently you need to use `ZipFile.writestr` to include `ZipInfo` about empty directories. – Ilja Everilä Apr 22 '17 at 06:07

0 Answers0