Question

I guess some bit in following structure is marking file as directory. But I can't find reference to that.

http://golang.org/pkg/archive/zip/#FileHeader

Was it helpful?

Solution

The zip package's FileHeader type, which you linked to, has a .FileInfo() method which returns an os.FileInfo type, which itself has an .IsDir() method.

So chaining that all together, you can tell if the file in the zip archive is a directory with f.FileInfo().IsDir().

Example:

package main

import (
    "archive/zip"
    "fmt"
)

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

    // Iterate through the files in the archive,
    // indicating if it is a directory.
    for _, f := range r.File {
        fmt.Printf("%s is directory? - %v\n", f.Name, f.FileInfo().IsDir())
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top