[C#/COMMON] ZipArchive 클래스 : ZIP 파일 생성하기/추출하기 (기능 개선)
■ ZipArchive 클래스를 사용해 ZIP 파일을 생성하고 추출하는 방법을 보여준다. (기능 개선) ▶ ZIPFileHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
using System.Collections.Generic; using System.IO; using System.IO.Compression; namespace TestProject { /// <summary> /// ZIP 파일 헬퍼 /// </summary> public static class ZIPFileHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region ZIP 파일 생성하기 - CreateZIPFile(sourceDirectoryPath, targetFilePath, exceptionDirectoryPathList) /// <summary> /// ZIP 파일 생성하기 /// </summary> /// <param name="sourceDirectoryPath">소스 디렉토리 경로</param> /// <param name="targetFilePath">타겟 파일 경로</param> /// <param name="exceptionDirectoryPathList">제외 디렉토리 경로</param> public static void CreateZIPFile(string sourceDirectoryPath, string targetFilePath, List<string> exceptionDirectoryPathList = null) { if(File.Exists(targetFilePath)) { File.SetAttributes(targetFilePath, FileAttributes.Normal); File.Delete(targetFilePath); } using(FileStream fileStream = new FileStream(targetFilePath, FileMode.Create, FileAccess.ReadWrite)) { using(ZipArchive zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Create)) { foreach(string directoryPath in Directory.EnumerateDirectories(sourceDirectoryPath, "*", SearchOption.AllDirectories)) { string relativePath = directoryPath.Substring(sourceDirectoryPath.Length + 1).Replace("\\", "/") + "/"; if(CanExcept(exceptionDirectoryPathList, relativePath)) { continue; } zipArchive.CreateEntry(relativePath); } foreach(string filePath in Directory.EnumerateFiles(sourceDirectoryPath, "*.*", SearchOption.AllDirectories)) { string relativePath = filePath.Substring(sourceDirectoryPath.Length + 1).Replace("\\", "/"); if(CanExcept(exceptionDirectoryPathList, relativePath)) { continue; } try { using(FileStream sourceStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite)) { ZipArchiveEntry zipArchiveEntry = zipArchive.CreateEntry(relativePath); using(Stream targetStream = zipArchiveEntry.Open()) { sourceStream.CopyTo(targetStream); } } } catch(PathTooLongException) { } } } } } #endregion #region ZIP 파일 추출하기 - ExtractZIPFile(sourceFilePath, targetDirectoryPath) /// <summary> /// ZIP 파일 추출하기 /// </summary> /// <param name="sourceFilePath">소스 파일 경로</param> /// <param name="targetDirectoryPath">타겟 디렉토리 경로</param> public static void ExtractZIPFile(string sourceFilePath, string targetDirectoryPath) { if(!Directory.Exists(targetDirectoryPath)) { Directory.CreateDirectory(targetDirectoryPath); } using(ZipArchive zipArchive = ZipFile.OpenRead(sourceFilePath)) { foreach(ZipArchiveEntry zipArchiveEntry in zipArchive.Entries) { try { string folderPath = Path.GetDirectoryName(Path.Combine(targetDirectoryPath, zipArchiveEntry.FullName)); if(!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } if(zipArchiveEntry.FullName.EndsWith("/")) { continue; } zipArchiveEntry.ExtractToFile(Path.Combine(targetDirectoryPath, zipArchiveEntry.FullName)); } catch(PathTooLongException) { } } } } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 제외 여부 구하기 - CanExcept(exceptionDirectoryPathList, relativePath) /// <summary> /// 제외 여부 구하기 /// </summary> /// <param name="exceptionDirectoryPathList">제외 디렉토리 경로 리스트</param> /// <param name="relativePath">상대 경로</param> /// <returns>제외 여부</returns> private static bool CanExcept(List<string> exceptionDirectoryPathList, string relativePath) { if(exceptionDirectoryPathList == null || exceptionDirectoryPathList.Count == 0) { return false; } foreach(string value in exceptionDirectoryPathList) { if(relativePath.StartsWith(value)) { return true; } } return false; } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using System.Collections.Generic; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { List<string> exceptionDirectoryPathList = new List<string> { "test/sample.txt" }; ZIPFileHelper.CreateZIPFile("d:\\source", "d:\\source.zip", exceptionDirectoryPathList); ZIPFileHelper.ExtractZIPFile("d:\\source.zip", "d:\\target"); } #endregion } } |
TestProject.zip