Nemo XXX
Nemo XXX

Reputation: 681

How to unzip a single file?

I've found lots of examples on how to extract all files from .zip, but I can't figure out how to extract a single file without iterating over all files in the .zip file.

Is it possible in Go to extract a single file from a .zip archive without iterating over all files in the .zip file?

For example, if a zip file contained:

folder1/file1.txt
folder1/file2.txt
folder1/file3.txt
folder2/file1.txt

How would I extract only folder2/file1.txt?

Upvotes: 2

Views: 2926

Answers (1)

icza
icza

Reputation: 417452

zip.Reader provides you the content of the archive, the files as a slice (of zip.File). There is no helper method to get a file by name, you have to iterate over the files with a loop. You don't need to open / extract the files, but to find a file by name, you have to use a loop.

For example:

r, err := zip.OpenReader("testdata/readme.zip")
if err != nil {
    log.Fatal(err)
}
defer r.Close()

for _, f := range r.File {
    if f.Name != "folder2/file1.txt" {
        continue
    }

    // Found it, print its content to terminal:
    rc, err := f.Open()
    if err != nil {
        log.Fatal(err)
    }
    _, err = io.Copy(os.Stdout, rc)
    if err != nil {
        log.Fatal(err)
    }
    rc.Close()
    fmt.Println()
    break
}

Upvotes: 3

Related Questions