0

I have 1 folder that contains many txt files. I want to zip them but separated.

Example:
In this folder I have A.txt, B.txt, C.txt.

I want to zip all the files but separated so the result will be A.zip, B.zip, C.zip.

string outputPath = "C:\\Users\\Desktop\\VA";
string path = outputPath + "\\VA_" + tglskrg;
foreach (string dirFile in Directory.GetDirectories(path))
{
    foreach (string fileName in Directory.GetFiles(dirFile))
    {
        using (ZipFile zip = new ZipFile())
        {
            zip.UseUnicodeAsNecessary = true;
            zip.AddFile(); //dont know what to put
            zip.Save(); //dont know what to put
        }
    }
}

Any help will be appreciated.

I'm using dotnetzip (Ionic.zip) and C# Visual Studio Express 2010.

SimonG
  • 312
  • 7
  • 20

1 Answers1

0

You could do that as follows:

foreach (string fileName in Directory.GetFiles(dirFile))
{
    var zipFile = Path.Combine(outputPath, Path.ChangeExtension(fileName, ".zip"));
    using (ZipFile zip = new ZipFile())
    {
        zip.AddFile(fileName); // add A.txt to the zip file
        zip.Save(zipFile); // save as A.zip
    }
}

This takes all the files found in the folder dirFile, and saves them under outputPath, with the same file name but replacing the extension with .zip.

steve16351
  • 5,372
  • 2
  • 16
  • 29
  • Welp, I have pretty much exactly the same but you were 20 secs faster. Tough luck, I'll delete mine. – Joelius May 28 '19 at 08:41
  • sry for late reply, just arrive in office , thankyou so much for this , it really works –  May 29 '19 at 01:36