class ZipArchive(BaseArchive): def __init__(self, file): self._archive = ZipFile(file) def list(self, *args, **kwargs): self._archive.printdir(*args, **kwargs) def filenames(self): return self._archive.namelist() def namelist(self): return self._archive.namelist() def close(self): return self._archive.close() def is_encrypted(self): for file_ in self._archive.infolist(): if file_.flag_bits & 0x1 != 0: return True else: return False def extractall(self, file): return self._archive.extractall(file)
def printContents(zip_file_path): #validate the source zip file zf = ZipFile(zip_file_path, 'r') #unzip the contents of the file print "\"%s\" contents:" % (zip_file_path) zf.printdir() return 0
def DownloadAndExtractZipFile(): zip_loc = "https://www.bseindia.com/markets/MarketInfo/BhavCopy.aspx" # connect to a URL website = urllib.request.urlopen(zip_loc) # read html code html = website.read() # use re.findall to get all the links # b'"((http|ftp)s?://.*?)"'.decode(encoding) links = re.findall( "http://www.bseindia.com/download/BhavCopy/Equity/.*_CSV.ZIP", html.decode('utf-8')) if (len(links) > 0): content = requests.get(links[0]) # unzip the content f = ZipFile(BytesIO(content.content)) f.extractall() # f.extract("EQ080120.CSV") f.printdir() # print(f.namelist()[0]) filepath = os.getcwd() + '\\' + f.namelist()[0] # filepath = os.path.join(os.getcwd(), f.namelist()[0]) return filepath else: return ""
def extractZipFile(fileName): zip = ZipFile(fileName,'r') print("\nAll files :-") zip.printdir() zip.extractall() foldName = os.path.splitext(fileName)[0] print(f"\nSuccessfully Save.\nFolder name :- {foldName}")
def main(args): visual_recognition = VisualRecognition( version='2016-05-20', api_key='7b851fccf7f17a35fc7569a5dad6e1eb4f650f70') with open('ingredients.txt') as f: lines = f.read().splitlines() for line in lines: directory = "C:/Dev/GitHub/flavortown/imagetesting/zips" + line query = line max_images = 25 save_directory = directory image_type = "Action" query = query.split() query = '+'.join(query) query = query + "+walmart+OR+amazon" url = "https://www.google.com/search?q=" + query + "&source=lnms&tbm=isch" header = { 'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36" } soup = get_soup(url, header) ActualImages = [ ] # contains the link for Large original images, type of image for a in soup.find_all("div", {"class": "rg_meta"}): link, Type = json.loads(a.text)["ou"], json.loads(a.text)["ity"] ActualImages.append((link, Type)) fileNames = [] for i, (img, Type) in enumerate(ActualImages[0:max_images]): try: req = urllib2.Request(img, headers={'User-Agent': header}) raw_img = urllib2.urlopen(req).read() fileName = "" if len(Type) == 0: fileName = "img" + "_" + str(i) + ".jpg" f = open(fileName, 'wb') else: fileName = "img" + "_" + str(i) + "." + Type f = open(fileName, 'wb') f.write(raw_img) f.close() fileNames.append(fileName) except Exception as e: print("could not load : " + img) myzip = ZipFile(line + '.zip', 'w', zipfile.ZIP_DEFLATED) for fileName in fileNames: myzip.write(fileName) myzip.printdir() myzip.close() for fileName in fileNames: os.remove(fileName)
def test_zip(self): url = "https://www.python.org/ftp/python/3.5.0/python-3.5.0-embed-amd64.zip" f = SeekableHTTPFile(url, debug=True) zf = ZipFile(f) zf.printdir() filelist = set(zf.namelist()) self.assertIn("python.exe", filelist) pyenv = zf.read("pyvenv.cfg") self.assertEqual(pyenv.rstrip(), b"applocal = true")
def unzip_fF(fFname): from zipfile import ZipFile ''' fFname (str): a file path to a target zip folder to be unzipped ''' zip = ZipFile(fFname, 'r') zip.printdir() # To print all the contents of the zip file zip.extractall() zip.close()
def test_zip(self): url = "https://www.python.org/ftp/python/3.5.0/python-3.5.0-embed-amd64.zip" f = SeekableHTTPFile(url, debug=True) zf = ZipFile(f) zf.printdir() filelist = set(zf.namelist()) self.assertIn("python.exe", filelist) pyenv = zf.read("pyvenv.cfg") self.assertEqual(pyenv.rstrip(), b"applocal = true")
def Chatbot(): os.makedirs('Chatbot') print('Making Model...') time.sleep(2.5) ic = ZipFile(os.getcwd() + '//MODEL//CHATBOT.zip', 'r') ic.printdir() time.sleep(2.5) print('Finishing Up!') ic.extractall() print('Done!')
def ObjectDetection(): # Extracting The Object Detection VirtualEnviorment= os.makedirs('Object Detection') print('Making Model...') time.sleep(2.5) ic = ZipFile(os.getcwd() + '//MODEL//OD.zip', 'r') ic.printdir() time.sleep(2.5) print('Finishing Up!') ic.extractall() print('Done!')
def StockPrediction(): os.makedirs('Stock Predictor') print('Making Model...') time.sleep(2.5) ic = ZipFile(os.getcwd() + '//MODEL//STOCK.zip', 'r') ic.printdir() time.sleep(2.5) print('Finishing Up!') ic.extractall() print('Done!')
def SpeechRecognition(): os.makedirs('Speech Recognition') print('Making Model...') time.sleep(2.5) ic = ZipFile(os.getcwd() + '//MODEL//SR.zip', 'r') ic.printdir() time.sleep(2.5) print('Finishing Up!') ic.extractall() print('Done!')
def ImageClassifier(): # Extracting The Image Classifier VirtualEnviorment os.makedirs('Image Classifier') print('Making Model...') time.sleep(2.5) ic = ZipFile(os.getcwd() + '//MODEL//IMAGE_CLASSIFIER.zip', 'r') ic.printdir() time.sleep(2.5) print('Finishing Up!') ic.extractall() print('Done!')
def unzip_ff(fname): """ fname (str): File path to a target zip folder to be unzipped """ # Library from zipfile import ZipFile z = ZipFile(fname, 'r') z.printdir() # To print all the contents of the zip file z.extractall() z.close()
def main(zfname, keys, generate=True): zf = ZipFile(zfname) #print(zf.infolist()[-3:]) zf.printdir() # print(zf.namelist()) if generate: dict_generator(keys) # zf.read(zf.namelist()[-1]) # RuntimeError: File LabAccessCodes-TopSecret.txt is encrypted, password required for extraction filename = zf.namelist()[-1] try: zf.read(filename) except RuntimeError as e: print(e) print("attempting to crack now..") attempt_cracker(zf, filename)
""" Many ==> One Deflate Algo is used to compress Why .zip ? - reduced size - encapsulate data in single file """ # printing contents of python file from zipfile import ZipFile # Note Capital # builting file file_name="MyZip.zip" f=ZipFile(file_name,'r') f.printdir() f.close()
def classify(self, imgFile): rgb = io.imread(imgFile) aspect_ratio = len(rgb) / len(rgb[1]) rgb = transform.resize(rgb, [int(1000 * aspect_ratio), 1000]) img = color.rgb2lab(rgb) thresholded = np.logical_and( *[img[..., i] > t for i, t in enumerate([40, 0, 0])]) ''' fig, ax = plt.subplots(ncols=2) ax[0].imshow(rgb); ax[0].axis('off') ax[1].imshow(thresholded); ax[1].axis('off') plt.show() ''' X = np.argwhere(thresholded)[::5] X = np.fliplr(X) db = DBSCAN(eps=50, min_samples=200).fit(X) core_samples_mask = np.zeros_like(db.labels_, dtype=bool) core_samples_mask[db.core_sample_indices_] = True labels = db.labels_ # Number of clusters in labels, ignoring noise if present. n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0) print('Estimated number of clusters: %d' % n_clusters_) unique_labels = set(labels) ''' # ############################################################################# # Plot result import matplotlib.pyplot as plt # Black removed and is used for noise instead. colors = [plt.cm.Spectral(each) for each in np.linspace(0, 1, len(unique_labels))] for k, col in zip(unique_labels, colors): if k == -1: # Black used for noise. col = [0, 0, 0, 1] class_member_mask = (labels == k) xy = X[class_member_mask & core_samples_mask] plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col), markeredgecolor='k', markersize=14) xy = X[class_member_mask & ~core_samples_mask] plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col), markeredgecolor='k', markersize=6) plt.title('Estimated number of clusters: %d' % n_clusters_) plt.show() x = edge_roberts.sum(axis=0) x = x - np.min(x[np.nonzero(x)]) averageVal = x.mean() x = x - 5 x[x < (averageVal / 6)] = 0 y = range(len(img[1])) plt.plot(y, x) X = np.array(list(zip(x,np.zeros(len(x)))), dtype=np.int) bandwidth = estimate_bandwidth(X, quantile=0.25) ms = MeanShift(bandwidth=bandwidth, bin_seeding=True) ms.fit(X) labels = ms.labels_ cluster_centers = ms.cluster_centers_ labels_unique = np.unique(labels) n_clusters_ = len(labels_unique) ''' cropped_images = [] unique_labels.remove(-1) col = 0 for k in unique_labels: #my_members = labels == k #members = X[my_members, 0] left = min(X[labels == k][:, 0]) right = max(X[labels == k][:, 0]) padding = 20 if left > padding: left = left - padding if right < len(img[1]) - padding: right = right + padding cropped_images.append(rgb[0:len(img), left:right]) # save each cropped image by its index number myzip = ZipFile('cutimgs.zip', 'w', zipfile.ZIP_DEFLATED) for c, cropped_image in enumerate(cropped_images): io.imsave(str(c) + ".png", cropped_image) myzip.write(str(c) + ".png") myzip.printdir() myzip.close() for c, cropped_image in enumerate(cropped_images): os.remove(str(c) + ".png") with open('cutimgs.zip', 'rb') as img: param = {'classifier_ids': "foodtest_843163904"} params = json.dumps(param) response = self.vr.classify(images_file=img, parameters=params) classes = [] for image in response['images']: if (image['classifiers'][0]['classes'][0]['class'] ) not in classes: classes.append( (image['classifiers'][0]['classes'][0]['class'])) os.remove('cutimgs.zip') return classes
from zipfile import ZipFile import os import sys if len(sys.argv) < 1: print "No filename specified here. Exiting..." sys.exit() filepath = sys.argv[1] if not os.path.exists(filepath): print "filepath {0} doesnot exist".format(filepath) sys.exit() zipfile_object = ZipFile(filepath) zipfile_object.printdir() zipfile_object.close()
def full(fileName): vr = VisualRecognition(version='2016-05-20', api_key='7b851fccf7f17a35fc7569a5dad6e1eb4f650f70') rgb = scipy.misc.imread(fileName, mode='RGB') aspect_ratio = len(rgb) / len(rgb[1]) rgb = transform.resize(rgb, [int(1000*aspect_ratio), 1000]) img = color.rgb2lab(rgb) thresholded = np.logical_and(*[img[..., i] > t for i, t in enumerate([40, 0, 0])]) if (np.sum(thresholded) > (thresholded.size / 2)): thresholded = np.invert(thresholded) X = np.argwhere(thresholded)[::5] X = np.fliplr(X) db = DBSCAN(eps=25, min_samples=200).fit(X) core_samples_mask = np.zeros_like(db.labels_, dtype=bool) core_samples_mask[db.core_sample_indices_] = True labels = db.labels_ # Number of clusters in labels, ignoring noise if present. n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0) print('Estimated number of clusters: %d' % n_clusters_) unique_labels = set(labels) cropped_images = [] unique_labels.remove(-1) col=0 for k in unique_labels: #my_members = labels == k #members = X[my_members, 0] left = min(X[labels==k][:,0]) right = max(X[labels==k][:,0]) padding = 20 if left > padding: left = left - padding if right < len(img[1]) - padding: right = right + padding cropped_images.append(rgb[0:len(img), left:right]) # save each cropped image by its index number myzip = ZipFile('test.zip', 'w',zipfile.ZIP_DEFLATED) for c, cropped_image in enumerate(cropped_images): io.imsave(str(c) + ".png", cropped_image) myzip.write(str(c) + ".png") myzip.printdir() myzip.close() for c, cropped_image in enumerate(cropped_images): os.remove(str(c) + ".png") classes = [] with open('test.zip', 'rb') as img: param = {'classifier_ids':"foodtest_1606116153"} params = json.dumps(param) response = vr.classify(images_file=img, parameters=params) for image in response['images']: max_score = 0 max_class = "" for classifier in image['classifiers']: for classif in classifier['classes']: if (classif['score'] > max_score): max_class = classif['class'] if max_class: max_class = max_class.replace('_', ' ') if (max_class) not in classes: classes.append(max_class) os.remove('test.zip') return(classes)
from zipfile import ZipFile zip = ZipFile("d:/demo1.war", 'r') zip.printdir()
def flash_release(path=None, st_serial=None): from panda import Panda, PandaDFU, ESPROM, CesantaFlasher from zipfile import ZipFile def status(x): print("\033[1;32;40m" + x + "\033[00m") if st_serial == None: # look for Panda panda_list = Panda.list() if len(panda_list) == 0: raise Exception( "panda not found, make sure it's connected and your user can access it" ) elif len(panda_list) > 1: raise Exception("Please only connect one panda") st_serial = panda_list[0] print("Using panda with serial %s" % st_serial) if path == None: print( "Fetching latest firmware from github.com/commaai/panda-artifacts") r = requests.get( "https://raw.githubusercontent.com/commaai/panda-artifacts/master/latest.json" ) url = json.loads(r.text)['url'] r = requests.get(url) print("Fetching firmware from %s" % url) path = io.StringIO(r.content) zf = ZipFile(path) zf.printdir() version = zf.read("version") status("0. Preparing to flash " + version) code_bootstub = zf.read("bootstub.panda.bin") code_panda = zf.read("panda.bin") code_boot_15 = zf.read("boot_v1.5.bin") code_boot_15 = code_boot_15[0:2] + "\x00\x30" + code_boot_15[4:] code_user1 = zf.read("user1.bin") code_user2 = zf.read("user2.bin") # enter DFU mode status("1. Entering DFU mode") panda = Panda(st_serial) panda.enter_bootloader() time.sleep(1) # program bootstub status("2. Programming bootstub") dfu = PandaDFU(PandaDFU.st_serial_to_dfu_serial(st_serial)) dfu.program_bootstub(code_bootstub) time.sleep(1) # flash main code status("3. Flashing main code") panda = Panda(st_serial) panda.flash(code=code_panda) panda.close() # flashing ESP if panda.is_white(): status("4. Flashing ESP (slow!)") align = lambda x, sz=0x1000: x + "\xFF" * ((sz - len(x)) % sz) esp = ESPROM(st_serial) esp.connect() flasher = CesantaFlasher(esp, 230400) flasher.flash_write(0x0, align(code_boot_15), True) flasher.flash_write(0x1000, align(code_user1), True) flasher.flash_write(0x81000, align(code_user2), True) flasher.flash_write(0x3FE000, "\xFF" * 0x1000) flasher.boot_fw() del flasher del esp time.sleep(1) else: status("4. No ESP in non-white panda") # check for connection status("5. Verifying version") panda = Panda(st_serial) my_version = panda.get_version() print("dongle id: %s" % panda.get_serial()[0]) print(my_version, "should be", version) assert (str(version) == str(my_version)) # done! status("6. Success!")
''' ZipFile-创建zip压缩文件,并向其中添加指定的文件 zipfile.ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, compresslevel=None, *, strict_timestamps=True) 形参mode值: 'r' 来读取一个存在的文件 'w' 向压缩包中添加文件,删除压缩包内已有的文件,只有新文件 'a' 向压缩包中添加文件,不会删除压缩包内已有的文件,新老文件都存在 'x' 来仅新建并写入新的文件 ''' from zipfile import ZipFile #创建一个空的压缩文件,并将指定文件添加到压缩包 file_name = 'new.zip' myzip = ZipFile(file=file_name, mode='w') file_name = 'test.txt' myzip.write(filename=file_name) #将zip文档内的信息打印到控制台上。 myzip.printdir()
def flash_release(path=None, st_serial=None): from panda import Panda, PandaDFU, ESPROM, CesantaFlasher from zipfile import ZipFile def status(x): print("\033[1;32;40m"+x+"\033[00m") if st_serial == None: # look for Panda panda_list = Panda.list() if len(panda_list) == 0: raise Exception("panda not found, make sure it's connected and your user can access it") elif len(panda_list) > 1: raise Exception("Please only connect one panda") st_serial = panda_list[0] print("Using panda with serial %s" % st_serial) if path == None: print("Fetching latest firmware from github.com/commaai/panda-artifacts") r = requests.get("https://raw.githubusercontent.com/commaai/panda-artifacts/master/latest.json") url = json.loads(r.text)['url'] r = requests.get(url) print("Fetching firmware from %s" % url) path = StringIO.StringIO(r.content) zf = ZipFile(path) zf.printdir() version = zf.read("version") status("0. Preparing to flash "+version) code_bootstub = zf.read("bootstub.panda.bin") code_panda = zf.read("panda.bin") code_boot_15 = zf.read("boot_v1.5.bin") code_boot_15 = code_boot_15[0:2] + "\x00\x30" + code_boot_15[4:] code_user1 = zf.read("user1.bin") code_user2 = zf.read("user2.bin") # enter DFU mode status("1. Entering DFU mode") panda = Panda(st_serial) panda.enter_bootloader() time.sleep(1) # program bootstub status("2. Programming bootstub") dfu = PandaDFU(PandaDFU.st_serial_to_dfu_serial(st_serial)) dfu.program_bootstub(code_bootstub) time.sleep(1) # flash main code status("3. Flashing main code") panda = Panda(st_serial) panda.flash(code=code_panda) panda.close() # flashing ESP status("4. Flashing ESP (slow!)") align = lambda x, sz=0x1000: x+"\xFF"*((sz-len(x)) % sz) esp = ESPROM(st_serial) esp.connect() flasher = CesantaFlasher(esp, 230400) flasher.flash_write(0x0, align(code_boot_15), True) flasher.flash_write(0x1000, align(code_user1), True) flasher.flash_write(0x81000, align(code_user2), True) flasher.flash_write(0x3FE000, "\xFF"*0x1000) flasher.boot_fw() del flasher del esp time.sleep(1) # check for connection status("5. Verifying version") panda = Panda(st_serial) my_version = panda.get_version() print("dongle id: %s" % panda.get_serial()[0]) print(my_version, "should be", version) assert(str(version) == str(my_version)) # done! status("6. Success!")
def flash_release(path=None, st_serial=None): from panda import Panda, PandaDFU from zipfile import ZipFile def status(x): print("\033[1;32;40m" + x + "\033[00m") if st_serial is not None: # look for Panda panda_list = Panda.list() if len(panda_list) == 0: raise Exception( "panda not found, make sure it's connected and your user can access it" ) elif len(panda_list) > 1: raise Exception("Please only connect one panda") st_serial = panda_list[0] print("Using panda with serial %s" % st_serial) if path is None: print( "Fetching latest firmware from github.com/commaai/panda-artifacts") r = requests.get( "https://raw.githubusercontent.com/commaai/panda-artifacts/master/latest.json" ) url = json.loads(r.text)['url'] r = requests.get(url) print("Fetching firmware from %s" % url) path = io.BytesIO(r.content) zf = ZipFile(path) zf.printdir() version = zf.read("version").decode() status("0. Preparing to flash " + str(version)) code_bootstub = zf.read("bootstub.panda.bin") code_panda = zf.read("panda.bin") # enter DFU mode status("1. Entering DFU mode") panda = Panda(st_serial) panda.reset(enter_bootstub=True) panda.reset(enter_bootloader=True) time.sleep(1) # program bootstub status("2. Programming bootstub") dfu = PandaDFU(PandaDFU.st_serial_to_dfu_serial(st_serial)) dfu.program_bootstub(code_bootstub) time.sleep(1) # flash main code status("3. Flashing main code") panda = Panda(st_serial) panda.flash(code=code_panda) panda.close() # check for connection status("4. Verifying version") panda = Panda(st_serial) my_version = panda.get_version() print("dongle id: %s" % panda.get_serial()[0]) print(my_version, "should be", version) assert (str(version) == str(my_version)) # done! status("6. Success!")
def analyse_zip_archive(zip_archive: zipfile.ZipFile): is_encrypted = __archive_is_encrypted(zip_archive=zip_archive) print('encrypted:'.ljust(20), is_encrypted) print() zip_archive.printdir()