python to download zipfile and unzip


#This simple script can use for download zip file from any url and unzip data. you need to have wget utility to download file.

import sys

import time
from urllib.request import FancyURLopener
import os

from datetime import datetime,date

import calendar
from os import path
import zipfile

def unzip(zf):
print("================unziping"+zf+"================")
zip = zipfile.ZipFile(zf,"r")
zip.extractall()


def runsystemcmd(cmd):
print("Cmd : " + cmd)
os.system(cmd)

class TestOpener(FancyURLopener, object):
    version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
            
def downloadFile(url):

starttime = time.time()
 testurlopen = TestOpener();
 response = testurlopen.open(url)

 meta = response.info()
 logging.error(meta)

 urlcontentlen = meta["Content-Length"]
 if urlcontentlen==None:
  urlcontentlen = 0
 outfile = url.split('/')[-1]

 f = open(outfile, "wb")
 read_bytes = response.read(1024*10)
 while read_bytes:
  f.write(read_bytes)
  read_bytes = response.read(1024*10)
 f.close()
 print("Elapsed Time: " +str(time.time() - starttime))




def unzipfile(zf):
if(os.path.exists(zf)):
unzip(zf)
os.remove(zf)
else:
print("File not exist " + zipfile)


if __name__ == '__main__':
    url="put the  zipfile url here"
    downloadFile(url)
    zf = url.split('/')[-1]
    unzipfile(zf)
    print("Finishing current downloads")



1 comment:

  1. PokerStars - Gaming & Slots at Aprcasino
    Join the fun at goyangfc.com Aprcasino and play the best of apr casino the best PokerStars casino gri-go.com games including Slots, kadangpintar Blackjack, Roulette, Video Poker and https://jancasino.com/review/merit-casino/ more!

    ReplyDelete

would you like it. :)