kdomanski / iso9660

A go library for reading and creating ISO9660 images

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Adding multiple folders recursively

aminya opened this issue · comments

I am trying to add multiple files in the sourceFolders by using the walk function for each folder. However, it only adds one single file for some reason.

package main

import (
	"fmt"
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/kdomanski/iso9660"
)

func main() {
	writer, err := iso9660.NewWriter()
	if err != nil {
		log.Fatalf("failed to create writer: %s", err)
	}
	defer writer.Cleanup()

	isoFile, err := os.OpenFile("C:/output.iso", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
	if err != nil {
		log.Fatalf("failed to create file: %s", err)
	}
	defer isoFile.Close()

	sourcePath := "F:\\"

	folders := []string{"F:\\test1", "F:\\test2"}

	for _, folderName := range folders {
		folderPath := strings.Join([]string{sourcePath, folderName}, "/")

		walk_err := filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
			if err != nil {
				log.Fatalf("walk: %s", err)
				return err
			}
			if info.IsDir() {
				return nil
			}
			outputPath := strings.TrimPrefix(path, sourcePath) // remove the source drive name
			fmt.Printf("Adding file: %s\n", outputPath)

			fileToAdd, err := os.Open(path)
			if err != nil {
				log.Fatalf("failed to open file: %s", err)
			}
			defer fileToAdd.Close()

			err = writer.AddFile(fileToAdd, outputPath)
			if err != nil {
				log.Fatalf("failed to add file: %s", err)
			}
			return nil
		})
		if walk_err != nil {
			log.Fatalf("%s", walk_err)
		}
	}

	err = writer.WriteTo(isoFile, "Test")
	if err != nil {
		log.Fatalf("failed to write ISO image: %s", err)
	}
}

This was an issue with manglePath function that didn't support \\ in the paths. #17 fixes the issue.