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

如何从解压缩的文件中删除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?

图片转代码服务由CSDN问答提供 功能建议

我有一个gzip文件,我尝试将其解压缩并保存结果,如下所示:

  bytesReader:= bytes.NewReader(gzipData)
gzipReader,err:= gzip.NewReader(bytesReader)
defer gzipReader.Close()
if err == nil {
 u1:= uuid。  NewV4()
文件名:= u1.String()+“ .json” 
文件,_:= os.Create(文件名)
延迟文件.Close()
 fileWriter:= bufio.NewWriter(file)  
 io.Copy(fileWriter,gzipReader)
 fileWriter.Flush()
}其他{
 log.Println(err.Error())
} 
   
 \  n 

当我检查生成的 json 文件时,我看到它以一些元数据开头,如下所示:

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

I' 无论原始文件是使用 gzip data.json 还是 tar -czf data.tar创建的,都会获取此标头。 gz data.json 。 我该如何从写输出文件的过程中删除前几个字节?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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
    
    点赞 打赏 评论

相关推荐