1

I have the following lines of code that work for creating a zip using ZipFile.CreateFromDirectory(selectedFile, zipPath)

        if (selectedFolder == string.Empty)
        {
            Console.WriteLine("Invalid folder, try again");
        }
        else
        {
            Console.WriteLine("\nSelect zipfile name: ");
            var zipName = Console.ReadLine();

            // Also available: extractToDirectory
            var zipPath = @"C:\Users\User\Documents\Dev\" + zipName + ".zip";
            ZipFile.CreateFromDirectory(selectedFolder, zipPath);

However, the following code which should for all intents and purposes do the same thing except for multiple files being archived into a single zip folder refuses to work:

public static void CreateZipFile(string folderToCreateZip, IEnumerable<string> files)
{   
    var zipPath = folderToCreateZip + "\\test6.zip";

    // Create a new ZIP in this location
    using (var zip = ZipFile.Open(zipPath, ZipArchiveMode.Create))
    {
        foreach (var file in files)
        {
            // Add entry for files
            zip.CreateEntryFromFile(file, zipPath, CompressionLevel.Optimal);
        }
    }
    // Dispose of zip object after files have been zipped
    //zip.Dispose();
}

var zip == ZipArchive zip

I've tried disabling read-only mode on the folders where the zip should get created, but I don't think this matters since the prior function with CreateFromDirectory() works fine. I've also tried creating a ZIP on desktop, but I get the same error.

This is the exception I'm getting:

Exception

As a note, I noticed that it does initially create the zip despite this error, just that it cannot add anything to it unlike CreateFromDirectory() can due to the folder either being in use, no permissions to that area or the folder already existing. Is there a way I can get CreateEntryFromFile() working or an alternative that would work for multiple files?

casperf1
  • 91
  • 1
  • 8
  • 1
    The second parameter of `CreateEntryFromFile` is not the path of the zip but the name the file should have in the zip file (e.g. `System.IO.Path.GetFileName(file)`). – Michael Hufnagel Jul 26 '18 at 10:05
  • @MichaelHufnagel thank you. I've changed the parameters around to the following: zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal); It continues to create an empty zip file, but I'm still getting the Unauthorized Access Exception. – casperf1 Jul 26 '18 at 10:23
  • 1
    My guess would be that one of the file paths (`IEnumerable files`) is a directory since the error message includes a directory path to "Test folder". – Michael Hufnagel Jul 26 '18 at 11:11
  • The IList has 'files' (file paths including the full folder path) inserted into it in an earlier for loop. Perhaps that's the issue? The list is just a list of strings that are the file's paths like so: "C:\\Users\\User\\Documents\\Dev\\Zip stuff\\fileName" – casperf1 Jul 26 '18 at 11:15
  • 1
    If there is folder path in the collection it won't work. [This stackoverflow](https://stackoverflow.com/questions/44459876/c-sharp-unauthorizedaccessexception-when-using-ziparchive-but-not-zipfile) post already has a good example how to use ZipArchive maybe it'll help^^. – Michael Hufnagel Jul 26 '18 at 11:19
  • That looks perfect! Thanks a lot for bearing with me, much appreciated. Take care. – casperf1 Jul 26 '18 at 11:26
  • No problem you too^^. – Michael Hufnagel Jul 26 '18 at 11:27

1 Answers1

0

I had the same problem. The solution was post the full path name at the destinationArchiveFileName parameter (and also a write alowed path). For example c:\my apps folder\my app\my temp\zipfile.zip

Daniel Hermosel
  • 423
  • 3
  • 9