BadZipFile:文件不是 zip 文件 [英] BadZipFile: File is not a zip file
问题描述
这是我的代码.尝试执行此脚本时出现错误
This is my code. I get the error when I try to execute this script
Error raise BadZipFile("File is not a zip file")
BadZipFile: File is not a zip file
这是我的源目录路径
data_dir = r'L:\DataQA\Python Unzip Files\Source Zipped'
我在Source Zipped"(未压缩)文件夹中有多个压缩文件夹.当我将 source Zipped 的所有子文件夹压缩到单个压缩文件夹中时,相同的代码有效.但我不想要这种方法.
I have multiple zipped folders within ‘Source Zipped’(uncompressed) folder. The same code works when I zip all the subfolder of source Zipped into single zipped folder. But I don’t want this approach.
import os
import zipfile
import shutil
import json
import logging
import logging.config
import time
def my_start_time():
global start_time, cumulative_time, start_time_stamp
start_time = time.time()
this_time = time.localtime(start_time)
start_time_stamp = '{:4d}{:02d}{:02d} {:02d}:{:02d}:{:02d}'.format(\
this_time.tm_year, this_time.tm_mon, this_time.tm_mday,\
this_time.tm_hour, this_time.tm_min, this_time.tm_sec)
cumulative_time = start_time - start_time
logging.info('Initial Setup: {:s}'.format(start_time_stamp))
def my_time():
global cumulative_time
time_taken = time.time() - start_time
incremental_time = time_taken - cumulative_time
cumulative_time = time_taken
logging.info("Started: %s Complete: Cumulative: %.4f s Incremental: %.4f s\n" \
% (start_time_stamp, cumulative_time, incremental_time) )
logging.basicConfig(filename='myunzip_task_log.txt',level=logging.DEBUG)
my_start_time()
logging.info('Initial Setup...')
def write_to_json(data, file):
value = False
with open(file, 'w') as f:
json.dump(json.dumps(data, sort_keys=True),f)
f.close()
value = True
return value
data_dir = r'L:\DataQA\Python Unzip Files\Source Zipped'
temp_dir = r'L:\DataQA\Python Unzip Files\temp1'
new_dir = r'L:\DataQA\Python Unzip Files\temp2'
final_dir = r'L:\DataQA\Python Unzip Files\Destination Unzipped files'
big_list = os.listdir(data_dir)
archive_count = 0
file_count = 152865
basename1 = os.path.join(final_dir,'GENERIC_ROUGHDRAFT')
basename2 = os.path.join(final_dir,'XACTDOC')
my_time()
archive_count = len(big_list)
logging.info('Unzipping {} archives...'.format(archive_count))
for folder in big_list:
prior_count = file_count
logging.info('Starting: {}'.format(folder))
try:
shutil.rmtree(temp_dir)
except FileNotFoundError:
pass
os.mkdir(temp_dir)
with zipfile.ZipFile(os.path.join(data_dir,folder),mode='r') as a_zip:
a_zip.extractall(path = temp_dir)
archive_count += 1
logging.info('Cumulative total of {} archive(s) unzipped'.format(archive_count))
bigger_list = os.listdir(temp_dir)
logging.info('Current archive contains {} subfolders'.format(len(bigger_list)))
for sub_folder in bigger_list:
with zipfile.ZipFile(os.path.join(temp_dir,sub_folder),mode='r') as b_zip:
b_zip.extractall(path = new_dir)
file1 = "%s (%d).%s" % (basename1, file_count, 'xml')
file2 = "%s (%d).%s" % (basename2, file_count, 'xml')
shutil.copy(os.path.join(new_dir, 'GENERIC_ROUGHDRAFT.xml'), file1)
shutil.copy(os.path.join(new_dir, 'XACTDOC.xml'), file2)
file_count += 1
logging.info('{} subfolders unzipped'.format(file_count - prior_count))
#os.remove(data_dir)
shutil.rmtree(data_dir)
os.mkdir(data_dir)
#os.unlink(data_dir)
my_time()
logging.info('Total of {0} files -- {1} pairs -- should be in {2}'.format(2*(file_count-1), file_count-1, final_dir))
time.sleep(1)
my_time()
推荐答案
在两个 zip 存档打开语句中:
in both zip archive open statements:
with zipfile.ZipFile(os.path.join(data_dir,folder),mode='r')
和
with zipfile.ZipFile(os.path.join(temp_dir,sub_folder),mode='r')
没有什么(至少没有什么我们可以检查)保证您传递的文件名实际上是 .zip
文件.它可能是一个目录,一个已经提取的文件,某个已经存在的文件......
nothing (at least nothing that we can check) guarantees that the file names you're passing are actually .zip
files. It could be a directory, an already extracted file, some file that was already there...
我建议您在提取之前检查文件扩展名,例如:
I suggest that you check the file extension prior to extracting, for instance:
import fnmatch
zfn = os.path.join(temp_dir,sub_folder)
if fnmatch.fnmatch(zfn,"*.zip"):
with zipfile.ZipFile(zfn,mode='r') as whatever:
某些 .zip 文件可能已损坏,但可能性较小.此外,如果您想提取 .jar
和其他具有不同扩展名的 zip 结构文件,请将 fnmatch
替换为
Some .zip files could be corrupt, but that's less likely. Also, if you wanted to extract .jar
and other zip-structured files with a different extension, replace the fnmatch
by
if zfn.lower().endswith(('.zip','.jar','.docx')):
这篇关于BadZipFile:文件不是 zip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!