objective-c - 不提示解压

标签 objective-c xcode macos

我正在创建一个应用程序来在没有提示的情况下解压缩 zip 文件。我在 Vmaware 中使用 MAC OS 10.5 镜像。

我是 cocoa 编程的新手。我搜索了一些关于此的代码,但它不起作用..我没有收到任何错误或警告消息..请告诉我它的正确解决方案.. Zip 文件未解压,我的应用程序在一段时间后关闭。我想在不提示和使用任何第三方存档应用程序的情况下解压缩文件。

我想将我的文件解压缩到其 ZIP 文件的同一位置。

这是我的代码:

/* Assumes sourcePath and targetPath are both
   valid, standardized paths. */

// Create the zip task
NSTask * backupTask = [[NSTask alloc] init];
[backupTask setLaunchPath:@"/usr/bin/ditto"];
[backupTask setArguments:
    [NSArray arrayWithObjects:@"-c", @"-k", @"-X", @"--rsrc", 
    @"~/Desktop/demos.zip", @"~/Desktop", nil]];

// Launch it and wait for execution
[backupTask launch];
[backupTask waitUntilExit];

// Handle the task's termination status
if ([backupTask terminationStatus] != 0)
    NSLog(@"Sorry, didn't work.");

// You *did* remember to wash behind your ears ...
// ... right?
[backupTask release];

-谢谢 -问候!

最佳答案

我使用了您的代码并更改了一些选项。您已指定所有内容的完整路径。

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// Need to use the full path to everything
// In this example, I am using my Downloads directory
NSString *destination = [@"~/Downloads" stringByExpandingTildeInPath];
NSString *zipFile = [@"~/Downloads/demos.zip" stringByExpandingTildeInPath];


NSTask *unzip = [[NSTask alloc] init];
[unzip setLaunchPath:@"/usr/bin/unzip"];
[unzip setArguments:[NSArray arrayWithObjects:@"-u", @"-d", 
                     destination, zipFile, nil]];

NSPipe *aPipe = [[NSPipe alloc] init];
[unzip setStandardOutput:aPipe];

[unzip launch];
[unzip waitUntilExit];
[unzip release];


// You can get rid of all the NSLog once you have finish testing
NSData *outputData = [[aPipe fileHandleForReading] readDataToEndOfFile];
NSString *outputString = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding];

NSLog(@"Zip File: %@", zipFile);
NSLog(@"Destination: %@", destination);
NSLog(@"Pipe: %@", outputString);
NSLog(@"------------- Finish -----------");
[outputString release];


[pool release];
return 0;
}

关于objective-c - 不提示解压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5499110/

相关文章:

iphone - 在不中断栏按钮项目的情况下为 iOS 7 中的 ui 导航栏设置色调颜色

iOS 8 : -[UITableViewWrapperView textField]: unrecognized selector sent to instance

iOS 不加载下一级

macos - 在 SwiftUI 中禁用 macOS 对焦环

objective-c - 我是否需要释放 sortedArrayUsingDescriptors 返回的 NSArray?

ios - 如何从两个不同的数组中找到共同的对象并打印另一个对象?

c++ - CPP - 编译错误 (G++)

xcode - clang 前端命令失败,退出代码为 70(使用 -v 查看调用)

ios - 需要什么以及如何在 Mac OSX 上安装 zucchini 框架?

bash - 来源来自 Standard In(OSX 上的 Bash)