Browse Source

【增加】增加ZipHelper:压缩文件和解压zip格式文件的功能;

新疆警官学校中职
dyy 1 year ago
parent
commit
3ef5f75324
2 changed files with 161 additions and 0 deletions
  1. +1
    -0
      Learun.Framework.Ultimate V7/Learun.Framework.Module/Learun.Util/Learun.Util/Learun.Util.csproj
  2. +160
    -0
      Learun.Framework.Ultimate V7/Learun.Framework.Module/Learun.Util/Learun.Util/Web/ZipHelper.cs

+ 1
- 0
Learun.Framework.Ultimate V7/Learun.Framework.Module/Learun.Util/Learun.Util/Learun.Util.csproj View File

@@ -141,6 +141,7 @@
<Compile Include="Tree\TreeModel.cs" />
<Compile Include="Web\VerifyCode.cs" />
<Compile Include="Web\WebHelper.cs" />
<Compile Include="Web\ZipHelper.cs" />
<Compile Include="XML\Xml.cs" />
</ItemGroup>
<ItemGroup>


+ 160
- 0
Learun.Framework.Ultimate V7/Learun.Framework.Module/Learun.Util/Learun.Util/Web/ZipHelper.cs View File

@@ -0,0 +1,160 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Core;

namespace ZipOneCode.ZipProvider
{
public class ZipHelper
{
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="sourceFilePath"></param>
/// <param name="destinationZipFilePath"></param>
public static void CreateZip(string sourceFilePath, string destinationZipFilePath)
{
if (sourceFilePath[sourceFilePath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
sourceFilePath += System.IO.Path.DirectorySeparatorChar;

ZipOutputStream zipStream = new ZipOutputStream(File.Create(destinationZipFilePath));
zipStream.SetLevel(6); // 压缩级别 0-9
CreateZipFiles(sourceFilePath, zipStream, sourceFilePath);

zipStream.Finish();
zipStream.Close();
}

/// <summary>
/// 递归压缩文件
/// </summary>
/// <param name="sourceFilePath">待压缩的文件或文件夹路径</param>
/// <param name="zipStream">打包结果的zip文件路径(类似 D:\WorkSpace\a.zip),全路径包括文件名和.zip扩展名</param>
/// <param name="staticFile"></param>
private static void CreateZipFiles(string sourceFilePath, ZipOutputStream zipStream, string staticFile)
{
//Crc32 crc = new Crc32();
string[] filesArray = Directory.GetFileSystemEntries(sourceFilePath);
foreach (string file in filesArray)
{
if (Directory.Exists(file)) //如果当前是文件夹,递归
{
CreateZipFiles(file, zipStream, staticFile);
}

else //如果是文件,开始压缩
{
string tempFile = file.Substring(staticFile.LastIndexOf("\\") + 1);
ZipEntry entry = new ZipEntry(tempFile);
zipStream.PutNextEntry(entry);

FileStream fileStream = File.OpenRead(file);

byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);

//entry.DateTime = DateTime.Now;
//entry.Size = fileStream.Length;
//crc.Reset();
//crc.Update(buffer);
//entry.Crc = crc.Value;

zipStream.Write(buffer, 0, buffer.Length);
fileStream.Close();

}
}
}

/// <summary>
/// 功能:解压zip格式的文件。
/// </summary>
/// <param name="zipFilePath">压缩文件路径</param>
/// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>
/// <returns>解压是否成功</returns>
public static bool UnZip(string zipFilePath, string unZipDir)
{
try
{
if (zipFilePath == string.Empty)
{
throw new Exception("压缩文件不能为空!");
}
if (!File.Exists(zipFilePath))
{
throw new FileNotFoundException("压缩文件不存在!");
}
//解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹
if (unZipDir == string.Empty)
{
unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
}
if (!unZipDir.EndsWith("/"))
{
unZipDir += "/";
}
if (!Directory.Exists(unZipDir))
{
Directory.CreateDirectory(unZipDir);
}
using (var s = new ZipInputStream(File.OpenRead(zipFilePath)))
{

ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if (!string.IsNullOrEmpty(directoryName))
{
Directory.CreateDirectory(unZipDir + directoryName);
}
if (directoryName != null && !directoryName.EndsWith("/"))
{
}

if (fileName != String.Empty)
{
using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
{

int size;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
}
}
s.Close();
}
return true;
}
catch (Exception e)
{

return false;
}

}


}
}

Loading…
Cancel
Save