Skip to content

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

@Aquilosion

Description

@Aquilosion

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions