9

I'm really sorry to be that newbie but I just can't figure out why this doesn't work by myself. There are already similar questions but they doesn't help at all. I try to zip & compress different files in a dir.

Here's my code:

import zipfile
import zlib

value_map =
['/home/shiva/Desktop/test2', 'None', False, False, True, False
['_MG_5290.JPG', '_MG_5294.JPG', '_MG_5293.JPG', '_MG_5295.JPG',
'_MG_5291.JPG', 'IMG_5434.JPG', '_MG_5292.JPG'], ['_MG_5298.CR2',
'_MG_5290.CR2', '_MG_5297.CR2', '_MG_5294.CR2', '_MG_5296.CR2',
'_MG_5291.CR2', '_MG_5292.CR2', '_MG_5299.CR2', '_MG_5293.CR2',
'_MG_5295.CR2']]

def compress(value_map):
    print "value_map:"
    print value_map

    try:
        compression = zipfile.ZIP_DEFLATED
        zf = zipfile.ZipFile(value_map[0] + "/RAWs.zip", mode="w")
        for x in value_map[7]:
            print "test1"  # prints
            zf.write(value_map[0] + x, compress_type=compression)  # nope
            print "test2"  # doesn't print
        zf.close()
        print ("[*] " + len(value_map[7]) + " have been moved to:")
        print ("[*] " + value_map[0] + "/RAWs.zip")
    except:
        print "[-] Couldn't compress\n[-] Exiting"
        sys.exit(0)

Just why???

Shiva
  • 121
  • 1
  • 1
  • 7
  • In which line it returns error? – Lupanoide Nov 22 '17 at 15:32
  • There is no error. It jumps to the exception block after printing "test1". – Shiva Nov 22 '17 at 15:34
  • can you modify except: to except Exception as e: print e ? – Lupanoide Nov 22 '17 at 15:38
  • It jumps to exception because you're using bare except that catches everything. Remove try/except block and you'll see the error. `value_map` is not a valid list. Is there a comma missing after last `False`? – Borut Nov 22 '17 at 15:40
  • oh damn.....I deleted the whole try/except stuff. – Shiva Nov 22 '17 at 15:42
  • and the answer is: i'm stupid. i forgot a "/" between value_map and x(=filename) after adding +"/" it worked. – Shiva Nov 22 '17 at 15:44
  • but it's still not compressed. Also i added the whole directory structure to the zip-file. – Shiva Nov 22 '17 at 15:47
  • the list is fine. – Shiva Nov 22 '17 at 15:52
  • but it still throws errors. Traceback (most recent call last): File "rawkiller.py", line 163, in main() File "rawkiller.py", line 48, in main initial_executing(value_map) File "rawkiller.py", line 62, in initial_executing compress(value_map) File "rawkiller.py", line 107, in compress print ("[*] " + len(value_map[7]) + " have been moved to:") TypeError: cannot concatenate 'str' and 'int' objects – Shiva Nov 22 '17 at 15:53

1 Answers1

17

Here is an example that shows how to use the zipfile package with compression. Your code looks okay for the most part, but you have a typo in your list, and you shouldn't use try & except without specifying an error that you want to catch. I think a FileNotFoundError would occur if the file that you want to add to the zip doesn't exists.

Here's an example:

import zlib
import zipfile

def compress(file_names):
    print("File Paths:")
    print(file_names)

    path = "C:/data/"

    # Select the compression mode ZIP_DEFLATED for compression
    # or zipfile.ZIP_STORED to just store the file
    compression = zipfile.ZIP_DEFLATED

    # create the zip file first parameter path/name, second mode
    zf = zipfile.ZipFile("RAWs.zip", mode="w")
    try:
        for file_name in file_names:
            # Add file to the zip file
            # first parameter file to zip, second filename in zip
            zf.write(path + file_name, file_name, compress_type=compression)

    except FileNotFoundError:
        print("An error occurred")
    finally:
        # Don't forget to close the file!
        zf.close()


file_names= ["test_file.txt", "test_file2.txt"]
compress(file_names)
Sylvester Kruin
  • 3,294
  • 5
  • 16
  • 39
clfaster
  • 1,450
  • 19
  • 26
  • awesome. but i still wonder how not to pack all the folders in the zip when i write them. but i cannot select them without the path. when i use zf.write(x.... i get in error. when i use zf.write(~/Desktop/test2/x.... the folders also appear in the zip which i dont want – Shiva Nov 22 '17 at 17:46
  • Now I get your problem, You only need to set the arcname parameter in the zf.write function. I will update my example. – clfaster Nov 22 '17 at 17:49
  • cool. that works so far. but what has to be in the paranthesis of the compress method? path to zip? or a list of content? – Shiva Nov 22 '17 at 19:28
  • I pass a list of filenames to that method, but you can pass whatever you want. I defined the path inside the compress function. This is pretty ugly... – clfaster Nov 22 '17 at 21:25
  • hm... but when i just pass a list of the file name (without and with path) in the method the result is an error. – Shiva Nov 22 '17 at 22:33
  • but im shure the compress()-method is the only problem now. – Shiva Nov 22 '17 at 22:40
  • what error do you get? – clfaster Nov 22 '17 at 22:48
  • T(most recent call last): File "rawkiller.py", line 170, in main() File "rawkiller.py", line 48, in main initial_executing(value_map) File "rawkiller.py", line 62, in initial_executing compress(value_map) File "rawkiller.py", line 106, in compress compress(value_map[0] + "/RAWs.zip") File "rawkiller.py", line 101, in compress zf = zipfile.ZipFile(value_map[0] + "/RAWs.zip", mode="w") File "/usr/lib/python2.7/zipfile.py", line 756, in __init__ self.fp = open(file, modeDict[mode]) IOError: [Errno 13] Permission denied: '//RAWs.zip' – Shiva Nov 22 '17 at 22:53
  • This error has nearly nothing to do with the compress function, it's an IOError. You probably passing the path. https://stackoverflow.com/questions/10575750/python-ioerror-errno-13-permission-denied – clfaster Nov 23 '17 at 11:03