关于java和C通用的压缩格式分析

经过亲测,java生成的gzip不能被c读取,但deflate压缩java和c可以通用读取


压缩代码略

// 解压缩

	public static byte[] uncompress(byte[] data) throws IOException {
	      byte[] output = new byte[0];

	        Inflater decompresser = new Inflater();
	        decompresser.reset();
	        decompresser.setInput(data);

	        ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
	        try {
	            byte[] buf = new byte[1024];
	            while (!decompresser.finished()) {
	                int i = decompresser.inflate(buf);
	                o.write(buf, 0, i);
	            }
	            output = o.toByteArray();
	        } catch (Exception e) {
	            output = data;
	            e.printStackTrace();
	        } finally {
	            try {
	                o.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }

	        decompresser.end();
	        return output;
	}

 部分解压c代码如下(zlib是很老的开源库,网上搜下就有下载)

 
  
#include "zlib.h"
#pragma comment(lib,"zlib.lib")
int srclen,deslen;
char *srcbuf,desbuf;
srcbuf=(char *)malloc(srclen);
desbuf=(char *)malloc(deslen);

然后是读取

fread(deslen,4,1,file);
fread(srclen,4,1,file);
fread(srcbuf,srclen,1,file);//被压缩的数据

//需要在被压缩的数据前srclen,deslen这两个变量,

if(uncompress(desbuf, &deslen, srcbuf, srclen)== Z_OK){成功执行}

你可能感兴趣的:(通用)