記錄一次windows平臺python解決zipfile解壓亂碼的問題

工作中使用到了解壓文件,提取壓縮文件,很簡單python的zipfile是一個很成熟的庫,沒錯,直接上手使用了,剛開始運行還是正常的,中間突然大批量的文件無法正常提取文件了,經過多方測試,發現是中文命名亂碼導致無法正常解壓,很多人說直接decode這些知識點,都無法正常解決問題,最後徹底解決問題的方法是直接執行壓縮包的命令進行操作,記錄代碼如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import os.path
import zipfile
import rarfile
import shutil
import win32com
import datetime
from nt import chdir
import chardet

def find_last(string,str):
    last_position=-1
    while True:
        position=string.find(str,last_position+1)
        if position==-1:
           return last_position
        last_position=position

#日誌記錄方法
def writeLog(content):
    log_name = str(datetime.datetime.now().year) + "-" + str(datetime.datetime.now().month) + "-" + str(datetime.datetime.now().day) + ".txt";
    # 檢查今天的日誌文件是否存在
    fp = open("D:\\py\\pyproject\\logs\\" + log_name, 'a')
    fp.write(content + "\r")
    fp.close()
def winRarZip():

    rar_zip_path = "D:\\officerar";
    file_path = "D:\\officeunzip\\";
    tmp_z_name = '';
    for fn in (fns for fns in os.listdir(rar_zip_path)):
        try:
               print fn
               print os.path.splitext(fn)[0]
               file_name = os.path.splitext(fn)[0]
               dest_dir = file_path+file_name
               is_exist = os.path.exists(dest_dir)
               
               if is_exist == False:
                   os.mkdir(dest_dir)
               src_zip = 'D:\\officerar\\'+fn
               for fn in (fns for fns in os.listdir(dest_dir)):
                          
                          son_file_path = dest_dir+"\\"+fn
                          is_exist_file = os.path.exists(son_file_path)
                          if is_exist_file == True:
                                os.remove(son_file_path)
               zip_command = 'WinRAR x {0} {1} -o+ -inul -iback -y'.format(src_zip,dest_dir)
               if os.system(zip_command)==0:
                      #print 'Successful'
                      flag = 0
                      for fn in (fns for fns in os.listdir(dest_dir)):
                          
                         print fn
                         position = find_last(fn,".")
                         #if(position == -1):
                         #    writeLog(os.path.splitext(fn)[0]+"是文件夾")
                         #print position
                         extension = fn[position+1:-1]
                         #print extension
                         if extension == "ppt" or extension == "pptx" :
                               flag = 1
                               mycopyfile(dest_dir+"\\"+fn,'D:\\officepsd\\'+file_name+'.pptx')
                               
                      if flag == 0:
                           writeLog(file_name+ "不是ppt文件")
                          
               else:  
                      print 'FAILED'
                      
        except Exception,e:
            
            print e
            writeLog(fn+"解壓異常")
            continue;

if __name__ == '__main__':

    print "unzip........."
    winRarZip()








 

解決問題的方法很多,路徑很多,不一定一條路找到底,zipfile確實費了很多時間,對中文就是支持不太好。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章