dongzhuo1930 2017-03-19 17:22
浏览 101
已采纳

为什么使用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.

  • 写回答

2条回答 默认 最新

  • douchi5822 2017-03-19 17:41
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler