【问题标题】:FileStream will not open Win32 devices such as disk partitions and tape drives. (DotNetZip)FileStream 不会打开 Win32 设备,例如磁盘分区和磁带驱动器。 (DotNetZip)
【发布时间】:2013-07-27 01:11:38
【问题描述】:

我正在尝试使用 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"

谁能指出我做错了什么?

谢谢。

【问题讨论】:

    标签: c# dotnetzip argumentexception


    【解决方案1】:

    我对 CON 文件名有同样的错误。这不是因为 Ionic.Zip 库,而是因为 Windows 文件命名约定。

    检查第一个 ZIP 文件的内容是否有一些不寻常的文件名。 例如,在 Windows 中,您无法创建名称为 CON、AUX、NUL、COM1 等的文件。

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

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

    【讨论】:

    • 是的。 Windows 不允许您在不使用第三方软件的情况下创建具有以下名称的文件:CON、PRN、AUX、NUL、COM1、COM2、COM3、COM4、COM5、COM6、COM7、COM8、COM9、LPT1、LPT2、LPT3、LPT4 、LPT5、LPT6、LPT7、LPT8 和 LPT9
    【解决方案2】:

    用法

    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;
        }
    

    【讨论】:

    • 通过进一步的测试,我似乎无法重现您的错误。 workspace 变量的值是多少?
    • 呵呵,我复制了你的代码来测试它,但它仍然抛出错误。工作空间的值为 "C:\\Users\\Admin\\documents\\visual studio 2010\\Projects\\ModsInstaller\\ModsInstaller\\bin\\Debug"
    • 错误是什么?和之前的错误是一样的吗?还是有什么区别?
    猜你喜欢
    • 2011-04-26
    • 2012-03-06
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 2014-10-04
    • 2012-08-10
    • 2017-01-27
    相关资源
    最近更新 更多