This commit is contained in:
caiwx86 2024-11-14 23:36:12 +08:00
parent cfc1c698df
commit f869222686

View File

@ -488,22 +488,30 @@ class CBZUtils:
return data
@classmethod
def zip_compression(cls, source_dir=None, target_file=None, remove=True):
def zip_compression(cls, source_dir=None, target_file=None, remove=True, min_files=1):
target_dir = os.path.dirname(target_file)
if not os.path.exists(target_dir):
os.makedirs(target_dir)
count_files = 0
if not os.path.exists(target_file) and source_dir is not None:
with ZipFile(target_file, mode='w') as zf:
for path, dir_names, filenames in os.walk(source_dir):
count_files = len(filenames)
path = Path(path)
arc_dir = path.relative_to(source_dir)
y = 0
for filename in filenames:
y = y + 1
print("打包中:" + str(y) + "/" + str(len(filenames)), os.path.join(source_dir, filename))
print("打包中:" + str(y) + "/" + str(count_files), os.path.join(source_dir, filename))
zf.write(path.joinpath(filename), arc_dir.joinpath(filename))
zf.close()
logging.info(f"打包完成:{target_file}")
if count_files <= min_files:
try:
os.path.remove(target_file)
logging.info(f"{target_file} 文件不符合最小文件数规则,已删除")
except Exception as e:
logging.error(f"{target_file} 删除报错")
@DeprecationWarning
@classmethod