Check zip file containing video content or not in swift

360 Views Asked by At

I have to preview zip files using Document Interaction Controller, but zip files containing video content should not be previewed. Is there a way to check zip file containing video content using swift?

1

There are 1 best solutions below

0
On

There's a 3rd party library called ZIPFoundation that makes it convenient to traverse zip entries.

  1. Install pod 'ZipFoundation' in your project.
  2. Copy / paste below helper code in your project.
import Foundation
import ZIPFoundation

extension String {
    var pathExtension: String {
        URL(fileURLWithPath: self).pathExtension
    }
}

extension Archive {
    var containsVideo: Bool {
        let videoTypes: [String] = ["MOV", "MP4", "AVI"]
        for entry in self where entry.type == .file {
            let type = entry.path.pathExtension.uppercased()
            if videoTypes.contains(type) {
                return true
            }
        }
        return false
    }
}

From the call site, you can use it like following -

if let zipURL = Bundle.main.url(forResource: "Test", withExtension: "zip"),
    let arhive = Archive(url: zipURL, accessMode: .read) {
    print(arhive.containsVideo)
}