在Objective-c端提取从c#发送的Zip文件的字节数组

本文关键字:Zip 文件 字节 数组 字节数 Objective-c 提取 | 更新日期: 2023-09-27 17:53:10

在Objective-c中通过Plugin从c#传递Zip文件的字节数组,我想在Objective-c方面解冻,但我们正在遭受不顺利。使用Zlib转换为NSData类型的字节数据到以下URL引用它试图解冻,但膨胀(&流,Z_NO_FLUSH);的回顾一下我们的返回值,应该返回的是Z_DATA_ERROR,即确定输入了无法被zlib解压的数据不知道原因,请告诉我是否有知心人

■URLhttps://gist.github.com/hacha/9207616

http://www.zlib.net/zlib_how.html


源代码里(c#)

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class CallPlugin : MonoBehaviour {
    [DllImport("__Internal")]
    private static extern void uncompressByGzip(byte[] zipbytearray,int ziplen);

    public void CallMain(byte[] unZipStream,int unZipLength){
        uncompressByGzip (unZipStream,unZipLength);
        Debug.Log ("Test CSharp");
    }
}

源代码里(objective - c)

#import <Foundation/Foundation.h>
#include <zlib.h>

extern "C"{
    void uncompressByGzip(const char** ptrSrc, const int srcLength);
}

void uncompressByGzip(const char** ptrSrc, const int srcLength)
{
    NSData *source = [NSData dataWithBytes:(const void *)ptrSrc length:(sizeof(unsigned char) * srcLength)];
    NSString* str = [NSString stringWithFormat:@"%d",srcLength];
    NSLog(@"src = %@",str);
    if (source.length == 0)
        return;
    NSLog(@"11111");
    z_stream stream;
    stream.zalloc = Z_NULL;
    stream.zfree = Z_NULL;
    stream.opaque = Z_NULL;
    stream.avail_in = (uInt)source.length;
    stream.next_in = (Bytef *)source.bytes;
    stream.total_out = 0;
    stream.avail_out = 0;
    NSLog(@"22222");
    if (inflateInit2(&stream, 31) != Z_OK)
        return;
    NSLog(@"33333");
    NSMutableData *FileSystemData = [NSMutableData dataWithCapacity:0];
    while (stream.avail_out == 0) {
        NSLog(@"12345");
        Bytef buffer[16384];
        stream.next_out = buffer;
        stream.avail_out = sizeof(buffer);
        NSString* avail_outbef = [NSString stringWithFormat:@"%d",(NSInteger)stream.avail_out];
        NSLog(@"avail_outbef = %@",avail_outbef);
//        inflate(&stream, Z_FINISH);
        int ret;
        inflate(&stream,Z_NO_FLUSH);
        size_t length = sizeof(buffer) - stream.avail_out;
        avail_outbef = [NSString stringWithFormat:@"%d",(NSInteger)stream.avail_out];
        NSLog(@"avail_outaft = %@",avail_outbef);
        NSString* strlength = [NSString stringWithFormat:@"%d",(NSInteger)length];
        NSLog(@"length = %@",strlength);
        if (length > 0)
            [FileSystemData appendBytes:buffer length:length];
    }
    inflateEnd(&stream);
    NSLog(@"44444");
}

在Objective-c端提取从c#发送的Zip文件的字节数组

您的uncompressByGzip函数正在为数据缓冲区获取const char**参数。我不熟悉c#代码的编组行为,但这看起来很可疑,因为你正在通过byte []。我的猜测是,您希望它是一个直接的const char *而不是间接的指针。当然,对-[NSData dataWithBytes:length:]的调用需要实际的缓冲区,而不是指向一个缓冲区的指针。