Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if zip entry is directory in Go language

Tags:

directory

zip

go

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

like image 403
Eugene Avatar asked Mar 20 '23 20:03

Eugene


1 Answers

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())
    }
}
like image 169
Greg Avatar answered Apr 01 '23 17:04

Greg