#region
|
|
using System;
|
using System.Collections;
|
using System.Data;
|
using System.IO;
|
using System.IO.Compression;
|
using System.Runtime.Serialization.Formatters.Binary;
|
using System.Text;
|
|
#endregion
|
|
namespace CSFrameworkV5.Common
|
{
|
/// <summary>
|
/// 压缩/解压工具
|
/// </summary>
|
public class ZipTools
|
{
|
#region 压缩解压自符串
|
|
public static string ZipString(string unCompressedString)
|
{
|
var bytData = Encoding.UTF8.GetBytes(unCompressedString);
|
|
using (var ms = new MemoryStream())
|
{
|
using (Stream s = new GZipStream(ms, CompressionMode.Compress))
|
{
|
s.Write(bytData, 0, bytData.Length);
|
var compressedData = ms.ToArray();
|
return Convert.ToBase64String(compressedData, 0,
|
compressedData.Length);
|
}
|
}
|
}
|
|
public static string UnzipString(string unCompressedString)
|
{
|
var uncompressedString = new StringBuilder();
|
var writeData = new byte[4096];
|
|
var bytData = Convert.FromBase64String(unCompressedString);
|
var totalLength = 0;
|
var size = 0;
|
|
using (Stream s = new GZipStream(new MemoryStream(bytData),
|
CompressionMode.Decompress))
|
{
|
while (true)
|
{
|
size = s.Read(writeData, 0, writeData.Length);
|
if (size > 0)
|
{
|
totalLength += size;
|
uncompressedString.Append(
|
Encoding.UTF8.GetString(writeData, 0, size));
|
}
|
else
|
{
|
break;
|
}
|
}
|
|
return uncompressedString.ToStringEx();
|
}
|
}
|
|
//Demo
|
//string abc = ZipString(txtCompressString.Text.Trim());
|
//string Result = UnzipString(abc);
|
|
#endregion
|
|
#region 压缩解压数据集
|
|
public static byte[] CompressionDataSet(DataSet dsOriginal)
|
{
|
if (dsOriginal == null) return null;
|
|
dsOriginal.RemotingFormat = SerializationFormat.Binary;
|
var bFormatter = new BinaryFormatter();
|
using (var mStream = new MemoryStream())
|
{
|
bFormatter.Serialize(mStream, dsOriginal);
|
var bytes = mStream.ToArray();
|
|
using (var oStream = new MemoryStream())
|
{
|
var zipStream =
|
new DeflateStream(oStream, CompressionMode.Compress);
|
zipStream.Write(bytes, 0, bytes.Length);
|
zipStream.Flush();
|
zipStream.Close();
|
return oStream.ToArray();
|
}
|
}
|
}
|
|
public static DataSet DecompressionDataSet(byte[] bytes)
|
{
|
if (bytes == null) return null;
|
|
using (var mStream = new MemoryStream(bytes))
|
{
|
mStream.Seek(0, SeekOrigin.Begin);
|
|
using (var unZipStream = new DeflateStream(mStream,
|
CompressionMode.Decompress, true))
|
{
|
var dsResult = new DataSet();
|
dsResult.RemotingFormat = SerializationFormat.Binary;
|
var bFormatter = new BinaryFormatter();
|
dsResult = (DataSet)bFormatter.Deserialize(unZipStream);
|
return dsResult;
|
}
|
}
|
}
|
|
#endregion
|
|
#region 压缩解压文件
|
|
public static void CompressFile(string sourceFile,
|
string destinationFile)
|
{
|
if (!File.Exists(CodeSafeHelper.GetSafePath(sourceFile)))
|
throw new FileNotFoundException();
|
|
FileStream sourceStream = null;
|
FileStream destinationStream = null;
|
GZipStream compressedStream = null;
|
|
try
|
{
|
using (sourceStream = new FileStream(sourceFile, FileMode.Open,
|
FileAccess.Read, FileShare.Read))
|
{
|
var buffer = new byte[sourceStream.Length];
|
var checkCounter =
|
sourceStream.Read(buffer, 0, buffer.Length);
|
if (checkCounter != buffer.Length)
|
throw new ApplicationException();
|
|
using (destinationStream =
|
new FileStream(destinationFile,
|
FileMode.OpenOrCreate, FileAccess.Write))
|
{
|
using (compressedStream =
|
new GZipStream(destinationStream,
|
CompressionMode.Compress, true))
|
{
|
compressedStream.Write(buffer, 0, buffer.Length);
|
}
|
}
|
}
|
}
|
finally
|
{
|
if (sourceStream != null) sourceStream.Dispose();
|
|
if (destinationStream != null) destinationStream.Dispose();
|
|
if (compressedStream != null) compressedStream.Dispose();
|
}
|
}
|
|
public static void DecompressFile(string sourceFile,
|
string destinationFile)
|
{
|
if (!File.Exists(sourceFile)) throw new FileNotFoundException();
|
|
using (var sourceStream = new FileStream(sourceFile, FileMode.Open))
|
{
|
var quartetBuffer = new byte[4];
|
var position = (int)sourceStream.Length - 4;
|
sourceStream.Position = position;
|
sourceStream.Read(quartetBuffer, 0, 4);
|
sourceStream.Position = 0;
|
var checkLength = BitConverter.ToInt32(quartetBuffer, 0);
|
var buffer = new byte[checkLength + 100];
|
|
using (var decompressedStream = new GZipStream(sourceStream,
|
CompressionMode.Decompress, true))
|
{
|
var total = 0;
|
for (var offset = 0;;)
|
{
|
var bytesRead =
|
decompressedStream.Read(buffer, offset, 100);
|
if (bytesRead == 0) break;
|
|
offset += bytesRead;
|
total += bytesRead;
|
}
|
|
using (var destinationStream =
|
new FileStream(destinationFile, FileMode.Create))
|
{
|
destinationStream.Write(buffer, 0, total);
|
}
|
}
|
}
|
}
|
|
#endregion
|
|
#region 压缩解压Ilist
|
|
public static byte[] CompressionArrayList(IList DataOriginal)
|
{
|
if (DataOriginal == null) return null;
|
|
var bFormatter = new BinaryFormatter();
|
using (var mStream = new MemoryStream())
|
{
|
bFormatter.Serialize(mStream, DataOriginal);
|
var bytes = mStream.ToArray();
|
|
using (var oStream = new MemoryStream())
|
{
|
var zipStream =
|
new DeflateStream(oStream, CompressionMode.Compress);
|
zipStream.Write(bytes, 0, bytes.Length);
|
zipStream.Flush();
|
zipStream.Close();
|
|
return oStream.ToArray();
|
}
|
}
|
}
|
|
public static IList DecompressionArrayList(byte[] bytes)
|
{
|
if (bytes == null) return null;
|
|
using (var mStream = new MemoryStream(bytes))
|
{
|
mStream.Seek(0, SeekOrigin.Begin);
|
|
using (var unZipStream = new DeflateStream(mStream,
|
CompressionMode.Decompress, true))
|
{
|
IList dsResult = null;
|
var bFormatter = new BinaryFormatter();
|
dsResult = (IList)bFormatter.Deserialize(unZipStream);
|
return dsResult;
|
}
|
}
|
}
|
|
#endregion
|
|
#region 压缩解压object
|
|
public static byte[] CompressionObject(object DataOriginal)
|
{
|
if (DataOriginal == null) return null;
|
|
var bFormatter = new BinaryFormatter();
|
using (var mStream = new MemoryStream())
|
{
|
bFormatter.Serialize(mStream, DataOriginal);
|
var bytes = mStream.ToArray();
|
|
using (var oStream = new MemoryStream())
|
{
|
var zipStream =
|
new DeflateStream(oStream, CompressionMode.Compress);
|
zipStream.Write(bytes, 0, bytes.Length);
|
zipStream.Flush();
|
zipStream.Close();
|
return oStream.ToArray();
|
}
|
}
|
}
|
|
public static object DecompressionObject(byte[] bytes)
|
{
|
if (bytes == null) return null;
|
|
using (var mStream = new MemoryStream(bytes))
|
{
|
mStream.Seek(0, SeekOrigin.Begin);
|
using (var unZipStream = new DeflateStream(mStream,
|
CompressionMode.Decompress, true))
|
{
|
object dsResult = null;
|
var bFormatter = new BinaryFormatter();
|
dsResult = bFormatter.Deserialize(unZipStream);
|
return dsResult;
|
}
|
}
|
}
|
|
#endregion
|
}
|
}
|