unzipping a file in cocoa

后端 未结 7 1134
长发绾君心
长发绾君心 2020-12-18 01:59

I have a zipped file, which i want to extract the contents of it. What is the exact procedure that i should do to achieve it. Is there any framework to unzip the files in co

相关标签:
7条回答
  • 2020-12-18 02:12

    Here's a more concise version based on codingFriend1's approach

    + (void)unzip {
        NSString *targetFolder = @"/tmp/unzipped";
        NSString* zipPath = @"/path/to/my.zip";
        NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/unzip" arguments:@[@"-o", zipPath, @"-d", targetFolder]];
        [task waitUntilExit];
    }
    

    -d specifies the destination directory, which will be created by unzip if not existent

    -o tells unzip to overwrite existing files (but not to deleted outdated files, so be aware)

    There's no error checking and stuff, but it's an easy and quick solution.

    0 讨论(0)
  • 2020-12-18 02:15

    Try Zip.framework.

    0 讨论(0)
  • 2020-12-18 02:18

    Here's a Swift 4 version similar to Sven Driemecker's answer.

    func unzipFile(at sourcePath: String, to destinationPath: String) -> Bool {
        let process = Process.launchedProcess(launchPath: "/usr/bin/unzip", arguments: ["-o", sourcePath, "-d", destinationPath])
        process.waitUntilExit()
        return process.terminationStatus <= 1
    }
    

    This implementation returns a boolean that determines if the process was successful or not. Is considering the process successful even if warnings were encountered.

    The return condition can be changed to return process.terminationStatus == 0 to not even accept warnings.

    See unzip docs for more details on diagnostics.

    You can also capture the output of the process using a Pipe instance.

    func unzipFile(at sourcePath: String, to destinationPath: String) -> Bool {
        let process = Process()
        let pipe = Pipe()
    
        process.executableURL = URL(fileURLWithPath: "/usr/bin/unzip")
        process.arguments = ["-o", sourcePath, "-d", destinationPath]
        process.standardOutput = pipe
    
        do {
            try process.run()
        } catch {
            return false
        }
    
        let resultData = pipe.fileHandleForReading.readDataToEndOfFile()
        let result = String (data: resultData, encoding: .utf8) ?? ""
        print(result)
    
        return process.terminationStatus <= 1
    }
    
    0 讨论(0)
  • On the Mac you can use the built in unzip command line tool using an NSTask:

    - (void) unzip {
        NSFileManager* fm = [NSFileManager defaultManager];
        NSString* zipPath = @"myFile.zip";
    
        NSString* targetFolder = @"/tmp/unzipped"; //this it the parent folder
                                                   //where your zip's content 
                                                   //goes to (must exist)
    
        //create a new empty folder (unzipping will fail if any
        //of the payload files already exist at the target location)
        [fm createDirectoryAtPath:targetFolder withIntermediateDirectories:NO 
                                               attributes:nil error:NULL];
    
    
        //now create a unzip-task
        NSArray *arguments = [NSArray arrayWithObject:zipPath];
        NSTask *unzipTask = [[NSTask alloc] init];
        [unzipTask setLaunchPath:@"/usr/bin/unzip"];
        [unzipTask setCurrentDirectoryPath:targetFolder];
        [unzipTask setArguments:arguments];
        [unzipTask launch];
        [unzipTask waitUntilExit]; //remove this to start the task concurrently
    
    }
    

    That is a quick and dirty solution. In real life you will probably want to do more error checking and have a look at the unzip manpage for fancy arguments.

    0 讨论(0)
  • 2020-12-18 02:25

    -openFile: (NSWorkspace) is the easiest way I know.

    0 讨论(0)
  • 2020-12-18 02:32

    If you are on iOS or don't want to use NSTask or whatever, I recommend my library SSZipArchive.

    Usage:

    NSString *path = @"path_to_your_zip_file";
    NSString *destination = @"path_to_the_folder_where_you_want_it_unzipped";
    [SSZipArchive unzipFileAtPath:path toDestination:destination];
    

    Pretty simple.

    0 讨论(0)
提交回复
热议问题