方案一:

脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.IO.Compression;
using System;public class EnDeBytes : MonoBehaviour {// Use this for initializationvoid Start () {byte[] test = System.Text.Encoding.Default.GetBytes ("Love lives on inside our hearts and always will.");print ("-----------compress before----------");print (test.Length);byte[] sucess = CompressBytes (test);print ("----------compress after----------");print (sucess.Length);byte[] test2 = Decompress(sucess);print ("----------decompress after----------");print (test2.Length);string a = System.Text.Encoding.Default.GetString(test2, 0, test.Length);print ("a is :" + a);}// Update is called once per framevoid Update () {}public static byte[] CompressBytes(byte[] bytes){using (MemoryStream compressStream = new MemoryStream()){using (var zipStream = new GZipStream(compressStream, CompressionMode.Compress))zipStream.Write(bytes, 0, bytes.Length);return compressStream.ToArray();}}public static byte[] Decompress(byte[] bytes){using (var compressStream = new MemoryStream(bytes)){using (GZipStream zipStream = new GZipStream(compressStream, CompressionMode.Decompress)){
//解决后数据的最后四位表示数据的长度byte[] nb = { bytes[bytes.Length - 4], bytes[bytes.Length - 3], bytes[bytes.Length - 2], bytes[bytes.Length - 1] };int count = BitConverter.ToInt32(nb, 0);byte[] resultStream = new byte[count];zipStream.Read(resultStream, 0, count);return resultStream;}}}}

运行

发现如果内容太短,压缩后还长了,所以小的东西没必需压缩。

把压缩的内容变长

byte[] test = System.Text.Encoding.Default.GetBytes ("Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.");

运行结果

方案二:A 引用 ICSharpCode.SharpZipLib.dll

 public static byte[] CompressByteToByte(byte[] inputBytes){MemoryStream ms = new MemoryStream();Stream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms);try{stream.Write(inputBytes, 0, inputBytes.Length);}finally{stream.Close();ms.Close();}return ms.ToArray();}public static byte[] DecompressByteToByte(byte[] inputBytes){MemoryStream ms = new MemoryStream(inputBytes);Stream sm = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(ms);byte[] data = new byte[sm.Length];int count = 0;MemoryStream re = new MemoryStream();while ((count = sm.Read(data, 0, data.Length)) != 0){re.Write(data, 0, count);}sm.Close();ms.Close();return re.ToArray();}

B, 也引用 ICSharpCode.SharpZipLib.dll

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.IO.Compression;
using System;
using ICSharpCode.SharpZipLib.GZip;public class EnDeBytes : MonoBehaviour {// Use this for initializationvoid Start () {//byte[] test = System.Text.Encoding.Default.GetBytes ("Love lives on inside our hearts and always will.");byte[] test = System.Text.Encoding.Default.GetBytes ("Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.");
//      print ("-----------compress before----------");
//      print (test.Length);
//      byte[] sucess = CompressBytes (test);
//      print ("----------compress after----------");
//      print (sucess.Length);
//
//      byte[] test2 = Decompress(sucess);
//      print ("----------decompress after----------");
//      print (test2.Length);
//byte[] sucess = DecompressByteToBytes (CompressByteToBytes (test));string a = System.Text.Encoding.Default.GetString(sucess, 0, test.Length);print ("a is :" + a);}// Update is called once per framevoid Update () {}public static byte[] CompressByteToBytes(byte[] inputBytes){MemoryStream ms = new MemoryStream();GZipOutputStream gzip = new GZipOutputStream(ms);gzip.Write(inputBytes, 0, inputBytes.Length);gzip.Close();byte[] press = ms.ToArray();return press;}public static byte[] DecompressByteToBytes(byte[] inputBytes){GZipInputStream gzi = new GZipInputStream (new MemoryStream (inputBytes));MemoryStream re = new MemoryStream ();int count = 0;byte[] data = new byte[1024 * 2];while ((count = gzi.Read (data, 0, data.Length)) != 0) {re.Write (data, 0, count);}byte[] depress = re.ToArray ();return depress;}}

unity byte[]的压缩和解压相关推荐

  1. Unity BZip2压缩和解压,基于C#

    基于BZip2的压缩方式(ICSharpCode.SharpZipLib)   压缩和解压代码举例: MemoryStream ms = new MemoryStream();         BZi ...

  2. 用SharpZipLib来压缩和解压文件 --zt

    from:http://www.cnblogs.com/zhangweiguo3984/archive/2007/03/15/314333.html#675634 1.建立工程,添加引用,添加Shar ...

  3. JAVA 7z Seven Zip 压缩和解压文件

    JAVA 7z Seven Zip 压缩和解压文件 7-Zip是基于GNU LGPL协议发布的软件,通过全新算法使压缩比率大幅提升 本文主要讲解通过JAVA方式把文件压缩成7z文件和对7z文件进行解压 ...

  4. 完美java.util.zip使用,实现zip压缩和解压

    之前找了很多demo代码在压缩中遇到各种问题,中文乱码.文本内容丢失等. 以下教程是本人亲自测试过,通过java.util.zip包实现java代码的文件zip压缩和解压: 1.压缩: 1)压缩一个文 ...

  5. android文件压缩库,Android文件压缩和解压

    Android文件压缩和解压 Java代码 package com.maidong.utils; import java.io.BufferedInputStream; import java.io. ...

  6. java代码实现文件或文件夹的压缩和解压

    这里写了个工具类,可以实现文件的压缩和解压功能. package com.cntaiping.tpi.common.utils;import java.io.BufferedInputStream; ...

  7. ZIP文件流压缩和解压

    前面写了一篇文章 "ZIP文件压缩和解压", 介绍了" SharpZipLib.Zip " 的使用, 最近的项目中,在使用的过程中, 遇到一些问题. 比如, 现 ...

  8. 文件的压缩和解压等操作

    .1.将文件压缩为zip/rar文件(实质都为zip格式) /*** 创建ZIP文件* @param sourcePath 文件或文件夹路径* @param zipPath 生成的zip文件存在路径( ...

  9. 【Linux】文件的压缩和解压

    欢迎来到博主 Apeiron 的博客,祝您旅程愉快 ! 时止则止,时行则行.动静不失其时,其道光明. 目录 1.压缩格式 2.压缩软件 3.tar  命令简介 4.tar  命令压缩 5.总结 1.压 ...

  10. java压缩和解压流,实现文件压缩和解压,代码都有注释

    压缩和解压流 压缩文件: ZipOutputStream 常用方法 方法名 介绍 ZipOutputStream(OutputStream out) 构造方法:创建新的ZIP输出流 public vo ...

最新文章

  1. 代码规范+设计模式落地之路
  2. Windows phone 8 学习笔记(5) 图块与通知
  3. 【Python刷题】_1
  4. python最新功能_Python在2020的新增功能:第1部分
  5. pip安装报错:is not a supported wheel on this platform
  6. maven项目添加jar包
  7. 使用Google Test的一个简单例子
  8. java 使用适当的签名_关于数字签名和policy文件设置!
  9. 2.6 子窗口赋值给父窗口并关闭子窗口 我的程序猿之路:第十六章
  10. 实现iframe_面试官:来说说单点登录的三种实现方式
  11. 什么是Azure Data Lake
  12. python文件管不了_Python文件_管道与模块编写
  13. 读EasyPR开发详解实践感想1
  14. 迅 捷PDF编辑器去水印
  15. 游戏框架(Unity3D游戏客户端基础框架)
  16. R语言数据分析笔记——方差分析(单因素方差分析、双因素方差分析、多因素方差分析)在Excel、SPSS、R语言中的操作)
  17. Python识别图片中的文字
  18. 深港澳金融科技师(SHMFTTP)一级考试
  19. vue项目添加百度统计
  20. python入门笔记——内置函数作业

热门文章

  1. c++ 数组指针和指针数组
  2. python关闭word_python自动化办公:玩转word之样式秘笈
  3. 开启本地git权限_Git入门使用和常见操作
  4. iOS贝塞尔曲线(UIBezierPath)的基本使用方法
  5. 简单概述PHP的命名空间及其在自动载入上的应用
  6. mongodb数据结构学习1--增删改查
  7. iOS开发-iOS学习完整路线
  8. CCIE安全Lab实战(2005)学习记录
  9. 成也萧何,败也萧何---PIG JOIN 的replicated
  10. ssh框架的构成分析和代码构架小结 .