为什么使用unix-compress并使用compress / lzw生成不同的文件,而其他解码器不可读? [英] Why using unix-compress and go compress/lzw produce different files, not readable by the other decoder?

查看:197
本文介绍了为什么使用unix-compress并使用compress / lzw生成不同的文件,而其他解码器不可读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用压缩file.txt 来压缩终端中的文件,并得到(如预期) file.txt.Z



当我在Go中将该文件传递给 ioutil.ReadFile 时,

  buf0,err:= ioutil.ReadFile(file.txt.Z)

我得到错误(上面的行是116):

$ $ $ $ $ $ c $ finder_test。 go:116:lzw:无效代码

我发现Go会接受文件 compress / lzw 包,我刚刚使用一个网站。我只修改了行

  outputFile,err:= os.Create(file.txt.lzw)

我将 .lzw 更改为 .Z 。然后在上面的Go代码中使用生成的 file.txt.Z ,它工作正常,没有错误。



注意: file.txt 是16.0 kB,unix-compressed file.txt.Z 是7.8 kB,并去压缩 file.txt.Z 是8.2 kB



现在,我试图理解为什么会发生这种情况。所以,我试图运行

  uncompress.real file.txt.Z 

并且它不起作用。我得到了

  file.txt.Z:不是压缩格式

我需要使用压缩器(最好是 unix-compress )来压缩文件,使用 lzw-compression 然后在两个不同的算法上使用相同的压缩文件,一个用C写,另一个用Go,因为我打算比较两种算法的性能。 C 程序将只接受使用 unix-compress 压缩的文件,Go程序将只接受压缩的文件Go的 compress / lzw



有人可以解释为什么会发生?为什么这两个.Z文件不相同?我该如何克服这一点?



注意:我正在使用安装在Mac上的VirtualBox上的Ubuntu。

解决方案

.Z文件不仅包含LZW压缩数据,还有一个3字节的头部,Go LZW代码不会生成,因为它意味着压缩数据,不会生成Z文件。


I compressed a file in a terminal with compress file.txt and got (as expected) file.txt.Z

When I pass that file to ioutil.ReadFile in Go,

buf0, err := ioutil.ReadFile("file.txt.Z")

I get the error (the line above is 116):

finder_test.go:116: lzw: invalid code

I found that Go would accept the file if I compress it using the compress/lzw package, I just used code from a website that does that. I only modified the line

outputFile, err := os.Create("file.txt.lzw")

I changed the .lzw to .Z. then used the resulting file.txt.Z in the Go code at the top, and it worked fine, no error.

Note: file.txt is 16.0 kB, unix-compressed file.txt.Z is 7.8 kB, and go-compressed file.txt.Z is 8.2 kB

Now, I was trying to understand why this happened. So, I tried to run

uncompress.real file.txt.Z

and it did not work. I got

file.txt.Z: not in compressed format

I need to use a compressor (preferably unix-compress) to compress files using lzw-compression then use the same compressed files on two different algorithms, one written in C and the other in Go, because I intend to compare the performance of the two algorithms. The C program will only accept the files compressed with unix-compress and the Go program will only accept the files compressed with Go's compress/lzw.

Can someone explain why that happened? Why are the two .Z files not equivalent? How can I overcome this?

Note: I am working on Ubuntu installed in VirtualBox on a Mac.

解决方案

A .Z file does not only contain LZW compressed data, there is also a 3-bytes header that the Go LZW code does not generate because it is meant to compress data, not generate a Z file.

这篇关于为什么使用unix-compress并使用compress / lzw生成不同的文件,而其他解码器不可读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆