Skip to content

Commit 4bd982f

Browse files
committed
modify gzip logic
1 parent 41cac74 commit 4bd982f

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed

QiniuSDK/Storage/QNUploadInfoReporter.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,8 @@ - (void)innerReport:(NSString *)jsonString token:(NSString *)token {
517517
[request setHTTPMethod:@"POST"];
518518
[request setTimeoutInterval:_config.timeoutInterval];
519519

520-
NSData *reportData = [[NSData dataWithContentsOfFile:_recorderFilePath] qn_gZip];
520+
NSData *reportData = [NSData dataWithContentsOfFile:_recorderFilePath];
521+
reportData = [NSData qn_gZip:reportData];
521522
__block NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
522523
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:reportData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
523524
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

QiniuSDK/Utils/NSData+QNGZip.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ NS_ASSUME_NONNULL_BEGIN
1212

1313
@interface NSData(QNGZip)
1414

15-
- (NSData *)qn_gZip;
15+
+ (NSData *)qn_gZip:(NSData *)data;
1616

17-
- (NSData *)qn_gUnzip;
17+
+ (NSData *)qn_gUnzip:(NSData *)data;
1818

1919
@end
2020

QiniuSDK/Utils/NSData+QNGZip.m

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
@implementation NSData(QNGZip)
1515

16-
- (NSData *)qn_gZip{
16+
+ (NSData *)qn_gZip:(NSData *)data{
1717

18-
if (self.length == 0 || [self qn_isGzippedData]){
19-
return self;
18+
if (data.length == 0 || [self qn_isGzippedData:data]){
19+
return data;
2020
}
2121

2222
z_stream stream;
@@ -25,8 +25,8 @@ - (NSData *)qn_gZip{
2525
stream.zfree = Z_NULL;
2626
stream.total_out = 0;
2727
stream.avail_out = 0;
28-
stream.avail_in = (uint)self.length;
29-
stream.next_in = (Bytef *)(void *)self.bytes;
28+
stream.avail_in = (uint)data.length;
29+
stream.next_in = (Bytef *)(void *)data.bytes;
3030

3131
static const NSUInteger chunkSize = 16384;
3232

@@ -49,26 +49,26 @@ - (NSData *)qn_gZip{
4949
return gzippedData;
5050
}
5151

52-
- (NSData *)qn_gUnzip{
53-
if (self.length == 0 || ![self qn_isGzippedData]){
54-
return self;
52+
+ (NSData *)qn_gUnzip:(NSData *)data{
53+
if (data.length == 0 || ![self qn_isGzippedData:data]){
54+
return data;
5555
}
5656

5757
z_stream stream;
5858
stream.zalloc = Z_NULL;
5959
stream.zfree = Z_NULL;
6060
stream.total_out = 0;
6161
stream.avail_out = 0;
62-
stream.avail_in = (uint)self.length;
63-
stream.next_in = (Bytef *)self.bytes;
62+
stream.avail_in = (uint)data.length;
63+
stream.next_in = (Bytef *)data.bytes;
6464

6565
NSMutableData *gunzippedData = nil;
6666
if (inflateInit2(&stream, 47) == Z_OK) {
6767
int status = Z_OK;
68-
gunzippedData = [NSMutableData dataWithCapacity:self.length * 2];
68+
gunzippedData = [NSMutableData dataWithCapacity:data.length * 2];
6969
while (status == Z_OK) {
7070
if (stream.total_out >= gunzippedData.length) {
71-
gunzippedData.length += self.length / 2;
71+
gunzippedData.length += data.length / 2;
7272
}
7373
stream.next_out = (uint8_t *)gunzippedData.mutableBytes + stream.total_out;
7474
stream.avail_out = (uInt)(gunzippedData.length - stream.total_out);
@@ -84,9 +84,12 @@ - (NSData *)qn_gUnzip{
8484
return gunzippedData;
8585
}
8686

87-
- (BOOL)qn_isGzippedData{
88-
const UInt8 *bytes = (const UInt8 *)self.bytes;
89-
return (self.length >= 2 && bytes[0] == 0x1f && bytes[1] == 0x8b);
87+
+ (BOOL)qn_isGzippedData:(NSData *)data{
88+
if (!data || data.length == 0) {
89+
return false;
90+
}
91+
const UInt8 *bytes = (const UInt8 *)data.bytes;
92+
return (data.length >= 2 && bytes[0] == 0x1f && bytes[1] == 0x8b);
9093
}
9194

9295
@end

QiniuSDKTests/QNGZipTest.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ - (void)tearDown {
2626
- (void)testGZip {
2727

2828
NSData *data = [NSData data];
29-
NSData *gzip = [data qn_gZip];
30-
XCTAssertTrue([gzip isEqualToData:gzip], "pass");
29+
NSData *gzip = [NSData qn_gZip:data];
30+
XCTAssertTrue([data isEqualToData:gzip], "pass");
3131

3232
NSString *string = @"ABCDEFG";
3333
data = [string dataUsingEncoding:NSUTF8StringEncoding];
34-
gzip = [data qn_gZip];
34+
gzip = [NSData qn_gZip:data];
3535

36-
NSData *gUnzip = [gzip qn_gUnzip];
36+
NSData *gUnzip = [NSData qn_gUnzip:gzip];
3737
NSString *stringGUnzip = [[NSString alloc] initWithData:gUnzip encoding:NSUTF8StringEncoding];
3838
XCTAssertTrue([string isEqualToString:stringGUnzip], "pass");
3939

40-
NSData *reGUnzip = [gzip qn_gZip];
41-
XCTAssertTrue([gzip isEqualToData:reGUnzip], "pass");
40+
NSData *reGUnzip = [NSData qn_gUnzip:gUnzip];
41+
XCTAssertTrue([gUnzip isEqualToData:reGUnzip], "pass");
4242
}
4343

4444

0 commit comments

Comments
 (0)