1

I am very new to Xcode and iOS, I have a device, let's call it Brains, that I'm connecting to via Bluetooth LE using an app I built with Swift 4 and Xcode 10 on my iPhone 5, call it Body. Brains is similar to an arduino board, but not exactly. I can connect and get all the data with BLE with no problems, until I tried to get a compressed file filled with json strings. I am receiving the compressed bytes but I can't seem to know what to do next. How can I get the compressed file, decompress it and read the data inside?

I have tried many things from using the Modules: GzipSwift, DataCompression and SSZipArchive

I have used gunzipped(), gunzip() and decompress() but none of them seem to work.

I have read this thread: iOS :: How to decompress .gz file using GZIP Utility? and it say that I have to get all the compressed bytes stream and convert that to NSData and then decompress it, trouble is he's Using objective-c and I cant seem to translate into swift 4.

I'm getting the bytes from the Bluetooth LE characteristic in a [UInt8] array, in this function:

    func received_logs(data: [UInt8]) {

    let data_array_example = [31, 139, 8, 8, 16, 225, 156, 92, 2, 255, 68, 97, 116, 97, 0, 181, 157, 107, 110, 220, 56, 16, 6, 175, 226, 3, 248, 71, 63, 73, 234, 44, 193, 222, 255, 26, 171, 30, 35, 192, 90, 20, 18, 121, 182, 11, 112, 16, 35, 48, 10, 31, 154, 197, 22, 135, 34, 227, 95, 191, 76, 244, 16, 183, 248, 252, 48, 137, 229, 38, 242, 249, 161, 231, 87, 156, 127, 207, 113, 126, 227, 159, 31, 231, 183, 110, 223, 255, 200, 239, 47, 203, 252, 253, 173, 255, 231, 159, 235, 235, 108, 105, 110, 101, 48, 47, 50, 48]

    for data_byte in stride(from: 0, to: data_array_example.count, by: 1) {
        let byte = String(data_array_example[data_byte])
        sourceString = sourceString + byte //getting all the bytes and converting to string to store in a variable
    }
    /******************************************************************/
    let text = sourceBuffer
    do {
        try text.write(to: path!, atomically: false, encoding: String.Encoding.utf8)
    }
    catch {
        print("Failed writing")
    } //dump the var into a txt file
    /**********UPDATED**********/
    var file_array : [UInt8] = []
    let byte2 = NSData(data: data_array_example.data)
    let asc_array = Data(bytes: byte2.data)
    let decompressedData: Data
    do {
        try decompressedData =  asc.gunzipped()
        print("Decom: ", String(data: decompressedData, encoding: .utf8))
    }
    catch {
        print(error) //Gives me the "unknown compression method error"
    }
}

I expect to see the Uncompressed file's contents but I only get:

GzipError(kind: Gzip.GzipError.Kind.data, message: "incorrect header check")

Maybe I'm just making it more complicated than It needs to be. Any help would be greatly appreciated!

Thank you very much :)

UPDATE: I created a .gz file and used the both the gunzipped() and gunzip() functions and both of them worked.

UPDATE:

Tried to directly convert the data to NSData and then gunzip() but now getting the error:

GzipError(kind: Gzip.GzipError.Kind.data, message: "unknown compression method")
  • It sounds like you can receive the file from your device successfully, but since you can't unzip, you can't be sure. Step 1) I would make an iOS app that contains a known good .gz file as a resource (or whatever) and write code to unzip. And test. Now you have a validated unzip operation. Step 2) apply code from step 1 to data obtained from device. – Rob Gorman Mar 27 '19 at 19:05
  • Yep, did that and both gunzip() and gunzipped() worked. My guess is I have to take those compressed bytes and somehow create a "physical" file out of them. Would that be a good guess? – Robogoblin01 Mar 27 '19 at 19:57
  • Yes. I think so. Probably byte array can be converted easily to NSData, then NSData can be converted to file. – Rob Gorman Mar 28 '19 at 03:51
  • I can do the conversion to NSData, but how can I convert that to a file? Does this mean I need to ask another question? – Robogoblin01 Mar 28 '19 at 18:22

2 Answers2

1

The updated example data has a correct gzip header, and so would not be giving you an incorrect header check if you are feeding the data correctly to the gunzipper.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158
0

I solve my issue. Turns out I was miscounting the bytes and some of them were in the wrong order. Thank you guys for your help!