-
-
Notifications
You must be signed in to change notification settings - Fork 376
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
parsing header.Name (filepath) for subfolders #7
Conversation
While trying to unrar an archive with subfolders, the call to header.IsDir evaluates as false because the folder structure does not exist yet. This is a less than ideal way to parse the path for subfolders and create them as needed before header.IsDir is checked against.
@@ -37,6 +38,23 @@ func Unrar(source, destination string) error { | |||
} else if err != nil { | |||
return err | |||
} | |||
|
|||
pathComponents := strings.Split(header.Name, "/") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could do filepath.Dir(header.Name)
and remove that if pi == len(pathComponents) - 1
check below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So is "/" always used in rar files, even on Windows?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have been testing on windows exclusively so far, so I can confirm that the forward-slash "/" is used on windows. Haven't tested on other platforms yet.
Thanks for finding a fix! You beat me to it. 😄 |
// check to see if the path exists already | ||
if stat, err := os.Stat(destination + path); err != nil || !stat.IsDir() { | ||
// make the directory | ||
mkdir(destination + path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function will make all parent folders necessary to make the folder at destination+path
so it should only need be called once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue is that header only gets set in the for loop, and this needs to be called after header is set, but before header.isDir is evaluated.
…reation code into makeSubfolders() method.
Made another update that prevents subfolder creation from being called multiple times. Still not 100% ideal solution, since user might want to unrar the same file twice, and folders may have been deleted in between calls to unrar. Open to suggestions on better implementation. |
Updated again with a simpler (better) way of making sure makeSubfolders() is only called once per call to Unrar() |
Hey, nice improvements! I think I was able to fix it simply with this just before the err = mkdir(filepath.Dir(filepath.Join(destination, header.Name)))
if err != nil {
return err
} Although I'm not 100% sure what the rar file you sent me is supposed to look like since I didn't ask for its zip equivalent. But I saw strange behavior before my change and it looks good now. Can you see if this works for you? If so I prefer it because it's much simpler. If this works, you can still get the credit by updating your PR. |
I can confirm that does work. Updated my branch accordingly. |
Cool, thanks for your help! |
While trying to unrar an archive with subfolders, the call to header.IsDir evaluates as false because the folder structure does not exist yet. This is a less than ideal way to parse the path for subfolders and create them as needed before header.IsDir is checked against.