native zlib inflate/deflate for swift3 on iOS
I just recently had to add that exact library and file to my project, and after a lot of troubleshooting finally got it working, so let me walk you through the steps!
Okay
1) Go to the top level directory of your project in finder, and create a new folder called Swiftzlib
or whatever you want the name of the module that you will be importing to be. (What we will do is add the zlib library as a module, so think of it as importing Foundation or some such other module). To clarify, this Swiftzlib
directory will end up as a child directory of the same directory that contains your *.xcodeproj
and *.xcworkspace
files.
2) Inside the folder you created, make two files.
- include.h
- module.modulemap
3) In your include.h
file, enter the following:
#include<zlib.h>
4) In your module.modulemap
file, enter the following:
module Swiftzlib [system] {
header "include.h"
export *
}
Where Swiftzlib is the same as the name of the folder that you created.
5) Open your Xcode project, and select your target
- 5a) In Build Phases -> Link Binary with Libraries, add
libz.tbd
- 5b) In Build Settings -> Swift Compiler - Search Paths, add
$(PROJECT_DIR)/Swiftzlib
non-recursively to the import paths - 5c) In Build Settings -> Other Linker Flags, add
-lz
as a flag
6) Select your project in Xcode (may not be necessary, but I've done it in my project and it works)
- 6a) In Build Settings -> Swift Compiler - Search Paths, add
$(PROJECT_DIR)/Swiftzlib
non-recursively to the import paths
7) In Data+Gzip.swfit
, add import Swiftzlib
to the top of the file
8) Clean, Build, and Run!
Native Swift implementation of DEFLATE (unzip) algorithm
There is a native swift implementation of zlib on github called DeflateSwift
Zlib compression between Swift and Go
I'm not familiar with Swift but the Go code returns a zlib (RFC 1950) compressed string while according to the documentation from Apple the Swift code should at most return a deflate compressed string (RFC 1951), i.e. like the zlib compressed but without the 2 byte zlib header.
With that knowledge the string returned from the Go code can be properly decompressed while the string returned from Swift can not. The difference in size is also obvious which lets me assume that something was cut off. It looks like you treat the compressed data destinationBuffer
as a cString
, which means that any \0
-byte inside the string will be treated as the end of the string. It is likely that such \0
byte exists as the result of compression and that the string got cut off there, i.e. that your output shows only part of the real destinationBuffer
. (EDIT: the last part does not apply anymore since the OP changed the question with a proper result).
Is there a practical way to compress NSData?
Yes, compress the data with zlib.
@Brad Larson posted on this: see here and added the code as well.
There is a CocoaPod which uses Objective-Zip by flyingdolphinstudio.
Related Topics
How to Set the Rootviewcontroller with Swift, iOS 7
How to Get 18-Digit Current Timestamp in Swift
Custom Font on Uibutton Title Clipped on Top of Word
Error Domain=Nsposixerrordomain Code=100 "Protocol Error"
Does iPhone Support Hardware-Accelerated Aes Encryption
Get Device Current Orientation (App Extension)
Checking Push Notification Registration: Isregisteredforremotenotifications Not Updating
Ignore Manual Entries from Apple Health App as Data Source
How to Get Assets.Xcassets File Names in an Array (Or Some Data Structure)
How to Display Uiview Over Keyboard in iOS
Universal Links Not Working on iOS10
How to Unwind Through Multiple Views Without Displaying Intermediate Views
Send Image Along with Other Parameters with Afnetworking
Itms-90809: Deprecated API Usage - Existing App That Use Uiwebview Are No Longer Accepted
Flutter: Disable Swipe to Navigate Back in iOS and Android
iPhone Camera, How to Avoid Cameraoverlay on Preivew View; How to Know When Entering Preview View
How to Create a PDF File Programmatically in an iOS Application