Python实现多级目录压缩与解压文件的方法
本文介绍了一个Python实现的轻量级文件压缩与解压模块,支持中文路径及多级目录处理,并兼容Linux和Windows系统。代码提供了带或不带父级目录的多种压缩模式,以及指定或默认路径的解压功能,旨在以少量代码实现高效的文件管理操作。
本文分享一个Python实现的多级目录文件压缩与解压方案,供读者参考使用。
本着资源共享的精神,特此提供相关代码供同行交流借鉴。
该方案遵循“代码精简、功能强大”的原则,虽代码量不大,但逻辑清晰,适合需要快速处理文件压缩任务的场景。
主要功能特点如下:
- 兼容中文路径,并支持对多级嵌套目录进行压缩与解压。
- 具备跨平台特性,在Linux和Windows环境下均可正常运行。
- 压缩功能的多态性实现
- 支持生成不包含父级文件夹目录的压缩包结构。
- 支持生成包含父级文件夹目录的压缩包结构。
- 支持不指定目标文件路径的默认压缩方式。
- 支持仅指定压缩包名称而不指定具体存储路径的压缩方式。
具体实现逻辑见以下代码示例。
#coding:utf-8
#压缩解压文件模块
#支持中文路径,支持多级目录
#支持跨平台,在linux和window下都可直接使用
#python 2.7.2
#author:xieShuxu
#QQ:258356793
#Email:sondx@qq.com
import zipfile,os
class ZipException(Exception):
pass
def unZipFile(zipPath,unZipPath=''):
'''解压文件
zipPath 要解压的文件路径
unZipPath 解压目标路径 默认解压到zipPath所在目录
'''
try:
filePath=filePath.decode('utf-8');
zipFilePath=zipFilePath.decode('utf-8');
except:
print '================'
if not os.path.exists(zipPath):
raise ZipException,'function unZipFile:not exists file or dir(%s)' %zipPath;
if unZipPath=='':
unZipPath=os.path.splitext(zipPath)[0];
if not unZipPath.endswith(os.sep):
unZipPath+=os.sep;
z = zipfile.ZipFile(zipPath, 'r')
#zipInfolist=z.namelist();
for k in z.infolist():
savePath=unZipPath+k.filename;
saveDir=os.path.dirname(savePath);
if not os.path.exists(saveDir):
os.makedirs(saveDir);
f=open(savePath,'wb');
f.write(z.read(k));
f.close();
z.close();
#print unZipPath
global _iterateExeZipFile;
def exeZipFile(filePath,zipFilePath=''):
'''压缩文件
filePath 要解压的文件路径 可以是文件或者目录
os.sep结尾表示压缩该目录下的子文件和文件夹 不包含该文件夹,否则包含该文件夹压缩
ZipFilePath 压缩包文件路径
也可只传文件名
默认压缩到filePath的父级目录下
'''
filePath=filePath.decode('utf-8');
zipFilePath=zipFilePath.decode('utf-8');
#压缩文件不存在直接返回
if not os.path.exists(filePath):
raise ZipException,'function exeZipFile:not exists file or dir(%s)' %filePath;
# 是否包含父级目录压缩
hasPDir=not filePath.endswith(os.sep);
if not hasPDir:
filePath=os.path.dirname(filePath);
print filePath
#校验备份文件路径
if zipFilePath=='':
zipFilePath=os.path.splitext(filePath)[0]+'.zip';
elif zipFilePath.find(os.sep)==-1:#只传文件名的处理
zipFilePath=os.path.dirname(filePath)+os.sep+zipFilePath;
#校验创建备份路径目录
if not os.path.exists(os.path.dirname(zipFilePath)):
os.makedirs(os.path.dirname(zipFilePath));
#初始化压缩包中的根目录
zipRoot='';
if hasPDir:
zipRoot=os.path.split(filePath)[1];
#开始压缩
z = zipfile.ZipFile(zipFilePath, 'w')
if os.path.isfile(filePath):
z.write(filePath,os.path.split(filePath)[1]);
else:
_iterateExeZipFile(filePath,zipRoot,z);
z.close();
def _iterateExeZipFile(dirPath,zipRoot,z):
以下是文件压缩功能的调用示例:
if __name__=='__main__':
#压缩包不带级父文件夹目录
testdir='D:\\codeSource\\linuxAgent\\'
zipFilePath='D:\\codeSource\\压缩包不带父级目录.zip'
exeZipFile(testdir,zipFilePath);
#压缩包带父级文件夹目录
testdir='D:\\codeSource\\linuxAgent'#不带后缀斜线
zipFilePath='D:\\codeSource\\压缩包带父级目录.zip'
exeZipFile(testdir,zipFilePath);
#不指定目标文件与路径压缩
testdir='D:\\codeSource\\linuxAgent'
exeZipFile(testdir);
#指定压缩包名称不指定路径压缩
testdir='D:\\codeSource\\linuxAgent\\'
exeZipFile(testdir,'仅指定名称压缩包.zip');
以下是文件解压功能的调用示例:
#指定解压目录解压文件
testdir=u'D:\\codeSource\\仅指定名称压缩包\\'
zipFilePath=u'D:\\codeSource\\仅指定名称压缩包.zip'
unZipFile(zipFilePath,testdir);
#不指定目录解压
zipFilePath=u'D:\\codeSource\\仅指定名称压缩包.zip'
unZipFile(zipFilePath);
以上就是全部内容。如果该方案对您有帮助,欢迎支持;如有问题,欢迎联系交流。
更多关于Python文件与目录操作的内容,可参考本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》。
希望本文所述内容能对您的Python编程实践有所帮助。
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









