Cannot unzip zip file when folders are present

Hello there,

I’m trying to extract a zip file, which has a series of folders and contents inside. Whenever I try this code, and no folders are present in the zip file, it works flawlessly. Thing is, I wanted to work with folders and it just doesn’t work no matter what.

  // Create a reader out of the zip archive
zipReader, err := zip.OpenReader("")
if err != nil {
defer zipReader.Close()

// Iterate through each file/dir found in
for _, file := range zipReader.Reader.File {
    // Open the file inside the zip archive
    // like a normal file
    zippedFile, err := file.Open()
    if err != nil {
    defer zippedFile.Close()
    // Specify what the extracted file name should be.
    // You can specify a full path or a prefix
    // to move it to a different directory. 
    // In this case, we will extract the file from
    // the zip to a file of the same name.
    targetDir := "./"
    extractedFilePath := filepath.Join(

    // Extract the item (or create directory)
    if file.FileInfo().IsDir() {
        // Create directories to recreate directory
        // structure inside the zip archive. Also
        // preserves permissions
        log.Println("Creating directory:", extractedFilePath)
        os.MkdirAll(extractedFilePath, file.Mode())
    } else {
        // Extract regular file since not a directory
        log.Println("Extracting file:", file.Name)

        // Open an output file for writing
        outputFile, err := os.OpenFile(
        if err != nil {
        defer outputFile.Close()

        // "Extract" the file by copying zipped file
        // contents to the output file
        _, err = io.Copy(outputFile, zippedFile)
        if err != nil {

Any suggestions? It returns folder/image.jpg no such file or directory

I’m new to go so sorry if this is a stupid question.


This code depends on the current working directory. This can sometimes not be the value you expect. How are you executing this code ?

Script.go and in the same folder.

Then: go run script.go

Thank you for replying.

The problem is go run switches to a temporary directory when running your code, this is why it cannot find

To solve this, use go build, the run the resulting program.

I don’t know the zip format very well, but another possibility that strikes me is that the directories are simply not included in the archive. That is, you expect to get, in order:

  • somefile (file)
  • folder (directory)
  • folder/image.jpg (file)

but things will break if you actually just get

  • somefile (file)
  • folder/image.jpg (file)

That is - verify that you actually get a directory entry when unzipping, or adjust accordingly.

Only while building, I think.

This sounds like the problem, but I thoguht that the first thing that would do is list the main files, including folder names and I’d the folder doesn’t exist create it, then cycle through its contents…

Thing is I don’t know his am I going to detect whether it’s a folder or not, as it cycles automatically to the final URL…

I’ll try to tokenize or something.

You can use filepath.Dir on each file to get the directory component, then create it if it does not already exist.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.