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条)

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启