[CSharp] Zip 파일 압축, 해제

CSharp
참조할 어셈블리System.IO.CompressionSystem.IO.Compression.FileSystem // 압축 풀기 using (ZipArchive zipArchive = ZipFile.OpenRead(LOCAL_FILE)) { foreach (ZipArchiveEntry zipArchiveEntry in zipArchive.Entries) { try { string folderPath = Path.GetDirectoryName(Path.Combine(LOCAL_ZIPARCHIVE_PATH, zipArchiveEntry.FullName)); // if (!Directory.Exists(LOCAL_ZIPARCHIVE_PATH)) { Directory.CreateDirectory(LOCAL_ZIPARCHIVE_PATH); } // zipArchiveEntry.ExtractToFile(Path.Combine(LOCAL_ZIPARCHIVE_PATH, zipArchiveEntry.FullName), true); } catch (PathTooLongException pt_ex) { Console.WriteLine(pt_ex); } } } // 압축하기 using (FileStream fileStream = new FileStream(SFTP_DOWNLOAD_LOCAL_PATH, FileMode.Create, FileAccess.ReadWrite)) { using (ZipArchive zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Update)) { try { foreach (string filePath in Directory.EnumerateFiles(DOWNLOAD_LOCAL_ZIPARCHIVE_PATH, "*.*", SearchOption.AllDirectories)) { string relativePath = filePath.Substring(DOWNLOAD_LOCAL_ZIPARCHIVE_PATH.Length + 1); try { zipArchive.CreateEntryFromFile(filePath, relativePath); } catch (PathTooLongException pt_ex) { Console.WriteLine(pt_ex); } } } catch (Exception ex) { Console.WriteLine(ex); } finally { zipArchive.Dispose(); fileStream.Close(); } } } 단순하게 참조만 추가하니 정상적으로 되지 않아서 참조 방법을 조금더 자세히 기록을 합니다. 변경일 : 2021.06.19
Read More