{"id":220,"date":"2018-09-06T15:03:57","date_gmt":"2018-09-06T07:03:57","guid":{"rendered":"http:\/\/www.yinyubo.cn\/?p=220"},"modified":"2022-05-17T09:50:38","modified_gmt":"2022-05-17T01:50:38","slug":"wxpythonzip","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=220","title":{"rendered":"wxpython-\u901a\u8fc7request\u8fdc\u7a0b\u4e0b\u8f7d\u7f51\u7edczip\u6587\u4ef6\uff0c\u5e76\u89e3\u538b\u5b89\u88c5\u6587\u4ef6"},"content":{"rendered":"<ul>\n<li>\n<h1><strong>\u660e\u786e\u9700\u6c42\uff08\u5347\u7ea7\u7a0b\u5e8f\uff09<\/strong><\/h1>\n<\/li>\n<\/ul>\n<p>1.\u901a\u8fc7wxpython\uff0c\u4ea7\u751f\u4e00\u4e2a\u7a97\u4f53\uff0c\u7a97\u4f53\u4e0a\u6709\u4e00\u6bb5[\u6587\u5b57\u6807\u7b7e]\uff0c\u4e00\u4e2a[\u8fdb\u5ea6\u6761]\uff0c\u4e00\u4e2a[\u5f00\u59cb\u6309\u94ae]\u3002<br \/>\n2.\u70b9\u51fb\u3010\u5f00\u59cb\u6309\u94ae\u3011\uff0c\u4e0b\u8f7d\u7f51\u7edc\u8d44\u6e90\u6587\u4ef6http:\/\/example.cn\/test.zip\u3002\u8fdb\u5ea6\u6761\u548c\u6587\u5b57\u6807\u7b7e\u540c\u65f6\u663e\u793a\u767e\u5206\u6bd4<br \/>\n3.\u4e0b\u8f7d\u5b8c\u6210\u540e\uff0c\u89e3\u538b\u5230\u6307\u5b9a\u76ee\u5f55\u3002\u5982\u679c\u6307\u5b9a\u76ee\u5f55\u4e0b\u6709\u6587\u4ef6\uff0c\u5219\u8986\u76d6\u6389\u3002<\/p>\n<ul>\n<li>\n<h1><strong>\u8bbe\u8ba1\u754c\u9762<\/strong><\/h1>\n<\/li>\n<\/ul>\n<h1><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-222 size-full\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/Image-16.png\" alt=\"\" width=\"461\" height=\"132\" \/><\/h1>\n<ul>\n<li>\n<h1><strong>\u7a0b\u5e8f\u5173\u952e\u70b9<\/strong><\/h1>\n<\/li>\n<\/ul>\n<p>1.wxpython\u7528\u7684\u8fdb\u5ea6\u6761\u63a7\u4ef6\u662fwx.guage.\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre class=\"\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">self.gauge_1 = wx.Gauge(self, wx.ID_ANY, 100, style=wx.GA_HORIZONTAL | wx.GA_SMOOTH)<\/code><\/pre>\n<p>\u8bbe\u7f6e\u8fdb\u5ea6\u6761\u7684\u65b9\u6cd5\u5982\u4e0b\uff1amsg\u586b\u7684\u662f\u6570\u5b57<\/p>\n<pre class=\"\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">self.download_gauge.SetValue(msg)<\/code><\/pre>\n<p>2.\u56e0\u4e3a\u4e0b\u8f7d\u65f6\u95f4\u957f\uff0c\u6240\u4ee5\u9700\u8981\u5728\u4e3b\u7ebf\u7a0b\u5916\u518d\u542f\u7528\u4e00\u4e2a\u7ebf\u7a0b\u4e0b\u8f7d\uff0c\u907f\u514d\u7a0b\u5e8f\u5047\u6b7b\u3002<br \/>\n3.\u901a\u8fc7request.Session().get\u65b9\u6cd5\u4e0b\u8f7d\u6bd4request.get\u4e0b\u8f7d\u8981\u5feb<\/p>\n<pre class=\"\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">requests.Session().get(url, verify=False, stream=True)<\/code><\/pre>\n<p>4.\u901a\u8fc7stream\u7684\u5f62\u5f0f\u53ef\u4ee5\u83b7\u5f97\u4e0b\u8f7d\u8fdb\u5ea6\u3002\u5982\u4e0b\uff0cmessage\u662f\u4e0b\u8f7d\u8fdb\u5ea6\uff0c\u4f8b\u598210%\uff0cmessage\u4e3a10<\/p>\n<pre class=\"\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">for chunk in response.iter_content(chunk_size=512):\n    if chunk:\n        code.write(chunk)\n        code.flush()\n    i = i + 512\n    number = int(i)\n    message = number * 100 \/ total_length<\/code><\/pre>\n<p>5.Zipfile\u89e3\u538b\u6587\u4ef6\uff0c\u5982\u679c\u89e3\u538b\u76ee\u5f55\u4e0b\u6709\u540c\u540d\u6587\u4ef6\uff0c\u5219\u4f1a\u76f4\u63a5\u8986\u76d6\u6389<\/p>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">azip = zipfile.ZipFile(zip_file_path)\nazip.extractall(path=unzip_to_path)<\/code><\/pre>\n<ul>\n<li>\n<h1><strong>\u5b8c\u6574\u4ee3\u7801(frame\u7a97\u4f53\u548cevent\u4e8b\u4ef6)<\/strong><\/h1>\n<\/li>\n<\/ul>\n<p><strong>updateOTAFrame.py<\/strong>\uff08\u7a97\u4f53\u6587\u4ef6\uff0c\u7528wxglade\u521b\u5efa\uff09<\/p>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">#!\/usr\/bin\/env python\n# -*- coding: UTF-8 -*-\n#\n# generated by wxGlade 0.9.0pre on Thu Sep 06 09:41:05 2018\n#\nimport wx\n# begin wxGlade: dependencies\n# end wxGlade\n# begin wxGlade: extracode\n# end wxGlade\nclass MyFrame(wx.Frame):\n    def __init__(self, *args, **kwds):\n        # begin wxGlade: MyFrame.__init__\n        kwds[\"style\"] = kwds.get(\"style\", 0) | wx.DEFAULT_FRAME_STYLE\n        wx.Frame.__init__(self, *args, **kwds)\n        self.SetSize((475, 137))\n        self.label_1 = wx.StaticText(self, wx.ID_ANY, \"start\")\n        self.gauge_1 = wx.Gauge(self, wx.ID_ANY, 100, style=wx.GA_HORIZONTAL | wx.GA_SMOOTH)\n        self.button_1 = wx.Button(self, wx.ID_ANY, \"start\")\n        self.__set_properties()\n        self.__do_layout()\n        # end wxGlade\n    def __set_properties(self):\n        # begin wxGlade: MyFrame.__set_properties\n        self.SetTitle(u\"\\u5347\\u7ea7\\u7a0b\\u5e8f\")\n        # end wxGlade\n    def __do_layout(self):\n        # begin wxGlade: MyFrame.__do_layout\n        sizer_1 = wx.BoxSizer(wx.VERTICAL)\n        sizer_2 = wx.BoxSizer(wx.VERTICAL)\n        sizer_2.Add(self.label_1, 1, 0, 0)\n        sizer_2.Add(self.gauge_1, 1, wx.EXPAND, 0)\n        sizer_2.Add(self.button_1, 1, wx.ALIGN_CENTER, 0)\n        sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)\n        self.SetSizer(sizer_1)\n        self.Layout()\n        # end wxGlade\n# end of class MyFrame\n<\/code><\/pre>\n<p><strong>updateOTAevent.py<\/strong>(\u4e8b\u4ef6\u6587\u4ef6\uff0c\u548c\u7a97\u4f53\u6587\u4ef6\u5206\u5f00\uff09<\/p>\n<pre class=\"lang:python decode:true EnlighterJSRAW\" data-enlighter-language=\"golang\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\"># -*- coding: UTF-8 -*-\nimport os\nimport zipfile\nfrom threading import Thread\nimport requests\nimport wx\nfrom wx.lib.pubsub import pub\nfrom view.window import updateOTAFrame\nclass MyApp(wx.App):\n    def OnInit(self):\n        self.frame = updateOTAFrame.MyFrame(None, wx.ID_ANY, \"\")\n        self.frame.CenterOnScreen()\n        self.download_gauge = self.frame.gauge_1\n        self.start_button = self.frame.button_1\n        self.frame.timer = wx.Timer(self.frame)  # \u521b\u5efa\u5b9a\u65f6\u5668\n        # \u7ed1\u5b9a\u4e00\u4e2a\u5b9a\u65f6\u5668\u4e8b\u4ef6\uff0cwxpython\u5b58\u5728bug,\u4e0d\u8bbe\u5b9a\u5b9a\u65f6\u5668\uff0cpub\u529f\u80fd\u4e0d\u4f1a\u6b63\u5e38\u542f\u7528\n        self.frame.Bind(wx.EVT_TIMER, self.on_timer, self.frame.timer)\n        self.frame.Show()\n        self.start_button.Bind(wx.EVT_BUTTON, self.start_event)\n        pub.subscribe(self.update_ota, \"ota_topic\")\n        pub.subscribe(self.download_text_topic, \"download_text_topic\")\n        self.download_text = self.frame.label_1\n        self.download_text.SetLabelText(\"\u70b9\u51fb\u5f00\u59cb\u5347\u7ea7\u6309\u94ae\uff0c\u5373\u523b\u5f00\u59cb\u5347\u7ea7\")\n        self.start_button.SetLabelText(\"\u5f00\u59cb\u5347\u7ea7\")\n        pub.subscribe(self.close_frame, \"close_download_topic\")\n        return True\n    # \u4e0b\u8f7d\u5b8c\u6210\u540e\uff0c\u5173\u95ed\u7a97\u53e3\n    def close_frame(self, msg):\n        self.frame.Destroy()\n    # \u5f00\u59cb\u4e0b\u8f7d\u6309\u94ae\u4e8b\u4ef6\n    def start_event(self, event):\n        self.start_button.SetLabelText(\"\u6b63\u5728\u5347\u7ea7\")\n        self.download_text.SetLabelText(\"\u6b63\u5728\u4e0b\u8f7d\u5347\u7ea7\u5305\uff0c\u8bf7\u4e0d\u8981\u5173\u95ed\u7a0b\u5e8f,\u76ee\u524d\u8fdb\u5ea6\uff1a0%\")\n        self.start_button.Disable()\n        GuageThread()\n        event.Skip()\n    # \u63a7\u5236\u4e0b\u8f7d\u7684\u65f6\u5019\u7684\u6587\u5b57\n    def download_text_topic(self, msg):\n        if msg &lt; 100:\n            self.download_text.SetLabelText(\"\u6b63\u5728\u4e0b\u8f7d\u5347\u7ea7\u5305\uff0c\u8bf7\u4e0d\u8981\u5173\u95ed\u7a0b\u5e8f,\u76ee\u524d\u8fdb\u5ea6\uff1a\" + str(msg) + \"%\")\n        else:\n            self.download_text.SetLabelText('\u4e0b\u8f7d\u6210\u529f\uff0c\u73b0\u5728\u5f00\u59cb\u89e3\u538b\uff0c\u8bf7\u8010\u5fc3\u7b49\u5f85\u5927\u4e8e10\u79d2')\n            self.start_button.SetLabelText('\u6b63\u5728\u89e3\u538b')\n    # \u63a7\u5236\u4e0b\u8f7d\u7684\u8fdb\u5ea6\u6761\n    def update_ota(self, msg):\n        if msg &lt; 100:  # \u5982\u679c\u662f\u6570\u5b57\uff0c\u8bf4\u660e\u7ebf\u7a0b\u6b63\u5728\u6267\u884c\uff0c\u663e\u793a\u6570\u5b57\n            self.download_gauge.SetValue(msg)\n        else:\n            self.download_gauge.SetValue(msg)\n    def on_timer(self, evt):  # \u5b9a\u65f6\u6267\u884c\u68c0\u67e5\u7f51\u7edc\u72b6\u6001\n        pass\n# \u53e6\u5916\u542f\u52a8\u4e00\u4e2a\u7ebf\u7a0b\u6765\u63a7\u5236\u8fdb\u5ea6\u6761\uff0c\u4e0d\u7136\u7a0b\u5e8f\u4f1a\u5047\u6b7b\nclass GuageThread(Thread):\n    def __init__(self):\n        # \u7ebf\u7a0b\u5b9e\u4f8b\u5316\u65f6\u7acb\u5373\u542f\u52a8\n        Thread.__init__(self)\n        self.start()\n    def run(self):\n        # \u7ebf\u7a0b\u6267\u884c\u7684\u4ee3\u7801\n        self.download_file()\n    def download_file(self):\n        # url = \"http:\/\/example.cn\/test.zip\" \u662f\u7f51\u7edc\u4e0a\u7684zip\u538b\u7f29\u5305\u6587\u4ef6\n        url = \"http:\/\/example.cn\/test.zip\"\n        # \u901a\u8fc7Session\u6765\u4e0b\u8f7d\uff0c\u901f\u5ea6\u6bd4\u76f4\u63a5requests.get\u5feb\u4e86\u5927\u7ea6\u767e\u5206\u4e4b30\n        response = requests.Session().get(url, verify=False, stream=True)\n        total_length = int(response.headers.get(\"Content-Length\"))\n        with open(os.path.abspath(os.getcwd() + \"\/resource\/download\/new.zip\"), \"wb\") as code:\n            i = 0\n            temp = 0\n            # \u7528chunk_size\u7684\u65b9\u6cd5\u6765\u4e0b\u8f7d\uff0c\u53ef\u4ee5\u77e5\u9053\u5f53\u524d\u7684\u4e0b\u8f7d\u8fdb\u5ea6\u3002chunk_size\u5f71\u54cd\u6bcf\u6b21\u5199\u5165\u7684\u5185\u5b58\u5927\u5c0f\n            for chunk in response.iter_content(chunk_size=512):\n                if chunk:\n                    code.write(chunk)\n                    code.flush()\n                i = i + 512\n                number = int(i)\n                # \u56e0\u4e3a\u8fdb\u5ea6\u6761\u7684\u957f\u5ea6\u8bbe\u7f6e\u6210\u4e86100\uff0c\u6240\u4ee5\u8fd9\u91cc\u8981\u4e58\u4ee5100\n                message = number * 100 \/ total_length\n                wx.CallAfter(pub.sendMessage, \"ota_topic\", msg=message)\n                if temp != message:\n                    temp = message\n                    wx.CallAfter(pub.sendMessage, \"download_text_topic\", msg=message)\n        filepath = os.path.abspath(os.getcwd() + \"\/resource\/download\/new.zip\")\n        # \u76f4\u63a5\u653e\u5728\u7a0b\u5e8f\u6839\u76ee\u5f55\u4e0b\u4e86\n        foldpath = os.path.abspath(os.getcwd())\n        self.unzip(filepath, foldpath)\n        wx.CallAfter(pub.sendMessage, \"close_download_topic\", msg=1)\n    # \u89e3\u538b\u6587\u4ef6\u7528\u7684zipfile\uff0c\u5c06\u89e3\u538b\u7684\u6587\u4ef6\uff0c\u653e\u7f6e\u5230\u6307\u5b9a\u8def\u5f84\u4e0b\uff08\u8986\u76d6\u590d\u5236\uff09\n    def unzip(self, zip_file_path, unzip_to_path):\n        unzip_flag = False\n        try:\n            check_zip_flag = zipfile.is_zipfile(zip_file_path)\n            if check_zip_flag:\n                azip = zipfile.ZipFile(zip_file_path)\n                azip.extractall(path=unzip_to_path)\n                unzip_flag = True\n        except Exception as e:\n            print e.message\n        finally:\n            print \"unzip_flag==========\", unzip_flag\n            return unzip_flag\n# end of class MyApp\nif __name__ == \"__main__\":\n    app = MyApp(0)\n    app.MainLoop()<\/code><\/pre>\n<p>\u6ce8\uff1a\u00a0 \u628a url = &#8220;http:\/\/example.cn\/test.zip&#8221; \u4fee\u6539\u4e3a\u81ea\u5df1\u8981\u4e0b\u8f7d\u7684\u7f51\u7edczip\u538b\u7f29\u5305\u6587\u4ef6\u3002\u7136\u540e\u5728updateOTAevent.py\u4e2d\u8fd0\u884c\u5373\u53ef\u3002zip\u6587\u4ef6\u4f1a\u4e0b\u8f7d\u5b8c\u6210\u540e\u89e3\u538b\u81f3\u7a0b\u5e8f\u6839\u76ee\u5f55<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u660e\u786e\u9700\u6c42\uff08\u5347\u7ea7\u7a0b\u5e8f\uff09 1.\u901a\u8fc7wxpython\uff0c\u4ea7\u751f\u4e00\u4e2a\u7a97\u4f53\uff0c\u7a97\u4f53\u4e0a\u6709\u4e00\u6bb5[\u6587\u5b57\u6807\u7b7e]\uff0c\u4e00\u4e2a[\u8fdb\u5ea6\u6761]\uff0c\u4e00\u4e2a[ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-220","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=220"}],"version-history":[{"count":1,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/220\/revisions"}],"predecessor-version":[{"id":951,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/220\/revisions\/951"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}