Dotnetzip zipping file without folder directory

Asked

Viewed 235 times

3

I am using dotnetzip to zip a file but the zip file takes the directory root of the file, wanted to zip only the file without the directories:

    public static void Zipar(FileInfo file)
    {
        using (ZipFile zip = new ZipFile())
        {
            zip.Password = "******";
            zip.AddFile(file.FullName);

            zip.Save(file.FullName.Replace(file.Extension,".zip"));
        }
    }

Inside the zip file it looks like this: \pasta\pasta\arquivo.txt

I wanted it to stay that way: arquivo.txt

  • I was trying to zip with password and it was not working, until I realized that it only puts password if the file is not empty, in my case it generated the file then zipped more to test, the program n put the data to save time in the tests, so I find it interesting to say this to anyone who goes through something like this.

1 answer

3


You just need to enter a second parameter in the method AddFile see:

public static void Zipar(FileInfo file)
{
    using (ZipFile zip = new ZipFile())
    {
        zip.Password = "******";
        zip.AddFile(file.FullName,""); //Aqui, se não informar o diretório ele vai ficar na raiz do arquivo

        zip.Save(file.FullName.Replace(file.Extension,".zip"));
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.