ZipFile 클래스 사용을 위해선 System.IO.Compression.FileSystem 참조가 필요합니다.
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression; //ZipFile 에 필요
using System.Windows.Forms;
namespace WindowsFormsApp13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) //zip 생성
{
string workFolder = Application.StartupPath;
string targetDirectory = Path.Combine(workFolder, "TestFiles");
string savePath = Path.Combine(workFolder, "Zipfile.zip");
if (File.Exists(savePath)) //기존 파일은 제거
{
File.Delete(savePath);
}
ZipFile.CreateFromDirectory(targetDirectory, savePath);
Process.Start(workFolder); //폴더 열어서 확인
}
private void button2_Click(object sender, EventArgs e) //zip 해제
{
string workFolder = Application.StartupPath;
string targetPath = Path.Combine(workFolder, "Zipfile.zip");
string saveFolder = Path.Combine(workFolder, "unZip");
if (Directory.Exists(saveFolder)) //기존 파일은 제거
{
Directory.Delete(saveFolder, true);
}
ZipFile.ExtractToDirectory(targetPath, saveFolder);
Process.Start(workFolder); //폴더 열어서 확인
}
}
}
파일 압축 및 풀기 결과
그 밖에도 다른 라이브러리들이 있는데, NuGet 에서 받아서 사용이 가능합니다.
GPT에서 제공한 표를 남겨두겠습니다.
- GPT 정리 내용
라이브 러리 암호화 포맷 속도 최신 유지장점 요약 (2025년 10월 06일 기준!)
라이브러리
암호화
포맷
속도
최신 유지
장점 요약
System.IO.Compression
❌
ZIP
✅ 빠름
✅ 최신
.NET 내장, 간단
DotNetZip (Ionic.Zip)
✅
ZIP
⚪ 보통
❌ 구버전
암호 ZIP 지원
SharpZipLib
✅
ZIP, TAR, GZIP 등
⚪ 보통
✅ 유지보수 중
다양한 포맷
SharpCompress
⚠️ 제한적
ZIP, 7z, TAR 등
⚪ 보통
✅ 최신
스트리밍 지원
SevenZipSharp
✅ AES
7z
⚠️ 느림
⚪ 부분적
고압축, 보안
DotNetZip 간단 사용법 예시
using Ionic.Zip;
// 압축
using (var zip = new ZipFile())
{
zip.Password = "1234"; // 암호 설정
zip.AddDirectory(@"C:\TestSource");
zip.Save(@"C:\Result.zip");
}
// 해제
using (var zip = ZipFile.Read(@"C:\Result.zip"))
{
zip.Password = "1234";
zip.ExtractAll(@"C:\Extracted", ExtractExistingFileAction.OverwriteSilently);
}
+) 추가적으로 보이지 않지만 특정 위치에 파일 다이얼로그, 프린트 등 기능을 보여주기 위해서
위치 값을 조정할 때가 있습니다. WindowState 와 Visible 처리를 통해 해당 위치 제어가 가능!
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Tray : Form
{
public Tray()
{
InitializeComponent();
this.ShowInTaskbar = false; //하단 테스트바에서 보이지 않도록 설정
this.Visible = false; //화면에 보이지 않도록 설정
this.WindowState = FormWindowState.Minimized; //최소화
this.Opacity = 0; //투명하게 하여 타이틀도 안보이게 설정
this.FormBorderStyle = FormBorderStyle.FixedToolWindow; //Alt + Tab 에서도 보이지 않도록
//this.종료ToolStripMenuItem.Click += new System.EventHandler(this.종료ToolStripMenuItem_Click);
//잠시 특정 위치 활성화
this.WindowState = FormWindowState.Normal;
this.Visible = true;
//new OpenFileDialog().ShowDialog();
//다시 특정 위치 해제
this.WindowState = FormWindowState.Minimized;
this.Visible = false;
}
private void 종료ToolStripMenuItem_Click(object sender, System.EventArgs e)
{
Close();
}
}
}