Files
2026-07-10 15:25:05 +08:00

329 lines
11 KiB
C#

using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO;
using System.Runtime.CompilerServices;
namespace Zhaizj.Framework.Utils
{
/// <summary>
/// 描述:压缩类
/// 作者:黄淑莺
/// 日期:2010-3-22
/// </summary>
public class Zip
{
private int _blockSize = 0;
private int _compressionLevel = 0;
private int _fileCount = 0;
private int _fileIndex = 0;
/// <summary>
/// 得到目录的数量
/// </summary>
/// <param name="directory">目录</param>
private void GetDirCount(string directory)
{
DirectoryInfo info = new DirectoryInfo(directory);
foreach (FileInfo info2 in info.GetFiles())
{
this._fileCount++;
}
foreach (DirectoryInfo info3 in info.GetDirectories())
{
this.GetDirCount(info3.FullName);
}
}
/// <summary>
/// 得到文件的数量
/// </summary>
/// <param name="sourceFile">源文件</param>
private void GetFileCount(string sourceFile)
{
ZipInputStream stream = new ZipInputStream(File.OpenRead(sourceFile));
while (stream.GetNextEntry() != null)
{
this._fileCount++;
}
stream.Close();
}
/// <summary>
/// 解压缩
/// </summary>
/// <param name="sourceFile">源文件</param>
/// <param name="destFolder">目标文件夹</param>
public void UnZip(string sourceFile, string destFolder)
{
ZipEntry entry;
this.GetFileCount(sourceFile);
ZipInputStream stream = new ZipInputStream(File.OpenRead(sourceFile));
while ((entry = stream.GetNextEntry()) != null)
{
if (!destFolder.EndsWith(@"\"))
{
destFolder = destFolder + @"\";
}
string directoryName = Path.GetDirectoryName(destFolder);
string fileName = Path.GetFileName(entry.Name);
DirectoryInfo info = new DirectoryInfo(directoryName);
if (!info.Exists)
{
Directory.CreateDirectory(directoryName);
}
string str3 = entry.Name.Replace("/", @"\");
if (str3.LastIndexOf(@"\") > 0)
{
str3 = str3.Substring(0, str3.LastIndexOf(@"\"));
info = new DirectoryInfo(destFolder + str3);
if (!info.Exists)
{
Directory.CreateDirectory(destFolder + str3);
}
}
if (fileName != string.Empty)
{
this._fileIndex++;
FileStream stream2 = File.Create(destFolder + entry.Name.Replace("/", @"\"));
try
{
try
{
int count = 0x800;
byte[] buffer = new byte[0x800];
int num2 = 0;
goto Label_01B7;
Label_013E:
count = stream.Read(buffer, 0, buffer.Length);
if (count <= 0)
{
goto Label_01F1;
}
stream2.Write(buffer, 0, count);
num2 += count;
long num3 = Math.Abs(entry.Size);
if (num3 == 1L)
{
num3 = num2;
}
//this.ZipProgress((long)num2, num3, this.FileIndex, this.FileCount, fileName);
Label_01B7:
goto Label_013E;
}
catch (Exception exception)
{
stream2.Close();
throw new Exception("error 2 :" + exception.Message);
}
}
finally
{
stream2.Close();
}
Label_01F1: ;
}
}
stream.Close();
//this.ZipProgress(1L, 1L, 1, 1, "");
this._fileCount = 0;
this._fileIndex = 0;
}
/// <summary>
/// 遍历ZIP目录
/// </summary>
/// <param name="s">s</param>
/// <param name="directory">directory</param>
/// <param name="zipDir">zipDir</param>
private void ZipDirectory(ZipOutputStream s, string directory, string zipDir)
{
DirectoryInfo info = new DirectoryInfo(directory);
foreach (FileInfo info2 in info.GetFiles())
{
this.ZipFile(s, info2.FullName, zipDir + @"\" + info2.Name);
}
foreach (DirectoryInfo info3 in info.GetDirectories())
{
this.ZipDirectory(s, info3.FullName, zipDir + @"\" + info3.Name);
}
}
/// <summary>
/// 遍历ZIP目录
/// </summary>
/// <param name="s">s</param>
/// <param name="directory"></param>
private void ZipDirectory(ZipOutputStream s,string directory)
{
DirectoryInfo info = new DirectoryInfo(directory);
foreach (FileInfo info2 in info.GetFiles())
{
this.ZipFile(s, new string[] { info2.FullName, info2.Name });
}
foreach (DirectoryInfo info3 in info.GetDirectories())
{
this.ZipDirectory(s, info3.FullName);
}
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="fileToZip">压缩前的文件</param>
/// <param name="zipedFile">压缩后的文件</param>
public void ZipFile(string fileToZip, string zipedFile)
{
if (!File.Exists(fileToZip))
{
throw new FileNotFoundException("The specified file " + fileToZip + " could not be found. Zipping aborderd");
}
FileInfo info = new FileInfo(fileToZip);
ZipOutputStream s = new ZipOutputStream(File.Create(zipedFile));
this._fileCount = 1;
this.ZipFile(s, fileToZip, info.Name);
s.Finish();
s.Close();
//this.ZipProgress(1L, 1L, 1, 1, "");
this._fileCount = 0;
this._fileIndex = 0;
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="s">s</param>
/// <param name="file">file</param>
/// <param name="zipDir">zipDir</param>
private void ZipFile(ZipOutputStream s, string file, string zipDir)
{
FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read);
ZipEntry entry = new ZipEntry(zipDir);
s.PutNextEntry(entry);
s.SetLevel(this.CompressionLevel);
byte[] buffer = new byte[this.BlockSize];
int count = stream.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, count);
this._fileIndex++;
try
{
while (count < stream.Length)
{
int num2 = stream.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, num2);
count += num2;
// this.ZipProgress((long)count, stream.Length, this.FileIndex, this.FileCount, file);
}
stream.Close();
}
catch (Exception exception)
{
stream.Close();
throw exception;
}
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="s">s</param>
/// <param name="param">param[0]文件路径及文件名param[1]文件名</param>
private void ZipFile(ZipOutputStream s,string[] param)
{
FileStream stream = new FileStream(param[0], FileMode.Open, FileAccess.Read);
ZipEntry entry = new ZipEntry(param[1]);
s.PutNextEntry(entry);
s.SetLevel(this.CompressionLevel);
byte[] buffer = new byte[this.BlockSize];
int count = stream.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, count);
this._fileIndex++;
try
{
while (count < stream.Length)
{
int num2 = stream.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, num2);
count += num2;
// this.ZipProgress((long)count, stream.Length, this.FileIndex, this.FileCount, file);
}
stream.Close();
}
catch (Exception exception)
{
stream.Close();
throw exception;
}
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="sourceFolder">源文件夹</param>
/// <param name="destFile">目标文件</param>
public void ZipFileMain(string sourceFolder, string destFile)
{
this.GetDirCount(sourceFolder);
DirectoryInfo info = new DirectoryInfo(sourceFolder);
ZipOutputStream s = new ZipOutputStream(File.Create(destFile));
s.SetLevel(this._compressionLevel);
this.ZipDirectory(s, sourceFolder, info.Name);
s.Finish();
s.Close();
// this.ZipProgress(1L, 1L, 1, 1, "");
this._fileCount = 0;
this._fileIndex = 0;
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="param">param[0]源路径param[1]目标路径</param>
public void ZipFileMain(string[] param)
{
this.GetDirCount(param[0]);
DirectoryInfo info = new DirectoryInfo(param[0]);
ZipOutputStream s = new ZipOutputStream(File.Create(param[1]));
s.SetLevel(this._compressionLevel);
this.ZipDirectory(s, param[0]);
s.Finish();
s.Close();
// this.ZipProgress(1L, 1L, 1, 1, "");
this._fileCount = 0;
this._fileIndex = 0;
}
/// <summary>
/// 包大小
/// </summary>
public int BlockSize
{
get
{
if (this._blockSize == 0)
{
return 0x400;
}
return this._blockSize;
}
set
{
this._blockSize = value;
}
}
/// <summary>
/// 压缩级别
/// </summary>
public int CompressionLevel
{
get
{
if (this._compressionLevel == 0)
{
return 6;
}
return this._compressionLevel;
}
set
{
this._compressionLevel = value;
}
}
}
}