【问题标题】:How to unzip a string in language Go [closed]如何用 Go 语言解压缩字符串 [关闭]
【发布时间】:2020-10-19 10:33:49
【问题描述】:

我在下面有一些 Go 代码,其中包含 PK 压缩字符串。如何获取此 zip 字符串中每个文件的内容?

以下是使用字符串操作的尝试,我可以获取第一个文件的内容。有没有另一种不使用字符串函数来获取内容的方法?

package main

import (
    "fmt"
    "strings"
)

func main() {
 
    x := "PK\x03\x04\n\x00\x00\x00\x00\x00\x14OOQ\xddDYc\v\x00\x00\x00\v\x00\x00\x00\t\x00\x00\x00file1.txttextfileonePK\x03\x04\n\x00\x00\x00\x00\x00\x1aOOQq?\xb5]\t\x00\x00\x00\t\x00\x00\x00\t\x00\x00\x00file2.txttextfile2PK\x01\x02?\x00\n\x00\x00\x00\x00\x00\x14OOQ\xddDYc\v\x00\x00\x00\v\x00\x00\x00\t\x00$\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00file1.txt\n\x00 \x00\x00\x00\x00\x00\x01\x00\x18\x00#~\xfb\xa7\x85\xa2\xd6\x01#~\xfb\xa7\x85\xa2\xd6\x01\x00\xa8\xdfE\xe7\x8b\xd6\x01PK\x01\x02?\x00\n\x00\x00\x00\x00\x00\x1aOOQq?\xb5]\t\x00\x00\x00\t\x00\x00\x00\t\x00$\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x002\x00\x00\x00file2.txt\n\x00 \x00\x00\x00\x00\x00\x01\x00\x18\x00u\xa7v\xaf\x85\xa2\xd6\x01u\xa7v\xaf\x85\xa2\xd6\x01\xa8\x9eˏ\x85\xa2\xd6\x01PK\x05\x06\x00\x00\x00\x00\x02\x00\x02\x00\xb6\x00\x00\x00b\x00\x00\x00\x00\x00"

    i := strings.Index(x , "PK")
    right := x[i+1:]
    i = strings.Index(right , "PK")
    left := right[:i]
    i = strings.LastIndex(left , ".txt")
    file1contents := left[i+4:]
    fmt.Println("file1contents : ", file1contents)
}

上面的输出 file1contents : textfileone

【问题讨论】:

  • 你试过什么?包括你的代码。你遇到了什么问题?
  • 我对 Go 的了解有限(大约 5 分钟),不知道从哪里开始。但再次感谢 SO 社区对英国人的赞誉。
  • 如果你只有 5 分钟的 Go 经验,那么首先要做的就是阅读有关 Go 的知识。以A Tour of Go开头。
  • 抱歉,只是需要一些快速的接口代码
  • 不幸的是,这不是 SO 的工作方式。

标签: go zip


【解决方案1】:

标准库中的zip 包将为您完成此操作:


func main() {
    z := []byte("PK\x03\x04\n\x00\x00\x00\x00\x00\x14OOQ\xddDYc\v\x00\x00\x00\v\x00\x00\x00\t\x00\x00\x00file1.txttextfileonePK\x03\x04\n\x00\x00\x00\x00\x00\x1aOOQq?\xb5]\t\x00\x00\x00\t\x00\x00\x00\t\x00\x00\x00file2.txttextfile2PK\x01\x02?\x00\n\x00\x00\x00\x00\x00\x14OOQ\xddDYc\v\x00\x00\x00\v\x00\x00\x00\t\x00$\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00file1.txt\n\x00 \x00\x00\x00\x00\x00\x01\x00\x18\x00#~\xfb\xa7\x85\xa2\xd6\x01#~\xfb\xa7\x85\xa2\xd6\x01\x00\xa8\xdfE\xe7\x8b\xd6\x01PK\x01\x02?\x00\n\x00\x00\x00\x00\x00\x1aOOQq?\xb5]\t\x00\x00\x00\t\x00\x00\x00\t\x00$\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x002\x00\x00\x00file2.txt\n\x00 \x00\x00\x00\x00\x00\x01\x00\x18\x00u\xa7v\xaf\x85\xa2\xd6\x01u\xa7v\xaf\x85\xa2\xd6\x01\xa8\x9eˏ\x85\xa2\xd6\x01PK\x05\x06\x00\x00\x00\x00\x02\x00\x02\x00\xb6\x00\x00\x00b\x00\x00\x00\x00\x00")
    r, err := zip.NewReader(bytes.NewReader(z), int64(len(z)))
    if err != nil {
        panic(err)
    }
    for _, f := range r.File {
        fmt.Printf("Contents of %s:\n", f.Name)
        rc, err := f.Open()
        if err != nil {
            panic(err)
        }
        c, err := ioutil.ReadAll(rc)
        if err != nil {
            panic(err)
        }
        fmt.Printf("%s\n\n", c)
        rc.Close()
        fmt.Println()
    }

}

playground试试吧

【讨论】:

猜你喜欢
  • 2013-10-30
  • 1970-01-01
  • 2011-06-19
  • 1970-01-01
  • 2014-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-12
相关资源
最近更新 更多