using System;
|
using System.IO;
|
using System.Threading;
|
using UnityEngine;
|
|
namespace Xdxn {
|
|
public delegate void ProgressDelegate(Int64 fileSize, Int64 processSize);
|
public class FileCompressInfo {
|
public string inPath;
|
public string outPath;
|
public ProgressDelegate progressDelegate;
|
}
|
|
public class CompressUtility {
|
|
public class CodeProgress : SevenZipEx.ICodeProgress {
|
|
private ProgressDelegate progressDelegate;
|
|
public CodeProgress(ProgressDelegate callBack) {
|
progressDelegate = callBack;
|
}
|
|
public void SetProgress(Int64 inSize, Int64 outSize) {
|
if (progressDelegate != null) {
|
progressDelegate(inSize, outSize);
|
}
|
}
|
}
|
|
public static void Compress(string inPath, string outPath, ProgressDelegate progressDelegate) {
|
FileCompressInfo _fileCompressInfo = new FileCompressInfo();
|
_fileCompressInfo.inPath = inPath;
|
_fileCompressInfo.outPath = outPath;
|
_fileCompressInfo.progressDelegate = progressDelegate;
|
CompressLZMA(_fileCompressInfo);
|
}
|
|
public static void DeCompress(string inPath, string outPath, ProgressDelegate progressDelegate) {
|
FileCompressInfo _fileCompressInfo = new FileCompressInfo();
|
_fileCompressInfo.inPath = inPath;
|
_fileCompressInfo.outPath = outPath;
|
_fileCompressInfo.progressDelegate = progressDelegate;
|
DeCompressLZMA(_fileCompressInfo);
|
}
|
|
public static void CompressAsync(string inPath, string outPath, ProgressDelegate progressDelegate) {
|
Thread _compressThread = new Thread(new ParameterizedThreadStart(CompressLZMA));
|
FileCompressInfo _fileCompressInfo = new FileCompressInfo();
|
_fileCompressInfo.inPath = inPath;
|
_fileCompressInfo.outPath = outPath;
|
_fileCompressInfo.progressDelegate = progressDelegate;
|
_compressThread.Start(_fileCompressInfo);
|
}
|
|
public static void DeCompressAsync(string inPath, string outPath, ProgressDelegate progressDelegate) {
|
Thread _deCompressThread = new Thread(new ParameterizedThreadStart(DeCompressLZMA));
|
FileCompressInfo _fileCompressInfo = new FileCompressInfo();
|
_fileCompressInfo.inPath = inPath;
|
_fileCompressInfo.outPath = outPath;
|
_fileCompressInfo.progressDelegate = progressDelegate;
|
_deCompressThread.Start(_fileCompressInfo);
|
}
|
|
private static void CompressLZMA(object obj) {
|
|
FileCompressInfo _fileCompressInfo = obj as FileCompressInfo;
|
|
FileStream _input = null;
|
FileStream _output = null;
|
SevenZipEx.Compression.LZMA.Encoder _encoder = null;
|
|
CodeProgress _codeProgress = null;
|
if (_fileCompressInfo.progressDelegate != null) {
|
_codeProgress = new CodeProgress(_fileCompressInfo.progressDelegate);
|
}
|
|
try {
|
_encoder = new SevenZipEx.Compression.LZMA.Encoder();
|
|
_input = new FileStream(_fileCompressInfo.inPath, FileMode.Open);
|
_output = new FileStream(_fileCompressInfo.outPath, FileMode.Create);
|
|
_encoder.WriteCoderProperties(_output);
|
|
_output.Write(BitConverter.GetBytes(_input.Length), 0, 8);
|
_encoder.Code(_input, _output, _input.Length, -1, _codeProgress);
|
|
_output.Flush();
|
}
|
catch (Exception e) {
|
DebugEx.LogFormat(e.Message);
|
}
|
finally {
|
if (_encoder != null) {
|
_encoder = null;
|
}
|
if (_input != null) {
|
_input.Close();
|
_input = null;
|
}
|
if (_output != null) {
|
_output.Close();
|
_output = null;
|
}
|
}
|
}
|
|
private static void DeCompressLZMA(object obj) {
|
|
FileCompressInfo _fileCompressInfo = obj as FileCompressInfo;
|
|
SevenZipEx.Compression.LZMA.Decoder _decoder = null;
|
FileStream _input = null;
|
FileStream _output = null;
|
|
CodeProgress _codeProgress = null;
|
if (_fileCompressInfo.progressDelegate != null) {
|
_codeProgress = new CodeProgress(_fileCompressInfo.progressDelegate);
|
}
|
|
try {
|
|
_decoder = new SevenZipEx.Compression.LZMA.Decoder();
|
|
_input = new FileStream(_fileCompressInfo.inPath, FileMode.Open);
|
_output = new FileStream(_fileCompressInfo.outPath, FileMode.Create);
|
|
byte[] _properties = new byte[5];
|
_input.Read(_properties, 0, 5);
|
|
byte[] fileLengthBytes = new byte[8];
|
_input.Read(fileLengthBytes, 0, 8);
|
long _fileLength = BitConverter.ToInt64(fileLengthBytes, 0);
|
|
_decoder.SetDecoderProperties(_properties);
|
_decoder.Code(_input, _output, _input.Length, _fileLength, _codeProgress);
|
|
_output.Flush();
|
|
}
|
catch (Exception e) {
|
DebugEx.LogFormat(e.Message);
|
}
|
finally {
|
if (_decoder != null) {
|
_decoder = null;
|
}
|
if (_input != null) {
|
_input.Close();
|
_input = null;
|
}
|
if (_output != null) {
|
_output.Close();
|
_output = null;
|
}
|
}
|
}
|
}
|
|
}
|