0

I'm trying to zip bunch of folders individually. The folders contain files. I wrote a script that seems to work perfectly, except that the resulting zip files are not actually compressed. THey're the same size as the original directory!

Here is my code:

import os, zipfile

workspace = "C:\\ziptest"

dirList = os.listdir(workspace)

def zipDir(path, zip):
  for root, dirs, files in os.walk(path):
    for file in files:
      zip.write(os.path.join(root, file))

for item in dirList:
  zip = zipfile.ZipFile('%s.zip' % item, 'w')
  zipDir('C:\\ziptest\%s' % item, zip)
  zip.close()

2 Answers 2

3

I'm not a Python expert, but a quick lookup shows that there is another argument for zip.write such as zipfile.ZIP_DEFLATED. I grabbed that from here. I quote:

The third, optional argument to the write method controls what compression method to use. Or rather, it controls whether data should be compressed at all. The default is zipfile.ZIP_STORED, which stores the data in the archive without any compression at all. If the zlib module is installed, you can also use zipfile.ZIP_DEFLATED, which gives you “deflate” compression.

The reference is here. Look for the constant ZIP_DEFLATED; it's definition:

The numeric constant for the usual ZIP compression method. This requires the zlib module. No other compression methods are currently supported.

I suppose that means that only default compression is supported... hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

0

Is there any reason you don't just call the shell command, like

def zipDir(path, zip):
    subprocess.Popen('7z a -tzip %s %s'%(path, zip))

1 Comment

python has direct zipfile support that is cross-platform and doesn't rely on external programs... that's why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.