|
@@ -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; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
} |