2
func main() {
    // Open a zip archive for reading.
    r, err := zip.OpenReader("e:\\demo.zip")
    if err != nil {
        log.Fatal(err)
    }
    defer r.Close()

    for _, f := range r.File {
        if f.FileInfo().IsDir() {
            fmt.Println(f.Name, "folder")
        } else {
            fmt.Println(f.Name, "file")
        }
    }

}

out:

loading/ folder
loading/loading.html file
loading.css file

I want:

loading/ folder
loading.css file

I just need to get the list of compressed file contents.

The program will print out all files, can subfolders be excluded,or have to handle it myself?

zeronofreya
  • 203
  • 2
  • 7

1 Answers1

0

Something like this should be enough to achieve what you need:

func main() {
    // Open a zip archive for reading.
    r, err := zip.OpenReader("demo.zip")
    if err != nil {
        log.Fatal(err)
    }
    defer r.Close()

    for _, f := range r.File {
        if f.FileInfo().IsDir() {
            fmt.Println(f.Name, "folder")
            continue
        }
        if !strings.Contains(f.FileHeader.Name, "/") {
            fmt.Println(f.Name, "file")
        }
    }
}

We simply need to check if the file name contains an / to understand if it's an element of a subdirectory.
Let me know if this solves your issue or if you need something else!

ossan
  • 1,665
  • 4
  • 10