Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

archive/zip: FileInfoHeader does not handle directories #24082

dsnet opened this issue Feb 23, 2018 · 5 comments

archive/zip: FileInfoHeader does not handle directories #24082

dsnet opened this issue Feb 23, 2018 · 5 comments


Copy link

@dsnet dsnet commented Feb 23, 2018

Consider the following:

fi, _ := os.Stat("/home/")
h, _ := zip.FileInfoHeader(fi)

This currently prints:


However, ZIP treats directories as files with a trailing "/" in the name, which FileInfoHeader should automatically append if os.FileInfo.IsDir reports true.

This difference is minor since users are usually expected to replace the FileHeader.Name shortly afterwards anyways.

@dsnet dsnet added this to the Go1.11 milestone Feb 23, 2018
Copy link

@ianlancetaylor ianlancetaylor commented Jun 13, 2018

@dsnet Please update milestone. Thanks.

@dsnet dsnet modified the milestones: Go1.11, Unplanned Jun 13, 2018
Copy link

@gopherbot gopherbot commented Jul 16, 2018

Change mentions this issue: archive/zip: fix regression when writing directories

Copy link
Member Author

@dsnet dsnet commented Jul 16, 2018

We should fix this. I found several cases where users are explicitly doing something like this:

fh, err := zip.FileInfoHeader(fi)
if err != nil {
    return err
if fi.IsDir() {
    fh.Name += "/"

Also, per CL/124155, the function should avoid setting the size for directories.

EDIT: Will have to think about what happens with existing code, since this will result in "/" appended twice.

@dsnet dsnet modified the milestones: Unplanned, Go1.12 Jul 16, 2018
gopherbot pushed a commit that referenced this issue Jul 16, 2018
Several adjustments:

1) When encoding the FileHeader for a directory, explicitly set all of the sizes
to zero regardless of their prior values. These values are currently populated
by FileInfoHeader as it calls os.FileInfo.Size regardless of whether the file is
a directory or not. We avoid fixing FileInfoHeader now as it is too late in the
release cycle (see #24082).

We silently adjust slightly wrong FileHeader fields as opposed to returning
an error because the CreateHeader method already does such mutations
(e.g., for UTF-8 detection, data descriptor, etc).

2) Have dirWriter.Write only return an error if some number of bytes are written.
Some code still call Write for both normal files and directories, but just pass
an empty []byte to Write for directories.

Change-Id: I85492a31356107fcf76dc89ceb00a28853754289
Run-TryBot: Joe Tsai <>
TryBot-Result: Gobot Gobot <>
Reviewed-by: Brad Fitzpatrick <>
Copy link

@ianlancetaylor ianlancetaylor commented Nov 29, 2018

@dsnet Is anything going to happen here for 1.12?

Copy link
Member Author

@dsnet dsnet commented Nov 29, 2018

This change has a risk of breaking people. Too late for 1.12. We'll submit early 1.13.

@dsnet dsnet modified the milestones: Go1.12, Go1.13 Nov 29, 2018
@andybons andybons modified the milestones: Go1.13, Go1.14 Jul 8, 2019
@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants