-2

I want to zip files and folders in a folder using c# and I've checked previously asked questions but they don't work for me...i'm currently trying to work with DotNetZip. Here's a bit of my code:

    using (ZipFile zip = new ZipFile())
        {
            string[] files = Directory.GetFiles(@"C:\Users\T1132\Desktop\logs");
            // add all those files to the logs folder in the zip file
            zip.AddFiles(files, "logs");
            zip.Comment = "This zip was created at " + System.DateTime.Now.ToString("G");
            var a = System.DateTime.Now.ToString();
            zip.Save(@"C:\Users\T1132\Desktop\logs\Archiver.zip");
            foreach (var f in files)
            {
                System.IO.File.Delete(f);
                System.Diagnostics.Debug.WriteLine(f + "will be deleted");
            }
        }

the code above works but it only zips the files and leaves the folders. Kindly assist me please, Thanks.

Praise Hart
  • 172
  • 11

3 Answers3

0

There are so many ways to zip files using DotNetZip.

using (ZipFile zip = new ZipFile())
{
    zip.Encoding = System.Text.Encoding.GetEncoding("big5"); // chinese
    zip.AddDirectory(@"MyDocuments\ProjectX");
    zip.Save(ZipFileToCreate);
}

Above zips a directory. For more uses you can follow the links

https://dotnetzip.codeplex.com/wikipage?title=CS-Examples&referringTitle=Examples

or

http://grapevine.dyndns-ip.com/download/authentec/download/dotnetzip/examples.htm

Thanks

0

I am using this and it is working great. Make sure directory has enough permission(s).

System.IO.Compression.ZipFile.CreateFromDirectory(zipPath, path + strFileName + ".zip");
summerGhost
  • 477
  • 4
  • 15
0

You can use below code.

 string zipFileName=  "zip full path with extension";
            var files = Directory.GetFiles(" directory path");
        Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile();
        foreach(var file in files) {
            zip.AddFile(file);
        }
        zip.Save(zipFileName);
Vijay Raheja
  • 290
  • 2
  • 10