142 lines
5.5 KiB
C#
142 lines
5.5 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
//using ICSharpCode.SharpZipLib.Zip;
|
|
|
|
namespace AllBase
|
|
{
|
|
public static class ZipHelper
|
|
{
|
|
/// <summary>
|
|
/// GZip压缩
|
|
/// </summary>
|
|
/// <param name="rawData"></param>
|
|
/// <returns></returns>
|
|
public static byte[] Compress(byte[] rawData)
|
|
{
|
|
if (rawData == null || rawData.Length < 1000)
|
|
return rawData;
|
|
MemoryStream ms = new MemoryStream();
|
|
GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true);
|
|
compressedzipStream.Write(rawData, 0, rawData.Length);
|
|
compressedzipStream.Close();
|
|
return ms.ToArray();
|
|
}
|
|
|
|
/// <summary>
|
|
/// ZIP解压
|
|
/// </summary>
|
|
/// <param name="zippedData"></param>
|
|
/// <returns></returns>
|
|
public static byte[] Decompress(byte[] zippedData)
|
|
{
|
|
if (zippedData == null || zippedData.Length == 0)
|
|
return null;
|
|
if (zippedData.Length < 100)
|
|
return zippedData;
|
|
if (zippedData[2] != 0x8 || zippedData[3] != 0x0 || zippedData[4] != 0x0
|
|
|| zippedData[5] != 0x0 || zippedData[6] != 0x0 || zippedData[7] != 0x0
|
|
|| zippedData[0] != 31 || zippedData[1] != 139)//软加速判断头尾是否正确
|
|
return zippedData;
|
|
try
|
|
{
|
|
MemoryStream ms = new MemoryStream(zippedData);
|
|
GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Decompress);
|
|
MemoryStream outBuffer = new MemoryStream();
|
|
byte[] block = new byte[1024];
|
|
while (true)
|
|
{
|
|
int bytesRead = compressedzipStream.Read(block, 0, block.Length);
|
|
if (bytesRead < 1)
|
|
break;
|
|
outBuffer.Write(block, 0, bytesRead);
|
|
}
|
|
compressedzipStream.Close();
|
|
return outBuffer.ToArray();
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
return zippedData;
|
|
}
|
|
/// <summary>
|
|
/// 解压文件到指定文件夹
|
|
/// </summary>
|
|
/// <param name="sourceFile">压缩文件</param>
|
|
/// <param name="destinationDirectory">目标文件夹,如果为空则解压到当前文件夹下</param>
|
|
/// <param name="password">密码</param>
|
|
/// <returns></returns>
|
|
//public static bool DecomparessFile(string sourceFile, string destinationDirectory = null, string password = null)
|
|
//{
|
|
// bool result = false;
|
|
// int BufferSize = 1024*128;
|
|
// if (!File.Exists(sourceFile))
|
|
// {
|
|
// return false;
|
|
// }
|
|
|
|
// if (string.IsNullOrWhiteSpace(destinationDirectory))
|
|
// {
|
|
// destinationDirectory = Path.GetDirectoryName(sourceFile);
|
|
// }
|
|
|
|
// try
|
|
// {
|
|
// if (!Directory.Exists(destinationDirectory))
|
|
// {
|
|
// Directory.CreateDirectory(destinationDirectory);
|
|
// }
|
|
|
|
// using (ZipInputStream zipStream = new ZipInputStream(File.Open(sourceFile, FileMode.Open,
|
|
// FileAccess.Read, FileShare.Read)))
|
|
// {
|
|
// zipStream.Password = password;
|
|
// ZipEntry zipEntry = zipStream.GetNextEntry();
|
|
|
|
// while (zipEntry != null)
|
|
// {
|
|
// if (zipEntry.IsDirectory)//如果是文件夹则创建
|
|
// {
|
|
// Directory.CreateDirectory(Path.Combine(destinationDirectory,
|
|
// Path.GetDirectoryName(zipEntry.Name)));
|
|
// }
|
|
// else
|
|
// {
|
|
// string fileName = Path.GetFileName(zipEntry.Name);
|
|
// if (!string.IsNullOrEmpty(fileName) && fileName.Trim().Length > 0)
|
|
// {
|
|
// FileInfo fileItem = new FileInfo(Path.Combine(destinationDirectory, zipEntry.Name));
|
|
// using (FileStream writeStream = fileItem.Create())
|
|
// {
|
|
// byte[] buffer = new byte[BufferSize];
|
|
// int readLength = 0;
|
|
|
|
// do
|
|
// {
|
|
// readLength = zipStream.Read(buffer, 0, BufferSize);
|
|
// writeStream.Write(buffer, 0, readLength);
|
|
// } while (readLength == BufferSize);
|
|
|
|
// writeStream.Flush();
|
|
// writeStream.Close();
|
|
// }
|
|
// fileItem.LastWriteTime = zipEntry.DateTime;
|
|
// }
|
|
// }
|
|
// zipEntry = zipStream.GetNextEntry();//获取下一个文件
|
|
// }
|
|
|
|
// zipStream.Close();
|
|
// }
|
|
// result = true;
|
|
// }
|
|
// catch (System.Exception ex)
|
|
// {
|
|
// throw new Exception("文件解压发生错误", ex);
|
|
// }
|
|
|
|
// return result;
|
|
//}
|
|
}
|
|
}
|