ZIPFoundation icon indicating copy to clipboard operation
ZIPFoundation copied to clipboard

FileManager.unzipItem doesn't work with some URLs created from relative paths

Open Aquilosion opened this issue 3 years ago • 3 comments

Summary

If I try to unzip a file (using the FileManager extension) to a URL which was created with a relative path, the extraction fails with an error. Using the relative URL's absoluteURL does work.

Steps to Reproduce

  1. Create a "relative" URL, such as: URL(fileURLWithPath: "../../Resources", isDirectory: true).
  2. Pass that URL as the to parameter of FileManager.unzipItem (along with a URL to a valid zip file).

Expected Results

The contents of the zip file is successfully extracted to the target location.

Actual Results

An error is thrown. For example, if the zip file contains a compressed directory called "media", the error is:

The item couldn’t be opened because the file name “media” is invalid.

If I get the absoluteURL of the target URL first, it does work.

Regression & Version

Tested with 0.9.12.

Findings

I think this is caused because the implementation of URL.isContained(in:) (defined in FileManager+ZIP.swift) calls standardized on the URLs first, which outputs the wrong URL when it is created with a relative path. For example:

fileManager.changeCurrentDirectoryPath("/Users/Robert/Documents")

let relativePath = "../../David/Documents"
let relativeURL = URL(fileURLWithPath: relativePath)

relativeURL.absoluteString
// file:///Users/David/Documents

relativeURL.standardized.absoluteString
// file:///Users/Robert/Documents/David/Documents

Aquilosion avatar Mar 16 '21 12:03 Aquilosion

Summary Extracting zip file is failing intermittently on latest release i.e 0.9.18. I was able to unzip with previous releases.

Steps To Reproduce: There are no consistent steps. It will fail intermittently.

Actual Result: On debugging, unzip API is throwing following error:

The item couldn’t be opened because the file name “Bundle” is invalid.

Expected: Should be able to unzip always.

Version: 0.9.18

ckp-aayushi avatar Feb 09 '24 07:02 ckp-aayushi

Extracting zip file is failing intermittently on latest release i.e 0.9.18. I was able to unzip with previous releases.

Do the archives you are trying to unzip contain symlinks that point outside of the destination directory? If so, you now have to opt-into pre-0.9.18 behavior by passing allowUncontainedSymlinks: true to unzipItem.

weichsel avatar Feb 09 '24 16:02 weichsel

Do the archives you are trying to unzip contain symlinks that point outside of the destination directory? If so, you now have to opt-into pre-0.9.18 behavior by passing allowUncontainedSymlinks: true to unzipItem.

No, zip file does not contain symlinks. This is working fine with 0.9.17 and is seen on 0.9.18 and 0.9.19 versions. We need to use 0.9.19 for Privacy manifest changes. Pls suggest how to resolve this issue.

ckp-aayushi avatar Apr 05 '24 08:04 ckp-aayushi