一个修改过的python zifile的包装模块,支持对目录压缩(包括空的文件夹),解压缩会解压缩空目录
2010-06-19 10:26
861 查看
'''
Created on 2010-5-26
@author: sean_long
'''
import os;
import zipfile
import os.path
class ZFile(object):
def __init__(self, filename, mode='r', basedir=''):
self.filename = filename
self.mode = mode
if self.mode in ('w', 'a'):
self.zfile = zipfile.ZipFile(filename, self.mode, compression=zipfile.ZIP_DEFLATED)
else:
self.zfile = zipfile.ZipFile(filename, self.mode)
self.basedir = basedir
if not self.basedir:
self.basedir = os.path.dirname(filename)
def addfile(self, path, arcname=None):
path = path.replace('//', '/')
if not arcname:
if path.startswith(self.basedir):
arcname = path[len(self.basedir):]
else:
arcname = '';
self.zfile.write(path, arcname)
def addfiles(self, paths):
for path in paths:
if isinstance(path, tuple):
self.addfile(*path)
else:
self.addfile(path)
def close(self):
self.zfile.close();
def extract_to(self, path):
for p in self.zfile.namelist():
self.extract(p, path)
def extract(self, filename, path):
if not filename.endswith('/'):
f = os.path.join(path, filename)
dir = os.path.dirname(f)
if not os.path.exists(dir):
os.makedirs(dir)
fd=file(f, 'wb');
fd.write(self.zfile.read(filename));
fd.close();
else:
d=os.path.join(path,filename);
if not os.path.exists(d):
os.makedirs(d);
def zipFolder(z,folderPath,basePath):
ar=os.listdir(folderPath);
for item in ar:
abtPath=os.path.join(folderPath,item);
bsPath=os.path.join(basePath,item);
if(os.path.isdir(abtPath)):
if(os.listdir(abtPath)==[]):
zif=zipfile.ZipInfo(bsPath+"/");
z.writestr(zif,"");
else:
zipFolder(z,abtPath,bsPath)
else:
z.write(abtPath,bsPath);
def createfolderZip(filePath,folderPath):
z=zipfile.ZipFile(filePath,"w",zipfile.ZIP_DEFLATED);
basePath=os.path.basename(folderPath);
zipFolder(z,folderPath,basePath);
z.close();
def extractZip(zfile, path):
z = ZFile(zfile)
z.extract_to(path)
z.close()
Created on 2010-5-26
@author: sean_long
'''
import os;
import zipfile
import os.path
class ZFile(object):
def __init__(self, filename, mode='r', basedir=''):
self.filename = filename
self.mode = mode
if self.mode in ('w', 'a'):
self.zfile = zipfile.ZipFile(filename, self.mode, compression=zipfile.ZIP_DEFLATED)
else:
self.zfile = zipfile.ZipFile(filename, self.mode)
self.basedir = basedir
if not self.basedir:
self.basedir = os.path.dirname(filename)
def addfile(self, path, arcname=None):
path = path.replace('//', '/')
if not arcname:
if path.startswith(self.basedir):
arcname = path[len(self.basedir):]
else:
arcname = '';
self.zfile.write(path, arcname)
def addfiles(self, paths):
for path in paths:
if isinstance(path, tuple):
self.addfile(*path)
else:
self.addfile(path)
def close(self):
self.zfile.close();
def extract_to(self, path):
for p in self.zfile.namelist():
self.extract(p, path)
def extract(self, filename, path):
if not filename.endswith('/'):
f = os.path.join(path, filename)
dir = os.path.dirname(f)
if not os.path.exists(dir):
os.makedirs(dir)
fd=file(f, 'wb');
fd.write(self.zfile.read(filename));
fd.close();
else:
d=os.path.join(path,filename);
if not os.path.exists(d):
os.makedirs(d);
def zipFolder(z,folderPath,basePath):
ar=os.listdir(folderPath);
for item in ar:
abtPath=os.path.join(folderPath,item);
bsPath=os.path.join(basePath,item);
if(os.path.isdir(abtPath)):
if(os.listdir(abtPath)==[]):
zif=zipfile.ZipInfo(bsPath+"/");
z.writestr(zif,"");
else:
zipFolder(z,abtPath,bsPath)
else:
z.write(abtPath,bsPath);
def createfolderZip(filePath,folderPath):
z=zipfile.ZipFile(filePath,"w",zipfile.ZIP_DEFLATED);
basePath=os.path.basename(folderPath);
zipFolder(z,folderPath,basePath);
z.close();
def extractZip(zfile, path):
z = ZFile(zfile)
z.extract_to(path)
z.close()
相关文章推荐
- 基于Python SimpleHTTPServer.py的修改脚本:HTTP文件服务器,修正中文目录列表,支持视频文件在线播放
- JAVA之File类 获取一个目录下的所有文件夹和文件,包括子文件夹和子文件
- 复制一个文件夹下所有文件(包括子文件夹)并修改文件后缀名 java实现加密文档unlock
- python中利用os模块打印任意文件夹里面的文件名字,当前文件夹路径,已经其的目录名字
- 在linux中怎样用命令完全拷贝一个目录下的所有文件(包括隐藏文件以及文件夹)到另外一个目录下
- Python压缩文件夹/解压缩zip文件
- 给Python中通过urllib2.urlopen获取网页的过程中,添加gzip的压缩与解压缩支持
- python 导入模块的坑。为什么整个项目能运行,单独运行某个文件模块就报错?多层目录不同文件夹怎么导入?
- 每天一个python小程序 007有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
- Python:遍历一个目录下所有的文件及文件夹,然后计算每个文件的字符和line的小程序
- python的tarfile模块实例 python把文件夹压缩成tar格式文件的例子
- Python 批量压缩png 支持过滤个别文件和文件夹
- JAVA之File类 获取一个目录下的所有文件夹和文件,包括子文件夹和子文件
- 利用Python将原数据压缩到一个pickle 文件夹里
- 一个获取指定目录下一定格式的文件名称和文件修改时间并保存为文件的python脚本
- python中导入其他目录或文件夹下的模块
- python如何获取一个文件夹内包括子文件夹所有文件的名字和路径
- (修改版本)一个JavaScript Function Outliner插件 支持内嵌javascript,且可以对javascript进行压缩
- Python实现压缩文件夹与解压缩zip文件的方法
- 关于在python中, 在同一个project中的同一个目录里面, 导入模块的问题