2011-03-31 57 views
2

我正在創建一個應用程序來解壓zip文件而不提示。我在Vmaware中使用MAC OS 10.5映像。解壓縮無提示

我是可可程序中的新人。我搜索關於該幾個代碼,但它不是我working..I沒有得到任何錯誤或警告消息。請請你告訴我它的正確的解決方案.. 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]; 

-Thanks -with的問候!

+0

+1最後的評論 – Till 2011-03-31 12:03:21

回答

6

我把你的代碼和改變了一些選項。你已經指定了一切的完整路徑。

#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; 
} 
+0

非常感謝黑蛙它現在工作。我需要用這些代碼找出我的錯誤。 -問候 – t3rmin4t0r 2011-04-04 04:54:14

0

不知道這是你唯一的問題,但你需要在你的路徑展開波浪號(~)。通常這是由shell完成的,但如果你不使用shell,你必須自己做。幸運的是,有一個NSString方法可以做到這一點,所以你可以做[@"~/Desktop/demos.zip" stringByExpandingTildeInPath][@"~/Desktop" stringByExpandingTildeInPath]

+0

感謝mipadi,我試過你的建議,但它仍然無法正常工作..文件不是解壓縮。任何其他建議你會喜歡嗎?在此先感謝..-問候 – t3rmin4t0r 2011-04-01 05:10:13