我想创建一个zip文件.将文件夹添加到zip文件,然后将一堆文件添加到该文件夹.
所以我想最终得到一个带有文件的单个文件夹的zip文件.
我不知道在zip文件中有文件夹或谷歌的东西是不是很糟糕的做法.
我从这开始:
def addFolderToZip(myZipFile,folder): folder = folder.encode('ascii') #convert path to ascii for ZipFile Method for file in glob.glob(folder+"/*"): if os.path.isfile(file): print file myZipFile.write(file, os.path.basename(file), zipfile.ZIP_DEFLATED) elif os.path.isdir(file): addFolderToZip(myZipFile,file) def createZipFile(filename,files,folders): curTime=strftime("__%Y_%m_%d", time.localtime()) filename=filename+curTime; print filename zipFilename=utils.getFileName("files", filename+".zip") myZipFile = zipfile.ZipFile( zipFilename, "w" ) # Open the zip file for writing for file in files: file = file.encode('ascii') #convert path to ascii for ZipFile Method if os.path.isfile(file): (filepath, filename) = os.path.split(file) myZipFile.write( file, filename, zipfile.ZIP_DEFLATED ) for folder in folders: addFolderToZip(myZipFile,folder) myZipFile.close() return (1,zipFilename) (success,filename)=createZipFile(planName,files,folders);
摘自:http://mail.python.org/pipermail/python-list/2006-August/396166.html
删除所有文件夹并将目标文件夹(及其子文件夹)中的所有文件放入单个zip文件中.我无法让它添加整个文件夹.
如果我将路径提供给myZipFile.write中的文件夹,我会得到
IOError:[Errno 13]权限被拒绝:'..\packed\bin'
任何帮助都非常受欢迎.
相关问题:如何使用python(2.5版)压缩文件夹的内容?
你也可以使用shutil
import shutil zip_name = 'path\to\zip_file' directory_name = 'path\to\directory' # Create 'path\to\zip_file.zip' shutil.make_archive(zip_name, 'zip', directory_name)
这会将整个文件夹放在zip中.
好吧,在我明白你想要的东西之后,它就像使用第二个参数一样简单zipfile.write
,你可以使用你想要的任何东西:
import zipfile myZipFile = zipfile.ZipFile("zip.zip", "w" ) myZipFile.write("test.py", "dir\\test.py", zipfile.ZIP_DEFLATED )
创建一个zipfile文件,test.py
将其解压缩到一个名为的目录dir
编辑:我曾经不得不在zip文件中创建一个空目录:这是可能的.在上面的代码刚刚从zipfile中删除文件test.py之后,文件就消失了,但是空目录仍然存在.
zip文件没有目录结构,只有一堆路径名及其内容.这些路径名应该相对于一个虚构的根文件夹(ZIP文件本身)."../"前缀在zip文件中没有定义的含义.
假设您有一个文件,a
并且您希望将其存储在zip文件中的"文件夹"中.在zipfile中存储文件时,您所要做的就是在文件名前添加文件夹名称:
zipi= zipfile.ZipInfo() zipi.filename= "folder/a" # this is what you want zipi.date_time= time.localtime(os.path.getmtime("a"))[:6] zipi.compress_type= zipfile.ZIP_DEFLATED filedata= open("a", "rb").read() zipfile1.writestr(zipi, filedata) # zipfile1 is a zipfile.ZipFile instance
我不知道任何ZIP实现允许在ZIP文件中包含空文件夹.我可以想到一个解决方法(在zip"文件夹"中存储虚拟文件名,在提取时应该忽略),但不能跨实现移植.
import zipfile import os class ZipUtilities: def toZip(self, file, filename): zip_file = zipfile.ZipFile(filename, 'w') if os.path.isfile(file): zip_file.write(file) else: self.addFolderToZip(zip_file, file) zip_file.close() def addFolderToZip(self, zip_file, folder): for file in os.listdir(folder): full_path = os.path.join(folder, file) if os.path.isfile(full_path): print 'File added: ' + str(full_path) zip_file.write(full_path) elif os.path.isdir(full_path): print 'Entering folder: ' + str(full_path) self.addFolderToZip(zip_file, full_path) def main(): utilities = ZipUtilities() filename = 'TEMP.zip' directory = 'TEMP' utilities.toZip(directory, filename) main()
我在跑:
python tozip.py
这是日志:
havok@fireshield:~$ python tozip.py File added: TEMP/NARF (7ª copia) Entering folder: TEMP/TEMP2 File added: TEMP/TEMP2/NERF (otra copia) File added: TEMP/TEMP2/NERF (copia) File added: TEMP/TEMP2/NARF File added: TEMP/TEMP2/NARF (copia) File added: TEMP/TEMP2/NARF (otra copia) Entering folder: TEMP/TEMP2/TEMP3 File added: TEMP/TEMP2/TEMP3/DOCUMENTO DEL FINAL File added: TEMP/TEMP2/TEMP3/DOCUMENTO DEL FINAL (copia) File added: TEMP/TEMP2/NERF File added: TEMP/NARF (copia) (otra copia) File added: TEMP/NARF (copia) (copia) File added: TEMP/NARF (6ª copia) File added: TEMP/NERF (copia) (otra copia) File added: TEMP/NERF (4ª copia) File added: TEMP/NERF (otra copia) File added: TEMP/NERF (3ª copia) File added: TEMP/NERF (6ª copia) File added: TEMP/NERF (copia) File added: TEMP/NERF (5ª copia) File added: TEMP/NARF (8ª copia) File added: TEMP/NARF (3ª copia) File added: TEMP/NARF (5ª copia) File added: TEMP/NERF (copia) (3ª copia) File added: TEMP/NARF File added: TEMP/NERF (copia) (copia) File added: TEMP/NERF (8ª copia) File added: TEMP/NERF (7ª copia) File added: TEMP/NARF (copia) File added: TEMP/NARF (otra copia) File added: TEMP/NARF (4ª copia) File added: TEMP/NERF File added: TEMP/NARF (copia) (3ª copia)
正如您所看到的,它可以工作,存档也可以.这是一个递归函数,可以压缩整个文件夹.唯一的问题是它不会创建一个空文件夹.
干杯.