[Tools] DotNetZip
.NET Framework 에서 사용할 Zip library 로 DotNetZip 을 사용했습니다.
사용도 편하여 사용 code snippet ? 을 적어봅니다.
공식 Source Page 는 사용이 정지된 Codeplex 의 https://dotnetzip.codeplex.com/ 입니다.
해당 페이지에 사용법도 간단하게? 설명해 놓았습니다.
NuGet Package Manager 에서도 DotNetZip 이라는 Keyword 로 찾으시면 되겠습니다.
제가 사용한 Version 은 1.10.1 입니다.
참조 추가는 해주시고요.
using Ionic.Zip; 으로 사용을 시작하겠습니다.
using Ionic.Zip;
. . .
string strFileName = string.Format("NewZip_{0}.zip", DateTime.Now.ToString("yyyyMMddHHmm"));
bool isOk = false;
saveFileDialog.CreatePrompt = false;
saveFileDialog.CheckFileExists = false;
saveFileDialog.CheckPathExists = true;
saveFileDialog.Filter = FileFilterZip;
saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
saveFileDialog.FileName = strFileName;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
strFileName = saveFileDialog.FileName;
. . .
using (ZipFile zip = new ZipFile { CompressionLevel = CompressionLevel.BestCompression })
{
. . .
strPath = Path.Combine(strFileFolderPath, "FileName.ext");
if (File.Exists(strPath))
{
// Zip 파일내에 FolderName 이라는 Folder 를 만들어 그 폴더 내에 저장합니다.
zip.AddFile(fi.FullName, "FolderName");
// Zip 파일의 Root 에 저장됩니다.
// zip.AddFile(strPath, "");
// 이렇게 하시면 System 의 Folder 구조대로 Zip 파일에 포함됩니다.
// 경로의 앞부분은 잘립니다.
// 어느 Depth 에서 잘리는지 확인하지 못했네요. 직접 확인해보시길... ^^;
// zip.AddFile(strPath);
}
if (Directory.Exists(strFolderPath))
{
// Directory 를 Zip 파일에 포함하고자 할 때 사용하는 구문이겠죠?
zip.AddDirectory(strFolderPath, strFolderName);
// 이렇게 하시면 System 의 Folder 구조대로 Zip 파일에 포함됩니다.
// 이 경우도 마찬가지로 경로의 앞부분은 잘립니다. ^^;
// zip.AddDirectory(strFolderPath);
}
string strRootPath = dirRoot.FullName;
foreach (var d in dirRoot.GetDirectories())
{
// 이렇게도 사용할 수 있습니다.
// 이렇게하면 Zip 파일내에 New_Folder 라는
// Directory 를 생성하고 그 아래에 d Directory를 포함시킵니다.
zip.AddDirectory(d.FullName, "New_Folder"
+ d.FullName.Replace(strRootPath, string.Empty));
}
. . .
zip.Save(strFileName);
isOk = true;
}
if (isOk)
{
DialogResult result = MessageBox.Show("Completed Export Work.\n\t Do you want to open result file's location?", "Network Designer", MessageBoxButtons.OKCancel);
//압축에 성공했으니 압축결과물을 봐야겠죠? ^^
if (result == DialogResult.OK)
{
if (File.Exists(strFileName))
{
Process.Start(new ProcessStartInfo("explorer.exe", " /select, " + strFileName));
}
}
}
}
사이트에 설명이 되어 있는 내용도 있고 제가 요리조리 잔머리 굴리며 작성한 내용입니다.
Folder 이름이나 파일명을 바꾸느라 코드에 오류가 있을 수도 있습니다. ^^;
오류가 있더라도 큰 줄기만 잘 살펴보시고...
도움이 되었으면 합니다.
행복한 고수되셔요.
woojja ))*
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\