1

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

谁能指出我做错了什么?

谢谢。

4

2 回答 2

3

我对 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 系统下解压或要求文件提供者发送具有不同文件名或至少小写的易受攻击的文件。

于 2019-06-30T20:05:36.577 回答
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;
    }
于 2013-07-27T01:35:48.140 回答