[置顶] LZMA C# SDK 子线程压缩与解压缩 Unity3d实例

参考雨松的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#编码 (一)



你可能感兴趣的:(压缩,unity3d,lzma,7zip,Assetbundle)