■ ZIP 파일 압축/압축 해제하는 방법을 보여준다.
▶ 예제 코드 (C#)
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 |
using System.IO; using ICSharpCode.SharpZipLib.Zip; #region 파일 압축하기 - CompressFile(sourceDirectoryPath, targetFilePath) /// <summary> /// 파일 압축하기 /// </summary> /// <param name="sourceDirectoryPath">소스 디렉토리 경로</param> /// <param name="targetFilePath">타겟 ZIP 파일 경로</param> public void CompressFile(string sourceDirectoryPath, string targetFilePath) { DirectoryInfo sourceDirectoryInfo = new DirectoryInfo(sourceDirectoryPath); FileStream targetFileStream = new FileStream(targetFilePath, FileMode.Create); ZipOutputStream zipOutputStream = new ZipOutputStream(targetFileStream); zipOutputStream.SetComment(sourceDirectoryPath); zipOutputStream.SetLevel(9); byte[] bufferByteArray = new byte[2048]; foreach(FileInfo sourceFileInfo in sourceDirectoryInfo.GetFiles("*.*", SearchOption.AllDirectories)) { string sourceFileName = sourceFileInfo.FullName.Substring(sourceDirectoryInfo.FullName.Length + 1); zipOutputStream.PutNextEntry(new ZipEntry(sourceFileName)); using(FileStream sourceFileStream = sourceFileInfo.OpenRead()) { while(true) { int readCount = sourceFileStream.Read(bufferByteArray, 0, bufferByteArray.Length); if(readCount == 0) { break; } zipOutputStream.Write(bufferByteArray, 0, readCount); } } zipOutputStream.CloseEntry(); } zipOutputStream.Finish(); zipOutputStream.Close(); } #endregion #region 파일 압축 해제하기 - DecompressFile(sourceFilePath, targetDirectoryPath) /// <summary> /// 파일 압축 해제하기 /// </summary> /// <param name="sourceFilePath">소스 파일 경로</param> /// <param name="targetDirectoryPath">타겟 디렉토리 경로</param> public void DecompressFile(string sourceFilePath, string targetDirectoryPath) { DirectoryInfo targetDirectoryInfo = new DirectoryInfo(targetDirectoryPath); if(!targetDirectoryInfo.Exists) { targetDirectoryInfo.Create(); } FileStream sourceFileStream = new FileStream(sourceFilePath, FileMode.Open); ZipInputStream zipInputStream = new ZipInputStream(sourceFileStream); byte[] bufferByteArray = new byte[2048]; while(true) { ZipEntry zipEntry = zipInputStream.GetNextEntry(); if(zipEntry == null) { break; } if(zipEntry.Name.LastIndexOf('\\') > 0) { string subdirectory = zipEntry.Name.Substring(0, zipEntry.Name.LastIndexOf('\\')); if(!Directory.Exists(Path.Combine(targetDirectoryInfo.FullName, subdirectory))) { targetDirectoryInfo.CreateSubdirectory(subdirectory); } } FileInfo targetFileInfo = new FileInfo(Path.Combine(targetDirectoryInfo.FullName, zipEntry.Name)); using(FileStream targetFileStream = targetFileInfo.Create()) { while(true) { int readCount = zipInputStream.Read(bufferByteArray, 0, bufferByteArray.Length); if(readCount == 0) { break; } targetFileStream.Write(bufferByteArray, 0, readCount); } } } zipInputStream.Close(); } #endregion |
※ 첨부 ICSharpCode.SharpZipLib.dll 파일을 참조한다.