原地址: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. 问题记录:android.os.NetworkOnMainThreadException异常

    在MainActivity的主线程中访问网络将会发生异常 查阅资料后发现 Android4.0 以后不允许在主线程进行网络连接

  2. [terry笔记]dblink

    dblink在分布式数据库集群中很常见,今天简单的总结一下. db link 的命名和global_names有关,global_name是数据库全局名称,global_name在你所管理的数据库中要 ...

  3. python之ftplib库

    检测ftp是否可用 #!/usr/bin/python #coding:utf-8 from ftplib import FTP def ftp_open(ip,user,passwd): try: ...

  4. LaTeX中无法显示中文问题

  5. mq消息队列

    rabbitmq学习9:使用spring-amqp发送消息及同步接收消息 通过对spring-amqp看重要类的认识,下面来通过spring-amqp的发送消息及同步接收消息是如何实现的.有兴趣的朋友 ...

  6. UVA 100 The 3*n+1 problem

      UVA 100 The 3*n+1 problem. 解题思路:对给定的边界m,n(m<n&&0<m,n<1 000 000);求X(m-1<X<n+ ...

  7. poj 3061 Subsequence

    题目连接 http://poj.org/problem?id=3061 Subsequence Description A sequence of N positive integers (10 &l ...

  8. 【linux命令系列】熟练运用每一个光标移动到最前和最后

    ctrl+e?a和e      ahead 和 end 看一个真正的专家操作命令行绝对是一种很好的体验-光标在单词之间来回穿梭,命令行不同的滚动.在这里强烈建立适应GUI节目的开发者尝试一下在提示符下 ...

  9. [转]Linux下的图形库介绍

    [转]Linux 下的图形库介绍 http://blog.csdn.net/gogor/article/details/5925925 在进行Linux下的图形系统编程时,我们常常会遇到以下这些概念: ...

  10. MNC - Multicast NetCat

    MNC - Multicast NetCat 使用nc测试udp多播,总是遇到奇怪的问题,搞的一头雾水.偶然发现了MNC,测试了一下果然好用. 下载地址: https://github.com/mar ...