027. 압축설정/압축해제

2021. 11. 9. 20:00·사소한 아이의 소소한 스킬/C#
반응형

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

 

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

 

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

 

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

 

바로 코드 ㄱㄱ

 

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>
 
Colored by Color Scripter
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);
                }
            }
        }
    }
}
Colored by Color Scripter
cs

 

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

 

 

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

 

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

 

파일은 요기

TestProject.zip
0.09MB

반응형
'사소한 아이의 소소한 스킬/C#' 카테고리의 다른 글
  • 029. Logical Operator (| , & 연산자)
  • 028. UI Thread
  • 025.Thread/Task
  • 024. Chart
JOOJI
JOOJI
그냥 혼자좋아하는 것들 남기는 블로그....
  • JOOJI
    사소한프로그래머의 소소한행복
    JOOJI
  • 전체
    오늘
    어제
    • 분류 전체보기 (952) N
      • 사소한 아이의 소소한 일상 (245)
      • 사소한 아이의 소소한 먹거리 (43)
      • 사소한 아이의 소소한 정보 (75)
      • 사소한 아이의 소소한 감사 (4)
      • 사소한 아이의 소소한 운동 (54) N
      • 사소한 아이의 소소한 여행 (40)
        • 2013_전주 (1)
        • 2014_독일 (13)
        • 2014_군산 (1)
        • 2015_제주도 (3)
        • 2015_서울모토쇼 (3)
        • 2015_진해 (1)
        • 2015_전주 (1)
        • 2016_여수 (1)
        • 2020_강릉 (1)
        • 2022_제주도 (4)
      • 사소한 아이의 소소한 강짱 (22)
        • 하트투하트 (10)
        • MAPS (1)
        • 화려한 유혹 (2)
        • 한여름의 추억 (2)
      • 사소한 아이의 TV (50)
        • Drama (9)
        • 예능 (32)
        • 사소한 아이의 다현 (9)
      • 사소한 아이의 소소한 스킬 (130)
        • Scaleform (2)
        • C# (74)
        • QT (3)
        • 알고리즘 (4)
        • Python (21)
        • PyQT5 (9)
        • C_C++ (2)
      • 사소한 아이의 소소한 축구 (283)
        • Korea (25)
        • Germany (45)
        • Bayern Munich (64)
        • Soccer_ETC (75)
        • Euro 2016 (12)
        • 친선경기 (3)
      • 사소한 아이의 소소한 생활정보 (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 미디어로그
    • 위치로그
    • 방명록
    • 관리
  • 링크

    • 독일여행
    • 레바티스토리
    • 프라치노 공간
    • 남성패션꿀템 블로그
  • 공지사항

  • 인기 글

  • 태그

    독일
    WPF
    문제
    c#
    회사밥
    python
    뮌헨
    바이에른 뮌헨
    러닝
    분데스리가
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
JOOJI
027. 압축설정/압축해제
상단으로

티스토리툴바