美文网首页
Py 文件MD5计算的一种方式

Py 文件MD5计算的一种方式

作者: 学习的橙子 | 来源:发表于2020-09-18 15:26 被阅读0次

    之前下载国家优课的文件(包含 txt,pdf,m3u8等)

    为了防止文件出现重复下载,于是写了一个文件md5的获取方式

    import binascii

    import os

    import hashlib

    import shutil

    def file_movie(path,move_path):

        shutil.move(path, move_path)

    def str_16(s):

        str_ = binascii.b2a_hex(str(s).encode('utf-8'))

        return str_

    def getHashCode(md5):

        seed = 131

        hash = 0

        bitarray = [i for i in md5]

        count = len(bitarray)

        while count>0:

            hash = hash * seed + int(ord(bitarray[len(bitarray)-count]))

            count-=1

        return (hash & 0x7FFFFFFF)

    def getHashPath(md5):

        if len(md5)==32:

            hashcode1 = getHashCode(md5[0:16])

            hashcode2 = getHashCode(md5[16:32])

            dir1 = str(hex(hashcode1 & 0xff)).ljust(2,'0').replace('0x','')

            dir2 = str(hex(hashcode2 & 0xff)).ljust(2,'0').replace('0x','')

            path = "/" + dir1 + "/" + dir2 + "/"

            return path

    def get_md5_id(id):

        hash_md5 = hashlib.md5()

        hash_md5.update(id)

        id = hash_md5.hexdigest()

        return id

    def get_md5_str(id):

        hmac = hashlib.md5(id.encode(encoding='UTF-8')).hexdigest()

        return hmac

    def get_big_file_md5(ip_file_path,ip_read_start,ip_read_len):

      with open(ip_file_path, 'rb') as f:

        f.seek(int(ip_read_start),1)

        context = get_md5_id(bytearray(f.read(ip_read_len)))

        f.close()

        return context

    def get_file(dow_path):

        filesize = os.path.getsize(dow_path)

        return filesize

    def get_file_md5(dow_path):

        print(dow_path)

        file_size = round(os.path.getsize(dow_path) / float(1024 * 1024), 2)

        if file_size < 28:

            with open(dow_path, 'rb') as f:

                md5obj = hashlib.md5()

                md5obj.update(f.read())

                hash = md5obj.hexdigest()

                return hash

        else:

            iStart = 0

            partLength = 16 * 1024 * 1024

            md5_one = get_big_file_md5(dow_path,iStart,partLength)

            iMidllePost = int(get_file(dow_path)/2)

            iHalfPartLength = int(partLength/2)

            iStart = iMidllePost - iHalfPartLength

            md5_two = get_big_file_md5(dow_path,iStart,partLength)

            iStart = int(get_file(dow_path)) - partLength

            md5_three = get_big_file_md5(dow_path,iStart,partLength)

            all_md5 = (md5_one + md5_two + md5_three).upper()

            all_md5 = get_md5_str(all_md5).lower()

            return all_md5

    def file_name_mode_modify(file_path,file_name,info_dict):

        file_path_split = file_path.split('\\')

        file_name_split = file_path_split[-1].split('.')

        file_name_split[0] = file_name

        n = ''

        for i in file_path_split[:-1]:

            n+=i+"\\"

        n+=  file_name_split[0]+'.'+file_name_split[1]

        os.rename(file_path,n)

        info_dict['new_file_ml'] = n

        return info_dict

    相关文章

      网友评论

          本文标题:Py 文件MD5计算的一种方式

          本文链接:https://www.haomeiwen.com/subject/scjjyktx.html