python-unzip

2017-03-31  本文已影响0人  amazing_bing
#-*- coding: UTF-8 -*- 
#__author__:Bing
#email:amazing_bing@outlook.com

#解压和重新命名文件,并删除zip文件或者目录下的非PDF文件
import os,sys  
import zipfile 

def unzip_dir(file_name = "test",zipfilename="m:\\scan02.zip", unzipdirname="m:\\"):  
    fullzipfilename = os.path.abspath(zipfilename)  
    fullunzipdirname = os.path.abspath(unzipdirname)  
    #if not os.path.exists(fullzipfilename): 
              
    #Start extract files ...  
    try:
      srcZip = zipfile.ZipFile(fullzipfilename, "r")  
      for eachfile in srcZip.namelist():  
          #print "Unzip file %s ..." % eachfile 
          eachfilename = os.path.normpath(os.path.join(fullunzipdirname, '{0}_{1}'.format(file_name,eachfile)))  
          eachdirname = os.path.dirname(eachfilename)
          if eachfile.endswith(".pdf",4):
            fd=open(eachfilename, "wb")  
            fd.write(srcZip.read(eachfile))  
            fd.close()
          else:
            pass 
      srcZip.close()
      return {"status":1}
    except:
      return {"status":0}


unzip_dir()
 
  
def zip_dir(dirname, zipfilename):  
    filelist = []  
    #Check input ...  
    fulldirname = os.path.abspath(dirname)  
    fullzipfilename = os.path.abspath(zipfilename)  
    print "Start to zip %s to %s ..." % (fulldirname, fullzipfilename)  
    if not os.path.exists(fulldirname):  
        print "Dir/File %s is not exist, Press any key to quit..." % fulldirname  
        inputStr = raw_input()  
        return  
    if os.path.isdir(fullzipfilename):  
        tmpbasename = os.path.basename(dirname)  
        fullzipfilename = os.path.normpath(os.path.join(fullzipfilename, tmpbasename))  
    if os.path.exists(fullzipfilename):      
        print "%s has already exist, are you sure to modify it ? [Y/N]" % fullzipfilename  
        while 1:  
            inputStr = raw_input()  
            if inputStr == "N" or inputStr == "n" :  
                return  
            else:  
                if inputStr == "Y" or inputStr == "y" :  
                    print "Continue to zip files..."  
                    break  
  
    #Get file(s) to zip ...  
    if os.path.isfile(dirname):  
        filelist.append(dirname)  
        dirname = os.path.dirname(dirname)  
    else:  
        #get all file in directory  
        for root, dirlist, files in os.walk(dirname):  
            for filename in files:  
                filelist.append(os.path.join(root,filename))  
  
    #Start to zip file ...  
    destZip = zipfile.ZipFile(fullzipfilename, "w")  
    for eachfile in filelist:  
        destfile = eachfile[len(dirname):]  
        print "Zip file %s..." % destfile  
        destZip.write(eachfile, destfile)  
    destZip.close()  
    print "Zip folder succeed!"  




# def download(domain):
#   import urllib2
#   print "downloading with urllib2"
#   url = 'http://scan02.secfuture.com:8081/wvs_scan_getresult/?id=2' 
#   f = urllib2.urlopen(url) 
#   data = f.read() 
#   with open("m:\\{0}.zip".format(domain), "wb") as code:     
#       code.write(data)
#       code.close()


上一篇下一篇

猜你喜欢

热点阅读