-2

I have some Go code below which is has PK zipped string. How to get the contents of each file in this zip string?

Below is an attempt using string manipulation I can get the contents of the first file. Is there another way to get the contents without using string functions?

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)
}

Output of above file1contents : textfileone

ozmike
  • 2,738
  • 1
  • 33
  • 40

1 Answers1

2

The zip package in the standard library will do this for you:


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()
    }

}

Try it in the playground

Brits
  • 14,829
  • 2
  • 18
  • 31
  • Truely awesome and helpful effort..glad there are some 'real' programmer still left in this world many thanks.. – ozmike Oct 15 '20 at 04:35
  • Here is the reverse process ie going from byte array to zip if any one needs it https://stackoverflow.com/questions/12440387/zip-a-byte-array-in-go – ozmike Nov 19 '20 at 02:56