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

时间:2013-07-27 01:11:39

标签: c# dotnetzip argumentexception

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

有谁可以指出我做错了什么?

感谢。

2 个答案:

答案 0 :(得分:1)

用法

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

答案 1 :(得分:0)

我对CON文件名有同样的错误。这不是因为Ionic.Zip lib。,而是由于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系统下,或要求文件提供者发送带有差异文件名或至少小写的易受攻击文件。