python实现DEFLATE算法压缩解压文件
时间: 2023-12-06 13:37:11 浏览: 150
使用Deflate算法对文件进行压缩与解压缩的方法详解
5星 · 资源好评率100%
以下是Python实现DEFLATE算法压缩解压文件的方法:
压缩文件:
```python
import zipfile
def zip_files(file_paths, zip_path):
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zip_file:
for file_path in file_paths:
zip_file.write(file_path)
# 示例
file_paths = ['file1.txt', 'file2.txt']
zip_path = 'files.zip'
zip_files(file_paths, zip_path)
```
解压文件:
```python
import zipfile
def unzip_files(zip_path, extract_path):
with zipfile.ZipFile(zip_path, 'r') as zip_file:
zip_file.extractall(extract_path)
# 示例
zip_path = 'files.zip'
extract_path = 'extracted_files'
unzip_files(zip_path, extract_path)
```
阅读全文