LZMA C# SDK 子线程压缩与解压缩 Unity3d实例
2015-06-22 02:43
302 查看
参考雨松的LZMA SDK使用方法:
http://www.xuanyusong.com/archives/3095 转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn
计划在项目中使用 不压缩的Assetbundle ,所以需要对Assetbundle 进行手动压缩打包,因为之前有对 十万个冷笑话的打包分析,所以这次坚定选择 LZMA压缩算法来压缩Assetbundle。
转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn
Note:
因为暂时不知道LZMA如何压缩与解压文件夹,所以目前是计划对文件夹进行UPK打包成UPK文件,然后LZMA压缩。解压的话先解压出来UPK文件,然后读取UPK文件来释放出源文件。
LZMA 官网下载SDK
http://www.7-zip.org/sdk.html
下载过来后直接把C#里面的7zip拖到Unity中,这时Setting.cs 会报错,删除它。
Note:
本文示例中的 LZMA SDK源代码被我修改了。
转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn
代码如下:
using UnityEngine;
using System.Collections;
using SevenZip;
using System.IO;
using UnityEditor;
using System;
using System.Threading;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour {
[SerializeField]
Slider m_Slider;
CodeProgress m_CodeProgress=null;//进度;
Thread m_CompressThread=null; //压缩线程;
Thread m_DecompressThread=null; //解压缩线程;
string ApplicationdataPath=string.Empty;
private float m_Percent=0f;
// Use this for initialization
void Start ()
{
ApplicationdataPath=Application.dataPath;
m_CodeProgress=new CodeProgress(SetProgressPercent);
}
void Update()
{
if(m_Percent>0f)
{
m_Slider.value=m_Percent;
if(m_Percent==1f)
{
Debug.Log("m_Percent==1f");
m_Percent=0f;
AssetDatabase.Refresh();
}
}
}
void OnGUI()
{
if(GUI.Button(new Rect(100,100,100,100),"Compress"))
{
m_Slider.value=0f;
m_CompressThread=new Thread(new ThreadStart(TestCompress));
m_CompressThread.Start();
}
if(GUI.Button(new Rect(300,100,100,100),"DeCompress"))
{
m_Slider.value=0f;
m_DecompressThread=new Thread(new ThreadStart(TestDeCompress));
m_DecompressThread.Start();
}
}
void TestCompress()
{
try
{
Compress(ApplicationdataPath+"/lib.UPK",ApplicationdataPath+"/lib.ZUPK");
}
catch(Exception ex)
{
Debug.Log(ex);
}
}
void TestDeCompress()
{
try
{
DeCompress(ApplicationdataPath+"/lib.ZUPK",ApplicationdataPath+"/lib1.UPK");
}
catch(Exception ex)
{
Debug.Log(ex);
}
}
void SetProgressPercent(Int64 fileSize,Int64 processSize)
{
m_Percent=(float)processSize/fileSize;
}
void Compress(string inpath,string outpath)
{
SevenZip.Compression.LZMA.Encoder encoder=new SevenZip.Compression.LZMA.Encoder();
FileStream inputFS=new FileStream(inpath,FileMode.Open);
FileStream outputFS=new FileStream(outpath,FileMode.Create);
encoder.WriteCoderProperties(outputFS);
outputFS.Write(System.BitConverter.GetBytes(inputFS.Length),0,8);
encoder.Code(inputFS,outputFS,inputFS.Length,-1,m_CodeProgress);
outputFS.Flush();
outputFS.Close();
inputFS.Close();
}
void DeCompress(string inpath,string outpath)
{
SevenZip.Compression.LZMA.Decoder decoder=new SevenZip.Compression.LZMA.Decoder();
FileStream inputFS=new FileStream(inpath,FileMode.Open);
FileStream outputFS=new FileStream(outpath,FileMode.Create);
int propertiesSize=SevenZip.Compression.LZMA.Encoder.kPropSize;
byte[] properties=new byte[propertiesSize];
inputFS.Read(properties,0,properties.Length);
byte[] fileLengthBytes=new byte[8];
inputFS.Read(fileLengthBytes,0,8);
long fileLength=System.BitConverter.ToInt64(fileLengthBytes,0);
decoder.SetDecoderProperties(properties);
decoder.Code(inputFS,outputFS,inputFS.Length,fileLength,m_CodeProgress);
outputFS.Flush();
outputFS.Close();
inputFS.Close();
}
}
class CodeProgress:ICodeProgress
{
public delegate void ProgressDelegate(Int64 fileSize,Int64 processSize);
public ProgressDelegate m_ProgressDelegate=null;
public CodeProgress(ProgressDelegate del)
{
m_ProgressDelegate=del;
}
public void SetProgress(Int64 inSize, Int64 outSize)
{
}
public void SetProgressPercent(Int64 fileSize,Int64 processSize)
{
m_ProgressDelegate(fileSize,processSize);
}
}
转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn 示例工程下载:
百度网盘
链接: http://pan.baidu.com/s/1jGlEDPO 密码: wure
CSDN下载
关于UPK打包
Unity3d 游戏资源打包加密(图片/XML/TXT等) C#编码 (一)
http://www.xuanyusong.com/archives/3095 转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn
计划在项目中使用 不压缩的Assetbundle ,所以需要对Assetbundle 进行手动压缩打包,因为之前有对 十万个冷笑话的打包分析,所以这次坚定选择 LZMA压缩算法来压缩Assetbundle。
转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn
Note:
因为暂时不知道LZMA如何压缩与解压文件夹,所以目前是计划对文件夹进行UPK打包成UPK文件,然后LZMA压缩。解压的话先解压出来UPK文件,然后读取UPK文件来释放出源文件。
LZMA 官网下载SDK
http://www.7-zip.org/sdk.html
下载过来后直接把C#里面的7zip拖到Unity中,这时Setting.cs 会报错,删除它。
Note:
本文示例中的 LZMA SDK源代码被我修改了。
转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn
代码如下:
using UnityEngine;
using System.Collections;
using SevenZip;
using System.IO;
using UnityEditor;
using System;
using System.Threading;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour {
[SerializeField]
Slider m_Slider;
CodeProgress m_CodeProgress=null;//进度;
Thread m_CompressThread=null; //压缩线程;
Thread m_DecompressThread=null; //解压缩线程;
string ApplicationdataPath=string.Empty;
private float m_Percent=0f;
// Use this for initialization
void Start ()
{
ApplicationdataPath=Application.dataPath;
m_CodeProgress=new CodeProgress(SetProgressPercent);
}
void Update()
{
if(m_Percent>0f)
{
m_Slider.value=m_Percent;
if(m_Percent==1f)
{
Debug.Log("m_Percent==1f");
m_Percent=0f;
AssetDatabase.Refresh();
}
}
}
void OnGUI()
{
if(GUI.Button(new Rect(100,100,100,100),"Compress"))
{
m_Slider.value=0f;
m_CompressThread=new Thread(new ThreadStart(TestCompress));
m_CompressThread.Start();
}
if(GUI.Button(new Rect(300,100,100,100),"DeCompress"))
{
m_Slider.value=0f;
m_DecompressThread=new Thread(new ThreadStart(TestDeCompress));
m_DecompressThread.Start();
}
}
void TestCompress()
{
try
{
Compress(ApplicationdataPath+"/lib.UPK",ApplicationdataPath+"/lib.ZUPK");
}
catch(Exception ex)
{
Debug.Log(ex);
}
}
void TestDeCompress()
{
try
{
DeCompress(ApplicationdataPath+"/lib.ZUPK",ApplicationdataPath+"/lib1.UPK");
}
catch(Exception ex)
{
Debug.Log(ex);
}
}
void SetProgressPercent(Int64 fileSize,Int64 processSize)
{
m_Percent=(float)processSize/fileSize;
}
void Compress(string inpath,string outpath)
{
SevenZip.Compression.LZMA.Encoder encoder=new SevenZip.Compression.LZMA.Encoder();
FileStream inputFS=new FileStream(inpath,FileMode.Open);
FileStream outputFS=new FileStream(outpath,FileMode.Create);
encoder.WriteCoderProperties(outputFS);
outputFS.Write(System.BitConverter.GetBytes(inputFS.Length),0,8);
encoder.Code(inputFS,outputFS,inputFS.Length,-1,m_CodeProgress);
outputFS.Flush();
outputFS.Close();
inputFS.Close();
}
void DeCompress(string inpath,string outpath)
{
SevenZip.Compression.LZMA.Decoder decoder=new SevenZip.Compression.LZMA.Decoder();
FileStream inputFS=new FileStream(inpath,FileMode.Open);
FileStream outputFS=new FileStream(outpath,FileMode.Create);
int propertiesSize=SevenZip.Compression.LZMA.Encoder.kPropSize;
byte[] properties=new byte[propertiesSize];
inputFS.Read(properties,0,properties.Length);
byte[] fileLengthBytes=new byte[8];
inputFS.Read(fileLengthBytes,0,8);
long fileLength=System.BitConverter.ToInt64(fileLengthBytes,0);
decoder.SetDecoderProperties(properties);
decoder.Code(inputFS,outputFS,inputFS.Length,fileLength,m_CodeProgress);
outputFS.Flush();
outputFS.Close();
inputFS.Close();
}
}
class CodeProgress:ICodeProgress
{
public delegate void ProgressDelegate(Int64 fileSize,Int64 processSize);
public ProgressDelegate m_ProgressDelegate=null;
public CodeProgress(ProgressDelegate del)
{
m_ProgressDelegate=del;
}
public void SetProgress(Int64 inSize, Int64 outSize)
{
}
public void SetProgressPercent(Int64 fileSize,Int64 processSize)
{
m_ProgressDelegate(fileSize,processSize);
}
}
转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn 示例工程下载:
百度网盘
链接: http://pan.baidu.com/s/1jGlEDPO 密码: wure
CSDN下载
关于UPK打包
Unity3d 游戏资源打包加密(图片/XML/TXT等) C#编码 (一)
相关文章推荐
- 【Unity3d】在Unity中播放视频
- 【Unity3d】注意C#的字符串拼接效率问题
- 【Unity3d】自动版本构建: 如何在Windows平台下自动打开资源管理器并且选中指定文件
- Unity该插件NGUI学习(1)—— 环境结构
- Unity教程之再谈Unity中的优化技术
- 开始Unity3D参观考察
- Mono2.0 对C#闭包 与 donet 不同的实现导致Unity的Bug 及解决方案
- unity平台的预处理
- 以游戏实例介绍Unity3d
- [Unity3D]如何渲染一个在camera frustum外面的物体
- 【Unity3d】如何解决Unity3d在引用外部插件时报DLLNotFoundException的问题
- Unity3d 反编译破解游戏 简单示例 (使用ildasm反编译DLL修改然后重新编译DLL)
- unity, switch platform
- Unity链表的应用之蛇形动画
- VS community 2013 安装配置Xamarin
- 【Unity3d】疑难杂症解决之系统报错:Supplied NxActorDesc is not valid. createActor returns NULL
- Unity着色器入门之一步一步来看坐标系变换
- Unity下的StreamingAssets路径不同平台的读取
- Unity3D实践系列05,为GameObject添加额外属性
- 关于Fatal Error in GC : Too Many threads