본문 바로가기
2_ 바삭바삭 프로그래밍/C# and Visual C++

[C#] 프로그램 실행시간 체크, 프로세스 메모리 체크, ICSharpCode 라이브러리를 이용한 파일 압축 / 해제

by 준환이형님_ 2011. 7. 15.

저 요즘 C# 공부합니다. 형철이 형님 고맙습니다~


[C#] 프로그램 실행시간 체크 

?
1
2
3
4
5
6
Stopwatch sw = new Stopwatch();
sw.Start();
 
//하고싶은 일을 수행
 
Console.WriteLine(sw.ElapsedMilliseconds.ToString());



[C#] 프로세스 메모리 체크
 

?
1
System.Diagnostics.Process.GetCurrentProcess().PrivateMemorySize64.ToString();




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


 
?
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
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();
        }
}