解压和压缩

import os
import time
import shutil


a='D:/7-Zip/7z.exe'                        #7zip.exe路径
b=''        #待解压文件路径
c=''           #指定解压路径
d=''       #指定新的压缩路径




def jieya(zip_exe_path,file_path,unzip_path):#(7zip.exe路径,待解压文件路径,指定解压路径,指定新的压缩路径)

# 调用7z命令批量解压文件
unzip= '{0} x "{1}" -o"{2}"  -r'.format(zip_exe_path,file_path,unzip_path)
tmp=os.popen(unzip)
tmp.close()
for root, dirs, files in os.walk(unzip_path):
    # print(root,dirs,files)
    for i in files:
        if i=='com.android.contacts':
            unzip= '{0} x "{1}" -o"{2}"  -r'.format(zip_exe_path,os.path.join(root,i),os.path.join(root,'com.android.contacts1'))

            tmp2=os.popen(unzip)
            time.sleep(0.5)
            os.remove(unzip_path + '/' + i)
            tmp2.close()

        if i == 'com.android.mms':
            unzip = '{0} x "{1}" -o"{2}"  -r'.format(zip_exe_path, os.path.join(root, i),
                                                 os.path.join(root, 'com.android.mms1'))
            tmp2 = os.popen(unzip)
            time.sleep(0.5)
            os.remove(unzip_path + '/' + i)
            tmp2.close()

        if i == 'com.android.phone':
            unzip = '{0} x "{1}" -o"{2}"  -r'.format(zip_exe_path, os.path.join(root, i),
                                                 os.path.join(root, 'com.android.phone1'))
            tmp2 = os.popen(unzip)
            time.sleep(0.5)
            os.remove(unzip_path + '/' + i)
            tmp2.close()


        if i == 'com.android.phone.recorder':
            unzip = '{0} x "{1}" -o"{2}"  -r'.format(zip_exe_path, os.path.join(root, i),
                                                 os.path.join(root, 'com.android.phone.recorder1'))
            tmp2 = os.popen(unzip)
            time.sleep(0.5)
            os.remove(unzip_path + '/' + i)
            tmp2.close()


        if i == 'com.android.server.telecom':
            unzip = '{0} x "{1}" -o"{2}"  -r'.format(zip_exe_path, os.path.join(root, i),
                                                 os.path.join(root, 'com.android.server.telecom1'))
            tmp2 = os.popen(unzip)
            time.sleep(0.5)
            os.remove(unzip_path + '/' + i)
            tmp2.close()


        if i == 'com.android.systemui':
            unzip = '{0} x "{1}" -o"{2}"  -r'.format(zip_exe_path, os.path.join(root, i),
                                                 os.path.join(root, 'com.android.systemui1'))
            tmp2 = os.popen(unzip)
            time.sleep(0.5)
            os.remove(unzip_path + '/' + i)
            tmp2.close()

        if i == 'com.huawei.android.launcher':
            unzip = '{0} x "{1}" -o"{2}"  -r'.format(zip_exe_path, os.path.join(root, i),
                                                 os.path.join(root, 'com.huawei.android.launcher1'))
            tmp2 = os.popen(unzip)
            time.sleep(0.5)
            os.remove(unzip_path + '/' + i)

            tmp2.close()
        if i == 'icons':
            unzip = '{0} x "{1}" -o"{2}"  -r'.format(zip_exe_path, os.path.join(root, i),
                                                 os.path.join(root, 'icons1'))
            tmp2 = os.popen(unzip)
            time.sleep(0.5)
            os.remove(unzip_path + '/' + i)
            tmp2.close()

    break



os.rename(unzip_path+'/icons1',unzip_path+'/icons')
os.rename(unzip_path+'/com.android.contacts1',unzip_path+'/com.android.contacts')
os.rename(unzip_path+'/com.android.mms1',unzip_path+'/com.android.mms')
os.rename(unzip_path+'/com.android.phone.recorder1',unzip_path+'/com.android.phone.recorder')
os.rename(unzip_path+'/com.android.phone1',unzip_path+'/com.android.phone')
os.rename(unzip_path+'/com.android.server.telecom1',unzip_path+'/com.android.server.telecom')
os.rename(unzip_path+'/com.android.systemui1',unzip_path+'/com.android.systemui')
os.rename(unzip_path+'/com.huawei.android.launcher1',unzip_path+'/com.huawei.android.launcher')




def yasuo(zip_exe_path,unzip_path,zip_path):#(7zip.exe路径,待解压文件路径,指定解压路径,指定新的压缩路径)

for root ,dirs, files in os.walk(unzip_path):
    # print(files)
    # print('****************')
    # print(dirs)
    # print('--------------')
    # print(root)
    # print('++++++++++++++')

    for i in dirs:
        if i == 'com.android.contacts':
            zzip='{0} a -tzip "{1}" "{2}" -r'.format(zip_exe_path, os.path.join(root,i+'1'),os.path.join(root,i)+'/*')
            tmp=os.popen(zzip)
            time.sleep(0.5)
            tmp.close()


        if i == 'com.android.mms':
            zzip='{0} a -tzip "{1}" "{2}" -r'.format(zip_exe_path, os.path.join(root,i+'1'),os.path.join(root,i)+'/*')
            tmp=os.popen(zzip)
            time.sleep(0.5)
            tmp.close()

        if i == 'com.android.phone':
            zzip='{0} a -tzip "{1}" "{2}" -r'.format(zip_exe_path, os.path.join(root,i+'1'),os.path.join(root,i)+'/*')
            tmp = os.popen(zzip)
            time.sleep(0.5)
            tmp.close()

        if i == 'com.android.phone.recorder':
            zzip='{0} a -tzip "{1}" "{2}" -r'.format(zip_exe_path, os.path.join(root,i+'1'),os.path.join(root,i)+'/*')

            tmp=os.popen(zzip)
            time.sleep(0.5)
            tmp.close()

        if i == 'com.android.server.telecom':
            zzip='{0} a -tzip "{1}" "{2}" -r'.format(zip_exe_path, os.path.join(root,i+'1'),os.path.join(root,i)+'/*')
            tmp=os.popen(zzip)
            time.sleep(0.5)
            tmp.close()

        if i == 'com.android.systemui':
            zzip='{0} a -tzip "{1}" "{2}" -r'.format(zip_exe_path, os.path.join(root,i+'1'),os.path.join(root,i)+'/*')

            tmp=os.popen(zzip)
            time.sleep(0.5)
            tmp.close()

        if i == 'com.huawei.android.launcher':
            zzip='{0} a -tzip "{1}" "{2}" -r'.format(zip_exe_path, os.path.join(root,i+'1'),os.path.join(root,i)+'/*')

            tmp=os.popen(zzip)
            time.sleep(0.5)
            tmp.close()


        if i == 'icons':
            zzip='{0} a -tzip "{1}" "{2}" -r'.format(zip_exe_path, os.path.join(root,i+'1'),os.path.join(root,i)+'/*')

            tmp=os.popen(zzip)
            time.sleep(0.5)
            tmp.close()
    break

shutil.rmtree(unzip_path + '/icons')
shutil.rmtree(unzip_path+'/com.android.contacts')
shutil.rmtree(unzip_path+'/com.android.mms')
shutil.rmtree(unzip_path+'/com.android.phone.recorder')
shutil.rmtree(unzip_path+'/com.android.phone')
shutil.rmtree(unzip_path+'/com.android.server.telecom')
shutil.rmtree(unzip_path+'/com.android.systemui')
shutil.rmtree(unzip_path+'/com.huawei.android.launcher')








time.sleep(1)
os.rename(unzip_path+'/icons1.zip',unzip_path+'/icons')
os.rename(unzip_path+'/com.android.contacts1',unzip_path+'/com.android.contacts')
os.rename(unzip_path+'/com.android.mms1',unzip_path+'/com.android.mms')
os.rename(unzip_path+'/com.android.phone.recorder1',unzip_path+'/com.android.phone.recorder')
os.rename(unzip_path+'/com.android.phone1',unzip_path+'/com.android.phone')
os.rename(unzip_path+'/com.android.server.telecom1',unzip_path+'/com.android.server.telecom')
os.rename(unzip_path+'/com.android.systemui1',unzip_path+'/com.android.systemui')
os.rename(unzip_path+'/com.huawei.android.launcher1',unzip_path+'/com.huawei.android.launcher')

time.sleep(1)
zzip='{0} a -tzip "{1}" "{2}/*" -r'.format(zip_exe_path,zip_path,unzip_path)
# print(zzip)
tmp=os.popen(zzip)
tmp.close()


shutil.rmtree(unzip_path)


jieya(a, b, c)


# yasuo(a, c, d)

你可能感兴趣的:(解压和压缩)