Hello World

C#(WINFORM, WINCE, WINDOWS MOBILE) 실전 활용 예제소스 및 프로그램 개발

[C#]파일 압축/해제하기 예제

댓글 0

C#

2016. 9. 2.

폴더의 파일을 압축과 압축된 파일을 폴더에 해제하기 예제 코드입니다.


첨부파일은 ICSharpCode.SharpZipLib.dll 파일이오니 참조 추가 후 사용하면 됩니다.


using System.IO;
using ICSharpCode.SharpZipLib.Zip;


//D 드라이브 test 폴더를 test.zip파일로 압축
CompressFile("D:\\test€", "D:\\test.zip");


//test.zip 파일을 D드라이브의 123 폴더에 압축해제
DecompressFile("D:\\test.zip", "D:\\123");


//파일 압축
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();
}

//파일 압축 해제
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();
}

ICSharpCode.SharpZipLib.zip



- 첨부파일

ICSharpCode.SharpZipLib.zip