点此去下载该类库的加强版 >> http://sevenzipsharp.codeplex.com/

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正式版本。

Unity3D研究院之LZMA压缩文件与解压文件 - 雨松MOMO程序研究院 - 1

 

解压后把整个CS文件夹拖入Unity工程即可。当我在拖入Unity的时候发现Settings.cs报错了,查了一下是因为mono并不是完整的.net 。不过这个文件我们不需要用,所以直接把Settings.cs删除即可。

Unity3D研究院之LZMA压缩文件与解压文件 - 雨松MOMO程序研究院 - 2

 

下面上代码,这是编辑时的一个类。我先把根目录下的一个文件压缩,接着在解压缩。

 

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。

Unity3D研究院之LZMA压缩文件与解压文件 - 雨松MOMO程序研究院 - 3

 

下载地址:http://pan.baidu.com/s/1dDBxyBv

如果你想运行时运行LZMA按照上述代码简单改改就可以使用了。比如你把压缩过的文件放在服务器,然后用www下载到内存以后,可以通过lzma的解压方法将文件还原在保存在本地。 欢迎大家一起讨论与学习。嘿嘿,或者有什么更好的压缩方式,欢迎在下面给我留言,谢谢。

 

 

使用C#压缩/解压缩7-zip文件

7-Zip 简介

 

7-Zip 是一款号称有着现今最高压缩比的压缩软件,它不仅支持独有的 7z 文件格式,而且还支持各种其它压缩文件格式,其中包括 ZIP, RAR, CAB, GZIP, BZIP2和 TAR 等等。此软件压缩的压缩比要比普通 ZIP 文件高 30-50% ,因此,它可以把 Zip 格式的文件再压缩 2-10% 。
7-Zip 主要特征
更新了算法来加大 7z 格式 的压缩比
支持格式:
压缩及解压缩:7z、ZIP、GZIP、BZIP2 和 TAR
仅解压缩:RAR、CAB、ISO、ARJ、LZH、CHM、WIM、Z、CPIO、RPM、DEB 和 NSIS
对于 ZIP 及 GZIP 格式,7-Zip 能提供比使用 PKZip 及 WinZip 高 2-10% 的压缩比
7z 格式支持创建自释放(SFX)压缩档案
集成 Windows 外壳扩展
强大的的文件管理
强大的命令行版本
支持 FAR Manager 插件
支持 69 种语言

 

C#中压缩/解压缩7-zip文件的方法

 

 1.控制台方式调用7z.exe文件

 
public static void Unzip(DirectoryInfo DirecInfo)
{
   if (DirectInfo.Exists)
   {
       foreach (FileInfo fileInfo in DirecInfo.GetFiles("*.zip"))
       {
           Process process = new Process();
           process.StartInfo.FileName = @"C:\Program Files\7-zip\7z.exe";
           process.StartInfo.Arguments = @" e C:\Directory\" + fileInfo.Name + @" -o C:\Directory";
           process.Start();
       }
   }
}

 

 


2.根据压缩算法LZMA SDK
LZMA SDK里面包括了压缩和解压缩算法的C#源码(当前版本是4.65)
下载地址: 
http://sourceforge.net/projects/sevenzip/files/LZMA%20SDK/4.65/lzma465.tar.bz2/download
 
3.在.NET应用程序中使用7-Zip的压缩/解压缩功能(作者 Abel Avram 译者 赵劼 )
开发人员Eugene Sichkar创建了一系列7-Zip动态链接库的C#接口,.NET应用程序中使用7-Zip的压缩/解压缩功能了。
据Eugene称,该项目实现了以下接口:
IProgress - 基本进度的回调


IArchiveOpenCallback - 打开压缩包的回调


ICryptoGetTextPassword - 为压缩提示密码的回调


IArchiveExtractCallback - 对压缩包进行解压的回调


IArchiveOpenVolumeCallback - 打开额外压缩卷的回调


ISequentialInStream - 基本的只读数据流接口


ISequentialOutStream - 基本的只写数据流的接口


IInStream - 可以随机读取的输入数据流接口


IOutStream - 输出数据流接口


IInArchive - 主要压缩接口

 

具体的使用方式可以参考源码中示例.
4.SevenZipSharp
markhor 创建了SevenZipSharp 项目,SevenZipSharp 是开源的,里面实现了自解压和压缩所有7-ZIP支持的格式.它改进了7-Zip动态链接库的C#接口的一些方法.


常用压缩/解压缩示例(引自SevenZipSharp示例文件):
解压缩文件


using (SevenZipExtractor tmp = new SevenZipExtractor(@"d:\Temp\7z465_extra.7z"))
{               
     for (int i = 0; i < tmp.ArchiveFileData.Count; i++)
     {
        tmp.ExtractFiles(@"d:\temp\!Пусто\", tmp.ArchiveFileData[i].Index);
     }
     //tmp.ExtractFiles(@"d:\temp\!Пусто\", 1, 3, 5);
}


分卷压缩
SevenZipExtractor.SetLibraryPath(@"d:\Work\Misc\7zip\9.04\CPP\7zip\Bundles\Format7zF\7z.dll");
using (SevenZipExtractor tmp = new SevenZipExtractor(@"d:\Temp\SevenZip.7z.001"))
{                
    tmp.ExtractArchive(@"d:\Temp\!Пусто");
}

 

 压缩文件
SevenZipCompressor tmp = new SevenZipCompressor();            
tmp.CompressFiles(@"d:\Temp\arch.7z", @"d:\Temp\log.txt");
tmp.CompressDirectory(@"c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\1033", @"D:\Temp\arch.7z");

 

 

压缩ZIP文件

 
SevenZipCompressor tmp = new SevenZipCompressor();
tmp.ArchiveFormat = OutArchiveFormat.Zip;
tmp.CompressFiles(@"d:\Temp\arch.zip", @"d:\Temp\gpl.txt", @"d:\Temp\ru_office.txt");

 

多线程解压缩
Thread t1 = new Thread(() =>
{
    using (SevenZipExtractor tmp = new SevenZipExtractor(@"D:\Temp\7z465_extra.7z"))
    {
        tmp.FileExtractionStarted += new EventHandler<FileInfoEventArgs>((s, e) =>
        {
            Console.WriteLine(String.Format("[{0}%] {1}",
                e.PercentDone, e.FileInfo.FileName));
        });
        tmp.ExtractionFinished += new EventHandler((s, e) => { Console.WriteLine("Finished!"); });
        tmp.ExtractArchive(@"D:\Temp\t1");
    }
});

 
Thread t2 = new Thread(() =>
{
    using (SevenZipExtractor tmp = new SevenZipExtractor(@"D:\Temp\7z465_extra.7z"))
    {
        tmp.FileExtractionStarted += new EventHandler<FileInfoEventArgs>((s, e) =>
        {
            Console.WriteLine(String.Format("[{0}%] {1}",
                e.PercentDone, e.FileInfo.FileName));
        });
        tmp.ExtractionFinished += new EventHandler((s, e) => { Console.WriteLine("Finished!"); });
        tmp.ExtractArchive(@"D:\Temp\t2");
    }
});

 
t1.Start();
t2.Start();
t1.Join();
t2.Join();

 

 

多线程压缩
Thread t1 = new Thread(() =>
{
    SevenZipCompressor tmp = new SevenZipCompressor();             
    tmp.FileCompressionStarted += new EventHandler<FileNameEventArgs>((s, e) =>
    {
        Console.WriteLine(String.Format("[{0}%] {1}",
            e.PercentDone, e.FileName));
    });
    tmp.CompressDirectory(@"D:\Temp\t1", @"D:\Temp\arch1.7z");
});

 

 
Thread t2 = new Thread(() =>
{
    SevenZipCompressor tmp = new SevenZipCompressor();
    tmp.FileCompressionStarted += new EventHandler<FileNameEventArgs>((s, e) =>
    {
        Console.WriteLine(String.Format("[{0}%] {1}",
            e.PercentDone, e.FileName));
    });
    tmp.CompressDirectory(@"D:\Temp\t2", @"D:\Temp\arch2.7z");
});
t1.Start();
t2.Start();
t1.Join();
t2.Join();