zip压缩+base64加密

wxj540924 2013-08-28 10:10:58
各位,有个问题需要大家帮忙,问题是这样的:
原数据Hello,中国经过zip压缩再经过base64加密得到数据UEsDBBQACAAIAIGMFUMAAAAAAAAAAAAAAAADAAAAemlw80jNycnXebJj7dPZewFQSwcImB9Mhg4AAAAMAAAAUEsBAhQAFAAIAAgAgYwVQ5gfTIYOAAAADAAAAAMAAAAAAAAAAAAAAAAAAAAAAHppcFBLBQYAAAAAAQABADEAAAA/AAAAAAA=,er我得到的数据是eAEBDADz/0hlbGxvLOS4reWbvSS5Bqc=。相差太大了。还请各位帮忙看看。
...全文
803 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hsl313 2013-11-04
  • 打赏
  • 举报
回复
不知道楼主可以共享下你的类用下吗
wxj540924 2013-09-02
  • 打赏
  • 举报
回复
搞好了 虽然java搞出来的字符串和ios压缩生成zip包的字符串不一样 但用java的解码能得到一样的结果 说一下 避免下次谁走弯路
wxj540924 2013-08-28
  • 打赏
  • 举报
回复
我用的压缩方法: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); } } 麻烦帮忙看看 实在没辙了
zx119koko 2013-08-28
  • 打赏
  • 举报
回复
检查你的zip方法
wxj540924 2013-08-28
  • 打赏
  • 举报
回复
我试了一下还是不对 你试过了吗 和那个预期结果一样吗?如果你试过并且可以,麻烦指点下
zx119koko 2013-08-28
  • 打赏
  • 举报
回复
用 https://github.com/samsoffes/ssziparchive

29,031

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧