dongxiangxie8181 2015-10-29 15:17
浏览 175
已采纳

如何从解压缩的文件中删除gzip标头元数据

I've a gzip file that I try to decompress and save the result as follows:

bytesReader := bytes.NewReader(gzipData)
gzipReader, err := gzip.NewReader(bytesReader)
defer gzipReader.Close()
if err == nil {
    u1 := uuid.NewV4()
    filename := u1.String() + ".json"
    file, _ := os.Create(filename)
    defer file.Close()
    fileWriter := bufio.NewWriter(file)
    io.Copy(fileWriter, gzipReader)
    fileWriter.Flush()
} else {
    log.Println(err.Error())
}

When I check the resulting json file, I see that it starts with some metadata as follows:

$ head -n 1 caf12e7b-e5e5-4453-ac0f-4d1d02770632.json
data.json000644 000765 000024 00001562330 12614372206 013272 0ustar00elsoufystaff000000 000000 {... json content ...}

I'm getting this header whether the original file was created with gzip data.json or tar -czf data.tar.gz data.json. How I can remove the few first bytes from beeing writing to the output file?

  • 写回答

1条回答 默认 最新

  • dozrhldy285751 2015-10-29 22:34
    关注

    You generated your compressed file as an archive. The difference between compressing something and creating a compressed archive is, that an archive is a file format to contain more than one file, or complex structure (such as a file structure with folders).

    tar -cz <input files> creates an archive and compresses that using gzip, so you can have more than one file in a compressed tar archive.

    To compress a file in a typical UNIX/Linux environment, use the gzip command:

    $ gzip foo.json
    

    This will create a file foo.json.gz for you. To access its contents, use gunzip or zcat:

    $ zcat foo.json.gz
    <contents of foo.json>
    
    $ gunzip foo.json.gz
    $ cat foo.json
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据