첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.00MB

▶ ZIPFileHelper.cs

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); 

                        try
                        {
                            zipArchive.CreateEntryFromFile(filePath, relativePath);
                        }
                        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
    }
}

 

728x90

 

▶ Program.cs

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            ZIPFileHelper.CreateZIPFile("d:\\source", "d:\\source.zip");

            ZIPFileHelper.ExtractZIPFile("d:\\source.zip", "d:\\target");
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요