Ionic.Zip.dll 链接:https://pan.baidu.com/s/1p2ziRd38gVmgReXpGIWiXw 提取码:d563
ICSharpCode.SharpZipLib net2.0 链接:https://pan.baidu.com/s/1lJSe2W9hmIRBZsdkldufHw 提取码:3mpt
ICSharpCode.SharpZipLib net4.0 链接:https://pan.baidu.com/s/14HKQr0YfRDD67MKeN4bcSA 提取码:8947
1.問題描述
採用:Ionic.Zip.dll的方法
zipFile.Save(ZIPFile); 卡死,無反應,
問題是某些文件在一起時,壓縮卡死,文件單獨壓縮又沒有問題,,具體原因未知
解決方案:換其他的DLL如ICSharpCode.SharpZipLib方法,Ionic.Zip.dll的方法為找有效的方式
Ionic.Zip代碼如下
/// <summary>
/// 文件壓縮 ,小概率事件zipFile.Save 卡死
/// </summary>
/// <param name="files"></param>
/// <param name="ZIPFile"></param>
public void OldZIP(string[] files, string ZIPFile)
{
try
{
using (Ionic.Zip.ZipFile zipFile = new Ionic.Zip.ZipFile(System.Text.Encoding.UTF8))
{
for (int i = 0; i < files.Length; i++)
{
string path = files[i];
using (System.IO.FileStream fileStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite))
{
byte[] array = new byte[fileStream.Length];
fileStream.Read(array, 0, array.Length);
string fileName = System.IO.Path.GetFileName(path);
zipFile.AddEntry(fileName, array);
}
}
zipFile.Save(ZIPFile);
}
// 以下壓縮,中文字出現亂碼,沒出現之前的卡死現象
//using (System.IO.FileStream fStream = System.IO.File.Create(ZIPFile))
//{
// using (Ionic.Zip.ZipOutputStream zipStream = new Ionic.Zip.ZipOutputStream(fStream))
// {
// for (int i = 0; i < files.Length; i++)
// {
// string path = files[i];
// using (System.IO.FileStream fileStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite))
// {
// byte[] array = new byte[fileStream.Length];
// fileStream.Read(array, 0, array.Length);
// string fileName = System.IO.Path.GetFileName(path);
// fileStream.Close();
// byte[] filbyte = System.Text.Encoding.UTF8.GetBytes(fileName);
// zipStream.PutNextEntry(System.Text.Encoding.UTF8.GetString(filbyte));
// zipStream.Write(array, 0, array.Length);
// }
// }
// }
//}
}
catch (System.Exception innerException)
{
throw new System.Exception("Error zipping folder ", innerException);
}
}
換用ICSharpCode.SharpZipLib的方法如下
/// <summary>
/// 文件壓縮
/// </summary>
/// <param name="files">文件集合</param>
/// <param name="ZIPFile">壓縮文件名稱</param>
public static void ZIP(string[] files, string ZIPFile)
{
try
{
ICSharpCode.SharpZipLib.Checksums.Crc32 crc = new ICSharpCode.SharpZipLib.Checksums.Crc32();
using (System.IO.FileStream fStream = System.IO.File.Create(ZIPFile))
{
using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(fStream))
{
for (int i = 0; i < files.Length; i++)
{
string FileToZip = files[i];
using (FileStream fs = File.OpenRead(FileToZip))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
string fileName = System.IO.Path.GetFileName(FileToZip);
ICSharpCode.SharpZipLib.Zip.ZipEntry entry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(fileName);
entry.DateTime = DateTime.Now;
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zipStream.PutNextEntry(entry);
zipStream.Write(buffer, 0, buffer.Length);
}
}
}
}
}
catch (System.Exception innerException)
{
throw new System.Exception("Error zipping folder ", innerException);
}
}