原地址:http://www.xuanyusong.com/archives/3095

前两天有朋友告诉我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的解压方法将文件还原在保存在本地。 欢迎大家一起讨论与学习。嘿嘿,或者有什么更好的压缩方式,欢迎在下面给我留言,谢谢。

Unity3D研究院之LZMA压缩文件与解压文件的更多相关文章

  1. SpringMVC上传压缩文件,解压文件,并检测上传文件中是否有index.html

    SpringMVC上传压缩文件,解压文件,并检测上传文件中是否有index.html 说明: 1.环境:SpringMVC+Spring+Tomcat7+JDK1.7 2.支持 zip和rar格式的压 ...

  2. .Net类库 压缩文件 与 Ionic.Zip 批量压缩不同目录文件与解压 文件

    using System; using System.IO; using System.IO.Compression; using System.Linq; using System.Text; us ...

  3. php上传zip文件在线解压文件在指定目录下,CI框架版本

    我从网上找的文件php在线解压zip压缩文件 文件为jy.php可以直接执行,但是怎样将其加到CI框架中呢?? jy.php文件 <?php header("content-Type: ...

  4. php下载文件,解压文件,读取并写入新文件

    以下代码都是本人在工作中遇到的问题,并完成的具体代码和注释,不多说,直接上代码: <?php      //组织链接      $dataurl = "http://118.194.2 ...

  5. 【转载】.NET压缩/解压文件/夹组件

    转自:http://www.cnblogs.com/asxinyu/archive/2013/03/05/2943696.html 阅读目录 1.前言 2.关于压缩格式和算法的基础 3.几种常见的.N ...

  6. 本地上传文件至服务器的技巧(linux文件压缩及解压文件)

    linux(ubuntu)文件解压及压缩文件 ubuntu支持文件的解压及压缩功能, 如果ubuntu上面没有安装过unzip工具的话,可以通过下面命令安装: sudo apt-get install ...

  7. java批量解压文件夹下的所有压缩文件(.rar、.zip、.gz、.tar.gz)

    // java批量解压文件夹下的所有压缩文件(.rar..zip..gz..tar.gz) 新建工具类: package com.mobile.utils; import com.github.jun ...

  8. Linux tar (打包.压缩.解压缩)命令说明 | tar如何解压文件到指定的目录?

    打包举例:将 /usr/local/src/zlib-1.2.5目录下的文件打包成 zlib-1.2.5.tar.gz cd /usr/local/src tar -czvf ./zlib-1.2.5 ...

  9. Linux命令(16)压缩,解压文件

    tar: 简介:tar命令只是把目录打包成一个归档(文件),并不负责压缩.在tar命令中可以带参数调用gzip或bzip2压缩.因为gzip和bzip2只能压缩单个文件. 在linux下是不需要后缀名 ...

随机推荐

  1. [terry笔记]IMPDP报错ORA-39083 Object type TYPE failed to create ORA-02304

    今天在使用impdp导入的时候(同一数据库中转换schema),遇到了 ORA-39083: Object type TYPE failed to create with error: ORA-023 ...

  2. 【C#】 装箱 (boxing) 和拆箱 (unboxing)

    目录: 1. 装箱和拆箱 2. 深入理解装箱和拆箱 3. int[] to object[],值类型数组到对象数组的转化 4. 使用泛型减少装箱和拆箱 1.  装箱和拆箱 装箱 就是把“值类型”转换成 ...

  3. 同时执行2个存储过程,2个SP中分别有相同的临时表名,会有冲突吗?

    同时执行2个存储过程,2个SP中分别有相同的临时表名,会有冲突吗?答案:不会 这就可以在以后写存储过程的时候统一临时表名了. alter procedure sp_01 as begin create ...

  4. Python 爬虫实例

    下面是我写的一个简单爬虫实例 1.定义函数读取html网页的源代码 2.从源代码通过正则表达式挑选出自己需要获取的内容 3.序列中的htm依次写到d盘 #!/usr/bin/python import ...

  5. Nginx源码结构

    上一章对Nginx的架构有了一个初步的了解.这章,为了对源码仔细的剖析,先要对Nginx的源码结构有一个了解.从宏观上把握源码模块的结构. 一.nginx源码的3个目录结构 在安装的nginx的目录下 ...

  6. iOS学习之Object-C语言简单的通讯录管理系统

    用这几天学的OC的知识,写了一个实现简单功能的通讯录管理系统,在这里分享给大家: 通讯录管理系统 *  需求: 1.定义联系人类Contact.实例变量:姓名(拼音,首字母大写).性别.电话号码.住址 ...

  7. inout用法浅析

    inout io_data; reg out_data; reg io_link; assign io_data=io_link? out_data:'bz; //当IO_data作为输入口使用时,一 ...

  8. 12.Warning (15714): Some pins have incomplete I/O assignments. Refer to the I/O Assignment Warnings report for details

    解释:对于一些管脚,缺少了部分描述,需要再添加一些设置,比如current strength,slew rate等: 措施:打开pin plannel界面,在current strength和slew ...

  9. 随机的30道四则运算题(简单的c)

    #include <stdio.h>#include <stdlib.h>#include <time.h> int main(void){ int i = 0; ...

  10. LNMP系列网站零基础开发记录(二)

    [目录] 扯淡吹逼之开发前奏 Django 开发环境搭建及配置 web 页面开发 Django app开发 Django 站点管理 Python 简易爬虫开发 Nginx&uWSGI 服务器配 ...