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

zip archive invalid but works with other utilities #45338

Closed
cpuguy83 opened this issue Apr 1, 2021 · 5 comments
Closed

zip archive invalid but works with other utilities #45338

cpuguy83 opened this issue Apr 1, 2021 · 5 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@cpuguy83
Copy link

cpuguy83 commented Apr 1, 2021

What version of Go are you using (go version)?

$ go version
go version go1.16.2 darwin/amd64

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN="/Users/cpuguy83/go/bin"
GOCACHE="/Users/cpuguy83/Library/Caches/go-build"
GOENV="/Users/cpuguy83/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/cpuguy83/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/cpuguy83/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.16.2/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.16.2/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.2"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/cpuguy83/dev/gh-actions-downloader/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/h0/019q23y92j58nhjw33nyjd1h0000gn/T/go-build2038630442=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I have this zip archive (linked to the source here, but also attached to the issue below) which archive/zip says is invalid.
This is created by github actions artifact uploader.

TestResults MacOS.zip

What did you expect to see?

Maybe at least details on what makes it invalid but genereally I'd expect a way to make it work since other zip utilities seem to be OK with it.

What did you see instead?

zip.NewReader gives me:

zip: not a valid zip file
@ianlancetaylor
Copy link
Contributor

I downloaded your zip file and ran this program. It worked for me, in that zip.OpenReader did not return an error.

Show us your Go code.

package main

import (
	"archive/zip"
	"fmt"
	"os"
)

func main() {
	_, err := zip.OpenReader(os.Args[1])
	if err != nil {
		fmt.Println(err)
	}
}

@ianlancetaylor ianlancetaylor added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Apr 1, 2021
@cpuguy83
Copy link
Author

cpuguy83 commented Apr 1, 2021

Note that "client" here is from google/go-github and client.Do writes the response into f.
On the particular job I am downloading artifacts for there are several artifacts that are all fine, its this one that it doesn't like.

	u, resp, err := client.Actions.DownloadArtifact(ctx, a.org, a.repo, a.ID, false)
	if err != nil {
		type ghErr struct {
			Message string `json:"message"`
		}
		e := &ghErr{}
		json.NewDecoder(io.LimitReader(resp.Body, 32*1024)).Decode(e)
		return errors.Wrapf(err, "error getting url to download artifact: %s", e.Message)
	}
	resp.Body.Close()

	req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
	if err != nil {
		return err
	}

	if err := os.MkdirAll(dir, 0700); err != nil {
		return errors.Wrap(err, "error creating artifact dir")
	}

	f, err := os.OpenFile(filepath.Join(dir, a.Name)+".zip", os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0600)
	if err != nil {
		return errors.Wrap(err, "error creating save file")
	}
	defer f.Close()

	resp, err = client.Do(ctx, req, f)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	if _, err := f.Seek(0, io.SeekStart); err != nil {
		return errors.Wrap(err, "error seeking back to save file start")
	}

	r, err := zip.NewReader(f, a.Size)
	if err != nil {
		return errors.Wrapf(err, "error making zip reader from file %s", f.Name())
	}
	if err := unzipGo(r, a, dir); err != nil {
		return err
	}

Working on a utility to download github actions artifacts... I'll post full code to a repo soon just need to clean up some hardcoded things first.

@ianlancetaylor
Copy link
Contributor

r, err := zip.NewReader(f, a.Size)

What is a.Size?

@cpuguy83
Copy link
Author

cpuguy83 commented Apr 1, 2021

Oh... that's it! Github is reporting the wrong size for the file.
If I change that to the position f was at before I reset it (which I realize I don't need to do also...) it works.

@cpuguy83
Copy link
Author

cpuguy83 commented Apr 1, 2021

❤️ Sorry for the noise.

@cpuguy83 cpuguy83 closed this as completed Apr 1, 2021
@golang golang locked and limited conversation to collaborators Apr 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

3 participants