为什么使用unix-compress和go compress / lzw会生成其他解码器无法读取的不同文件?

问题描述:

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.

我在终端中使用 compress file.txt code>压缩了文件并得到了(如预期的那样) ) file.txt.Z code> p>

当我将该文件传递给Go中的 ioutil.ReadFile code>时, p> \ n

  buf0,err:= ioutil.ReadFile(“ file.txt.Z”)
  code>  pre> 
 
 

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

  finder_test.go:116:lzw:无效代码
  code>  pre> 
 
 

我发现Go会接受 如果我使用 compress / lzw code>包压缩文件,则我只是使用了执行此操作的网站。 我只修改了这一行 p>

  outputFile,err:= os.Create(“ file.txt.lzw”)
  code>  pre> 
 
  

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

注意: file。 txt code>是16.0 kB,unix压缩的 file.txt.Z code>是7.8 kB,go压缩的 file.txt.Z code>是8.2 kB p >

现在,我正试图了解发生这种情况的原因。 因此,我尝试运行 p>

  uncompress.real file.txt.Z 
  code>  pre> 
 
 

,但此方法不起作用 。 我得到了 p>

  file.txt.Z:不是压缩格式
  code>  pre> 
 
 

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

有人可以解释为什么会这样吗? 为什么两个.Z文件不相等? 我该如何克服呢? p>

注意:我正在Mac上安装在VirtualBox中的Ubuntu上。 p> div>

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.

Presumably you only want to test the performance of two of your/some third party algorithms (& not the compression algorithms themselves), you may want to write a shell script which calls the compress command passing the files/dir's required and then call this script from your C / GO program. This is one way you can overcome this, but leaves open other parts of your queries on the correct way to use the compression libraries.