I
I
IgnoreGTO2021-03-20 09:37:44
PyQt
IgnoreGTO, 2021-03-20 09:37:44

How to unpack downloaded archive into python + pyqt5 GUI application?

Please tell me how to automatically unzip the archive after downloading and hide the download bar

import shutil
import subprocess
import sys
import zipfile
from multiprocessing import process

from PyQt5.QtWidgets import QApplication, QProgressBar, QWidget, QLineEdit, QPushButton, \
    QVBoxLayout, QHBoxLayout
from PyQt5.QtCore import QThread, pyqtSignal
import urllib.request

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *


class Downloader(QThread):
    # Сигнал о количестве данных (PyQt5)
    preprogress = pyqtSignal(float)
    progress = pyqtSignal(float)

    # fileUrl - url - файла, включая сам файл
    # filename - имя файла
    def __init__(self, fileUrl, fileName):
        QThread.__init__(self)
        # Флаг инициализации
        self._init = False
        self.fileUrl = fileUrl
        self.fileName = fileName

    def run(self):
        # тест на локальных данных, но работать должно и с сетью
        urllib.request.urlretrieve(self.fileUrl, self.fileName, self._progress)

    def _progress(self, block_num, block_size, total_size):
        if not self._init:
            self.preprogress.emit(total_size)
            self._init = True

        # Расчет текущего количества данных
        downloaded = block_num * block_size
        if downloaded < total_size:
            # Отправляем промежуток
            self.progress.emit(downloaded)
        else:
            # Чтобы было 100%
            self.progress.emit(total_size)


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(100, 100, 600, 300)
        self.setWindowTitle('GTO Games')

        self.downloader = None

        self.fileUrl = QLineEdit('http://site.com/dw/Mods.zip')
        self.buttonga = QPushButton('Играть', self)
        self.loadButton = QPushButton('Скачать', self)
        self.loadButton.clicked.connect(self._loadFile)
        hbox = QHBoxLayout()

            # hbox.addWidget(self.fileUrl)
        hbox.addWidget(self.loadButton)

        vbox = QVBoxLayout(self)
        vbox.addLayout(hbox)
        self.bar = QProgressBar()
        vbox.addWidget(self.bar)

    def initializeUI(self):
        self.setGeometry(100, 100, 600, 200)
        self.setWindowTitle('GTO Games')

        self.buttonga.clicked.connect(self.exerun)
        self.buttonga.move(10, 10)

        self.show()

    def _loadFile(self):
        ar = self.fileUrl.text().split('/')
        if len(ar) == 0:
            return
        fileName = f'_{ar[len(ar) - 1]}'

        self._download = Downloader(self.fileUrl.text(), fileName)
        # Устанавливаем максимальный размер данных
        self._download.preprogress.connect(lambda x: self.bar.setMaximum(round(x)))
        # Промежуточный/скачанный размер
        self._download.progress.connect(lambda d: self.bar.setValue(round(d)))
        self._download.start()
        print("D_Start")
        self.loadButton.hide()

    def exerun(self):
        args = [r".\Vintagestory.exe", "--connect=xx.xxx.xxx.xx", "--pw=9029"]
        subprocess.call(args)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexa2007, 2021-03-20
@IgnorGTO

def extract_zip(zip_file_,extract_folder):
    '''
    extract all files to directory
    extract_zip('zip_2MB.zip','C://test')
    '''
    zf = zipfile.ZipFile(zip_file_, 'r')
    for _ in zf.namelist():
        zf.extractall(extract_folder)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question