첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170
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 파일을 참조한다.

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

댓글을 달아 주세요