027. 압축설정/압축해제
반응형

이번엔 압축하기와 압축해제하기..

 

보통은 그냥 반디집으로 다 압축해버리지... 누가 또 프로그래밍을 해서...하겠는가.....

 

근데 또 모를일이지...압축까지 해버리면.....또 편하니깐.....

 

그러니..압축하는것과 압축해제하는 부분을 해보자!!

 

바로 코드 ㄱㄱ

 

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
<Window x:Class="TestProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"      
        xmlns:local="clr-namespace:TestProject"
        mc:Ignorable="d"
        Title="MainWindow" Height="500" Width="650">
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>
 
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
 
            <Button Grid.Column="0" Content="ZIP" Click="Button_Click"/>
            <Button Grid.Column="1" Content="UnZIP" Click="Button_Click_1"/>
            <Button Grid.Column="2" Content="FOLDER_ZIP" Click="Button_Click_2"/>
 
        </Grid>
    </Grid>
</Window>
 
cs

 

일단 UI는 뭐가 없다...

 

그냥 버튼3개.. 압축버튼/압축해제 버튼/폴더압축버튼!!

 

각각 버튼에 Code-behind에다가.... 처리...

 

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
 
namespace TestProject
{
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
 
        public bool FileCompression_Multiple(string[] files)
        {
            //예외처리
            if (files == null)
            {
                return false;
            }
            if (files.Length == 0)
            {
                return false;
            }
 
            FileInfo fi = new FileInfo(files[0]);
            string parentFolderPath = fi.DirectoryName;
            string tempFolderName = fi.Name.Split('.')[0];
            DirectoryInfo di = new DirectoryInfo(parentFolderPath + "\\" + tempFolderName);
 
            //첫번째 파일의 명을 가지고 폴더를 생성하므로 동일한 폴더가 없는지 체크 후 압축함.
            if (di.Exists == false)
            {
                //폴더 생성
                di.Create();
 
                //선택 파일 폴더로 이동
                foreach (var filePath in files)
                {
                    FileInfo f = new FileInfo(filePath);
                    File.Copy(filePath, di.FullName + "\\" + f.Name);
                }
 
                //폴더 압축
                ZipFile.CreateFromDirectory(di.FullName, di.FullName + ".zip");
 
                //임시 생성 폴더 삭제
                di.Delete(true);
                return true;
            }
 
            return false;
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog() { Multiselect = true };
 
            if (ofd.ShowDialog() == true)
            {
                FileCompression_Multiple(ofd.FileNames);
            }
        }
 
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog() { Multiselect = true };
 
            if (ofd.ShowDialog() == true)
            {
                System.IO.Compression.ZipFile.ExtractToDirectory(ofd.FileName, ofd.FileName.Replace(".zip"""));
            }
        }
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            /// 폴더 압축
 
            FolderBrowserDialog folderSelectorDialog = new FolderBrowserDialog();
            SaveFileDialog SF = new SaveFileDialog();
 
            if (folderSelectorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string folderPath = folderSelectorDialog.SelectedPath;
 
                if (SF.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string savePath = SF.FileName + ".zip";
                    ZipFile.CreateFromDirectory(folderPath, savePath);
                }
            }
        }
    }
}
cs

 

뭐..코드가 없다... 걍 간단...함.....

 

 

각 버튼 누를때마다 파일/폴더 등을 선택해서 압축하고.. 파일을 선택해서 압축 해제...

 

다 라이브러리로 제공을 해주기 때문에 간단한 알고리즘?(파일선택에 대한)처리만 해주면 끝난다..

 

파일은 요기

TestProject.zip
0.09MB

반응형