前两天有朋友告诉我Unity的Assetbundle是LZMA压缩的,刚好今天有时间那么就研究研究LZMA。它是一个开源的类库,有C、 C++、C#、JAVA的类库,那么在我大Unity里面我们当然要使用C#的类库啦。
下载地址:http://www.7-zip.org/sdk.html 或者在文章的最后下载我的测试工程、如下图所示,因为9.22是Beta版本,所以我们还是老老实实下载9.20正式版本。
解压后把整个CS文件夹拖入Unity工程即可。当我在拖入Unity的时候发现Settings.cs报错了,查了一下是因为mono并不是完整的.net 。不过这个文件我们不需要用,所以直接把Settings.cs删除即可。
下面上代码,这是编辑时的一个类。我先把根目录下的一个文件压缩,接着在解压缩。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
using
UnityEngine
;
using
System
.
Collections
;
using
UnityEditor
;
using
SevenZip
.
Compression
.
LZMA
;
using
System
.
IO
;
using
System
;
public
class
Test
:
Editor
{
[
MenuItem
(
"MyMenu/CompressFile"
)
]
static
void
CompressFile
(
)
{
//压缩文件
CompressFileLZMA
(
Application
.
dataPath
+
"/1.jpg"
,
Application
.
dataPath
+
"/2.zip"
)
;
AssetDatabase
.
Refresh
(
)
;
}
[
MenuItem
(
"MyMenu/DecompressFile"
)
]
static
void
DecompressFile
(
)
{
//解压文件
DecompressFileLZMA
(
Application
.
dataPath
+
"/2.zip"
,
Application
.
dataPath
+
"/3.jpg"
)
;
AssetDatabase
.
Refresh
(
)
;
}
private
static
void
CompressFileLZMA
(
string
inFile
,
string
outFile
)
{
SevenZip
.
Compression
.
LZMA
.
Encoder
coder
=
new
SevenZip
.
Compression
.
LZMA
.
Encoder
(
)
;
FileStream
input
=
new
FileStream
(
inFile
,
FileMode
.
Open
)
;
FileStream
output
=
new
FileStream
(
outFile
,
FileMode
.
Create
)
;
// Write the encoder properties
coder
.
WriteCoderProperties
(
output
)
;
// Write the decompressed file size.
output
.
Write
(
BitConverter
.
GetBytes
(
input
.
Length
)
,
0
,
8
)
;
// Encode the file.
coder
.
Code
(
input
,
output
,
input
.
Length
,
-
1
,
null
)
;
output
.
Flush
(
)
;
output
.
Close
(
)
;
input
.
Close
(
)
;
}
private
static
void
DecompressFileLZMA
(
string
inFile
,
string
outFile
)
{
SevenZip
.
Compression
.
LZMA
.
Decoder
coder
=
new
SevenZip
.
Compression
.
LZMA
.
Decoder
(
)
;
FileStream
input
=
new
FileStream
(
inFile
,
FileMode
.
Open
)
;
FileStream
output
=
new
FileStream
(
outFile
,
FileMode
.
Create
)
;
// Read the decoder properties
byte
[
]
properties
=
new
byte
[
5
]
;
input
.
Read
(
properties
,
0
,
5
)
;
// Read in the decompress file size.
byte
[
]
fileLengthBytes
=
new
byte
[
8
]
;
input
.
Read
(
fileLengthBytes
,
0
,
8
)
;
long
fileLength
=
BitConverter
.
ToInt64
(
fileLengthBytes
,
0
)
;
// Decompress the file.
coder
.
SetDecoderProperties
(
properties
)
;
coder
.
Code
(
input
,
output
,
input
.
Length
,
fileLength
,
null
)
;
output
.
Flush
(
)
;
output
.
Close
(
)
;
input
.
Close
(
)
;
}
}
|
我参考了这篇文章,不过它点问题,所以我改了改。
http://stackoverflow.com/questions/7646328/how-to-use-the-7z-sdk-to-compress-and-decompress-a-file
如下图所示,我把1.jpg先压缩成2.zip ,然后在把2.zip在解压成3.jpg。
下载地址:http://pan.baidu.com/s/1dDBxyBv
如果你想运行时运行LZMA按照上述代码简单改改就可以使用了。比如你把压缩过的文件放在服务器,然后用www下载到内存以后,可以通过lzma的解压方法将文件还原在保存在本地。 欢迎大家一起讨论与学习。嘿嘿,或者有什么更好的压缩方式,欢迎在下面给我留言,谢谢。
- 本文固定链接: http://www.xuanyusong.com/archives/3095
- 转载请注明: 雨松MOMO 2014年08月12日 于 雨松MOMO程序研究院 发表