英文:
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
的文件。要访问其内容,请使用gunzip
或zcat
:
$ 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 <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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论