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

huangapple go评论92阅读模式
英文:

How to remove the gzip header metada from decompressed file

问题

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

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())
}

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

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

无论原始文件是使用gzip data.json还是tar -czf data.tar.gz data.json创建的,我都会得到这个头部。我该如何删除写入输出文件的前几个字节?

英文:

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

得分: 0

你将你的压缩文件生成为一个归档文件。压缩和创建压缩归档文件的区别在于,归档文件是一种文件格式,可以包含多个文件或复杂结构(例如带有文件夹的文件结构)。

tar -cz <输入文件> 创建一个归档文件,并使用gzip进行压缩,这样你可以在一个压缩的tar归档文件中包含多个文件。

在典型的UNIX/Linux环境中,要压缩一个文件,请使用gzip命令:

$ gzip foo.json

这将为你创建一个名为foo.json.gz的文件。要访问其内容,请使用gunzipzcat

$ zcat foo.json.gz
<foo.json的内容>

$ gunzip foo.json.gz
$ cat foo.json
英文:

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 &lt;input files&gt; 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
&lt;contents of foo.json&gt;

$ gunzip foo.json.gz
$ cat foo.json

huangapple
  • 本文由 发表于 2015年10月29日 23:17:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/33418110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定