packagemainimport("bytes""encoding/hex""fmt""io/ioutil""log""os""github.com/golang/snappy")funcmain(){// Read and decode the decompressed file// 读取并解码解压缩的文件plainhex,err:=ioutil.ReadFile(os.Args[1])iferr!=nil{log.Fatalf("Failed to read decompressed file %s: %v",os.Args[1],err)}plain,err:=hex.DecodeString(string(plainhex))iferr!=nil{log.Fatalf("Failed to decode decompressed file: %v",err)}// Read and decode the compressed file// 读取并解码压缩的文件comphex,err:=ioutil.ReadFile(os.Args[2])iferr!=nil{log.Fatalf("Failed to read compressed file %s: %v",os.Args[2],err)}comp,err:=hex.DecodeString(string(comphex))iferr!=nil{log.Fatalf("Failed to decode compressed file: %v",err)}// Make sure they match// 确保它们匹配decomp,err:=snappy.Decode(nil,comp)iferr!=nil{log.Fatalf("Failed to decompress compressed file: %v",err)}if!bytes.Equal(plain,decomp){fmt.Println("Booo, decompressed file does not match provided plain text!")return}fmt.Println("Yay, decompressed data matched provided plain text!")}
$ go run main.go block.rlp block.go.snappy
Yay, decompressed data matched provided plain text!
$ go run main.go block.rlp block.py.snappy
Yay, decompressed data matched provided plain text!
Python
$ pip install python-snappy
importsnappyimportsys# Read and decode the decompressed file
# 读取并解码解压缩的文件
withopen(sys.argv[1],'rb')asfile:plainhex=file.read()plain=plainhex.decode("hex")# Read and decode the compressed file
# 读取并解码压缩的文件
withopen(sys.argv[2],'rb')asfile:comphex=file.read()comp=comphex.decode("hex")# Make sure they match
# 确保它们匹配
decomp=snappy.uncompress(comp)ifplain!=decomp:print"Booo, decompressed file does not match provided plain text!"else:print"Yay, decompressed data matched provided plain text!"