dragon202076 2018-11-29 22:41
浏览 152
已采纳

如何将zip文件从字节数组写入磁盘

I'm downloading a zip file from S3 in Go like so:

buff := &aws.WriteAtBuffer{}
downloader := s3manager.NewDownloader(session.New(config))
_, err := downloader.Download(buff, &input)
if err != nil {
    log.Println(err)
    return err
}

data := buff.Bytes()

I send 'data' to a client that's written in Python3, and need to convert this byte array back to a zip file and put it in a specified directory. I've tried this:

file_bytes = msg_obj["Params"]

try:
    zf = zipfile.ZipFile(file_bytes, "r")
    for fileinfo in zf.infolist():
        print(zf.read(fileinfo).decode('ascii'))
except: print("Err:", sys.exc_info()[0])

But I get this error:

OSError: [Errno 36] File name too long"

I just want to 'rebuild' the zip file and save it to a directory.

  • 写回答

2条回答 默认 最新

  • drjtua5953 2018-12-03 23:13
    关注

    I figured out how to do this. In go, you'll want to base64 encode it (as a string).

    buff := &aws.WriteAtBuffer{}
    downloader := s3manager.NewDownloader(session.New(config))
    _, err := downloader.Download(buff, &input)
    if err != nil {
        log.Println(err)
        return err
    }
    
    data := b64.StdEncoding.EncodeToString(buff.Bytes())
    

    Then in python it's as simple as this (where 'file_bytes' is the base64 encoded string):

    d = base64.b64decode(file_bytes)
    f = open('home/update_file', 'wb')
    f.write(d)
    f.close()
    

    And bam, you have a reassembled zip file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名