1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-13 00:29:14 +00:00
CLK/OSBindings/Mac/Clock SignalTests/NSData+dataWithContentsOfGZippedFile.m
2022-08-08 10:52:55 -04:00

36 lines
778 B
Objective-C

//
// NSData+dataWithContentsOfGZippedFile.m
// Clock SignalTests
//
// Created by Thomas Harte on 08/08/2022.
// Copyright © 2022 Thomas Harte. All rights reserved.
//
#import "NSData+dataWithContentsOfGZippedFile.h"
#include <zlib.h>
@implementation NSData (dataWithContentsOfGZippedFile)
+ (instancetype)dataWithContentsOfGZippedFile:(NSString *)path {
gzFile compressedFile = gzopen([path UTF8String], "rb");
if(!compressedFile) {
return nil;
}
NSMutableData *data = [[NSMutableData alloc] init];
uint8_t buffer[64 * 1024];
while(true) {
int length = gzread(compressedFile, buffer, sizeof(buffer));
if(!length) break;
[data appendBytes:buffer length:length];
if(length != sizeof(buffer)) break;
}
gzclose(compressedFile);
return data;
}
@end