ArchiveZip'i dosya yazabilmesi için de generic hale getiriyoruz. Aslında ayrı bir Zip modülü olsun dememin nedeni de buydu. ArchiveZip'i böyle generic yapmaya çalışmak "zorlama" gözüküyor.
This commit is contained in:
+10
-3
@@ -54,13 +54,20 @@ class ArchiveTarFile(ArchiveBase):
|
|||||||
tar.close()
|
tar.close()
|
||||||
|
|
||||||
class ArchiveZip(ArchiveBase):
|
class ArchiveZip(ArchiveBase):
|
||||||
def __init__(self, filepath, type="zip"):
|
def __init__(self, filepath, type="zip", mode='r'):
|
||||||
super(ArchiveZip, self).__init__(filepath, type)
|
super(ArchiveZip, self).__init__(filepath, type)
|
||||||
|
self.zip = zipfile.ZipFile(filepath, mode)
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.zip.close()
|
||||||
|
|
||||||
|
def add_file(self, zipPath, filePath):
|
||||||
|
self.zip.write(filePath, filePath, zipfile.ZIP_DEFLATED)
|
||||||
|
|
||||||
def unpack_file_cond(self, pred, targetDir, archiveRoot=''):
|
def unpack_file_cond(self, pred, targetDir, archiveRoot=''):
|
||||||
""" unpack file according to predicate function filename -> bool"""
|
""" unpack file according to predicate function filename -> bool"""
|
||||||
super(ArchiveZip, self).unpack(targetDir)
|
super(ArchiveZip, self).unpack(targetDir)
|
||||||
zip = zipfile.ZipFile(self.filePath, 'r')
|
zip = self.zip
|
||||||
for file in zip.namelist():
|
for file in zip.namelist():
|
||||||
if pred(file): # check if condition holds
|
if pred(file): # check if condition holds
|
||||||
|
|
||||||
@@ -94,7 +101,6 @@ class ArchiveZip(ArchiveBase):
|
|||||||
fileContent = zip.read(file)
|
fileContent = zip.read(file)
|
||||||
buff.write(fileContent)
|
buff.write(fileContent)
|
||||||
buff.close()
|
buff.close()
|
||||||
zip.close()
|
|
||||||
|
|
||||||
def unpack_files(self, paths, targetDir):
|
def unpack_files(self, paths, targetDir):
|
||||||
self.unpack_file_cond(lambda f:f in paths, targetDir)
|
self.unpack_file_cond(lambda f:f in paths, targetDir)
|
||||||
@@ -104,6 +110,7 @@ class ArchiveZip(ArchiveBase):
|
|||||||
|
|
||||||
def unpack(self, targetDir):
|
def unpack(self, targetDir):
|
||||||
self.unpack_file_cond(lambda f: True, targetDir)
|
self.unpack_file_cond(lambda f: True, targetDir)
|
||||||
|
self.close()
|
||||||
return
|
return
|
||||||
|
|
||||||
class Archive:
|
class Archive:
|
||||||
|
|||||||
Reference in New Issue
Block a user