Sample code for 30+ languages & platforms
Objective-C

Transition from Compression.BeginCompressBytes to FirstChunk/LastChunk

Provides instructions for replacing deprecated BeginCompressBytes method calls with using FirstChunk and LastChunk properties.

Chilkat Objective-C Downloads

Objective-C
#import <CkoFileAccess.h>
#import <CkoCompression.h>

BOOL success = NO;

CkoFileAccess *facSrc = [[CkoFileAccess alloc] init];
CkoFileAccess *facDest = [[CkoFileAccess alloc] init];

// Open a large binary file for reading.
success = [facSrc OpenForRead: @"qa_data/bmp/big.bmp"];
if (success == NO) {
    NSLog(@"%@",facSrc.LastErrorText);
    return;
}

// If we compress in 64K chunks, find out how many blocks there will be.
int blockSize = 65536;
int numBlocks = [[facSrc GetNumBlocks: [NSNumber numberWithInt: blockSize]] intValue];

// Open an output file for the compressed data.
success = [facDest OpenForWrite: @"c:/temp/qa_output/compressedBmp.dat"];
if (success == NO) {
    NSLog(@"%@",facDest.LastErrorText);
    return;
}

CkoCompression *compress = [[CkoCompression alloc] init];
compress.Algorithm = @"deflate";

NSData fileBytes;
NSData compressedBytes;

// ----------------------------------------------------------------------------------
// This is the deprecated way of compressing in chunks using Begin/More/End methods..
// ----------------------------------------------------------------------------------
int i = 0;
while (i < numBlocks) {
    fileBytes = [facSrc ReadBlock: [NSNumber numberWithInt: i] blockSize: [NSNumber numberWithInt: blockSize]];
    if (i == 0) {
        compressedBytes = [compress BeginCompressBytes: fileBytes];
    }
    else {
        compressedBytes = [compress MoreCompressBytes: fileBytes];
    }

    [facDest FileWrite: compressedBytes];

    i = i + 1;
}

// At the very end, flush any remaining compressed bytes, if any.
compressedBytes = [compress EndCompressBytes];
[facDest FileWrite: compressedBytes];

[facSrc FileClose];
[facDest FileClose];

NSLog(@"%@",@"Finished compressing file.");

// ----------------------------------------------------------------------------------
// This is new way of compressing in chunks using FirstChunk and LastChunk properties
// ----------------------------------------------------------------------------------

success = [facSrc OpenForRead: @"qa_data/bmp/big.bmp"];
if (success == NO) {
    NSLog(@"%@",facSrc.LastErrorText);
    return;
}

success = [facDest OpenForWrite: @"c:/temp/qa_output/compressedBmp2.dat"];
if (success == NO) {
    NSLog(@"%@",facDest.LastErrorText);
    return;
}

// Assuming numBlocks > 1
compress.FirstChunk = YES;
compress.LastChunk = NO;

i = 0;
while (i < numBlocks) {
    fileBytes = [facSrc ReadBlock: [NSNumber numberWithInt: i] blockSize: [NSNumber numberWithInt: blockSize]];
    compressedBytes = [compress CompressBytes: fileBytes];

    [facDest FileWrite: compressedBytes];

    i = i + 1;

    compress.FirstChunk = NO;
    if (i == (numBlocks - 1)) {
        compress.LastChunk = YES;
    }

}

[facSrc FileClose];
[facDest FileClose];

NSLog(@"%@",@"Finished compressing file 2.");