Represent NSData in String in compact way

Parse.com supports local database, use case is to save file locally first then later upload to server when internet is available.

Offline time maybe weeks.

Native solution as saving data as PFFile (s3) doesn’t work in this case.

Need to find a way to save NSData in String then save as a regular parse db column.

There is a size limit of 128kb for each column.

Good news is my files aren’t too large, one docx is only 86kb.

The problem is if I use NSData.hexstring representation, (same as data.description), the string representation size is quite big, up to 176kb, almost doubled from original 86kb.

Found 2 github lib, (https://github.com/1024jp/NSData-GZIP) and (https://github.com/leemorgan/NSData-Compression), compress ratio aren’t good enough.

original file size: 86069
original string representation size: 172152
size compressedDataLZ4: 163114
size compressedDataLZFSE: 163538
size compressedDataLZMA: 162296
size compressedDataZLIB: 161740

compress size using gzip: 161712

Must be a better way to represent nsdata in string.

For now, this works. At least the docx file can be open after conversion.


let str = NSString(data: self.docData!, encoding: NSWindowsCP1254StringEncoding)
print("original size: \((str as! String).characters.count)");

let data = str?.dataUsingEncoding(NSWindowsCP1254StringEncoding, allowLossyConversion:false )

let tmpPath = NSString(string:"~/tmp/")  .stringByExpandingTildeInPath

let result = data?.writeToFile(tmpPath+"/1.docx", atomically: true)

In fact the base64encoding for NSData is pretty small already. no need to waste time to compress by yourself.

Advertisements

One thought on “Represent NSData in String in compact way

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s