0

I'm trying to decompress a zip file in golang (with password).

    r, err := zip.OpenReader("./path/to/the.zip")
    if err != nil {
        log.Fatal(err)
    }
    defer r.Close()

    for _, f := range r.File {
        if f.IsEncrypted() {
            f.SetPassword("$83?Lu{5nKbXxncjdH/_B)+}C`")
        }
        r, err := f.Open()
        if err != nil {
            log.Fatal(err)
        }

        buf, err := ioutil.ReadAll(r)
        if err != nil {
            log.Fatal(err)
        }
        defer r.Close()

        fmt.Printf("Size of %v: %v byte(s)\n", f.Name, len(buf))
    }

I'm at this point where using the repository https://github.com/yeka/zip I get the following output:

Size of cmxxxgapi/: 0 byte(s)
Size of cmxpi/vxxxxxxxx.yml: 326 byte(s)
Size of cmxxxpi/valxxxxxxe.yml: 379 byte(s)
Size of validations_javxxxo_mxxxxxxxon.yml: 722 byte(s)
Size of validations_xxx_mxxxxxxxxo.yml: 4322 byte(s)
Size of validations_xxxxse.yml: 65 byte(s)

How can I decompress all those files and have them accessible from my binary.

Thank you very much.

Ps. I can't use exec.

Hugo L.M
  • 1,053
  • 17
  • 31
  • You already did. `buf` contains the file contents. – Peter Oct 08 '18 at 11:16
  • Thank you @Peter. I've got the content, but I don't know how to dump it into a file. Because I need to be able to access this content later. I'm trying and I can't find the correct way. – Hugo L.M Oct 08 '18 at 11:31
  • Thank you, I know how to read the documentation and I don't know how your answer can help. I have tried: `ioutil.WriteFile("./sentinel/validations/"+f.Name, buf ,os.ModePerm)`. But I get: `37mDEBU[0m[0000] cloneValidations - Starting 31mFATA[0m[0000] flate: corrupt input before offset 4` – Hugo L.M Oct 08 '18 at 11:56
  • It seems every time I comment you change the question. `ioutil.WriteFile` will not produce an error message that starts with `"flate:"`. I would expect that from either the `f.Open` or `ioutil.ReadAll` call, but your question suggests that both are successful. It's really not clear what you're asking here. – Peter Oct 08 '18 at 13:00

1 Answers1

2

I finally managed to decompress the contents of the zip. I wasn't getting all the files correctly because it didn't differentiate from the paths to the files directly.

I have solved it in the following way:

r, err := zip.OpenReader("./path/to/the.zip")
if err != nil {
    log.Fatal(err)
}
defer r.Close()

for _, f := range r.File {
    if f.IsEncrypted() {
        f.SetPassword("$83?ksdfkjsdKJHJKjdH/_B)+}C`")
    }
    r, err := f.Open()
    if err != nil {
        log.Fatal(err)
    }

    buf, err := ioutil.ReadAll(r)
    if err != nil {
        log.Fatal(err)
    }
    defer r.Close()

    if f.FileInfo().IsDir() {
        // Make Folder
        os.MkdirAll(fpath, os.ModePerm)
    } else {
        ioutil.WriteFile("./sentinel/validations/"+f.Name, buf ,os.ModePerm)
    }
}
Hugo L.M
  • 1,053
  • 17
  • 31