当前位置:首页 > 项目 > Wpython > 正文内容

micropython 实现tar 文件解包

Watrt3年前 (2023-05-31)Wpython14610
import upip_utarfile as tar
import os

def run(src,dest_dir):
  t = tar.TarFile(src)
  if not dest_dir in os.listdir():
    os.mkdir(dest_dir)
  dest_dir=dest_dir+"/"
  prefix=dest_dir
  for i in t:
    i.name=i.name.rstrip('/')
    print(i,i.name)
    i.name=prefix+i.name
    print(i.name)
    if i.type == tar.DIRTYPE:
      try:
        os.mkdir(i.name)
      except OSError as e:
        if e.args[0] != errno.EEXIST and e.args[0] != errno.EISDIR:
          raise e
     
    else:
      f = t.extractfile(i)
      print(i.name)
      copyfileobj(f, open(i.name, "wb+"))

def rmtree(top):
    for path, dirs, files in os.walk(top, False):
        for f in files:
            os.unlink(path + "/" + f)
        os.rmdir(path)

def copyfileobj(src, dest, length=512):
    if hasattr(src, "readinto"):
        buf = bytearray(length)
        while True:
            sz = src.readinto(buf)
            if not sz:
                break
            if sz == length:
                dest.write(buf)
            else:
                b = memoryview(buf)[:sz]
                dest.write(b)
    else:
        while True:
            buf = src.read(length)
            if not buf:
                break
            dest.write(buf)

run("/2.tar","/tt")


run("/2.tar","/tt")  第一参数为解包文件,第二个参数为解包目录

打赏 支付宝打赏 微信打赏
分享给朋友:

相关文章

micrpython编译javascript版

micrpython编译javascript版

1、首先安装emsdkgit clone https://github.com/emscripten-core/emsdk.git cd <path-to-emsdk> git pull ./emsdk install latest ./emsdk activate latest source ./emsdk_env.sh2、获取micropython源码git clone&...

LVGL_micropython 模拟器

LVGL_micropython 模拟器

在使用micropython编写lvgl界面时有时间非常的麻烦。要在设备上才看得到效果。比较麻烦。npx的GUI-Guider软件中的模拟器给抠出来了单独使用,非常不错。下面是运行实例:import SDL import utime as time import usys as sys import lvgl as lv import lodepng as p...

micropython 连接到mqtt

micropython 连接到mqtt

from umqtt.simple import MQTTClient from machine import Pin import ujson import urequests import network import time SERVER = "www.028sd.net" CLIENT_ID = "e...

micropython文本编辑器 界面

micropython文本编辑器 界面

##### startup script ##### #!/opt/bin/lv_micropython -i import lvgl as lv import display_driver ##### main script ##### import fs_driver fs_drv = lv.fs_drv_t() f...

micropython 日历页

micropython 日历页

# Initialize  import display_driver import lvgl as lv # Create a button with a label  # Copyright 2022 NXP # SPDX-License-Identifier: MIT # The ...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。