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

        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;
            strsourcedir = txtSourceDir.Text;
            strzipdir = txtZipDir.Text;
            if (rdobtnFull.Checked)
                bt = BackupType.Full;
                strfilename = string.Format("BackupFull-{0}.zip", fullbacknum);
                Incnumber = 0;  
            if (rdobtnInc.Checked)
                bt = BackupType.Incremental;
                strfilename = string.Format("BackupInc-{0}-{1}.zip", fullbacknum, Incnumber);
            strfilename = strzipdir + "\\" + strfilename;
            txtFilename.Text = strfilename;
            hc = new HCompress();
            hc.ZipFiles(strfilename, strsourcedir, bt);

        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
Updated 10-Sep-19 23:01pm
Dave Kreskowiak 10-Sep-19 22:49pm    
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 11-Sep-19 2:32am    
Sounds like an answer, Dave.

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[^]
Share this answer
Maciej Los 11-Sep-19 2:33am    
Share this answer
Maciej Los 11-Sep-19 6:42am    
CPallini 11-Sep-19 6:49am    
Thank you, Maciej!

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