IT/C#

[C#] ํด๋” ํ˜น์€ ํŒŒ์ผ, Zip ์••์ถ• ๋ฐ ์ถ”์ถœ(์••์ถ•ํ’€๊ธฐ)ํ•˜๋Š” ๋ฐฉ๋ฒ•. Send to Compressed (zipped) folder, Extract All.

Ella.J 2024. 6. 13. 15:53
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“Œ UI ๊ตฌ์„ฑ

๐Ÿ”Ž Folder Browser

: ์ง€์ •ํ•œ ๊ฒฝ๋กœ์—์„œ ํด๋” ๋ฐ ํŒŒ์ผ ๊ฐ€์ ธ์™€์„œ ๋ณด์—ฌ์ฃผ๋Š” ListView (listView1)

๐Ÿ”Ž  Send to Compressed (Zipped) Folder

: ์„ ํƒํ•œ ํด๋” ํ˜น์€ ํŒŒ์ผ ์••์ถ• Button (btnSendToZip)

๐Ÿ”Ž  Extract Compressed (Zipped) Folder

: ์„ ํƒํ•œ zip ํด๋” ์••์ถ• ํ•ด์ œ Button (btnExtractZip)

 

๋ˆˆ์น˜ ์ฑ„์‹  ๋ถ„...? ํŒŒ์ผ๋ช…์€ ์ œ๊ฐ€ ์ข‹์•„ํ•˜๋Š” ๋กค ์ฑ”ํ”ผ์–ธ๋“ค ์ž…๋‹ˆ๋‹ค ><

 



๐Ÿ“Œ ์ฝ”๋“œ ์„ค๋ช… 1

๐Ÿ”Ž  InitListView()

: ListView ์ดˆ๊ธฐ ์„ค์ • (ํ‘œ์‹œ๋ฐฉ์‹ : ๋””ํ…Œ์ผ, ์ค‘๋ณต์„ ํƒ : ๊ฐ€๋Šฅ, ์ปฌ๋Ÿผ 3๊ฐœ ์ถ”๊ฐ€)

๐Ÿ”Ž  ResetListView()

: Default Folder ์•ˆ์˜ ํด๋”์™€ ํŒŒ์ผ ๊ฐ€์ ธ์™€์„œ ListView์— ๋ณด์—ฌ์ฃผ๊ธฐ

 

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
public Form1()
{
    InitializeComponent();
 
    InitListView();
}
public string DefaultFolder = @"E:\Folder";
void InitListView()
//listview ์ดˆ๊ธฐ ์„ค์ • (ํ‘œ์‹œ๋ฐฉ์‹:๋””ํ…Œ์ผ, ์ค‘๋ณต์„ ํƒ:๊ฐ€๋Šฅ, ์ปฌ๋Ÿผ 3๊ฐœ ์ถ”๊ฐ€)
    listView1.View = View.Details;
    listView1.MultiSelect = true;
    listView1.Columns.Add("Name"200);
    listView1.Columns.Add("Date modified"180);
    listView1.Columns.Add("Type"180);
 
    ResetListView();
}
void ResetListView()
//Default Folder ์•ˆ์˜ ํด๋”์™€ ํŒŒ์ผ ๊ฐ€์ ธ์™€์„œ listView์— ๋ณด์—ฌ์ฃผ๊ธฐ (์ƒˆ๋กœ๊ณ ์นจ)
    listView1.Items.Clear();
 
    DirectoryInfo di = new DirectoryInfo(DefaultFolder);
    //foler list
    foreach (var folders in di.GetDirectories()) 
    {
        ListViewItem lvi = new ListViewItem(folders.Name);
        lvi.SubItems.Add(folders.LastAccessTime.ToString());
        lvi.SubItems.Add("Folder");
        listView1.Items.Add(lvi);
    }
    //file list
    foreach (var files in di.GetFiles()) 
    {
        ListViewItem lvi = new ListViewItem(files.Name);
        lvi.SubItems.Add(files.LastAccessTime.ToString());
        lvi.SubItems.Add(files.Extension);
        listView1.Items.Add(lvi);
    }
}
cs

 


 

๐Ÿ“Œ ์ฝ”๋“œ ์„ค๋ช… 2 

๐Ÿ”Ž  btnSendToZip_Click() : Send to Compressed (Zipped) Folder ๋ฒ„ํŠผ ํด๋ฆญ์ด๋ฒคํŠธ

: ListView์—์„œ ์„ ํƒ๋œ ํด๋” ๋ฐ ํŒŒ์ผ ์••์ถ•

: ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ ๋งจ ์ฒ˜์Œ ํด๋” ํ˜น์€ ํŒŒ์ผ๋ช…์œผ๋กœ zip ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜๋ฉฐ, ๋‚˜๋จธ์ง€ ํŒŒ์ผ๋“ค์„ zip ํŒŒ์ผ ์•ˆ์— ์ถ”๊ฐ€ํ•˜๋Š” ํ˜•์‹

: ์ฒซ๋ฒˆ์งธ๊ฐ€ ํด๋”๊ฐ€ ์•„๋‹Œ ํŒŒ์ผ์ธ ๊ฒฝ์šฐ DirectoryNotFoundException์ด ๋ฐœ์ƒํ•œ๋‹ค. ์˜ˆ์™ธ๋กœ ๋น ์ง€๋‚˜ zip ํŒŒ์ผ์€ ์ƒ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด ํŒŒ์ผ์„ zip ํŒŒ์ผ์— ์••์ถ•ํ•˜์—ฌ ์ถ”๊ฐ€ํ•œ๋‹ค.

ZipFile.CreateFromDirectory(startPath, zipPath);

 

ํด๋” ํ•˜๋‚˜๋งŒ ์••์ถ•ํ•˜๋Š” ๊ฒฝ์šฐ
ํŒŒ์ผ ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ์••์ถ•ํ•˜๋Š” ๊ฒฝ์šฐ

 

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
private void btnSendToZip_Click(object sender, EventArgs e)
//ํŒŒ์ผ ์••์ถ•ํ•˜๊ธฐ
    if (listView1.SelectedItems.Count < 1return;
 
    string startPath = "";
    string zipPath = "";
    ListViewItem lvi;
    string fileName = "";
 
    for (int i = 0; i < listView1.SelectedItems.Count; i++)
    {
        lvi = listView1.SelectedItems[i];
        fileName = lvi.SubItems[0].Text;
        //ํด๋”๊ฐ€ ์•„๋‹Œ ํŒŒ์ผ์„ ์••์ถ•ํ•˜๋Š” ๊ฒฝ์šฐ ํ™•์žฅ์ž ์ œ์™ธ
        if (lvi.SubItems[2].Text != "Folder") fileName = fileName.Replace(lvi.SubItems[2].Text, "");
 
        if (i == 0)
        { //ํ•˜๋‚˜๋งŒ ์„ ํƒํ•˜๋Š” ๊ฒฝ์šฐ ํ˜น์€ ์—ฌ๋Ÿฌ๊ฐœ ์„ ํƒ ์‹œ ๋งจ ์ฒ˜์Œ ํŒŒ์ผ๋ช…์œผ๋กœ zip ํŒŒ์ผ ์ƒ์„ฑ
            try
            {
                startPath = DefaultFolder + $@"\{fileName}";
                zipPath = DefaultFolder + $@"\{fileName}.zip";
 
                //*** ํŒŒ์ผ ์••์ถ• ***
                ZipFile.CreateFromDirectory(startPath, zipPath);
                //ํด๋” ์••์ถ•์˜ ๊ฒฝ์šฐ์—๋Š” ์˜ˆ์™ธ๋กœ ๋น ์ง€์ง€ ์•Š์Œ
            }
            catch (DirectoryNotFoundException)
            { //ํŒŒ์ผ ์••์ถ•์˜ ๊ฒฝ์šฐ, ํด๋”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ด ์—๋Ÿฌ๋กœ ๋น ์ง
                //zip ํŒŒ์ผ์€ ์ƒ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด ํŒŒ์ผ์„ zip ํŒŒ์ผ์— ์••์ถ•ํ•˜์—ฌ ์ถ”๊ฐ€
                using (FileStream zipToOpen = new FileStream(zipPath, FileMode.Open))
                {
                    using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                    {
                        archive.CreateEntryFromFile(DefaultFolder + $@"\{lvi.SubItems[0].Text}", lvi.SubItems[0].Text);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error : {ex}");
            }
        }
        else
        {
            //์—ฌ๋Ÿฌ๊ฐœ ์„ ํƒ์‹œ ์œ„์—์„œ ์ƒ์„ฑ๋œ zip ํŒŒ์ผ์— ์„ ํƒ๋œ ํŒŒ์ผ ์••์ถ•ํ•˜์—ฌ ์ถ”๊ฐ€
            using (FileStream zipToOpen = new FileStream(zipPath, FileMode.Open))
            {
                using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                {
                    archive.CreateEntryFromFile(DefaultFolder + $@"\{lvi.SubItems[0].Text}", lvi.SubItems[0].Text);
                }
            }
        }
    }
    //ListView ์ƒˆ๋กœ๊ณ ์นจ
    ResetListView();
}
cs

 


 

โญ๐Ÿ“Ž ์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š”๋ฐ,

.NET Framework๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ์•„๋ž˜ ๋‘ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.

๐Ÿ”Ž  System.IO.Compression

๐Ÿ”Ž  System.IO.Compression.FileSystem

 

 



๐Ÿ“Œ ์ฝ”๋“œ ์„ค๋ช… 3 

๐Ÿ”Ž  btnExtractZip_Click() : Extract Compressed (Zipped) Folder ๋ฒ„ํŠผ ํด๋ฆญ์ด๋ฒคํŠธ

: ListView์—์„œ ์„ ํƒ๋œ zip ํŒŒ์ผ ์••์ถ• ํ•ด์ œ

: ์ด๋ฏธ ํ•ด๋‹น ํด๋”๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ IOException์ด ๋ฐœ์ƒํ•˜๋Š” ๋ฐ, ์ด๋•Œ ๋ฉ”์‹œ์ง€ ๋ฐ•์Šค๋ฅผ ํ†ตํ•ด Copy Folder๋ฅผ ์ƒ์„ฑํ•  ๊ฒƒ์ธ์ง€ ํ™•์ธ ํ›„์— ํด๋”๋ช…์— _Copy๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋‹ค์‹œ ํŒŒ์ผ์„ ์ถ”์ถœํ•œ๋‹ค.

ZipFile.ExtractToDirectory(zipPath, extractPath);

 

์ค‘๋ณต๋œ ํŒŒ์ผ๋ช…์ด ์žˆ๋Š” ๊ฒฝ์šฐ ๋ฉ”์„ธ์ง€ ๋ฐ•์Šค

 

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
private void btnExtractZip_Click(object sender, EventArgs e)
{
    if (listView1.SelectedItems.Count < 1return;
 
    foreach (ListViewItem item in listView1.SelectedItems)
    {
        if (item.SubItems[2].Text == ".zip"//zip ํŒŒ์ผ์˜ ๊ฒฝ์šฐ์—๋งŒ ์ถ”์ถœ
        {
            string zipPath = DefaultFolder + $@"\{item.SubItems[0].Text}";
            string extractPath = DefaultFolder + $@"\{item.SubItems[0].Text.Replace(".zip", "")}";
 
            try
            {
                //*** ํŒŒ์ผ ์ถ”์ถœ ***
                ZipFile.ExtractToDirectory(zipPath, extractPath);
            }
            catch (IOException)
            { //์ด๋ฏธ ํ•ด๋‹น ํด๋”๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ Copy Folder๋ฅผ ์ƒ์„ฑํ• ๊บผ๋ƒ?
                if (MessageBox.Show("File Already Exists.\r\nCreate Copy Folder?.""Care!",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    //OK ํด๋ฆญ ์‹œ _Copy ํด๋”์— ์ถ”์ถœ
                    extractPath = DefaultFolder + $@"\{item.SubItems[0].Text.Replace(".zip", "_Copy")}";
                    //*** ํŒŒ์ผ ์ถ”์ถœ ***
                    ZipFile.ExtractToDirectory(zipPath, extractPath);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error : {ex}");
            }
        }
    }
    //ListView ์ƒˆ๋กœ๊ณ ์นจ
    ResetListView();
}
cs

 


 

(์ฐธ๊ณ ๋ฌธ์„œ)

 

๋ฐฉ๋ฒ•: ํŒŒ์ผ ์••์ถ• ๋ฐ ์ถ”์ถœ - .NET

System.IO.Compression์„ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ์„ ์••์ถ•ํ•˜๊ณ  ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค. ZipFile, ZipArchive, ZipArchiveEntry, DeflateStream ๋ฐ GZipStream์„ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

learn.microsoft.com

 

 

728x90
๋ฐ˜์‘ํ˜•