Zip icon indicating copy to clipboard operation
Zip copied to clipboard

Zip file extension validation

Open yvbeek opened this issue 8 years ago • 7 comments

Great zip library but the file extension validation is making it difficult to use for me. I download a file with NSURLSession and it doesn't have the .zip extension (but it is a zip file though). Now I have to explicitly rename the file to .zip to pass the extension validation.

Would you consider removing the validation? Or making it optional?

yvbeek avatar Jul 24 '16 22:07 yvbeek

From what I understand, this will not be easy to implement. The NSFileManager does not recognize files that do not have an extension. (I've tried this and I'm getting false from fileExistsAtPath, when the file does exist). Perhaps your solution would be to explicitly set the extension, like you mentioned.

jwelton avatar Jul 28 '16 18:07 jwelton

Is it necessary to check if the file exists? Apple, in their documentation, recommends to try to open the file and to handle any errors that might occur gracefully.

NOTE Attempting to predicate behavior based on the current state of the file system or a particular file on the file system is not recommended. Doing so can cause odd behavior or race conditions. It’s far better to attempt an operation (such as loading a file or creating a directory), check for errors, and handle those errors gracefully than it is to try to figure out ahead of time whether the operation will succeed. For more information on file-system race conditions, see Race Conditions and Secure File Operations in Secure Coding Guide.

Source: NSFileManager fileExistsAtPath(_:)

yvbeek avatar Jul 28 '16 21:07 yvbeek

This is true, however when bypassing validation, the following line was failing: try fileManager.attributesOfItemAtPath(path)

jwelton avatar Jul 31 '16 19:07 jwelton

In my test it doesn't seem to fail. Am I missing something?

Test file /tmp/test0, created with:

mkfile -n 5m /tmp/test0

Swift code:

let fileManager = NSFileManager.defaultManager()
let attributes = try? fileManager.attributesOfItemAtPath("/tmp/test0")
print(attributes)

Output:

Optional([
  "NSFileCreationDate": 2016-08-03 01:31:10 +0000, 
  "NSFileGroupOwnerAccountName": wheel, 
  "NSFileExtensionHidden": 0, 
  "NSFileSize": 5242880, 
  "NSFileGroupOwnerAccountID": 0, 
  "NSFileOwnerAccountID": 501, 
  "NSFilePosixPermissions": 384,
  "NSFileType": NSFileTypeRegular, 
  "NSFileSystemFileNumber": 39560369, 
  "NSFileSystemNumber": 16777220, 
  "NSFileReferenceCount": 1, 
  "NSFileModificationDate": 2016-08-03 01:31:10 +0000
])

File size, creation and modification date etc. all seem to make sense. It also works fine if the file without extension is in the app document directory.

yvbeek avatar Aug 03 '16 01:08 yvbeek

Just to let you know, I use your project and it's working well but having to add .zip is unnatural. There are multiple other file formats using zip compression, in my case mxl : http://www.musicxml.com/tutorial/compressed-mxl-files/

PierreMardon avatar Mar 24 '17 10:03 PierreMardon

+1 for eliminating the extension verifying. It is not necessary or a good reason to use the extension to determine whether the file can be opened or not. My situation is similar to @jwelton 's: a downloaded file by URLSession gives a tmp extension for now. I can add tmp to the allowed list, but there is no guarantee the downloaded file would be always end with tmp. Manually adding zip suffix to the file name is quite unnature for an elegant API.

onevcat avatar Sep 02 '20 07:09 onevcat

Tried the code by removing the validation, and has no issues with other extensions. Also this validation is case sensitive so if you need to add all the combinations zip Zip ZIP etc making it extra annoying if you need to add others too.

awulf avatar Jun 17 '21 07:06 awulf