๐ 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 < 1) return;
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 < 1) return;
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 |
(์ฐธ๊ณ ๋ฌธ์)