2012-01-04 66 views

回答

1

我想通了使用這些腳本: http://www.cocoadev.com/index.pl?BzipValueTransformer & http://www.cocoadev.com/index.pl?NSDataPlusBzip

你需要做的是包括libbz2.a我在/opt/local/lib/libbz2.a中找到,因爲我使用macports安裝了bzip2。我draged文件到我的項目和寫的代碼:

- (void)bunzip 
{ 
    NSString *path = @"/var/log/kernel.log.1.bz2"; 

    NSData *data = [NSData dataWithContentsOfFile:path]; 

    NSString *dataString = [[Bzip2ValueTransformer alloc] transformedValue:data]; 

    NSLog(@"Data: %@", dataString); 
} 

結果:

Dec 16 23:26:58 Paul-Peelens-MacBook-Pro kernel[0]: wlEvent: en1 en1 Link UP 
Dec 16 23:26:58 Paul-Peelens-MacBook-Pro kernel[0]: AirPort: Link Up on en1 
Dec 16 23:26:58 Paul-Peelens-MacBook-Pro kernel[0]: en1: BSSID changed to 32:7b:98:82:9d:7f 
Dec 16 23:30:10 paul-peelens-macbook-pro newsyslog[13862]: logfile turned over due to size>1000K 
0

我會使用NSTask或my own PRHTask打開一個bunzip2進程,其標準輸入連接到bzip2文件,並將其標準輸出設置爲管道。使用NSFileHandle的異步讀取或GCD的dispatch_source的數據來自在後臺從這個管道讀取