Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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# xtOSEKInstaller xtOSEKSetupWindowsFormsApplication1Form1.cs:Zeile 192.

Code:

string zipPath = @"D:C#
xtOSEKInstaller
xtOSEKSetupWindowsFormsApplication1inDebug
espackage.zip";
string extractPath = @"D:estcyginstallcygwin";

textBoxProgress.AppendText("Entpacke .... ");
try {
    ZipFile.ExtractToDirectory(zipPath, extractPath);
} catch (System.ArgumentException ex) {
    textBoxProgress.AppendText("
" + "Error
" + 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
625 views
Welcome To Ask or Share your Answers For Others

1 Answer

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...