Search code examples
objective-ccocoacompressionosx-lionbzip2

Read log file from bz2 file in objective-c


I would like to read the contents of a logfile that has been rotated to a bz2 file. How can I do this while using as little memory as possible?


Solution

  • I just figured it out using these scripts: http://www.cocoadev.com/index.pl?BzipValueTransformer & http://www.cocoadev.com/index.pl?NSDataPlusBzip

    What you need to do is include the libbz2.a which I found in /opt/local/lib/libbz2.a since I installed bzip2 using macports. I draged the file into my project and wrote the code:

    - (void)bunzip
    {
        NSString *path = @"/var/log/kernel.log.1.bz2";
    
        NSData *data = [NSData dataWithContentsOfFile:path];
    
        NSString *dataString = [[Bzip2ValueTransformer alloc] transformedValue:data];
    
        NSLog(@"Data: %@", dataString);
    }
    

    Result:

    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