2011-03-31 8 views
2

메시지를 표시하지 않고 압축을 해제하는 응용 프로그램을 만듭니다. Vmaware에서 MAC OS 10.5 이미지를 사용하고 있습니다.프롬프트없이 압축 해제

저는 코코아 프로그래밍에서 새로운입니다. 이 문제에 관한 몇 가지 코드를 검색했지만 작동하지 않습니다 ... 오류 또는 경고 메시지가 나타나지 않습니다. 친절하게도 정확한 해결책을 말해주십시오. Zip 파일 압축 풀기가 아니며 시간이 지나면 응용 프로그램이 닫힙니다. 제 3 자 아카이브 응용 프로그램을 묻지 않고 파일을 압축 해제하고 싶습니다.

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]; 

고마워요 가진 - 관련 :

여기 내 코드입니다!

+0

+1 – Till

답변

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

고마워요. Black Frog 이제 효과가있었습니다. 이 코드로 실수를 찾아야합니다. -문안 인사 – t3rmin4t0r

0

유일한 문제인지는 확실치 않지만 경로에서 물결표 (~)를 확장해야합니다. 일반적으로 이것은 쉘에 의해 수행되지만 쉘을 사용하지 않는다면 직접해야합니다. 다행히도이를 수행하는 방법은 NSString이므로 [@"~/Desktop/demos.zip" stringByExpandingTildeInPath][@"~/Desktop" stringByExpandingTildeInPath]을 사용할 수 있습니다.

+0

감사합니다 mipadi, 나는 당신의 제안을 시도했지만 여전히 작동하지 않습니다. 파일 압축이 풀리지 않았습니다. 당신이나 다른 사람이 선호하는 다른 제안이 있습니까? 미리 감사드립니다 .-- 안부 – t3rmin4t0r