NEWS

Viết ứng dụng nén file hoặc folder thành file ZIP (Create file or folder to zip file)

Viết ứng dụng nén file hoặc folder thành file ZIP (Create file or folder to zip file)
Đăng bởi: Thảo Meo - Lượt xem: 16267 23:35:18, 30/11/2015C#   In bài viết

Hôm nay, mình xin hướng dẫn các bạn sử dụng thư viện System.IO.Compression, để tạo nén file hoặc folder, và dùng thư viện này đọc và giải nén file chúng ta vừa nén.

Ở bài viết này, thư viện chúng ta sử dụng sẽ nén file thành file có phần mở rộng *.Zip.

Thực tế trong ứng dụng, nhiều lúc các bạn muốn backup database mặc định thành là file *.bak, nếu chúng ta tích hợp thuật toán nén file này vào dung lượng sẽ giảm đi đáng kể. Thường database của mình có dung lượng gần 1GB, nhưng khi sử dụng thuật toán nén này thì chỉ còn 70MB, giảm rất nhiều phải không các bạn.

Tuy theo, từng mục đích sử dụng các bạn có thể áp dụng cho project của mình.

Dưới đây là giao diện demo của chương trình, các bạn có thể tham khảo:

- Đầu tiên, các bạn cần import thư viện System.IO.Compression vào.

using System.IO;
using System.IO.Compression;

- Viết sự kiện cho nút chọn file để nén: 

 private void button1_Click(object sender, EventArgs e)
  {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string[] files = openFileDialog1.FileNames;
                textBox1.Text = string.Join(",", files);
                isFolder = false;
            }
 }

- Viết sự kiện cho nút chọn folder để nén: 

 private void button2_Click(object sender, EventArgs e)
  {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;
                isFolder = true;
            }
 }

- Viết sự kiện cho nút nén file: 

private void button3_Click_1(object sender, EventArgs e)
{
            DialogResult result = saveFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                if (isFolder)
                {
                    ZipFile.CreateFromDirectory(textBox1.Text, saveFileDialog1.FileName);
                }
                else
                {
                    string[] files = textBox1.Text.Split(',');
                    ZipArchive zip = ZipFile.Open(saveFileDialog1.FileName, ZipArchiveMode.Create);
                    foreach (string file in files)
                    {
                        zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
                    }
                    zip.Dispose();
                }
                MessageBox.Show("Đã nén thành công!");
            }
}

- Viết sự kiện cho nút đọc file nén (*.zip):

 private void button4_Click_1(object sender, EventArgs e)
 {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                ZipArchive zip = ZipFile.OpenRead(openFileDialog1.FileName);
                foreach (ZipArchiveEntry entry in zip.Entries)
                {
                    listBox1.Items.Add(entry.FullName);
                }
            }
}

- Và cuối cùng là mình viết sự kiện cho nút giải file nén:

private void button5_Click(object sender, EventArgs e)
 {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                textBox2.Text = openFileDialog1.FileName;
                DialogResult result2 = folderBrowserDialog1.ShowDialog();
                if (result2 == DialogResult.OK)
                {
                    ZipFile.ExtractToDirectory(openFileDialog1.FileName, folderBrowserDialog1.SelectedPath);
                    MessageBox.Show("Đã giải nén thành công!");
                }
            }
}

Vậy là kết thúc, bây giờ các bạn có thể chạy ứng dụng, để test chương trình, chương trình này mình đã so sánh thử với phần mềm nén và giải nén file winrar, thì mình cảm thấy chất lượng cũng tương đối gần nhau.

Chúc các bạn thành công. Hãy Like và Share để ủng hộ chúng mình

Link download project

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

Viết ứng dụng nén file hoặc folder thành file ZIP (Create file or folder to zip file)
Đăng bởi: Thảo Meo - Lượt xem: 16267 23:35:18, 30/11/2015C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.