各位,有个问题需要大家帮忙,问题是这样的:
           原数据Hello,中国经过zip压缩再经过base64加密得到数据UEsDBBQACAAIAIGMFUMAAAAAAAAAAAAAAAADAAAAemlw80jNycnXebJj7dPZewFQSwcImB9Mhg4AAAAMAAAAUEsBAhQAFAAIAAgAgYwVQ5gfTIYOAAAADAAAAAMAAAAAAAAAAAAAAAAAAAAAAHppcFBLBQYAAAAAAQABADEAAAA/AAAAAAA=,er我得到的数据是eAEBDADz/0hlbGxvLOS4reWbvSS5Bqc=。相差太大了。还请各位帮忙看看。

解决方案 »

  1.   

    我用的压缩方法:1,- (NSData *)zlibDeflate
    {
            if ([self length] == 0) return self;
        
            z_stream strm;
        
            strm.zalloc = Z_NULL;
            strm.zfree = Z_NULL;
            strm.opaque = Z_NULL;
            strm.total_out = 0;
            strm.next_in=(Bytef *)[self bytes];
            strm.avail_in = [self length];
        
            // Compresssion Levels:
            //   Z_NO_COMPRESSION
            //   Z_BEST_SPEED
            //   Z_BEST_COMPRESSION
            //   Z_DEFAULT_COMPRESSION
        
            if (deflateInit(&strm, Z_NO_COMPRESSION) != Z_OK) return nil;
        
            NSMutableData *compressed = [NSMutableData dataWithLength:16384];  // 16K chuncks for expansion
        
            do {
            
                    if (strm.total_out >= [compressed length])
                            [compressed increaseLengthBy: 16384];
            
                    strm.next_out = [compressed mutableBytes] + strm.total_out;
                    strm.avail_out = [compressed length] - strm.total_out;
            
                    deflate(&strm, Z_FINISH);
            
            } while (strm.avail_out == 0);
        
            deflateEnd(&strm);
        
            [compressed setLength: strm.total_out];
            return [NSData dataWithData: compressed];
    }
    2,- (NSData *)gzipDeflate
    {
            if ([self length] == 0) return self;
        
            z_stream strm;
        
            strm.zalloc = Z_NULL;
            strm.zfree = Z_NULL;
            strm.opaque = Z_NULL;
            strm.total_out = 0;
            strm.next_in=(Bytef *)[self bytes];
            strm.avail_in = [self length];
        
            // Compresssion Levels:
            //   Z_NO_COMPRESSION
            //   Z_BEST_SPEED
            //   Z_BEST_COMPRESSION
            //   Z_DEFAULT_COMPRESSION
        
            if (deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, (15+16), 8, Z_DEFAULT_STRATEGY) != Z_OK) return nil;
        
            NSMutableData *compressed = [NSMutableData dataWithLength:16384];  // 16K chunks for expansion
        
            do {
            
                    if (strm.total_out >= [compressed length])
                            [compressed increaseLengthBy: 16384];
            
                    strm.next_out = [compressed mutableBytes] + strm.total_out;
                    strm.avail_out = [compressed length] - strm.total_out;
            
                    deflate(&strm, Z_FINISH);
            
            } while (strm.avail_out == 0);
        
            deflateEnd(&strm);
        
            [compressed setLength: strm.total_out];
            return [NSData dataWithData:compressed];
    }
    一种是zlib压缩 一种是gzip压缩结果都不一样 ,
    java源码是package com.g3.biz.util;import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    import java.util.zip.ZipOutputStream;
    import org.apache.commons.codec.binary.StringUtils;
    import org.apache.commons.codec.binary.Base64;/**
    * 压缩解压及Base64工具类
    * @author wufuwei
    *
    */
    public class ZipUtil {
            
            /**
             * Base64解码
             * @param source
             * @return
             */
            private static byte[] decodeBase64(String source) {
                    return Base64.decodeBase64(source);
            }        /**
             * base64编码
             * @param source
             * @return
             */
            private static byte[] encodeBase64(byte[] source) {
                    return Base64.encodeBase64(source);
            }        /***
             * 压缩Zip
             * @param data
             * @return
             * @throws IOException
             */
            private static byte[] zip(byte[] bContent) {                byte[] b = null;
                    try {
                            ByteArrayOutputStream bos = new ByteArrayOutputStream();
                            ZipOutputStream zip = new ZipOutputStream(bos);
                            ZipEntry entry = new ZipEntry("zip");
                            entry.setSize(bContent.length);
                            zip.putNextEntry(entry);
                            zip.write(bContent);
                            zip.closeEntry();
                            zip.close();
                            b = bos.toByteArray();
                            bos.close();
                    } catch (Exception ex) {
                            ex.printStackTrace();
                    }
                    return b;
            }        /***
             * 解压Zip
             * @param data
             * @return
             * @throws IOException
             */
            private static byte[] unZip(byte[] bContent) {
                    byte[] b = null;
                    try {
                            ByteArrayInputStream bis = new ByteArrayInputStream(bContent);
                            ZipInputStream zip = new ZipInputStream(bis);
                            while (zip.getNextEntry() != null) {
                                    byte[] buf = new byte[1024];
                                    int num = -1;
                                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                    while ((num = zip.read(buf, 0, buf.length)) != -1) {
                                            baos.write(buf, 0, num);
                                    }
                                    b = baos.toByteArray();
                                    baos.flush();
                                    baos.close();
                            }
                            zip.close();
                            bis.close();
                    } catch (Exception ex) {
                            ex.printStackTrace();
                    }
                    return b;
            }        /**
             * zip压缩后进行Base64编码
             * @param source
             * @return
             */
            public static String zipAndEncode(String source) {
                    return new String(encodeBase64(zip(StringUtils.getBytesUtf8(source))));
            }        /**
             * Base64解码后进行Zip解压
             * @param source
             * @return
             */
            public static String decodeAndUnzip(String source) {
                    return StringUtils.newStringUtf8(unZip(decodeBase64(source)));
            }        public static void main(String[] args) {
            
                    String xml = "Hello,中国";
                    System.out.println(new String(ZipUtil.encodeBase64(StringUtils.getBytesUtf8(xml))));                String compString = ZipUtil.zipAndEncode(xml);
                    System.out.println(compString);                String decompString = ZipUtil.decodeAndUnzip(compString);
                    System.out.println(decompString);        }}
    麻烦帮忙看看 实在没辙了
      

  2.   

    用 https://github.com/samsoffes/ssziparchive
      

  3.   

    搞好了 虽然java搞出来的字符串和ios压缩生成zip包的字符串不一样 但用java的解码能得到一样的结果 说一下 避免下次谁走弯路