-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSData+ZipCompression.m
More file actions
135 lines (115 loc) · 3.73 KB
/
Copy pathNSData+ZipCompression.m
File metadata and controls
135 lines (115 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// NSData+ZipCompression.m
// CFXKit
//
// Created by Aurélien Hugelé on 18/11/2019.
// Copyright © 2019 Carrafix. All rights reserved.
//
#import "NSData+ZipCompression.h"
// for GZIP support
#import <zlib.h>
#import <dlfcn.h>
#pragma clang diagnostic ignored "-Wcast-qual"
@implementation NSData (ZipCompression)
static void *libzOpen()
{
static void *libz;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
libz = dlopen("/usr/lib/libz.dylib", RTLD_LAZY);
});
return libz;
}
- (NSData *)gzippedDataWithCompressionLevel:(float)level
{
if (self.length == 0 || [self isGzippedData])
{
return self;
}
void *libz = libzOpen();
int (*deflateInit2_)(z_streamp, int, int, int, int, int, const char *, int) =
(int (*)(z_streamp, int, int, int, int, int, const char *, int))dlsym(libz, "deflateInit2_");
int (*deflate)(z_streamp, int) = (int (*)(z_streamp, int))dlsym(libz, "deflate");
int (*deflateEnd)(z_streamp) = (int (*)(z_streamp))dlsym(libz, "deflateEnd");
z_stream stream;
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;
stream.avail_in = (uint)self.length;
stream.next_in = (Bytef *)(void *)self.bytes;
stream.total_out = 0;
stream.avail_out = 0;
static const NSUInteger ChunkSize = 16384;
NSMutableData *output = nil;
int compression = (level < 0.0f)? Z_DEFAULT_COMPRESSION: (int)(roundf(level * 9));
if (deflateInit2(&stream, compression, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY) == Z_OK)
{
output = [NSMutableData dataWithLength:ChunkSize];
while (stream.avail_out == 0)
{
if (stream.total_out >= output.length)
{
output.length += ChunkSize;
}
stream.next_out = (uint8_t *)output.mutableBytes + stream.total_out;
stream.avail_out = (uInt)(output.length - stream.total_out);
deflate(&stream, Z_FINISH);
}
deflateEnd(&stream);
output.length = stream.total_out;
}
return output;
}
- (NSData *)gzippedData
{
return [self gzippedDataWithCompressionLevel:-1.0f];
}
- (NSData *)gunzippedData
{
if (self.length == 0 || ![self isGzippedData])
{
return self;
}
void *libz = libzOpen();
int (*inflateInit2_)(z_streamp, int, const char *, int) =
(int (*)(z_streamp, int, const char *, int))dlsym(libz, "inflateInit2_");
int (*inflate)(z_streamp, int) = (int (*)(z_streamp, int))dlsym(libz, "inflate");
int (*inflateEnd)(z_streamp) = (int (*)(z_streamp))dlsym(libz, "inflateEnd");
z_stream stream;
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.avail_in = (uint)self.length;
stream.next_in = (Bytef *)self.bytes;
stream.total_out = 0;
stream.avail_out = 0;
NSMutableData *output = nil;
if (inflateInit2(&stream, 47) == Z_OK)
{
int status = Z_OK;
output = [NSMutableData dataWithCapacity:self.length * 2];
while (status == Z_OK)
{
if (stream.total_out >= output.length)
{
output.length += self.length / 2;
}
stream.next_out = (uint8_t *)output.mutableBytes + stream.total_out;
stream.avail_out = (uInt)(output.length - stream.total_out);
status = inflate (&stream, Z_SYNC_FLUSH);
}
if (inflateEnd(&stream) == Z_OK)
{
if (status == Z_STREAM_END)
{
output.length = stream.total_out;
}
}
}
return output;
}
- (BOOL)isGzippedData
{
const UInt8 *bytes = (const UInt8 *)self.bytes;
return (self.length >= 2 && bytes[0] == 0x1f && bytes[1] == 0x8b);
}
@end