有什么方法可以压缩 python 中的 ZIP 文件吗?
Is there any method to compress ZIP files in python?
我试过使用 zipfile 和 shutil 创建 zip。但是,这两种方法都没有明显的文件大小压缩。
压缩文件
with zipfile.ZipFile(f'{directory_name}.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
shutil
shutil.make_archive(directory_path, 'zip', directory_path)
有什么方法可以确保文件压缩吗?
谢谢。
你做到了
with zipfile.ZipFile(f'{directory_name}.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
The compresslevel parameter controls the compression level to use when
writing files to the archive. When using ZIP_STORED
or ZIP_LZMA
it has
no effect. When using ZIP_DEFLATED
integers 0
through 9
are accepted
(see zlib for more information). When using ZIP_BZIP2
integers 1
through 9
are accepted (see bz2 for more information).
请尝试不同的 compresslevel
即
with zipfile.ZipFile(f'{directory_name}.zip', 'w', zipfile.ZIP_DEFLATED, compresslevel=0) as zipf:
到
with zipfile.ZipFile(f'{directory_name}.zip', 'w', zipfile.ZIP_DEFLATED, compresslevel=9) as zipf:
然后检查它是否影响创建文件的大小
看起来你需要 pathlib
模块
从路径库导入路径
从 zipfile 导入 ZIP_DEFLATED, ZipFile
from os import PathLike
from typing import Union
def zip_dir(zip_name: str, source_dir: Union[str, PathLike]):
src_path = Path(source_dir).expanduser().resolve(strict=True)
with ZipFile(zip_name, 'w', ZIP_DEFLATED) as zf:
for file in src_path.rglob('*'):
zf.write(file, file.relative_to(src_path.parent))
或
使用 zipfile module
我会使用下面的代码 :-
import os
import zipfile
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file))
zipf = zipfile.ZipFile('Zipped_file.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('./my_folder', zipf)
zipf.close()
这应该将 my_folder
的内容压缩到文件 'Zipped_file.zip'
中。并存入当前directory
.
我试过使用 zipfile 和 shutil 创建 zip。但是,这两种方法都没有明显的文件大小压缩。
压缩文件
with zipfile.ZipFile(f'{directory_name}.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
shutil
shutil.make_archive(directory_path, 'zip', directory_path)
有什么方法可以确保文件压缩吗?
谢谢。
你做到了
with zipfile.ZipFile(f'{directory_name}.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
The compresslevel parameter controls the compression level to use when writing files to the archive. When using
ZIP_STORED
orZIP_LZMA
it has no effect. When usingZIP_DEFLATED
integers0
through9
are accepted (see zlib for more information). When usingZIP_BZIP2
integers1
through9
are accepted (see bz2 for more information).
请尝试不同的 compresslevel
即
with zipfile.ZipFile(f'{directory_name}.zip', 'w', zipfile.ZIP_DEFLATED, compresslevel=0) as zipf:
到
with zipfile.ZipFile(f'{directory_name}.zip', 'w', zipfile.ZIP_DEFLATED, compresslevel=9) as zipf:
然后检查它是否影响创建文件的大小
看起来你需要 pathlib
模块
从路径库导入路径 从 zipfile 导入 ZIP_DEFLATED, ZipFile
from os import PathLike
from typing import Union
def zip_dir(zip_name: str, source_dir: Union[str, PathLike]):
src_path = Path(source_dir).expanduser().resolve(strict=True)
with ZipFile(zip_name, 'w', ZIP_DEFLATED) as zf:
for file in src_path.rglob('*'):
zf.write(file, file.relative_to(src_path.parent))
或
使用 zipfile module
我会使用下面的代码 :-
import os
import zipfile
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file))
zipf = zipfile.ZipFile('Zipped_file.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('./my_folder', zipf)
zipf.close()
这应该将 my_folder
的内容压缩到文件 'Zipped_file.zip'
中。并存入当前directory
.