1 回答

TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個(gè)贊
JAR 文件是zip 檔案,而不僅僅是 zlib 或 flate 壓縮數(shù)據(jù)。
所以使用archive/zip
包來正確處理它們。例如,要列出 JAR 文件中的文件:
r, err := zip.NewReader(bytes.NewReader(buff), int64(len(buff)))
if err != nil {
? ? panic(err)
}
for _, f := range r.File {
? ? fmt.Println("Found in jar:", f.Name)
}
當(dāng)然你必須提供完整的文件內(nèi)容,否則你很可能在獲取zip.Reader
.
如果您還想打印存檔中文件的內(nèi)容,可以這樣做:
for _, f := range r.File {
? ? fmt.Printf("Found in jar: %s, contents:\n", f.Name)
? ? rc, err := f.Open()
? ? if err != nil {
? ? ? ? log.Fatal(err)
? ? }
? ? _, err = io.CopyN(os.Stdout, rc, int64(f.UncompressedSize64))
? ? if err != nil {
? ? ? ? log.Fatal(err)
? ? }
? ? rc.Close()
? ? fmt.Println()
}
這是一個(gè) JAR 文件,其中包含一個(gè)名為 的文件a.txt,其內(nèi)容為"Hello Gopher":
buff := []byte{80, 75, 3, 4, 10, 0, 0, 0, 0, 0, 91, 109, 103, 78, 132, 225, 60, 127, 13, 0, 0, 0, 13, 0, 0, 0, 5, 0, 28, 0, 97, 46, 116, 120, 116, 85, 84, 9, 0, 3, 206, 17, 129, 92, 219, 17, 129, 92, 117, 120, 11, 0, 1, 4, 232, 3, 0, 0, 4, 232, 3, 0, 0, 72, 101, 108, 108, 111, 32, 71, 111, 112, 104, 101, 114, 10, 80, 75, 1, 2, 30, 3, 10, 0, 0, 0, 0, 0, 91, 109, 103, 78, 132, 225, 60, 127, 13, 0, 0, 0, 13, 0, 0, 0, 5, 0, 24, 0, 0, 0, 0, 0, 1, 0, 0, 0, 164, 129, 0, 0, 0, 0, 97, 46, 116, 120, 116, 85, 84, 5, 0, 3, 206, 17, 129, 92, 117, 120, 11, 0, 1, 4, 232, 3, 0, 0, 4, 232, 3, 0, 0, 80, 75, 5, 6, 0, 0, 0, 0, 1, 0, 1, 0, 75, 0, 0, 0, 76, 0, 0, 0, 0, 0}
在此緩沖區(qū)上運(yùn)行上述代碼,輸出為(在Go Playground上嘗試):
Found in jar: a.txt, contents:
Hello Gopher
- 1 回答
- 0 關(guān)注
- 154 瀏覽
添加回答
舉報(bào)