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?

C#
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 10-Sep-19 23:01pm
Comments
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.
Cheers!
Maciej

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
 
v2
Comments
Maciej Los 11-Sep-19 2:33am    
5ed!
 
Share this answer
 
Comments
Maciej Los 11-Sep-19 6:42am    
5ed!
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