일단 Ionic.Zip 이라는 dll파일을 Download하도록 하자 (첨부파일에 있다)
그래서 참조 추가에서 다음과 같이 Ionic.Zip.dll을 찾아서 Import 시켜주도록 하자.
그러면 위와 같이 참조에 Ionic.Zip파일이 추가 된 것을 알 수 있다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ionic.Zip;
namespace UnZipping
{
class ZipClass
{
/// <summary>
/// 진행 상태를 알리기 위한 대리자
/// </summary>
/// <param name="progress"></param>
public delegate void ProgressEvent(long progress);
/// <summary>
/// Unzip할때 Progress를 나타내는 이벤트
/// </summary>
public event ProgressEvent Unzipping;
/// <summary>
/// Zip할때 Progress를 나타내는 이벤트
/// </summary>
public event ProgressEvent Zipping;
public ZipClass()
{
}
/// <summary>
/// 파일을 Unzip합니다.
/// </summary>
/// <param name="file">파일이름 (fullpath) </param>
/// <param name="dir">디렉토리명 (fullpath)</param>
public void UnZipFile(string file, string dir)
{
using (ZipFile zip = ZipFile.Read(file))
{
/// Unzipping 될때 이벤트 핸들러를 등록 (현재 진행 상황을 위해)
zip.ReadProgress += new EventHandler<ReadProgressEventArgs>(zip_ReadProgress);
/// Unzipping
zip.ExtractAll(dir, true);
}
}
/// <summary>
/// 파일들을 Zipping합니다.
/// </summary>
/// <param name="files">Zipping할 파일 이름들(fullPath)</param>
/// <param name="zipfileName">생성될 Zip파일 이름</param>
public void ZipFiles(string[] files, string zipfileName)
{
using (ZipFile zip = new ZipFile())
{
/// Zipping 될때 이벤트 핸들러를 등록 (현재 진행 상황을 위해)
zip.SaveProgress += new EventHandler<SaveProgressEventArgs>(zip_SaveProgress);
foreach (string file in files)
{
/// zip 할 List에 추가
zip.AddFile(file);
}
///Zipping
zip.Save(zipfileName);
}
}
#region 이벤트 핸들러
void zip_SaveProgress(object sender, SaveProgressEventArgs e)
{
if (Zipping != null)
{
Zipping((long)((double)e.BytesTransferred / ((double)e.TotalBytesToTransfer + 1) * 100));
}
}
private void zip_ReadProgress(object sender, ReadProgressEventArgs e)
{
if (Unzipping != null)
{
Unzipping((long)((double)e.BytesTransferred / ((double)e.TotalBytesToTransfer + 1) * 100));
}
}
#endregion
}
}
위는 제작한 ZipClass이다.
뭐 주석에 상세하게 써 놓았으니 이해 될 거라 생각된다.
private void button1_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
string fileName;
ZipClass zip = new ZipClass();
zip.Unzipping += new ZipClass.ProgressEvent(zip_Unzipping);
if (file.ShowDialog() == true)
{
fileName = file.FileName;
zip.UnZipFile(fileName, file.FileName.Substring(0, fileName.Length - file.SafeFileName.Length));
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.Multiselect = true;
ZipClass zip = new ZipClass();
zip.Zipping += new ZipClass.ProgressEvent(zip_Zipping);
if (file.ShowDialog() == true)
{
zip.ZipFiles(file.FileNames, Directory.GetCurrentDirectory()+"\\abc.zip");
}
}
void zip_Zipping(long progress)
{
this.zip.Text = progress.ToString();
}
void zip_Unzipping(long progress)
{
this.unzip.Text = progress.ToString();
}
실제적 사용은 위와 같은 형식으로 사용하면 된다.