I am using SSZipArchive to unzip files from a Url (http://www.colorado.edu/conflict/peace/download/peace.zip). For this I am using the function :
SSZipArchive.unzipFile(atPath: path, toDestination: documentsPath, progressHandler: {
Is it possible to extract files from Url using SSZipArchive ?
Is yes, how do I pass Url in atPath ?
You can't directly unzip without downloading the file from the URL.
This is how I do this,
func downLoad() {
let url : String = "http://www.colorado.edu/conflict/peace/download/peace.zip"
var localPath: NSURL?
Alamofire.download(.GET,url,
destination: { (temporaryURL, response) in
let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let pathComponent = response.suggestedFilename
localPath = directoryURL.URLByAppendingPathComponent(pathComponent!)
return localPath!
})
.response { (request, response, _, error) in
SwiftEventBus.post("DownloadedSuccessfully", sender: localPath!)
}
}
After downloading the content, unzip it where you want to.
SwiftEventBus.onMainThread(self, name:"DownloadedSuccessfully")
{
result in
let resultStatus = result.object as! NSURL
guard let zipPath: String = resultStatus.path! as String else {
return
}
guard let unZipPath = unzipPath() else {
return
}
let success = SSZipArchive.unzipFileAtPath(zipPath, toDestination: unZipPath)
print(unZipPath)
if !success {
return
}
}
I unzipped the file and stored it in the document directory
func unzipPath() -> String? {
let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let url = NSURL(fileURLWithPath: path)
do {
try NSFileManager.defaultManager().createDirectoryAtURL(url, withIntermediateDirectories: true, attributes: nil)
} catch {
return nil
}
if let path = url.path {
return path
}
return nil
}
From there you can access the data of the file. Here I used third party frameworks Alamofire and swiftEventBus. Hope this works for you.
Related
I have already copy the file absolute path and paste in simulator browser, the image can be opened. But the fileExists is fail, i dont know why..... Can anyone help
let defaultImage = "302C3FA1-E4E1-4CD8-B6DF-2FF4E4E24C11.jpeg"
loadImage(at: defaultImage)
func fileExists(at path: String) -> Bool {
return FileManager.default.fileExists(atPath: path)
}
func loadImage(at path: String) -> UIImage? {
let tempPath = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
let imagePath = "\(tempPath)\(path.trimmingCharacters(in: .whitespacesAndNewlines))"
guard fileExists(at: imagePath) else { return nil }
guard let image = UIImage(contentsOfFile: imagePath) else { return nil }
return image
}
You need split filename and extension filename.
If you use main bundle. you can follow this code
let stringPath = Bundle.main.path(forResource: "your_filename", ofType: "txt")
let urlPath = Bundle.main.url(forResource: "your_filename", withExtension: "txt")
or you can use my code.
func readConfigFromBundle(fileExtension: String) -> TCBConfigure? {
let bundle = Bundle.main
if let resPath = bundle.resourcePath {
do {
let dirContents = try FileManager.default.contentsOfDirectory(atPath: resPath)
let filteredFiles = dirContents.filter { $0.contains(fileExtension) }
for fileName in filteredFiles {
let sourceURL = bundle.bundleURL.appendingPathComponent(fileName)
let data: NSData? = NSData.init(contentsOf: sourceURL)
if let fileData = data {
// implement your logic
}
}
} catch {
// implement when error
}
}
return nil
}
I am writing and reading files from my iOS application:
func saveChecklistItems() {
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWith: data)
archiver.encode(items, forKey: "ChecklistItems")
archiver.finishEncoding()
print("save \(dataFileDir())")
data.write(toFile: dataFileDir(), atomically: true)
}
func loadChecklistItems() {
print("load \(dataFileDir())")
let path = dataFileDir()
if FileManager.default.fileExists(atPath: path) {
if let data = NSData.init(contentsOfFile: path) {
do {
let unarchiver = try NSKeyedUnarchiver(forReadingFrom: data as Data)
items = unarchiver.decodeObject(forKey: "ChecklistItems") as! [CheckListItem]
unarchiver.finishDecoding()
} catch {
print(error)
}
}
}
}
func documentsDirectory() -> String {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as [String]
return paths[0]
}
func dataFileDir() -> String {
return documentsDirectory().appending("Checklists.plist")
}
But every time I press Run in Xcode application is deployed on my iPhone with new ID:
save
/var/mobile/Containers/Data/Application/4684F231-7A41-461C-AD5C-FB0F66A9DA31/DocumentsChecklists.plist
load
/var/mobile/Containers/Data/Application/CFE42D59-6F3F-4D47-96B5-4F81C640127A/DocumentsChecklists.plist
It is my first steps in iOS development and I am wondering what do I need to do in this case ? Set application id as static ? Or something else ?
I would recommend that you use URL instead of strings when working with folders and files
func documentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
func dataFileDir() -> URL {
return documentsDirectory().appendingPathComponent("Checklists.plist")
}
and change your write to
try data.write(to: dataFileDir(), options: .atomic)
Using Alamofire for downloading. File is being downloaded in app document folder, in attachment, there is .hub file.
I need to change .hub file to .zip than i need to unzip this file for audio file.
Code for downloading ->
func getAudioFileFromServer(url: String, uuid: String) {
let fileURL = URL(string: url)
var request = URLRequest(url:fileURL!)
request.setValue("myapikey", forHTTPHeaderField: "x-api-key")
let destination = DownloadRequest.suggestedDownloadDestination()
Alamofire.download(request, to: destination).validate().responseData { response in
debugPrint(response)
print(response.destinationURL!)
}
}
Response from server ->
file:///var/mobile/Containers/Data/Application/FC5F17C4-E8D3-4406-926A-97EB9447D87B/Documents/'bac6151ffbe74140a31408938c91fa33.hub'
To rename a file, use the function moveItem(atPath:toPath:) of FileManager
To unzip, the easiest way would be to integrate some zip library: https://github.com/ZipArchive/ZipArchive or https://github.com/marmelroy/Zip
After downloading the file change the extension to .zip with "saveFileInDirectory" method and with success block we can get the main file.
self.saveFileInDirectory(data: responce.result.value, fileName: "\(name!).zip", successblock: { (path) in
print(path!)
var filepath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
let url = URL(fileURLWithPath: filepath)
do {
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil)
let done = SSZipArchive.unzipFile(atPath: path!, toDestination: url.path)
if done{
let items = try FileManager.default.contentsOfDirectory(atPath: url.path)
print(items)
let destinationUrl = url.appendingPathComponent(items[0])
print(destinationUrl)
}
} catch let error as NSError{
print(error)
}
})
func saveFileInDirectory(data: Data?, fileName: String?, successblock: #escaping (_ path: String?) -> Void) { // To add the image to cache for given identifier.
let paths = NSSearchPathForDirectoriesInDomains( .documentDirectory, .userDomainMask, true)[0] as String
let path = paths.appending("/\(fileName!)")
if (FileManager.default.fileExists(atPath: path)) {
try! FileManager.default.removeItem(atPath: path)
} else {
do {
try data?.write(to: URL(fileURLWithPath: path, isDirectory: false))
successblock(path)
} catch {
successblock(nil)
print("Error while caching the data in cache folder.")
}
}}
The cache.db-wal file have the sensitive information in my application.Need to remove the cache files in Caches directory.
This should work for you.
let fileManager = NSFileManager.defaultManager()
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask).first! as NSURL
let documentsPath = documentsUrl.path
do {
if let documentPath = documentsPath
{
let fileNames = try fileManager.contentsOfDirectoryAtPath("\(documentPath)")
for fileName in fileNames {
if (fileName == "cache.db-wal")
{
let filePathName = "\(documentPath)/\(fileName)"
try fileManager.removeItemAtPath(filePathName)
}
}
let files = try fileManager.contentsOfDirectoryAtPath("\(documentPath)")
}
} catch {
print("Could not clear: \(error)")
}
In swift 3.1
** In my case i wanted to remove JSON data through the folder so i named it as "fsCachedData" you can change name accordingly..**
func removeCache() {
let caches = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0])
let appId = Bundle.main.infoDictionary!["CFBundleIdentifier"] as! String
let path = String(format:"%#/%#/Cache.db-wal",caches, appId)
do {
try FileManager.default.removeItem(atPath: path)
} catch {
print("ERROR DESCRIPTION: \(error)")
}
}
swift 4
func removeNetworkDictionaryCache() {
let caches = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0])
let appId = Bundle.main.infoDictionary!["CFBundleIdentifier"] as! String
let path = String(format:"%#/%#/Cache.db-wal",caches, appId)
do {
try FileManager.default.removeItem(atPath: path)
} catch {
print("ERROR DESCRIPTION: \(error)")
}
}
In Swift 5.2 solution (Just converted code)
func removeNetworkCache() {
let caches = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0])
let appId = Bundle.main.infoDictionary!["CFBundleIdentifier"] as! String
let path = String(format:"%#/%#/Cache.db-wal",caches, appId)
do {
try FileManager.default.removeItem(atPath: path)
} catch {
print("ERROR DESCRIPTION: \(error)")
}
}
I could find a solution for the above question.
func removeNetworkCache() {
let caches = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0])
let appId = NSBundle.mainBundle().infoDictionary!["CFBundleIdentifier"] as! String
let path = String(format:"%#/%#/Cache.db-wal",caches, appId)
do {
try NSFileManager.defaultManager().removeItemAtPath(path)
} catch {
print("ERROR DESCRIPTION: \(error)")
}
}
I am downloading file from firebase. let say the request url is following
social-cam-storage/albm-72/owner-2/1484043313786.jpeg
i can download the file using the following code
func downloadFile(url : String) {
let storageR = FIRStorage.storage().reference(withPath: url)
let maxSize : Int64 = 3 * 1024 * 1024 // 3MB
storageR.data(withMaxSize: maxSize) { (data, error) in
if error != nil {
print(error.debugDescription)
return
}
print(data!)
}
}
Now i need to store this data maintaining the directory structure of the url
I have tried
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
print(FileManager.default.createFile(atPath: "\(documentsURL.absoluteString)/\(url)", contents: data!, attributes: nil))
but i am getting false
so how to fix this or is there any other way to save??
Have you tried something like this? :
If you have the exact path already as a string:
try? data.write(to: URL(fileURLWithPath: path), options: [.atomic])
If you need the path there are a few methods:
func saveFile() {
let filePath = getDocumentsURL().absoluteString.appending(path)
try? data.write(to: URL(fileURLWithPath: filePath), options: [.atomic])
}
func getDocumentsURL() -> URL {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
return documentsURL
}
You could also just try saving the filename, and then loading later when you need it:
func fileInDocumentsDirectory(_ filename: String) -> String {
let fileURL = getDocumentsURL().appendingPathComponent(filename)
return fileURL.path
}
// To save file
func saveFile(data: Data) {
let fileName:String = "createUniqueFileName"
let filePath = fileInDocumentsDirectory(fileName)
saveData(data, filePath)
}
// To load file with saved file name
func loadFile(fileName: String) {
if let loadedData = loadData(fileName) {
// Handle data however you wish
}
}
func saveData(_ data: Data, path: String ) {
try? data.write(to: URL(fileURLWithPath: path), options: [.atomic])
}
func loadData(_ path: String) -> Data? {
let data:Data? = try? Data(contentsOf: URL(fileURLWithPath: path))
return data
}
Have you tried using the built in "download to file" API in Firebase Storage?
// Create a reference to the file you want to download
let fileURL = storage.reference(withPath: url)
// Create local filesystem URL
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileURL = ...
// Download to the local filesystem
let downloadTask = islandRef.write(toFile: fileURL) { url, error in
if let error = error {
// Uh-oh, an error occurred!
} else {
// Local file URL is returned
}
}