Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
632 views
in Technique[技术] by (71.8m points)

c# - ZipFile.ExtractToDirectory "Illegal characters in path"

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

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

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...