Python zip文件夹,不包括'./'(当前目录)

时间:2016-03-11 09:59:57

标签: python zip

我正在尝试压缩test文件夹的内容:

first.txt
pof/
pof/second.txt

如果我cd进入test,请使用

压缩它
zip -r folder.zip *

并使用

检查生成的存档
zipinfo folder.zip

我得到了这个输出:

Archive:  folder.zip
Zip file size: 7573 bytes, number of entries: 3
-rw-r--r--  3.0 unx     6473 tx defN 16-Mar-11 10:19 first.txt
drwxr-xr-x  3.0 unx        0 bx stor 16-Mar-11 10:20 pof/
-rw-r--r--  3.0 unx     2841 tx defN 16-Mar-11 10:20 pof/second.txt
3 files, 9314 bytes uncompressed, 7113 bytes compressed:  23.6%

所有内容似乎都按预期工作,但如果我使用

压缩同一个文件夹
shutil.make_archive('folder', 'zip', 'test')

然后用

检查存档
zipinfo folder.zip

我得到了这个输出:

Archive:  folder.zip
Zip file size: 7497 bytes, number of entries: 4
drwxr-xr-x  2.0 unx        0 b- defN 16-Mar-11 10:28 ./
drwxr-xr-x  2.0 unx        0 b- defN 16-Mar-11 10:20 pof/
-rw-r--r--  2.0 unx     6473 b- defN 16-Mar-11 10:19 first.txt
-rw-r--r--  2.0 unx     2841 b- defN 16-Mar-11 10:20 pof/second.txt
4 files, 9314 bytes uncompressed, 7113 bytes compressed:  23.6%

我不喜欢的是./包含在Python生成的zip存档中:我该如何避免这种情况?

1 个答案:

答案 0 :(得分:7)

确保test位于其他空文件夹中并压缩该父文件夹。 shutil.make_archive()包含 in 您指定的文件夹中的所有内容,而不是文件夹本身:

$ tree parent/
parent/
└── test
    ├── first.txt
    └── pof
        └── second.txt
$ bin/python
Python 2.7.11 (default, Feb 20 2016, 23:04:20)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import shutil
>>> shutil.make_archive('folder', 'zip', 'parent')
'/.../folder.zip'
>>> ^Z
$ zipinfo folder.zip
Archive:  folder.zip   504 bytes   5 files
drwxr-xr-x  2.0 unx        0 b- stor 11-Mar-16 11:19 ./
drwxr-xr-x  2.0 unx        0 b- stor 11-Mar-16 11:19 test/
drwxr-xr-x  2.0 unx        0 b- stor 11-Mar-16 11:19 test/pof/
-rw-r--r--  2.0 unx        0 b- defN 11-Mar-16 11:19 test/first.txt
-rw-r--r--  2.0 unx        0 b- defN 11-Mar-16 11:19 test/pof/second.txt
5 files, 0 bytes uncompressed, 4 bytes compressed:  0.0%

但是,您无法阻止shutil.make_archive()包含./当前目录。在这种情况下编写自己的目录:

import os
import os.path
import zipfile

zip_filename = 'folder.zip'
base_dir = os.path.abspath('parent')

with zipfile.ZipFile(zip_filename, "w",
                     compression=zipfile.ZIP_DEFLATED) as zf:
    base_path = os.path.normpath(base_dir)
    for dirpath, dirnames, filenames in os.walk(base_dir):
        for name in sorted(dirnames):
            path = os.path.normpath(os.path.join(dirpath, name))
            zf.write(path, os.path.relpath(path, base_path))
        for name in filenames:
            path = os.path.normpath(os.path.join(dirpath, name))
            if os.path.isfile(path):
                zf.write(path, os.path.relpath(path, base_path))
相关问题