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

Windows path handling (tar.gz, probably others, too) #313

Closed
seizmo opened this issue Jan 20, 2022 · 5 comments
Closed

Windows path handling (tar.gz, probably others, too) #313

seizmo opened this issue Jan 20, 2022 · 5 comments

Comments

@seizmo
Copy link

seizmo commented Jan 20, 2022

What version of the package or command are you using?

v4 lastest (d777df9)

What are you trying to do?

Create a tar.gz file under windows.

What steps did you take?

       files, err := archiver.FilesFromDisk(nil, map[string]string{
		".\res\test\tenant_files\tenant_1": "", // contents added recursively
	})
	if err != nil {
		return err
	}

	archiveDir, err := job.ArchiveDirectory(true)

	// create the output file we'll write to
	out, err := os.Create(filepath.Join(archiveDir, "files.tar.gz"))
	if err != nil {
		return err
	}
	defer out.Close()

	// we can use the CompressedArchive type to gzip a tarball
	// (compression is not required; you could use Tar directly)
	format := archiver.CompressedArchive{
		Compression: archiver.Gz{},
		Archival:    archiver.Tar{},
	}

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

What did you expect to happen, and what actually happened instead?

Opening the file with 7-zip should extract to the same directory structure of the original files

How do you think this should be fixed?

https://github.com/mholt/archiver/blob/master/archiver.go should probably use filepath instead of path since path only supports slashes and no backslashes

Bonus: What do you use archiver for, and do you find it useful?

Just got started but really like the API (and the author, thanks for Caddy!!!) :)

@mholt
Copy link
Owner

mholt commented Jan 20, 2022

Thanks; I don't have Windows to test this. Can you give more information? What exactly is broken? What is in the created archive?

archiver.go should probably use filepath instead of path since path only supports slashes and no backslashes

We do use filepath. The only time we use path is to join the components of the path in the archive, where it is required to use slash separator.

@seizmo
Copy link
Author

seizmo commented Jan 21, 2022

The problem seems to be that the Windows backlash is not removed/replaced with slashes but included in the archive entries in escaped form:

tenant_1
tenant_1/\\1
tenant_1/\\1\\file-example_PDF_1MB.pdf
tenant_1/\\2
tenant_1/\\2\\file-example_PDF_500_kB.pdf
tenant_1/\\3
tenant_1/\\3\\file-sample_150kB.pdf

This is what I get if I unpack that file on Linux (using tar xzvf)

@mholt
Copy link
Owner

mholt commented Jan 21, 2022

Ah, indeed, path.Join() calls path.Clean() which does NOT call ToSlash(). Only filepath.Join() calls filepath.Clean() which DOES call FromSlash(). 🙄 I get why, but that was a silly oversight on my part.

Can you test what I push to a branch? (Until I have a chance to write actual automated tests, heh. Just been busy lately.)

@seizmo
Copy link
Author

seizmo commented Jan 21, 2022

I changed the two lines and it worked for me on Windows with Go 1.18. 7zip on windows now also opens and extracts the archive to a Windows folder with correctly named files.

Thanks a lot!

@mholt mholt closed this as completed in a44c8d2 Jan 21, 2022
@mholt
Copy link
Owner

mholt commented Jan 21, 2022

Great, thanks for confirming!

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

No branches or pull requests

2 participants