본문 바로가기
Study/Programming Language

[C#] ICSharpCode 라이브러리를 이용한 파일 압축 / 해제

by Leo-Kim 2010. 12. 13.

class ZipLib
{
        //파일 경로 가져오기
        public static string[] GetFileList(string path)
        {
            string[] files = Directory.GetFiles(path);
            string[] directories = Directory.GetDirectories(path);

            if (directories.Length > 0)
            {
                foreach (string directory in directories)
                {
                    string[] subfiles = GetFileList(directory);  //재귀
                    if (subfiles.Length > 0)
                    {
                        int len = files.Length;
                        string[] newlist = new string[len + subfiles.Length];
                        files.CopyTo(newlist, 0);
                        for (int i = 0; i < subfiles.Length; i++)
                        {
                            newlist[len + i] = subfiles[i];
                        }
                        files = newlist;
                    }
                }
            }
            return files;
        }

        // 압축하기
        public static void Zip(string TargetFolder, string ZipName)
        {
            string[] FileNames = GetFileList(TargetFolder);
            Crc32 crc = new Crc32();

            ZipOutputStream s = new ZipOutputStream(File.Create(ZipName));

            s.SetLevel(9);

            foreach (string file in FileNames)
            {
                FileStream fs = File.OpenRead(file);

                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);

                ZipEntry entry = new ZipEntry(file.Replace(TargetFolder, ""));

                entry.DateTime = DateTime.Now;

                entry.Size = fs.Length;
                fs.Close();

                crc.Reset();
                crc.Update(buffer);

                entry.Crc = crc.Value;

                s.PutNextEntry(entry);
                s.Write(buffer, 0, buffer.Length);
            }
            s.Finish();
            s.Close();
        }

        //압축 해제
        public static void Unzip(string ZipName, string SavePath)
        {
            ZipInputStream s = new ZipInputStream(File.OpenRead(ZipName));

            ZipEntry theEntry;

            while ((theEntry = s.GetNextEntry()) != null)
            {
                string FullName = SavePath + "\\" + theEntry.Name;
                string DirName = Path.GetDirectoryName(FullName);
                string FileName = Path.GetFileName(FullName);

                if (!Directory.Exists(DirName)) Directory.CreateDirectory(DirName);

                if (FileName != String.Empty)
                {
                    FileStream SW = File.Create(FullName);

                    int Size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        Size = s.Read(data, 0, data.Length);
                        if (Size > 0) SW.Write(data, 0, Size);
                        else break;
                    }
                    SW.Close();
                }
            }
            s.Close();
        }
}

'Study > Programming Language' 카테고리의 다른 글

[C#] 프로그램 실행시간 체크  (0) 2011.06.23
[C#] 프로세스 메모리 체크  (0) 2011.06.23

댓글