Click here to Skip to main content
14,298,693 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi all, I need to compress folders AND files. In source folder, if there are folders and files, the program will compress only files...where did I go wrong?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading.Tasks;
using System.Threading;


namespace DBBackupns
{
    public partial class TestDirBackup : Form
    {
        int fullbacknum;
        int Incnumber;
        
        public TestDirBackup()
        {
            InitializeComponent();
        }

        private void GetSettings()
        {
            Settings settings;
            fullbacknum = 1;
            Incnumber = 1;
            settings = new Settings();
            fullbacknum = settings.GetSetting("BackupNumber", fullbacknum);
            Incnumber = settings.GetSetting("Incnumber", Incnumber);
        }

        private void SaveSettings()
        {
            Settings settings;
            settings = new Settings();
            settings.PutSetting("BackupNumber", fullbacknum);
            settings.PutSetting("Incnumber", Incnumber);
        }

        private void btnCompress_Click(object sender, EventArgs e)
        {
            string strsourcedir = "";    
            string strzipdir = "";      
            string strfilename ="";    
            HCompress   hc;
            BackupType bt = BackupType.Full;
            GetSettings();
            errorProvider1.Clear();
            strsourcedir = txtSourceDir.Text;
            strzipdir = txtZipDir.Text;
            
            if (rdobtnFull.Checked)
            {
                bt = BackupType.Full;
                fullbacknum++;
                strfilename = string.Format("BackupFull-{0}.zip", fullbacknum);
                Incnumber = 0;  
            }
            if (rdobtnInc.Checked)
            {
                bt = BackupType.Incremental;
                Incnumber++;
                strfilename = string.Format("BackupInc-{0}-{1}.zip", fullbacknum, Incnumber);
            }
            strfilename = strzipdir + "\\" + strfilename;
            txtFilename.Text = strfilename;
            hc = new HCompress();
            hc.ZipFiles(strfilename, strsourcedir, bt);
            SaveSettings();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            using (var fldrDlg = new FolderBrowserDialog())
            {
                if (fldrDlg.ShowDialog() == DialogResult.OK)
                {
                    txtSourceDir.Text = fldrDlg.SelectedPath;
                }
            }
        }

        
    }


What I have tried:

Tried to use the zip dot net but with no success at all
Posted
Updated 5 days ago
Comments
Dave Kreskowiak 5 days ago
   
What are you using to compress the files? Not every library supports adding a folder tree to the resulting .ZIP.

All I could find on HCompress is it's used for lossless compression of images.
Maciej Los 5 days ago
   
Sounds like an answer, Dave.
Cheers!
Maciej
Rate this:
Please Sign up or sign in to vote.

Solution 1

You can use 7zip, see answers here: How would I compress a folder with 7Zip in C#? - Stack Overflow[^]
It can be downloaded here: 7-Zip[^]
   
v2
Comments
Maciej Los 5 days ago
   
5ed!
Rate this:
Please Sign up or sign in to vote.

Solution 2

   
Comments
Maciej Los 5 days ago
   
5ed!
CPallini 5 days ago
   
Thank you, Maciej!

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100