AutoPackage/AutoPekage/ZipControl.cs

203 lines
7.1 KiB
C#
Raw Permalink Normal View History

2017-02-05 15:30:23 +08:00
using SharpCompress.Archives;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
using SharpCompress.Readers;
using SharpCompress.Writers;
using SharpCompress.Common;
namespace AutoPekage
{
class ZipControl
{
public delegate void NewFileDecompressHandle(string filePath);
public static event NewFileDecompressHandle NewFileDecompress;
public delegate bool FileWillDecompressHandle(string filePath);
public static event FileWillDecompressHandle FileWillDecompress;
/// <summary>
/// ZIP:解压一个zip文件
/// add yuangang by 2016-06-13
/// </summary>
/// <param name="ZipFile">需要解压的Zip文件绝对路径</param>
/// <param name="Password">解压密码</param>
/// <param name="OverWrite">是否覆盖已存在的文件</param>
public static void UnZip(string ZipFile, string Password, bool OverWrite = true)
{
//如果解压到的目录不存在,则报错
//if (!System.IO.Directory.Exists(Form1.tempPath))
//{
// Directory.CreateDirectory(Form1.tempPath);
//}
//using (ZipInputStream zipfiles = new ZipInputStream(File.OpenRead(ZipFile)))
//{
// zipfiles.Password = Password;
// ZipEntry theEntry;
// while ((theEntry = zipfiles.GetNextEntry()) != null)
// {
// string pathToZip = "";
// pathToZip = theEntry.Name;
// string fileName = Path.GetFileName(pathToZip);
// if (fileName != "")
// {
// string filePath = Path.Combine(Form1.tempPath, fileName);
// if ((File.Exists(Form1.tempPath + fileName) && OverWrite) || (!File.Exists(filePath)))
// {
// using (FileStream streamWriter = File.Create(filePath))
// {
// int size = 2048;
// byte[] data = new byte[2048];
// while (true)
// {
// size = zipfiles.Read(data, 0, data.Length);
// if (size > 0)
// streamWriter.Write(data, 0, size);
// else
// break;
// }
// streamWriter.Close();
// }
// if (NewFileDecompress != null)
// {
// NewFileDecompress(filePath);
// }
// }
// }
// }
// zipfiles.Close();
//}
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="zipPath">压缩文件路径</param>
/// <param name="fileList">文件列表</param>
public static void Zip(string zipPath, List<string> fileList)
{
//ZipOutputStream zipStream = null;
//FileStream streamWriter = null;
//try
//{
// //Use Crc32
// Crc32 crc32 = new Crc32();
// //Create Zip File
// zipStream = new ZipOutputStream(File.Create(zipPath));
// //Specify Level
// zipStream.SetLevel(9);
// //Foreach File
// foreach (string file in fileList)
// {
// //Check Whether the file exists
// if (!File.Exists(file))
// {
// throw new FileNotFoundException(file);
// }
// //Read the file to stream
// streamWriter = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
// byte[] buffer = new byte[streamWriter.Length];
// streamWriter.Read(buffer, 0, buffer.Length);
// streamWriter.Close();
// //Specify ZipEntry
// crc32.Reset();
// crc32.Update(buffer);
// ZipEntry zipEntry = new ZipEntry(Path.GetFileName(file));
// zipEntry.DateTime = DateTime.Now;
// zipEntry.Size = buffer.Length;
// zipEntry.Crc = crc32.Value;
// //Put file info into zip stream
// zipStream.PutNextEntry(zipEntry);
// //Put file data into zip stream
// zipStream.Write(buffer, 0, buffer.Length);
// }
//}
//catch
//{
// throw;
//}
//finally
//{
// //Clear Resource
// if (streamWriter != null)
// {
// streamWriter.Close();
// }
// if (zipStream != null)
// {
// zipStream.Finish();
// zipStream.Close();
// }
//}
using (Stream stream = File.OpenWrite(zipPath))
{
using (var writer = WriterFactory.Open(stream, ArchiveType.Zip, new WriterOptions(CompressionType.Deflate)
{
LeaveStreamOpen = true
}))
{
foreach (var file in fileList)
{
writer.Write(Path.GetFileName(file), file);
}
}
if (!stream.CanWrite)
{
throw new InvalidOperationException();
}
}
}
public static void UnCompress(string ZipFile, string unCompressPath, bool OverWrite = true)
{
if (!Directory.Exists(unCompressPath))
{
Directory.CreateDirectory(unCompressPath);
}
using (Stream stream = File.OpenRead(ZipFile))
using (var archive = ArchiveFactory.Open(stream))
{
SharpCompress.Common.ArchiveEncoding.Default = Encoding.Default;
foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
{
if (FileWillDecompress != null)
{
if (!FileWillDecompress(entry.Key))
{
continue;
}
}
entry.WriteToDirectory(unCompressPath, new ExtractionOptions()
{
Overwrite = true
});
if (NewFileDecompress != null)
{
NewFileDecompress(Path.Combine(unCompressPath, Path.GetFileName(entry.Key)));
}
}
}
}
}
}