FileStream不会打开Win32设备,如磁盘分区和磁带驱动器.(DotNetZip)

本文关键字:分区 磁盘 磁带驱动器 DotNetZip 设备 Win32 FileStream | 更新日期: 2023-09-27 18:25:11

我试图使用DotNetZip来处理zip文件,但每当我试图打开文件时,我都会收到以下错误:

[SEVERE] System.ArgumentException: FileStream will not open Win32 devices such as disk partitions and tape drives. Avoid use of "''.'" in the path.
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at Ionic.Zip.ZipEntry.InternalExtract(String baseDir, Stream outstream, String password)
at Ionic.Zip.ZipEntry.Extract(String baseDirectory)
at Ionic.Zip.ZipFile._InternalExtractAll(String path, Boolean overrideExtractExistingProperty)
at Ionic.Zip.ZipFile.ExtractAll(String path)
at ModsInstaller.Form1.MergeDirectories(String Path1, String Path2) in C:'Users'Admin'documents'visual studio 2010'Projects'ModsInstaller'ModsInstaller'Form1.cs:line 275
at ModsInstaller.Form1.CustomInstallForge() in C:'Users'Admin'documents'visual studio 2010'Projects'ModsInstaller'ModsInstaller'Form1.cs:line 259
at ModsInstaller.Form1.btn_install_Click(Object sender, EventArgs e) in C:'Users'Admin'documents'visual studio 2010'Projects'ModsInstaller'ModsInstaller'Form1.cs:line 120

这是代码:

    private void MergeDirectories(string Path1, string Path2)
    {
        string outDirectory = Path.GetFullPath(workspace + "''temp''dir");
        if (!Directory.Exists(outDirectory))
            Directory.CreateDirectory(outDirectory);
        Path1 = Path.GetFullPath(Path1);
        Path2 = Path.GetFullPath(Path2);            
        Log("Extracting {0} to temp dir.", Path1);
        using (ZipFile zip = ZipFile.Read(Path1))
        {
            zip.ExtractAll(outDirectory); //this line throws the error
        }
        Log("Extraction sucessfull");
        Log("Extracted {0} to temp dir.", Path2);
        ZipFile.Read(Path2).ExtractAll(Path.GetFullPath(workspace + "''temp''dir"));
        Log("Extraction sucessfull");
        ZipFile z = new ZipFile(workspace + "''temp''build.jar");
        z.AddDirectory(workspace + "''temp''dir");
        z.Save();
        z.Dispose();
    }

当我插入一个断点时,我看到:

outDirectory = "C:''Users''Admin''documents''visual studio 2010''Projects''ModsInstaller''ModsInstaller''bin''Debug''temp''dir"

有人能指出我做错了什么吗?

谢谢。

FileStream不会打开Win32设备,如磁盘分区和磁带驱动器.(DotNetZip)

我在CON文件名上也出现了同样的错误。这不是因为爱奥尼亚。,而是由于Windows文件命名约定。

如果第一个ZIP文件有一些不寻常的文件名,请检查其内容。例如,在Windows中,您不能创建名为CON、AUX、NUL、COM1等的文件。

您可以在保留名称部分阅读更多信息:https://learn.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#file_and_directory_names

解决方案是采用其他zip文件进行测试或在unix系统下提取它,或者要求文件提供商发送具有不同文件名或至少小写的易受攻击文件。

使用

MergeDirectories("Sample 1.zip", "Sample 2.zip", "Merged.zip");

代码:

    private void MergeDirectories(string filePath1, string filePath2, string mergedName)
    {
        string workspace = Environment.CurrentDirectory;
        filePath1 = Path.Combine(workspace, filePath1);
        filePath2 = Path.Combine(workspace, filePath2);
        mergedName = Path.Combine(workspace, mergedName);
        if (File.Exists(mergedName))
        {
            File.Delete(mergedName);
        }
        DirectoryInfo zip1 = OpenAndExtract(filePath1);
        DirectoryInfo zip2 = OpenAndExtract(filePath2);
        string merged = Path.GetTempFileName();
        using (ZipFile z = new ZipFile())
        {
            z.AddDirectory(zip1.FullName);
            z.AddDirectory(zip2.FullName);
            z.Save(merged);
        }
        zip1.Delete(true);
        zip2.Delete(true);
        File.Move(merged, mergedName);
    }
    private DirectoryInfo OpenAndExtract(string path)
    {
        string tmpName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName());
        string tmp = Path.Combine(Path.GetTempPath(), tmpName);
        FileInfo sourcePath = new FileInfo(path);
        DirectoryInfo tempPath = Directory.CreateDirectory(tmp);
        using (ZipFile zip = ZipFile.Read(sourcePath.FullName))
        {
            zip.ExtractAll(tempPath.FullName);
        }
        return tempPath;
    }