반응형
public class ZipFileHelper
{
#region Public Delegates
public delegate void ZippingListProgressEvent(int entriesTotal, int entriesSaved);
public delegate void ZippingFileProgressEvent(int fileProgress);
public delegate void ZippingDoneEvent();
public delegate void UnzippingProgressEvent(int progress);
#endregion Public Delegates
#region Public Events
public event ZippingDoneEvent ZippingDone;
public event ZippingListProgressEvent ZippingListProgress;
public event ZippingFileProgressEvent ZippingFileProgress;
public event UnzippingProgressEvent Unzipping;
#endregion Public Events
#region Public Methods
int _numEntriesToAdd = 0;
int _numEntriesAdded = 0;
public void CreateZipFile(string directoryPath, string zipFilePath)
{
using (ZipFile zip = new ZipFile())
{
// add this map file into the "images" directory in the zip archive
//zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
// add the report into a different directory in the archive
//zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
//zip.AddFile("ReadMe.txt");
zip.SaveProgress += zip_SaveProgress;
zip.AddProgress += zip_AddProgress;
zip.AddDirectory(directoryPath);
zip.Save(zipFilePath);
}
}
public void CreateZipFile(string directoryPath, string zipFilePath, string strPassword)
{
using (ZipFile zip = new ZipFile())
{
// add this map file into the "images" directory in the zip archive
//zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
// add the report into a different directory in the archive
//zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
//zip.AddFile("ReadMe.txt");
if (!strPassword.IsNullOrEmpty())
{
zip.Password = strPassword;
zip.Encryption = EncryptionAlgorithm.WinZipAes256;
}
zip.SaveProgress += zip_SaveProgress;
zip.AddProgress += zip_AddProgress;
zip.AddDirectory(directoryPath);
zip.Save(zipFilePath);
}
}
public void CreateZipFileFromMultiFolder(string[] arrDirectoryPath, string zipFilePath, string strPassword)
{
using (ZipFile zip = new ZipFile())
{
// add this map file into the "images" directory in the zip archive
//zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
// add the report into a different directory in the archive
//zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
//zip.AddFile("ReadMe.txt");
if (!strPassword.IsNullOrEmpty())
{
zip.Password = strPassword;
zip.Encryption = EncryptionAlgorithm.WinZipAes256;
}
zip.SaveProgress += zip_SaveProgress;
zip.AddProgress += zip_AddProgress;
string[] folderName = new string[arrDirectoryPath.GetLength(0)];
int i = 0;
foreach (string path in arrDirectoryPath)
{
int index = arrDirectoryPath[0].LastIndexOf("\\");
folderName[i] = path.Substring(index + 1);
zip.AddDirectory(path, folderName[i]);
}
zip.Save(zipFilePath);
}
}
void zip_AddProgress(object sender, AddProgressEventArgs e)
{
switch (e.EventType)
{
case ZipProgressEventType.Adding_Started:
Console.WriteLine("Adding files to the zip...");
break;
case ZipProgressEventType.Adding_AfterAddEntry:
_numEntriesAdded++;
Console.WriteLine(String.Format("Adding file {0}/{1} :: {2}",
_numEntriesAdded, _numEntriesToAdd, e.CurrentEntry.FileName));
break;
case ZipProgressEventType.Adding_Completed:
Console.WriteLine("Added all files");
break;
}
}
public void CreateZipFile(string[] files, string zipFilePath)
{
using (ZipFile zip = new ZipFile())
{
zip.SaveProgress += zip_SaveProgress;
foreach (string file in files)
{
zip.AddFile(file);
}
zip.Save(zipFilePath);
}
}
public void CreateZipFileFromFile(string directoryFile, string fileNameInArchive, string zipFilePath, string strPassword)
{
using (ZipFile zip = new ZipFile())
{
// add this map file into the "images" directory in the zip archive
//zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
// add the report into a different directory in the archive
//zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
//zip.AddFile("ReadMe.txt");
if (!strPassword.IsNullOrEmpty())
{
zip.Password = strPassword;
zip.Encryption = EncryptionAlgorithm.WinZipAes256;
}
zip.SaveProgress += zip_SaveProgress;
zip.AddProgress += zip_AddProgress;
zip.AddFile(directoryFile, fileNameInArchive);
//zip.ParallelDeflateThreshold = -1;
zip.Save(zipFilePath);
}
}
public bool ExtractZIPFile(string zipFilePath, string targetDirPath)
{
bool isSuccess = false;
try
{
using (ZipFile zip = new ZipFile(zipFilePath))
{
zip.ExtractProgress += zip_ExtractProgress;
zip.ExtractAll(targetDirPath, ExtractExistingFileAction.OverwriteSilently);
}
if (!Directory.Exists(targetDirPath))
{
isSuccess = false;
}
}
catch (Exception ex)
{
isSuccess = false;
}
return isSuccess;
}
#endregion Public Methods
#region Private Methods
private void zip_ExtractProgress(object sender, ExtractProgressEventArgs e)
{
if (Unzipping != null)
{
Unzipping((int)((double)e.BytesTransferred / ((double)e.TotalBytesToTransfer + 1) * 100));
}
}
private void zip_SaveProgress(object sender, SaveProgressEventArgs e)
{
if (e.EventType == ZipProgressEventType.Saving_Started)
{
//MessageBox.Show("Begin Saving: " + e.ArchiveName);
}
else if (e.EventType == ZipProgressEventType.Saving_BeforeWriteEntry)
{
//labelCompressionStatus.Text = "Writing: " + e.CurrentEntry.FileName + " (" + (e.EntriesSaved + 1) + "/" + e.EntriesTotal + ")";
//labelFilename.Text = "Filename:" + e.CurrentEntry.LocalFileName;
//progressBar2.Maximum = e.EntriesTotal;
//progressBar2.Value = e.EntriesSaved + 1;
if (ZippingListProgress != null)
{
ZippingListProgress(e.EntriesTotal, e.EntriesSaved + 1);
}
}
else if (e.EventType == ZipProgressEventType.Saving_EntryBytesRead)
{
//progressBar1.Value = (int)((e.BytesTransferred * 100) / e.TotalBytesToTransfer);
}
else if (e.EventType == ZipProgressEventType.Saving_Completed)
{
//MessageBox.Show("Done: " + e.ArchiveName);
if (ZippingDone != null)
{
ZippingDone();
}
}
if (ZippingFileProgress != null)
{
int progress = 0;
//progress = (int)((double)e.BytesTransferred / ((double)e.TotalBytesToTransfer + 1) * 100);
if (e.TotalBytesToTransfer > 0)
{
progress = (int)((e.BytesTransferred * 100) / e.TotalBytesToTransfer);
}
ZippingFileProgress(progress);
}
}
#endregion Private Methods
}
반응형
'[====== Development ======] > C#' 카테고리의 다른 글
WeakReference Class (0) | 2021.05.20 |
---|---|
File 변경 감시 (0) | 2021.05.12 |
WPF Code behind에서 Grid안의 Control 의 위치를 변경하는 방법 (0) | 2021.04.19 |
C# Coding Standards and Naming Conventions (0) | 2021.04.19 |
자동 시작 프로그램에 등록하는 기능 (0) | 2021.04.19 |