如何比较两个zip文件中的文件是否完全相同?

问题描述

我有两个 zip 文件。我想看看所有内容文件名和 zip 中每个文件内容)是否相同。

一个 similar question。但答案不支持 zip 文件

有人有好主意吗?

解决方法

即使您压缩相同的项目,似乎 zip 也会有不同的哈希值。我们可以把它分成两部分:一是解压,二是解压后比较文件夹。

import os
import filecmp
import zipfile

def are_dir_trees_equal(dir1,dir2):

    dirs_cmp = filecmp.dircmp(dir1,dir2)
    if len(dirs_cmp.left_only)>0 or len(dirs_cmp.right_only)>0 or \
       len(dirs_cmp.funny_files)>0:
       return False
    (_,mismatch,errors) =  filecmp.cmpfiles(
        dir1,dir2,dirs_cmp.common_files,shallow=False)
    if len(mismatch)>0 or len(errors)>0:
        return False
    for common_dir in dirs_cmp.common_dirs:
        new_dir1 = os.path.join(dir1,common_dir)
        new_dir2 = os.path.join(dir2,common_dir)
        if not are_dir_trees_equal(new_dir1,new_dir2):
           return False
    return True


BASE_PATH = '/Users/Documents/'
model1 = os.path.join(BASE_PATH,'test1.zip')
model2 = os.path.join(BASE_PATH,'test2.zip')

with zipfile.ZipFile(model1,"r") as zip_ref:
    zip_ref.extractall(BASE_PATH)
with zipfile.ZipFile(model2,"r") as zip_ref:
    zip_ref.extractall(BASE_PATH)

folder1 = model1.split('.')[0]
folder2 = model2.split('.')[0]

is_equal = are_dir_trees_equal(folder1,folder2)
print(is_equal)
,

我尝试在 python 中使用 zipfile 内置模块。

from zipfile import ZipFile

def compare(file1,file2):
    try:
        with ZipFile(file1,'r') as file:
            f1 = str([x for x in file.infolist()])
        with ZipFile(file2,'r') as file:
            f2 = str([x for x in file.infolist()])
        return f1 == f2
    except FileNotFoundError:
        return f"Either file at {file1} or {file2} does not exist"


f = compare(file1='storage/1.zip',file2='storage/2.zip')
print(f)

我不知道这是不是正确的方法(如果不是请纠正我)

,

这是我的尝试。确保 ZipFiles 包含相同的项目并且这些项目具有匹配的 CRC32 可能就足够了。 (被比较的两个 ZipFiles 具有相同名称和相同 CRC32 但它们是不同文件的可能性有多大?)如果足够好,请省略比较文件内容的循环。

from zipfile import ZipFile

BUFSIZE = 1024

def are_equivalent(filename1,filename2):
    """Compare two ZipFiles to see if they would expand into the same directory structure
    without actually extracting the files.
    """
    
    with ZipFile(filename1,'r') as zip1,ZipFile(filename2,'r') as zip2:
        
        # Index items in the ZipFiles by filename. For duplicate filenames,a later
        # item in the ZipFile will overwrite an ealier item; just like a later file
        # will overwrite an earlier file with the same name when extracting.
        zipinfo1 = {info.filename:info for info in zip1.infolist()}
        zipinfo2 = {info.filename:info for info in zip2.infolist()}
        
        # Do some simple checks first
        # Do the ZipFiles contain the same the files?
        if zipinfo1.keys() != zipinfo2.keys():
            return False
        
        # Do the files in the archives have the same CRCs? (This is a 32-bit CRC of the
        # uncompressed item. Is that good enough to confirm the files are the same?)
        if any(zipinfo1[name].CRC != zipinfo2[name].CRC for name in zipinfo1.keys()):
            return False
        
        # Skip/omit this loop if matching names and CRCs is good enough.
        # Open the corresponding files and compare them.
        for name in zipinfo1.keys():
            
            # 'ZipFile.open()' returns a ZipExtFile instance,which has a 'read()' method
            # that accepts a max number of bytes to read. In contrast,'ZipFile.read()' reads
            # all the bytes at once.
            with zip1.open(zipinfo1[name]) as file1,zip2.open(zipinfo2[name]) as file2:
                
                while True:
                    buffer1 = file1.read(BUFSIZE)
                    buffer2 = file2.read(BUFSIZE)
                    
                    if buffer1 != buffer2:
                        return False
                    
                    if not buffer1:
                        break
                        
        return True