2

I want to unzip a file with ZipFile class in c# (VS2012). Even if I copy the paths directly from win explorer I get this error:

System.ArgumentException: Illegales Zeichen im Pfad. bei System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional) bei System.IO.Path.GetFileName(String path) bei System.IO.Compression.ZipHelper.EndsWithDirChar(String test) bei System.IO.Compression.ZipArchiveEntry.set_FullName(String value)
bei System.IO.Compression.ZipArchiveEntry..ctor(ZipArchive archive, ZipCentralDirectoryFileHeader cd) bei System.IO.Compression.ZipArchive.ReadCentralDirectory() bei System.IO.Compression.ZipArchive.get_Entries() bei System.IO.Compression.ZipFileExtensions.ExtractToDirectory(ZipArchive source, String destinationDirectoryName) bei System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileName, String destinationDirectoryName, Encoding entryNameEncoding) bei System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileName, String destinationDirectoryName) bei WindowsFormsApplication1.MainForm.buttonStartNxtOSEK_Click(Object sender, EventArgs e) in d:\C#\nxtOSEKInstaller\nxtOSEKSetup\WindowsFormsApplication1\Form1.cs:Zeile 192.

Code:

string zipPath = @"D:\C#\nxtOSEKInstaller\nxtOSEKSetup\WindowsFormsApplication1\bin\Debug\res\package.zip";
string extractPath = @"D:\testcyginstall\cygwin";

textBoxProgress.AppendText("Entpacke .... ");
try {
    ZipFile.ExtractToDirectory(zipPath, extractPath);
} catch (System.ArgumentException ex) {
    textBoxProgress.AppendText("\n" + "Error\n" + ex.ToString());
    return;
}

EDIT Problem solved: Some files with chinese file names in the zip file caused the problem. It's very frustrating when the exception does not output the problematic path name.

MAumair
  • 23
  • 1
  • 4

1 Answers1

5

As you already know some characters are not valid on windows:

\ / : * ? " < > |

This would bring a lot of situations when your application receives zip from different OS since some of those invalid characters are valid in other OS.

In order to solve this problem you can sanitize your files names before you extract them:

public void ExtractZipFileToPath(
        string zipFilePath,
        string ouputPath
        )
    {
        using (var zip = ZipFile.Read(zipFilePath))
        {
            foreach (var entry in zip.Entries.ToList())
            {
                entry.FileName = SanitizeFileName(entry.FileName);
                entry.Extract(ouputPath);
            }
        }
    } 

Sanitizing examples here How to remove illegal characters from path and filenames?

Community
  • 1
  • 1
nramirez
  • 5,230
  • 2
  • 23
  • 39