- [DEVEXPRESS] Chia sẻ source code cách tạo biểu đồ sơ đồ tổ chức công ty Org Chart trên Winform C#
- [C#] Hướng dẫn tạo Auto Number trên Datagridview winform
- [DATABASE] Hướng dẫn tạo Procedure String Split in Mysql
- [C#] Thiết lập dấu (,) hay dấu (.) ở định dạng số đúng với định dạng số Việt Nam
- [C#] Chia sẻ source code Game Spin Lucky Wheel
- [C#] Hướng dẫn Encode and Decode HTML
- Danh sách tài khoản ChatGPT miễn phí - Hướng dẫn tạo tài khoản Chat Open AI GPT tại Việt Nam
- [C#] Hướng dẫn thay đổi giao diện ứng dụng Winform theo giao diện của Windows
- [VB.NET] Hiệu ứng Acrylic, Mica, Tabbed Blur Effect trên Winform
- [DEVEXPRESS] Hướng dẫn sử dụng HTML Template trên Combobox Edit
- [C#] Chia sẻ source code Orange Rain in Winform
- [DEVEXPRESS] Hướng dẫn sử dụng HTML Template trên XtraMessageBox Winform Devexpress 22.2.3
- [DEVEXPRESS] Hướng dẫn sử dụng HTML and CSS Code Viewer trên Winform
- [C#] Number Effect Counter up and down in winform
- [C#] Hướng dẫn Supend and Resume Process ID in Winform
- [C#] Hiển thị line number trên Richtextbox Winform
- [C#] Fake Blue Screen BSOD in winform
- [C#] Chia sẽ code demo sử dụng Async Parallel Foreach and For in Winform
- [C#] Sử dụng ActionBlock run X task at time winform
- [C#] Hướng dẫn sử dụng Property Grid để lưu và tải lại thông tin cấu hình user trên winform
Viết ứng dụng nén file hoặc folder thành file ZIP (Create file or folder to zip file)
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