我从网络服务器获得的NSString
(例如eNoLycgsVgChvILSEoWS1IoSPQBFZgb4
)只是使用zlib
压缩的普通字符串。如何在Objective-C中解压缩?我找到了一些库,但它们只接受完整的文件,而不是简单的字符串。
答案 0 :(得分:3)
基于上面的确定,你似乎并不真正知道格式是什么 - 但似乎它是ZLib deflate
,Base64编码。无耻地从其他StackOverflow答案中剔除,这里有一大块代码可以解压缩您的数据:
#import <Foundation/Foundation.h>
#import <zlib.h>
int main( int argc, const char * argv[] )
{
@autoreleasepool
{
NSLog( @"Starting..." );
NSString * base64String = @"eJwLycgsVgChvILSEoWS1IoSPQBFZgb4";
NSData * compressedData = [ [ NSData alloc ] initWithBase64EncodedString: base64String options: 0 ];
NSUInteger full_length = [ compressedData length ];
NSUInteger half_length = [ compressedData length ] / 2;
NSMutableData * decompressedData = [ NSMutableData dataWithLength: full_length + half_length ];
BOOL done = NO;
int status;
z_stream strm;
strm.next_in = ( Bytef * )[ compressedData bytes ];
strm.avail_in = ( unsigned int ) full_length;
strm.total_out = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
if ( inflateInit2( &strm, ( 15 + 32 ) ) != Z_OK )
{
NSLog( @"Could not initialise ZLib" );
return EXIT_FAILURE;
}
while ( done == NO )
{
// Make sure we have enough room and reset the lengths.
if ( strm.total_out >= [ decompressedData length ] )
{
[ decompressedData increaseLengthBy: half_length ];
}
strm.next_out = [ decompressedData mutableBytes ] + strm.total_out;
strm.avail_out = ( unsigned int )( [ decompressedData length ] - strm.total_out );
// Inflate another chunk.
status = inflate( &strm, Z_SYNC_FLUSH );
if ( status == Z_STREAM_END )
{
done = YES;
}
else if ( status != Z_OK )
{
NSLog( @"Decompression failed with status %i", status );
break;
}
}
if ( inflateEnd( &strm ) != Z_OK )
{
NSLog( @"Could not complete decompression" );
return EXIT_FAILURE;
}
// Set real length.
if ( done )
{
[ decompressedData setLength: strm.total_out ];
NSString * string = [ [ NSString alloc ] initWithData: decompressedData
encoding: NSUTF8StringEncoding ];
NSLog( @"String: %@", string );
}
else
{
return EXIT_FAILURE;
}
}
NSLog( @"...Success" );
return EXIT_SUCCESS;
}
运行时,会显示如下内容:
2016-06-16 12:40:14.454 Objective Z[82569:7532295] Starting...
2016-06-16 12:40:14.455 Objective Z[82569:7532295] String: This is input text.
2016-06-16 12:40:14.455 Objective Z[82569:7532295] ...Success
Program ended with exit code: 0
您需要确保链接ZLib。从XCode项目的Build Phases部分,在“Link Binary With Libraries”下,“+”按钮提供一个可搜索的弹出窗口;寻找“libz”。这可能不会出现在iOS上;它出现在OS X SDK中;这超出了这个答案的范围,如果你遇到麻烦,谷歌无疑会迅速提出答案。