使用SSZipArchive解压缩文件-Swift [英] Unzipping files with SSZipArchive - Swift

查看:82
本文介绍了使用SSZipArchive解压缩文件-Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SSZipArchive框架解压缩文件.

I'm trying to unzip a file using the SSZipArchive framework.

let unzipper = SSZipArchive.unzipFileAtPath(String(document), toDestination: String(documentsUrl))

这是我目前正在尝试解压缩文件的路径,这是文件的路径:

This is what I'm trying at the moment to unzip the file, and here is the path of the file:

unzipFileAtPath-路径中的文档:file:///private/var/mobile/Containers/Data/Application/94ADDB12-78A2-4798-856D-0626C41B7AC2/Documents/tSOUTrIayb.zip错误

我正在尝试将其解压缩到以下路径:

And I am trying to unzip it to this path:

NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!

不幸的是,它似乎无法正常工作,每次都没有新内容被保存到我正在打印的documents目录中.我还打印了'unzipper'变量,该变量只打印 false ,无论什么意思.

Unfortunately it doesn't seem to be working, each time there is nothing new being saved into the documents directory which I am printing out. I also print the 'unzipper' variable which just prints false, whatever that means.

我找不到该框架的任何文档,所以我不确定如何使它正常工作

I can't find any documentation for the framework so I'm not entirely sure how to get this working

推荐答案

假设您已经实现了所有功能,然后只需几个步骤即可提供解决方案

Assume that you have implement all things, then also providing solution in few steps

  1. 从演示中拖动 SSZipArchive 文件夹,您可以从此处a>.

  1. Drag SSZipArchive folder from the demo which you can download from here.

yourProjectname-Bridging-Header.h 中编写 #import"SSZipArchive.h" (如果已有的话),否则请创建如上所述的新头文件.

Write #import "SSZipArchive.h" in yourProjectname-Bridging-Header.h if you already have. Otherwise create new header file named as mention above.

如果要使用委托方法,请将委托设置为类

If you want to use delegate methods set delegate to class

class YourViewController: UIViewController, SSZipArchiveDelegate {.......

  • 为创建zip表单文件夹,我已将示例文件夹添加到项目(主捆绑包)

  • For creating zip form folder I have added sample folder to project (Main bundle)

    首先在文档目录中创建要保存zip文件的文件夹

    First create folder in document directory where you want to save your zip file

    var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
    let documentsDir = paths[0]
    let zipPath =  documentsDir.stringByAppendingString("/MyZipFiles") // My folder name in document directory
    
    let fileManager = NSFileManager.defaultManager()
    
    let success = fileManager.fileExistsAtPath(zipPath) as Bool
    
    if success == false {
    
        do {
    
            try! fileManager.createDirectoryAtPath(zipPath, withIntermediateDirectories: true, attributes: nil)
        }
    }
    

  • 现在创建zip文件

  • Now create zip file

    var inputPath = NSBundle.mainBundle().resourcePath
    inputPath = inputPath!.stringByAppendingString("/Sample") // My folder which already put into project
    
    let archivePath = zipPath.stringByAppendingString("/Demo.zip") // Sample folder is going to zip with name Demo.zip
    SSZipArchive.createZipFileAtPath(archivePath, withContentsOfDirectory:inputPath)
    

  • 用于在文档目录(要保存的位置)中解压缩文件创建文件夹

  • For unzipping file create folder in document directory (Where you want to save)

    let destPath = zipPath.stringByAppendingString("/Hello")
    let fileManager = NSFileManager.defaultManager()
    
    let success = fileManager.fileExistsAtPath(destPath) as Bool
    
    if success == false {
    
        do {
    
            try! fileManager.createDirectoryAtPath(destPath, withIntermediateDirectories: true, attributes: nil)
        }
    }
    

  • 解压缩文件夹

  • Unzip folder

    SSZipArchive.unzipFileAtPath(archivePath, toDestination:destPath, delegate:self)
    

  • 打印您的路径,然后可以检查您的文件将被保存

  • Print your path, and you can check there your file will be saved

    print(zipPath)
    

  • 这篇关于使用SSZipArchive解压缩文件-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆