Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

What I have tried:

Below are my code.

<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using Ionic.Zip;


namespace Zip_Test
{
    public partial class Form1 : Form
    {
        private bool isFolder = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void btn_zip_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtbox_folder.Text))
            {
                MessageBox.Show("PLEASE SELECT A FOLDER OR FILE TO BE ZIP");
                return;
            }
            else
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.InitialDirectory = @"C:\";
                sfd.Filter = "Zip Files|*.zip;*.rar";
                sfd.FilterIndex = 0;
                sfd.RestoreDirectory = true;
                sfd.Title = "SAVE ZIP FILE TO";
                sfd.FileName = "ZIP FILE " + DateTime.Now.ToString("dd-MMMM-yyyy hh.mm.ss tt");

                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    if (isFolder == true)
                    {
                        //ZipFile.CreateFromDirectory(txt_folderPath.Text, sfd.FileName);

                        string path = txtbox_folder.Text;
                        Thread thread = new Thread(t =>
                        {
                            using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
                            {
                                zip.AddDirectory(path);
                                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path);
                                //zip.SaveProgress += Zip_SaveProgress;
                                zip.Save(string.Format("{0}\\{1}.zip", di.Parent.FullName, di.Name));

                            }
                        }) { IsBackground = true };
                        thread.Start();
                    }
                    else
                    {
                        string fileName = txtbox_folder.Text;
                        Thread thread = new Thread(t =>
                        {
                            using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
                            {
                                FileInfo fi = new FileInfo(fileName);
                                zip.AddFile(fileName);
                                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(fileName);
                                //zip.SaveProgress += Zip_SaveFileProgress;
                                zip.Save(string.Format("{0}/{1}.zip", di.Parent.FullName, di.Name));


                            }
                        }) { IsBackground = true };
                        thread.Start();
                        // string[] files = txt_folderPath.Text.Split(',');
                        //ZipArchive zip = ZipFile.Open(sfd.FileName, ZipArchiveMode.Create);
                        // foreach (string file in files)
                        //{
                        //zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
                        //}
                    }
                    MessageBox.Show("ZIP file created successfully!");
                }
                else
                {
                    return;
                }
            }
        }

        private void btn_folder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowNewFolderButton = true;

            if (fbd.ShowDialog() == DialogResult.OK)
            {
                btn_reset.Enabled = true;

                txtbox_folder.Text = fbd.SelectedPath;
                isFolder = true;

                if (Directory.GetFiles(txtbox_folder.Text).Length > 0)
                {
                    foreach (string file in Directory.GetFiles(txtbox_folder.Text))
                    {
                        //Add file in ListBox.
                        listBox1.Items.Add(file);
                    }
                }

                //string[] filePaths = Directory.GetFiles(txt_folderPath.Text, "*.*", SearchOption.TopDirectoryOnly);

                //for (int i = 0; i < filePaths.Length; i++)
                //{
                //    listBox1.Items.Add(filePaths[i]);
                //}

            }
            else
            {
                return;
            }
        }

        private void btn_reset_Click(object sender, EventArgs e)
        {
            txtbox_folder.Text = null;
            listBox1.Items.Clear();
            btn_reset.Enabled = false;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
Posted
Updated 27-Mar-18 1:57am

1 solution

The most efficient way to do it and guarantee a ZIP file does not exceed a specified size is to only compress files that, together, are less than or equal to your desired max zip file size. Without knowing the file types and typical resulting compression ratio, that's the approach I would use.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900