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

Broken Zip archive #350

Closed
nicola-spb opened this issue Oct 7, 2022 · 7 comments · Fixed by #355
Closed

Broken Zip archive #350

nicola-spb opened this issue Oct 7, 2022 · 7 comments · Fixed by #355

Comments

@nicola-spb
Copy link

What version of the package or command are you using?

v4.0.0-alpha.7

What are you trying to do?

I trying extract ZIP archive which created by archiver.

What steps did you take?


// "from" is directory with files
// "to" is path of archive
err = MakeArchive(context.Background(), from, to, archiver.CompressedArchive{
	Archival: archiver.Zip{},
})

// 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
}

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

When I creating archive and use archiver.FilesFromDisk (for all files in folder) with ZIP format then I have broken archive. I can open archive, open files in archive but I cannot extract files (in Ubuntu) and when I open archive in windows I see root folder but archive should not have it's folder.

How do you think this should be fixed?

I think it's depends on archive type because when I create tar with gzip using MakeArchive it's ok.

Please link to any related issues, pull requests, and/or discussion

See too #331

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

Using for reading and creating archive.

@mholt
Copy link
Owner

mholt commented Oct 7, 2022

Thanks, but this is pretty much the exact same info that's in your other post, so I don't know what to do with this. Please provide minimal reproduction steps from scratch so I can investigate, or, feel free to submit a PR with a fix. Thanks

@nicola-spb
Copy link
Author

nicola-spb commented Oct 7, 2022

Hi,

  1. Create folder "test" ( "/home/.../test") and add some files.
  2. Create empty folder "testresult" ( "/home/.../testresult")
  3. Create main.go
package main

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

	"github.com/mholt/archiver/v4"
)

func main() {
	err := MakeArchive(context.Background(), "/home/.../test", "/home/.../testresult/result.zip", archiver.CompressedArchive{
		Archival: archiver.Zip{},
	})

	if err != nil {
		fmt.Printf("failed make archive; %v\n", err)
		return
	}
}

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
}
  1. Run :)
  2. Open folder testresult
  3. Click right mouse on "result.zip" and select Extract. You will see error in Ubuntu.
    If you open archive in Windows you will see folder name "test" in archive but archive should not have root folder.

@mholt
Copy link
Owner

mholt commented Oct 7, 2022

Thanks! I will do this ASAP

@WeidiDeng
Copy link
Contributor

@nicola-spb Do you mean to put all the files in /home/.../test directly under the root of the zip file?

While on my mac, the zip file is valid, because of the extra / entry, when unzip, there are some error complaining.

@WeidiDeng
Copy link
Contributor

@nicola-spb see if 355 fix your problem

@nicola-spb
Copy link
Author

@WeidiDeng Hi,
Your changes fix it.

mholt pushed a commit that referenced this issue Oct 19, 2022
* ignore empty root dir name when compressing file

fix [350](#350)

* check return err from filepath.Walk
@mholt
Copy link
Owner

mholt commented Oct 19, 2022

Thank you @WeidiDeng !!

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

Successfully merging a pull request may close this issue.

3 participants