Fatimah Zakaria

Fatimah Zakaria

  • NA
  • 2
  • 6.4k

How to set limit size of zip file

Mar 26 2018 9:49 PM
Currently, i have been develop a zip project that need to set a limit size while zipping process and set the process to be auto run smoothly based on the limit size. For example, firstly we must analyze the total size of the data and then set the first file to be zipped as 200GB for the limit space then it will still execute the second file continuously based on the limit size. Can anyone shared idea/solution.
Below are my code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.IO;  
  10. using System.Threading;  
  11. using Ionic.Zip;  
  12.   
  13.   
  14. namespace Zip_Test  
  15. {  
  16.     public partial class Form1 : Form  
  17.     {  
  18.         private bool isFolder = false;  
  19.   
  20.         public Form1()  
  21.         {  
  22.             InitializeComponent();  
  23.         }  
  24.   
  25.         private void btn_zip_Click(object sender, EventArgs e)  
  26.         {  
  27.             if (string.IsNullOrEmpty(txtbox_folder.Text))  
  28.             {  
  29.                 MessageBox.Show("PLEASE SELECT A FOLDER OR FILE TO BE ZIP");  
  30.                 return;  
  31.             }  
  32.             else  
  33.             {  
  34.                 SaveFileDialog sfd = new SaveFileDialog();  
  35.                 sfd.InitialDirectory = @"C:\";  
  36.                 sfd.Filter = "Zip Files|*.zip;*.rar";  
  37.                 sfd.FilterIndex = 0;  
  38.                 sfd.RestoreDirectory = true;  
  39.                 sfd.Title = "SAVE ZIP FILE TO";  
  40.                 sfd.FileName = "ZIP FILE " + DateTime.Now.ToString("dd-MMMM-yyyy hh.mm.ss tt");  
  41.   
  42.                 if (sfd.ShowDialog() == DialogResult.OK)  
  43.                 {  
  44.                     if (isFolder == true)  
  45.                     {  
  46.                         //ZipFile.CreateFromDirectory(txt_folderPath.Text, sfd.FileName);  
  47.   
  48.                         string path = txtbox_folder.Text;  
  49.                         Thread thread = new Thread(t =>  
  50.                         {  
  51.                             using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())  
  52.                             {  
  53.                                 zip.AddDirectory(path);  
  54.                                 System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path);  
  55.                                 //zip.SaveProgress += Zip_SaveProgress;  
  56.                                 zip.Save(string.Format("{0}\\{1}.zip", di.Parent.FullName, di.Name));  
  57.   
  58.                             }  
  59.                         }) { IsBackground = true };  
  60.                         thread.Start();  
  61.                     }  
  62.                     else  
  63.                     {  
  64.                         string fileName = txtbox_folder.Text;  
  65.                         Thread thread = new Thread(t =>  
  66.                         {  
  67.                             using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())  
  68.                             {  
  69.                                 FileInfo fi = new FileInfo(fileName);  
  70.                                 zip.AddFile(fileName);  
  71.                                 System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(fileName);  
  72.                                 //zip.SaveProgress += Zip_SaveFileProgress;  
  73.                                 zip.Save(string.Format("{0}/{1}.zip", di.Parent.FullName, di.Name));  
  74.   
  75.   
  76.                             }  
  77.                         }) { IsBackground = true };  
  78.                         thread.Start();  
  79.                         // string[] files = txt_folderPath.Text.Split(',');  
  80.                         //ZipArchive zip = ZipFile.Open(sfd.FileName, ZipArchiveMode.Create);  
  81.                         // foreach (string file in files)  
  82.                         //{  
  83.                         //zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);  
  84.                         //}  
  85.                     }  
  86.                     MessageBox.Show("ZIP file created successfully!");  
  87.                 }  
  88.                 else  
  89.                 {  
  90.                     return;  
  91.                 }  
  92.             }  
  93.         }  
  94.   
  95.         private void btn_folder_Click(object sender, EventArgs e)  
  96.         {  
  97.             FolderBrowserDialog fbd = new FolderBrowserDialog();  
  98.             fbd.ShowNewFolderButton = true;  
  99.   
  100.             if (fbd.ShowDialog() == DialogResult.OK)  
  101.             {  
  102.                 btn_reset.Enabled = true;  
  103.   
  104.                 txtbox_folder.Text = fbd.SelectedPath;  
  105.                 isFolder = true;  
  106.   
  107.                 if (Directory.GetFiles(txtbox_folder.Text).Length > 0)  
  108.                 {  
  109.                     foreach (string file in Directory.GetFiles(txtbox_folder.Text))  
  110.                     {  
  111.                         //Add file in ListBox.  
  112.                         listBox1.Items.Add(file);  
  113.                     }  
  114.                 }  
  115.   
  116.                 //string[] filePaths = Directory.GetFiles(txt_folderPath.Text, "*.*", SearchOption.TopDirectoryOnly);  
  117.   
  118.                 //for (int i = 0; i < filePaths.Length; i++)  
  119.                 //{  
  120.                 //    listBox1.Items.Add(filePaths[i]);  
  121.                 //}  
  122.   
  123.             }  
  124.             else  
  125.             {  
  126.                 return;  
  127.             }  
  128.         }  
  129.   
  130.         private void btn_reset_Click(object sender, EventArgs e)  
  131.         {  
  132.             txtbox_folder.Text = null;  
  133.             listBox1.Items.Clear();  
  134.             btn_reset.Enabled = false;  
  135.         }  
  136.   
  137.         private void listBox1_SelectedIndexChanged(object sender, EventArgs e)  
  138.         {  
  139.   
  140.         }  
  141.     }  
  142. }  
 

Answers (2)