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

Using FilesFromDisk() for compressing files to root of archive #331

Closed
nicola-spb opened this issue Apr 14, 2022 · 8 comments
Closed

Using FilesFromDisk() for compressing files to root of archive #331

nicola-spb opened this issue Apr 14, 2022 · 8 comments

Comments

@nicola-spb
Copy link

What would you like to have changed?

I wanna use archiver.FilesFromDisk for archiving files from folder to root of archive like this:

files, err := archiver.FilesFromDisk(nil, map[string]string{
	"/path/on/disk/folder/*":    "", // contents added recursively to root of archive without "folder"
})

Why is this feature a useful, necessary, and/or important addition to this project?

I think it is useful feature.

What alternatives are there, or what are you doing in the meantime to work around the lack of this feature?

Without this feature I need to write:

fileNames := make(map[string]string)
err := filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error {
	if err != nil {
		return err
	}

	// skip root directory
	if path == sourceDir {
		return nil
	}

	fileNames[path] = ""
	return nil
})

if err != nil {
	return err
}

// map files on disk to their paths in the archive
files, err := archiver.FilesFromDisk(nil, fileNames)

Please link to any relevant issues, pull requests, or other discussions.

@mholt
Copy link
Owner

mholt commented Apr 14, 2022

Thanks for the request. Just to be sure I understand, can you provide a sample directory tree and the expected names in the archives? Basically the expected inputs and outputs.

@nicola-spb
Copy link
Author

Example. I have folder:

/path/on/disk/folder/file1.txt
/path/on/disk/folder/file2.txt
/path/on/disk/folder/file3.txt
/path/on/disk/folder/subfolder/file4.txt

And I wanna put all files to root of archive folder.zip with structure:

file1.txt
file2.txt
file3.txt
subfolder/file4.txt

I tried like this:

files, err := archiver.FilesFromDisk(nil, map[string]string{
	"/path/on/disk/folder/":    "",
	"/path/on/disk/folder/":    "/",
	"/path/on/disk/folder/":    ".",
	"/path/on/disk/folder":     "",
})

@mholt
Copy link
Owner

mholt commented Apr 14, 2022

@nicola-spb Perfect, thanks. I'm headed out in a moment for lunch, but can you tell me real quick what the result is when you do this variant (which I know you already tried):

files, err := archiver.FilesFromDisk(nil, map[string]string{
	"/path/on/disk/folder": "",
})

Because I think that should do what you're looking for... 🤔 I'd test it myself but I'll read your reply during lunch.

@nicola-spb
Copy link
Author

nicola-spb commented Apr 14, 2022

@mholt I tried now. It make archive with structure:

folder/file1.txt
folder/file2.txt
folder/file3.txt
folder/subfolder/file4.txt

@nicola-spb
Copy link
Author

nicola-spb commented Apr 14, 2022

@mholt And when I tried before I had strange result with this:
"/path/on/disk/folder/": ".",

It created archive with folder "." and have this structure:

.
file1.txt
file2.txt
file3.txt

Maybe it's bug...

mholt added a commit that referenced this issue Apr 14, 2022
Now permit suffixing root on disk with separator to enumerate only
its contents and not to create that actual file in archive.

And attempt improving Windows compatibility.
@mholt
Copy link
Owner

mholt commented Apr 14, 2022

@nicola-spb You really like breaking my code huh 😆

I opened #332 which should hopefully address your needs, if you suffix the file on disk with a trailing slash:

files, err := archiver.FilesFromDisk(nil, map[string]string{
	"/path/on/disk/folder/": "",
})

I have not yet looked into the "." input, that will probably have to happen another day.

@mholt mholt closed this as completed in f7aa48e Apr 15, 2022
@nicola-spb
Copy link
Author

nicola-spb commented Sep 27, 2022

@mholt

Hi, when I use create archive and use archiver.FilesFromDisk with ZIP format then I have broken archive. I cannot extract archive (in Ubuntu) and when I open archive in windows I see folder sourceDir (root) but archive should not have it's folder.

format := archiver.CompressedArchive{
	Archival: archiver.Zip{},
}

err = MakeArchive(ctx, tmpDir, archivePath, format)
// MakeArchive make archive of files in directory.
func MakeArchive(ctx context.Context, sourceDir string, archivePath string, format archiver.CompressedArchive) error {
	sep := string(filepath.Separator)

	// add "/" to the path "path/folder/"
	if !strings.HasSuffix(sourceDir, sep) {
		sourceDir = sourceDir + sep
	}

	// map files on disk to their paths in the archive
	files, err := archiver.FilesFromDisk(&archiver.FromDiskOptions{FollowSymlinks:  true}, map[string]string{
		sourceDir: "",
	})
	if err != nil {
		return err
	}

	// create the output file we'll write to
	out, err := os.Create(archivePath)
	if err != nil {
		return err
	}
	defer out.Close()

	// create the archive
	err = format.Archive(ctx, out, files)
	if err != nil {
		return err
	}

	return nil
}

@mholt
Copy link
Owner

mholt commented Sep 30, 2022

@nicola-spb Can you please open a new issue and provide enough information for me to reproduce the behavior as simply as possible? (Directory structure, a full main() function that I can run to repro, etc.) Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants