728x90
폴더 내 파일 압축하기
String startPath = @"C:\test"; // 압축할 폴더 경로
String zipPath = @"C:\test.zip"; // 압축된 파일 경로 & 파일명
System.IO.FileInfo fi = new System.IO.FileInfo(zipPath);
if (!fi.Exists){
System.IO.Compression.ZipFile.CreateFromDirectory(startPath, zipPath); // 파일 압축하여 저장하기
}
다수의 파일 압축하기
// 압축할 파일들의 배열
String[] files = {@"C:\example.txt", @"C:\example2.txt"};
// 새로운 zip파일을 생성하고 Open하기
String zipPath = @"C:\test.zip";
var zip = ZipFile.Open(zipPath, ZipArchiveMode.Create);
foreach (var file in files){
// zip파일 안에 배열 내 파일들을 집어넣기
zip.CreateEntryFromFile(file, System.IO.Path.GetFileName(file), CompressionLevel.Optimal);
}
// zip 객체를 삭제
zip.Dispose();
압축 해제하기
String zipPath = @"C:\test.zip"; // zip 파일 경로
String extractPath = @"C:\test"; // 압축 해제할 폴더 경로
System.IO.Compression.ZipFile.ExtractToDirectory(zipPath, extractPath);
728x90
'프로그래밍 > C#' 카테고리의 다른 글
[C#] datatable타입 변수를 엑셀에 기록(Office 라이센스 필요없음) (0) | 2021.06.24 |
---|---|
[C#] URL에서 파일 바로 다운로드 받기 (0) | 2021.03.29 |
[C#] C#에서 VBScript 실행시키기 (0) | 2021.03.29 |
[C#] C#에서 python 스크립트를 불러오고 argument 주고받기 (0) | 2021.03.26 |
[C#] pop3로 메일 수신정보 가져오기 (0) | 2021.03.26 |